blob: 0e7cb4d3cb34f4f6cc9f8cd7df30231b11baedfb [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"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080040#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080041#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010042#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070044#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080046#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080047#include <linux/reservation.h>
48#include <linux/dma-buf.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080049
Matt Roper465c1202014-05-29 08:06:54 -070050/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010051static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010052 DRM_FORMAT_C8,
53 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070054 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010055 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070056};
57
58/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010059static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010060 DRM_FORMAT_C8,
61 DRM_FORMAT_RGB565,
62 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070063 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010064 DRM_FORMAT_XRGB2101010,
65 DRM_FORMAT_XBGR2101010,
66};
67
68static const uint32_t skl_primary_formats[] = {
69 DRM_FORMAT_C8,
70 DRM_FORMAT_RGB565,
71 DRM_FORMAT_XRGB8888,
72 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010073 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070074 DRM_FORMAT_ABGR8888,
75 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070076 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053077 DRM_FORMAT_YUYV,
78 DRM_FORMAT_YVYU,
79 DRM_FORMAT_UYVY,
80 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070081};
82
Matt Roper3d7d6512014-06-10 08:28:13 -070083/* Cursor formats */
84static const uint32_t intel_cursor_formats[] = {
85 DRM_FORMAT_ARGB8888,
86};
87
Jesse Barnesf1f644d2013-06-27 00:39:25 +030088static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020089 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030090static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020091 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030092
Jesse Barneseb1bfe82014-02-12 12:26:25 -080093static int intel_framebuffer_init(struct drm_device *dev,
94 struct intel_framebuffer *ifb,
95 struct drm_mode_fb_cmd2 *mode_cmd,
96 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020097static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
98static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020099static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700100 struct intel_link_m_n *m_n,
101 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200102static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200103static void haswell_set_pipeconf(struct drm_crtc *crtc);
104static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200105static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200106 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200107static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200108 const struct intel_crtc_state *pipe_config);
Maarten Lankhorst613d2b22015-07-21 13:28:58 +0200109static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
110static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700111static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
112 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200113static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
114 int num_connectors);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200115static void skylake_pfit_enable(struct intel_crtc *crtc);
116static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
117static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200118static void intel_modeset_setup_hw_state(struct drm_device *dev);
Matt Roper200757f2015-12-03 11:37:36 -0800119static void intel_pre_disable_primary(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100120
Jesse Barnes79e53942008-11-07 14:24:08 -0800121typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400122 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800123} intel_range_t;
124
125typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400126 int dot_limit;
127 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800128} intel_p2_t;
129
Ma Lingd4906092009-03-18 20:13:27 +0800130typedef struct intel_limit intel_limit_t;
131struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400132 intel_range_t dot, vco, n, m, m1, m2, p, p1;
133 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800134};
Jesse Barnes79e53942008-11-07 14:24:08 -0800135
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300136/* returns HPLL frequency in kHz */
137static int valleyview_get_vco(struct drm_i915_private *dev_priv)
138{
139 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
140
141 /* Obtain SKU information */
142 mutex_lock(&dev_priv->sb_lock);
143 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
144 CCK_FUSE_HPLL_FREQ_MASK;
145 mutex_unlock(&dev_priv->sb_lock);
146
147 return vco_freq[hpll_freq] * 1000;
148}
149
150static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
151 const char *name, u32 reg)
152{
153 u32 val;
154 int divider;
155
156 if (dev_priv->hpll_freq == 0)
157 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
158
159 mutex_lock(&dev_priv->sb_lock);
160 val = vlv_cck_read(dev_priv, reg);
161 mutex_unlock(&dev_priv->sb_lock);
162
163 divider = val & CCK_FREQUENCY_VALUES;
164
165 WARN((val & CCK_FREQUENCY_STATUS) !=
166 (divider << CCK_FREQUENCY_STATUS_SHIFT),
167 "%s change in progress\n", name);
168
169 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
170}
171
Daniel Vetterd2acd212012-10-20 20:57:43 +0200172int
173intel_pch_rawclk(struct drm_device *dev)
174{
175 struct drm_i915_private *dev_priv = dev->dev_private;
176
177 WARN_ON(!HAS_PCH_SPLIT(dev));
178
179 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
180}
181
Jani Nikula79e50a42015-08-26 10:58:20 +0300182/* hrawclock is 1/4 the FSB frequency */
183int intel_hrawclk(struct drm_device *dev)
184{
185 struct drm_i915_private *dev_priv = dev->dev_private;
186 uint32_t clkcfg;
187
188 /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
Wayne Boyer666a4532015-12-09 12:29:35 -0800189 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Jani Nikula79e50a42015-08-26 10:58:20 +0300190 return 200;
191
192 clkcfg = I915_READ(CLKCFG);
193 switch (clkcfg & CLKCFG_FSB_MASK) {
194 case CLKCFG_FSB_400:
195 return 100;
196 case CLKCFG_FSB_533:
197 return 133;
198 case CLKCFG_FSB_667:
199 return 166;
200 case CLKCFG_FSB_800:
201 return 200;
202 case CLKCFG_FSB_1067:
203 return 266;
204 case CLKCFG_FSB_1333:
205 return 333;
206 /* these two are just a guess; one of them might be right */
207 case CLKCFG_FSB_1600:
208 case CLKCFG_FSB_1600_ALT:
209 return 400;
210 default:
211 return 133;
212 }
213}
214
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300215static void intel_update_czclk(struct drm_i915_private *dev_priv)
216{
Wayne Boyer666a4532015-12-09 12:29:35 -0800217 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300218 return;
219
220 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
221 CCK_CZ_CLOCK_CONTROL);
222
223 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
224}
225
Chris Wilson021357a2010-09-07 20:54:59 +0100226static inline u32 /* units of 100MHz */
227intel_fdi_link_freq(struct drm_device *dev)
228{
Chris Wilson8b99e682010-10-13 09:59:17 +0100229 if (IS_GEN5(dev)) {
230 struct drm_i915_private *dev_priv = dev->dev_private;
231 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
232 } else
233 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100234}
235
Daniel Vetter5d536e22013-07-06 12:52:06 +0200236static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400237 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200238 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200239 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400240 .m = { .min = 96, .max = 140 },
241 .m1 = { .min = 18, .max = 26 },
242 .m2 = { .min = 6, .max = 16 },
243 .p = { .min = 4, .max = 128 },
244 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700245 .p2 = { .dot_limit = 165000,
246 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700247};
248
Daniel Vetter5d536e22013-07-06 12:52:06 +0200249static const intel_limit_t intel_limits_i8xx_dvo = {
250 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200251 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200252 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200253 .m = { .min = 96, .max = 140 },
254 .m1 = { .min = 18, .max = 26 },
255 .m2 = { .min = 6, .max = 16 },
256 .p = { .min = 4, .max = 128 },
257 .p1 = { .min = 2, .max = 33 },
258 .p2 = { .dot_limit = 165000,
259 .p2_slow = 4, .p2_fast = 4 },
260};
261
Keith Packarde4b36692009-06-05 19:22:17 -0700262static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400263 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200264 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200265 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400266 .m = { .min = 96, .max = 140 },
267 .m1 = { .min = 18, .max = 26 },
268 .m2 = { .min = 6, .max = 16 },
269 .p = { .min = 4, .max = 128 },
270 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700271 .p2 = { .dot_limit = 165000,
272 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700273};
Eric Anholt273e27c2011-03-30 13:01:10 -0700274
Keith Packarde4b36692009-06-05 19:22:17 -0700275static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400276 .dot = { .min = 20000, .max = 400000 },
277 .vco = { .min = 1400000, .max = 2800000 },
278 .n = { .min = 1, .max = 6 },
279 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100280 .m1 = { .min = 8, .max = 18 },
281 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400282 .p = { .min = 5, .max = 80 },
283 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700284 .p2 = { .dot_limit = 200000,
285 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700286};
287
288static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400289 .dot = { .min = 20000, .max = 400000 },
290 .vco = { .min = 1400000, .max = 2800000 },
291 .n = { .min = 1, .max = 6 },
292 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100293 .m1 = { .min = 8, .max = 18 },
294 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400295 .p = { .min = 7, .max = 98 },
296 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700297 .p2 = { .dot_limit = 112000,
298 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700299};
300
Eric Anholt273e27c2011-03-30 13:01:10 -0700301
Keith Packarde4b36692009-06-05 19:22:17 -0700302static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700303 .dot = { .min = 25000, .max = 270000 },
304 .vco = { .min = 1750000, .max = 3500000},
305 .n = { .min = 1, .max = 4 },
306 .m = { .min = 104, .max = 138 },
307 .m1 = { .min = 17, .max = 23 },
308 .m2 = { .min = 5, .max = 11 },
309 .p = { .min = 10, .max = 30 },
310 .p1 = { .min = 1, .max = 3},
311 .p2 = { .dot_limit = 270000,
312 .p2_slow = 10,
313 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800314 },
Keith Packarde4b36692009-06-05 19:22:17 -0700315};
316
317static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700318 .dot = { .min = 22000, .max = 400000 },
319 .vco = { .min = 1750000, .max = 3500000},
320 .n = { .min = 1, .max = 4 },
321 .m = { .min = 104, .max = 138 },
322 .m1 = { .min = 16, .max = 23 },
323 .m2 = { .min = 5, .max = 11 },
324 .p = { .min = 5, .max = 80 },
325 .p1 = { .min = 1, .max = 8},
326 .p2 = { .dot_limit = 165000,
327 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700328};
329
330static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700331 .dot = { .min = 20000, .max = 115000 },
332 .vco = { .min = 1750000, .max = 3500000 },
333 .n = { .min = 1, .max = 3 },
334 .m = { .min = 104, .max = 138 },
335 .m1 = { .min = 17, .max = 23 },
336 .m2 = { .min = 5, .max = 11 },
337 .p = { .min = 28, .max = 112 },
338 .p1 = { .min = 2, .max = 8 },
339 .p2 = { .dot_limit = 0,
340 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800341 },
Keith Packarde4b36692009-06-05 19:22:17 -0700342};
343
344static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700345 .dot = { .min = 80000, .max = 224000 },
346 .vco = { .min = 1750000, .max = 3500000 },
347 .n = { .min = 1, .max = 3 },
348 .m = { .min = 104, .max = 138 },
349 .m1 = { .min = 17, .max = 23 },
350 .m2 = { .min = 5, .max = 11 },
351 .p = { .min = 14, .max = 42 },
352 .p1 = { .min = 2, .max = 6 },
353 .p2 = { .dot_limit = 0,
354 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800355 },
Keith Packarde4b36692009-06-05 19:22:17 -0700356};
357
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500358static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400359 .dot = { .min = 20000, .max = 400000},
360 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700361 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400362 .n = { .min = 3, .max = 6 },
363 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700364 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400365 .m1 = { .min = 0, .max = 0 },
366 .m2 = { .min = 0, .max = 254 },
367 .p = { .min = 5, .max = 80 },
368 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700369 .p2 = { .dot_limit = 200000,
370 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700371};
372
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500373static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400374 .dot = { .min = 20000, .max = 400000 },
375 .vco = { .min = 1700000, .max = 3500000 },
376 .n = { .min = 3, .max = 6 },
377 .m = { .min = 2, .max = 256 },
378 .m1 = { .min = 0, .max = 0 },
379 .m2 = { .min = 0, .max = 254 },
380 .p = { .min = 7, .max = 112 },
381 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700382 .p2 = { .dot_limit = 112000,
383 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700384};
385
Eric Anholt273e27c2011-03-30 13:01:10 -0700386/* Ironlake / Sandybridge
387 *
388 * We calculate clock using (register_value + 2) for N/M1/M2, so here
389 * the range value for them is (actual_value - 2).
390 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800391static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700392 .dot = { .min = 25000, .max = 350000 },
393 .vco = { .min = 1760000, .max = 3510000 },
394 .n = { .min = 1, .max = 5 },
395 .m = { .min = 79, .max = 127 },
396 .m1 = { .min = 12, .max = 22 },
397 .m2 = { .min = 5, .max = 9 },
398 .p = { .min = 5, .max = 80 },
399 .p1 = { .min = 1, .max = 8 },
400 .p2 = { .dot_limit = 225000,
401 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700402};
403
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800404static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700405 .dot = { .min = 25000, .max = 350000 },
406 .vco = { .min = 1760000, .max = 3510000 },
407 .n = { .min = 1, .max = 3 },
408 .m = { .min = 79, .max = 118 },
409 .m1 = { .min = 12, .max = 22 },
410 .m2 = { .min = 5, .max = 9 },
411 .p = { .min = 28, .max = 112 },
412 .p1 = { .min = 2, .max = 8 },
413 .p2 = { .dot_limit = 225000,
414 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800415};
416
417static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700418 .dot = { .min = 25000, .max = 350000 },
419 .vco = { .min = 1760000, .max = 3510000 },
420 .n = { .min = 1, .max = 3 },
421 .m = { .min = 79, .max = 127 },
422 .m1 = { .min = 12, .max = 22 },
423 .m2 = { .min = 5, .max = 9 },
424 .p = { .min = 14, .max = 56 },
425 .p1 = { .min = 2, .max = 8 },
426 .p2 = { .dot_limit = 225000,
427 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800428};
429
Eric Anholt273e27c2011-03-30 13:01:10 -0700430/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800431static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700432 .dot = { .min = 25000, .max = 350000 },
433 .vco = { .min = 1760000, .max = 3510000 },
434 .n = { .min = 1, .max = 2 },
435 .m = { .min = 79, .max = 126 },
436 .m1 = { .min = 12, .max = 22 },
437 .m2 = { .min = 5, .max = 9 },
438 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400439 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700440 .p2 = { .dot_limit = 225000,
441 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800442};
443
444static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700445 .dot = { .min = 25000, .max = 350000 },
446 .vco = { .min = 1760000, .max = 3510000 },
447 .n = { .min = 1, .max = 3 },
448 .m = { .min = 79, .max = 126 },
449 .m1 = { .min = 12, .max = 22 },
450 .m2 = { .min = 5, .max = 9 },
451 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400452 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700453 .p2 = { .dot_limit = 225000,
454 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800455};
456
Ville Syrjälädc730512013-09-24 21:26:30 +0300457static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300458 /*
459 * These are the data rate limits (measured in fast clocks)
460 * since those are the strictest limits we have. The fast
461 * clock and actual rate limits are more relaxed, so checking
462 * them would make no difference.
463 */
464 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200465 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700466 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700467 .m1 = { .min = 2, .max = 3 },
468 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300469 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300470 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700471};
472
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300473static const intel_limit_t intel_limits_chv = {
474 /*
475 * These are the data rate limits (measured in fast clocks)
476 * since those are the strictest limits we have. The fast
477 * clock and actual rate limits are more relaxed, so checking
478 * them would make no difference.
479 */
480 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200481 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300482 .n = { .min = 1, .max = 1 },
483 .m1 = { .min = 2, .max = 2 },
484 .m2 = { .min = 24 << 22, .max = 175 << 22 },
485 .p1 = { .min = 2, .max = 4 },
486 .p2 = { .p2_slow = 1, .p2_fast = 14 },
487};
488
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200489static const intel_limit_t intel_limits_bxt = {
490 /* FIXME: find real dot limits */
491 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530492 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200493 .n = { .min = 1, .max = 1 },
494 .m1 = { .min = 2, .max = 2 },
495 /* FIXME: find real m2 limits */
496 .m2 = { .min = 2 << 22, .max = 255 << 22 },
497 .p1 = { .min = 2, .max = 4 },
498 .p2 = { .p2_slow = 1, .p2_fast = 20 },
499};
500
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200501static bool
502needs_modeset(struct drm_crtc_state *state)
503{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200504 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200505}
506
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300507/**
508 * Returns whether any output on the specified pipe is of the specified type
509 */
Damien Lespiau40935612014-10-29 11:16:59 +0000510bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300511{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300512 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300513 struct intel_encoder *encoder;
514
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300515 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300516 if (encoder->type == type)
517 return true;
518
519 return false;
520}
521
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200522/**
523 * Returns whether any output on the specified pipe will have the specified
524 * type after a staged modeset is complete, i.e., the same as
525 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
526 * encoder->crtc.
527 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200528static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
529 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200530{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200531 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300532 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200533 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200534 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200535 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200536
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300537 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200538 if (connector_state->crtc != crtc_state->base.crtc)
539 continue;
540
541 num_connectors++;
542
543 encoder = to_intel_encoder(connector_state->best_encoder);
544 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200545 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200546 }
547
548 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200549
550 return false;
551}
552
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553static const intel_limit_t *
554intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800555{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200556 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800557 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800558
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200559 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100560 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000561 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800562 limit = &intel_limits_ironlake_dual_lvds_100m;
563 else
564 limit = &intel_limits_ironlake_dual_lvds;
565 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000566 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800567 limit = &intel_limits_ironlake_single_lvds_100m;
568 else
569 limit = &intel_limits_ironlake_single_lvds;
570 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200571 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800572 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800573
574 return limit;
575}
576
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200577static const intel_limit_t *
578intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800579{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200580 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800581 const intel_limit_t *limit;
582
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200583 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100584 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700585 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800586 else
Keith Packarde4b36692009-06-05 19:22:17 -0700587 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200588 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
589 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700590 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200591 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700592 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800593 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700594 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800595
596 return limit;
597}
598
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200599static const intel_limit_t *
600intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800601{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200602 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800603 const intel_limit_t *limit;
604
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200605 if (IS_BROXTON(dev))
606 limit = &intel_limits_bxt;
607 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200608 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800609 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200610 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500611 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200612 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500613 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800614 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500615 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300616 } else if (IS_CHERRYVIEW(dev)) {
617 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700618 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300619 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100620 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200621 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100622 limit = &intel_limits_i9xx_lvds;
623 else
624 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800625 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200626 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700627 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200628 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700629 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200630 else
631 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800632 }
633 return limit;
634}
635
Imre Deakdccbea32015-06-22 23:35:51 +0300636/*
637 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
638 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
639 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
640 * The helpers' return value is the rate of the clock that is fed to the
641 * display engine's pipe which can be the above fast dot clock rate or a
642 * divided-down version of it.
643 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500644/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300645static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800646{
Shaohua Li21778322009-02-23 15:19:16 +0800647 clock->m = clock->m2 + 2;
648 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200649 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300650 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300651 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
652 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300653
654 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800655}
656
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200657static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
658{
659 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
660}
661
Imre Deakdccbea32015-06-22 23:35:51 +0300662static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800663{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200664 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800665 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200666 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300667 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300668 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
669 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300670
671 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800672}
673
Imre Deakdccbea32015-06-22 23:35:51 +0300674static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300675{
676 clock->m = clock->m1 * clock->m2;
677 clock->p = clock->p1 * clock->p2;
678 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300679 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300680 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
681 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300682
683 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300684}
685
Imre Deakdccbea32015-06-22 23:35:51 +0300686int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300687{
688 clock->m = clock->m1 * clock->m2;
689 clock->p = clock->p1 * clock->p2;
690 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300691 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300692 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
693 clock->n << 22);
694 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300695
696 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300697}
698
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800699#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800700/**
701 * Returns whether the given set of divisors are valid for a given refclk with
702 * the given connectors.
703 */
704
Chris Wilson1b894b52010-12-14 20:04:54 +0000705static bool intel_PLL_is_valid(struct drm_device *dev,
706 const intel_limit_t *limit,
707 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800708{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300709 if (clock->n < limit->n.min || limit->n.max < clock->n)
710 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800711 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400712 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800713 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400714 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800715 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400716 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300717
Wayne Boyer666a4532015-12-09 12:29:35 -0800718 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
719 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300720 if (clock->m1 <= clock->m2)
721 INTELPllInvalid("m1 <= m2\n");
722
Wayne Boyer666a4532015-12-09 12:29:35 -0800723 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300724 if (clock->p < limit->p.min || limit->p.max < clock->p)
725 INTELPllInvalid("p out of range\n");
726 if (clock->m < limit->m.min || limit->m.max < clock->m)
727 INTELPllInvalid("m out of range\n");
728 }
729
Jesse Barnes79e53942008-11-07 14:24:08 -0800730 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400731 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800732 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
733 * connector, etc., rather than just a single range.
734 */
735 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400736 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800737
738 return true;
739}
740
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300741static int
742i9xx_select_p2_div(const intel_limit_t *limit,
743 const struct intel_crtc_state *crtc_state,
744 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800745{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300746 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800747
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200748 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800749 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100750 * For LVDS just rely on its current settings for dual-channel.
751 * We haven't figured out how to reliably set up different
752 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800753 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100754 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300755 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800756 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300757 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800758 } else {
759 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300760 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800761 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300762 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800763 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300764}
765
766static bool
767i9xx_find_best_dpll(const intel_limit_t *limit,
768 struct intel_crtc_state *crtc_state,
769 int target, int refclk, intel_clock_t *match_clock,
770 intel_clock_t *best_clock)
771{
772 struct drm_device *dev = crtc_state->base.crtc->dev;
773 intel_clock_t clock;
774 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800775
Akshay Joshi0206e352011-08-16 15:34:10 -0400776 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800777
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300778 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
779
Zhao Yakui42158662009-11-20 11:24:18 +0800780 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
781 clock.m1++) {
782 for (clock.m2 = limit->m2.min;
783 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200784 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800785 break;
786 for (clock.n = limit->n.min;
787 clock.n <= limit->n.max; clock.n++) {
788 for (clock.p1 = limit->p1.min;
789 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800790 int this_err;
791
Imre Deakdccbea32015-06-22 23:35:51 +0300792 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000793 if (!intel_PLL_is_valid(dev, limit,
794 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800795 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800796 if (match_clock &&
797 clock.p != match_clock->p)
798 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800799
800 this_err = abs(clock.dot - target);
801 if (this_err < err) {
802 *best_clock = clock;
803 err = this_err;
804 }
805 }
806 }
807 }
808 }
809
810 return (err != target);
811}
812
Ma Lingd4906092009-03-18 20:13:27 +0800813static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200814pnv_find_best_dpll(const intel_limit_t *limit,
815 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200816 int target, int refclk, intel_clock_t *match_clock,
817 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200818{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300819 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200820 intel_clock_t clock;
821 int err = target;
822
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200823 memset(best_clock, 0, sizeof(*best_clock));
824
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300825 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
826
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200827 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
828 clock.m1++) {
829 for (clock.m2 = limit->m2.min;
830 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200831 for (clock.n = limit->n.min;
832 clock.n <= limit->n.max; clock.n++) {
833 for (clock.p1 = limit->p1.min;
834 clock.p1 <= limit->p1.max; clock.p1++) {
835 int this_err;
836
Imre Deakdccbea32015-06-22 23:35:51 +0300837 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800838 if (!intel_PLL_is_valid(dev, limit,
839 &clock))
840 continue;
841 if (match_clock &&
842 clock.p != match_clock->p)
843 continue;
844
845 this_err = abs(clock.dot - target);
846 if (this_err < err) {
847 *best_clock = clock;
848 err = this_err;
849 }
850 }
851 }
852 }
853 }
854
855 return (err != target);
856}
857
Ma Lingd4906092009-03-18 20:13:27 +0800858static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200859g4x_find_best_dpll(const intel_limit_t *limit,
860 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200861 int target, int refclk, intel_clock_t *match_clock,
862 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800863{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300864 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800865 intel_clock_t clock;
866 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300867 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400868 /* approximately equals target * 0.00585 */
869 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800870
871 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300872
873 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
874
Ma Lingd4906092009-03-18 20:13:27 +0800875 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200876 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800877 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200878 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800879 for (clock.m1 = limit->m1.max;
880 clock.m1 >= limit->m1.min; clock.m1--) {
881 for (clock.m2 = limit->m2.max;
882 clock.m2 >= limit->m2.min; clock.m2--) {
883 for (clock.p1 = limit->p1.max;
884 clock.p1 >= limit->p1.min; clock.p1--) {
885 int this_err;
886
Imre Deakdccbea32015-06-22 23:35:51 +0300887 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000888 if (!intel_PLL_is_valid(dev, limit,
889 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800890 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000891
892 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800893 if (this_err < err_most) {
894 *best_clock = clock;
895 err_most = this_err;
896 max_n = clock.n;
897 found = true;
898 }
899 }
900 }
901 }
902 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800903 return found;
904}
Ma Lingd4906092009-03-18 20:13:27 +0800905
Imre Deakd5dd62b2015-03-17 11:40:03 +0200906/*
907 * Check if the calculated PLL configuration is more optimal compared to the
908 * best configuration and error found so far. Return the calculated error.
909 */
910static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
911 const intel_clock_t *calculated_clock,
912 const intel_clock_t *best_clock,
913 unsigned int best_error_ppm,
914 unsigned int *error_ppm)
915{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200916 /*
917 * For CHV ignore the error and consider only the P value.
918 * Prefer a bigger P value based on HW requirements.
919 */
920 if (IS_CHERRYVIEW(dev)) {
921 *error_ppm = 0;
922
923 return calculated_clock->p > best_clock->p;
924 }
925
Imre Deak24be4e42015-03-17 11:40:04 +0200926 if (WARN_ON_ONCE(!target_freq))
927 return false;
928
Imre Deakd5dd62b2015-03-17 11:40:03 +0200929 *error_ppm = div_u64(1000000ULL *
930 abs(target_freq - calculated_clock->dot),
931 target_freq);
932 /*
933 * Prefer a better P value over a better (smaller) error if the error
934 * is small. Ensure this preference for future configurations too by
935 * setting the error to 0.
936 */
937 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
938 *error_ppm = 0;
939
940 return true;
941 }
942
943 return *error_ppm + 10 < best_error_ppm;
944}
945
Zhenyu Wang2c072452009-06-05 15:38:42 +0800946static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200947vlv_find_best_dpll(const intel_limit_t *limit,
948 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200949 int target, int refclk, intel_clock_t *match_clock,
950 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700951{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200952 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300953 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300954 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300955 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300956 /* min update 19.2 MHz */
957 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300958 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700959
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300960 target *= 5; /* fast clock */
961
962 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700963
964 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300965 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300966 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300967 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300968 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300969 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700970 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300971 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200972 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300973
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300974 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
975 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300976
Imre Deakdccbea32015-06-22 23:35:51 +0300977 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300978
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300979 if (!intel_PLL_is_valid(dev, limit,
980 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300981 continue;
982
Imre Deakd5dd62b2015-03-17 11:40:03 +0200983 if (!vlv_PLL_is_optimal(dev, target,
984 &clock,
985 best_clock,
986 bestppm, &ppm))
987 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300988
Imre Deakd5dd62b2015-03-17 11:40:03 +0200989 *best_clock = clock;
990 bestppm = ppm;
991 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700992 }
993 }
994 }
995 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700996
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300997 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700998}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700999
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001000static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02001001chv_find_best_dpll(const intel_limit_t *limit,
1002 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001003 int target, int refclk, intel_clock_t *match_clock,
1004 intel_clock_t *best_clock)
1005{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02001006 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03001007 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +02001008 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001009 intel_clock_t clock;
1010 uint64_t m2;
1011 int found = false;
1012
1013 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +02001014 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001015
1016 /*
1017 * Based on hardware doc, the n always set to 1, and m1 always
1018 * set to 2. If requires to support 200Mhz refclk, we need to
1019 * revisit this because n may not 1 anymore.
1020 */
1021 clock.n = 1, clock.m1 = 2;
1022 target *= 5; /* fast clock */
1023
1024 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1025 for (clock.p2 = limit->p2.p2_fast;
1026 clock.p2 >= limit->p2.p2_slow;
1027 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001028 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001029
1030 clock.p = clock.p1 * clock.p2;
1031
1032 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1033 clock.n) << 22, refclk * clock.m1);
1034
1035 if (m2 > INT_MAX/clock.m1)
1036 continue;
1037
1038 clock.m2 = m2;
1039
Imre Deakdccbea32015-06-22 23:35:51 +03001040 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001041
1042 if (!intel_PLL_is_valid(dev, limit, &clock))
1043 continue;
1044
Imre Deak9ca3ba02015-03-17 11:40:05 +02001045 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1046 best_error_ppm, &error_ppm))
1047 continue;
1048
1049 *best_clock = clock;
1050 best_error_ppm = error_ppm;
1051 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001052 }
1053 }
1054
1055 return found;
1056}
1057
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001058bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1059 intel_clock_t *best_clock)
1060{
1061 int refclk = i9xx_get_refclk(crtc_state, 0);
1062
1063 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
1064 target_clock, refclk, NULL, best_clock);
1065}
1066
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001067bool intel_crtc_active(struct drm_crtc *crtc)
1068{
1069 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1070
1071 /* Be paranoid as we can arrive here with only partial
1072 * state retrieved from the hardware during setup.
1073 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001074 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001075 * as Haswell has gained clock readout/fastboot support.
1076 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001077 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001078 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001079 *
1080 * FIXME: The intel_crtc->active here should be switched to
1081 * crtc->state->active once we have proper CRTC states wired up
1082 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001083 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001084 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001085 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001086}
1087
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001088enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1089 enum pipe pipe)
1090{
1091 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1092 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1093
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001094 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001095}
1096
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001097static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1098{
1099 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001100 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001101 u32 line1, line2;
1102 u32 line_mask;
1103
1104 if (IS_GEN2(dev))
1105 line_mask = DSL_LINEMASK_GEN2;
1106 else
1107 line_mask = DSL_LINEMASK_GEN3;
1108
1109 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001110 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001111 line2 = I915_READ(reg) & line_mask;
1112
1113 return line1 == line2;
1114}
1115
Keith Packardab7ad7f2010-10-03 00:33:06 -07001116/*
1117 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001118 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001119 *
1120 * After disabling a pipe, we can't wait for vblank in the usual way,
1121 * spinning on the vblank interrupt status bit, since we won't actually
1122 * see an interrupt when the pipe is disabled.
1123 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001124 * On Gen4 and above:
1125 * wait for the pipe register state bit to turn off
1126 *
1127 * Otherwise:
1128 * wait for the display line value to settle (it usually
1129 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001130 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001131 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001132static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001133{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001134 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001135 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001136 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001137 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001138
Keith Packardab7ad7f2010-10-03 00:33:06 -07001139 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001140 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001141
Keith Packardab7ad7f2010-10-03 00:33:06 -07001142 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001143 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1144 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001145 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001146 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001147 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001148 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001149 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001150 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001151}
1152
Jesse Barnesb24e7172011-01-04 15:09:30 -08001153/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001154void assert_pll(struct drm_i915_private *dev_priv,
1155 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001156{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001157 u32 val;
1158 bool cur_state;
1159
Ville Syrjälä649636e2015-09-22 19:50:01 +03001160 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001161 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001162 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001163 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001164 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001165}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001166
Jani Nikula23538ef2013-08-27 15:12:22 +03001167/* XXX: the dsi pll is shared between MIPI DSI ports */
1168static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1169{
1170 u32 val;
1171 bool cur_state;
1172
Ville Syrjäläa5805162015-05-26 20:42:30 +03001173 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001174 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001175 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001176
1177 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001178 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001179 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001180 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001181}
1182#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1183#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1184
Daniel Vetter55607e82013-06-16 21:42:39 +02001185struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001186intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001187{
Daniel Vettere2b78262013-06-07 23:10:03 +02001188 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1189
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001190 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001191 return NULL;
1192
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001193 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001194}
1195
Jesse Barnesb24e7172011-01-04 15:09:30 -08001196/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001197void assert_shared_dpll(struct drm_i915_private *dev_priv,
1198 struct intel_shared_dpll *pll,
1199 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001200{
Jesse Barnes040484a2011-01-03 12:14:26 -08001201 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001202 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001203
Jani Nikula87ad3212016-01-14 12:53:34 +02001204 if (WARN(!pll, "asserting DPLL %s with no DPLL\n", onoff(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001205 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001206
Daniel Vetter53589012013-06-05 13:34:16 +02001207 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001208 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001209 "%s assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001210 pll->name, onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001211}
Jesse Barnes040484a2011-01-03 12:14:26 -08001212
1213static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1214 enum pipe pipe, bool state)
1215{
Jesse Barnes040484a2011-01-03 12:14:26 -08001216 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001217 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1218 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001219
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001220 if (HAS_DDI(dev_priv->dev)) {
1221 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001222 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001223 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001224 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001225 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001226 cur_state = !!(val & FDI_TX_ENABLE);
1227 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001228 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001229 "FDI TX state 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#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1233#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1234
1235static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1236 enum pipe pipe, bool state)
1237{
Jesse Barnes040484a2011-01-03 12:14:26 -08001238 u32 val;
1239 bool cur_state;
1240
Ville Syrjälä649636e2015-09-22 19:50:01 +03001241 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001242 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001243 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001244 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001245 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001246}
1247#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1248#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1249
1250static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1251 enum pipe pipe)
1252{
Jesse Barnes040484a2011-01-03 12:14:26 -08001253 u32 val;
1254
1255 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001256 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001257 return;
1258
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001259 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001260 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001261 return;
1262
Ville Syrjälä649636e2015-09-22 19:50:01 +03001263 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001264 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 -08001265}
1266
Daniel Vetter55607e82013-06-16 21:42:39 +02001267void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1268 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001269{
Jesse Barnes040484a2011-01-03 12:14:26 -08001270 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001271 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001272
Ville Syrjälä649636e2015-09-22 19:50:01 +03001273 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001274 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001275 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001276 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001277 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001278}
1279
Daniel Vetterb680c372014-09-19 18:27:27 +02001280void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1281 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001282{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001283 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001284 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001285 u32 val;
1286 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001287 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001288
Jani Nikulabedd4db2014-08-22 15:04:13 +03001289 if (WARN_ON(HAS_DDI(dev)))
1290 return;
1291
1292 if (HAS_PCH_SPLIT(dev)) {
1293 u32 port_sel;
1294
Jesse Barnesea0760c2011-01-04 15:09:32 -08001295 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001296 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1297
1298 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1299 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1300 panel_pipe = PIPE_B;
1301 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001302 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001303 /* presumably write lock depends on pipe, not port select */
1304 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1305 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001306 } else {
1307 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001308 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1309 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001310 }
1311
1312 val = I915_READ(pp_reg);
1313 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001314 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001315 locked = false;
1316
Rob Clarke2c719b2014-12-15 13:56:32 -05001317 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001318 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001319 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001320}
1321
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001322static void assert_cursor(struct drm_i915_private *dev_priv,
1323 enum pipe pipe, bool state)
1324{
1325 struct drm_device *dev = dev_priv->dev;
1326 bool cur_state;
1327
Paulo Zanonid9d82082014-02-27 16:30:56 -03001328 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001329 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001330 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001331 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001332
Rob Clarke2c719b2014-12-15 13:56:32 -05001333 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001334 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001335 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001336}
1337#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1338#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1339
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001340void assert_pipe(struct drm_i915_private *dev_priv,
1341 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001342{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001343 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001344 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1345 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001346 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001347
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001348 /* if we need the pipe quirk it must be always on */
1349 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1350 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001351 state = true;
1352
Imre Deak4feed0e2016-02-12 18:55:14 +02001353 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1354 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001355 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001356 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001357
1358 intel_display_power_put(dev_priv, power_domain);
1359 } else {
1360 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001361 }
1362
Rob Clarke2c719b2014-12-15 13:56:32 -05001363 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001364 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001365 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001366}
1367
Chris Wilson931872f2012-01-16 23:01:13 +00001368static void assert_plane(struct drm_i915_private *dev_priv,
1369 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001370{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001371 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001372 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001373
Ville Syrjälä649636e2015-09-22 19:50:01 +03001374 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001375 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001376 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001377 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001378 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001379}
1380
Chris Wilson931872f2012-01-16 23:01:13 +00001381#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1382#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1383
Jesse Barnesb24e7172011-01-04 15:09:30 -08001384static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1385 enum pipe pipe)
1386{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001387 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001388 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001389
Ville Syrjälä653e1022013-06-04 13:49:05 +03001390 /* Primary planes are fixed to pipes on gen4+ */
1391 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001392 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001393 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001394 "plane %c assertion failure, should be disabled but not\n",
1395 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001396 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001397 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001398
Jesse Barnesb24e7172011-01-04 15:09:30 -08001399 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001400 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001401 u32 val = I915_READ(DSPCNTR(i));
1402 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001403 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001404 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001405 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1406 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001407 }
1408}
1409
Jesse Barnes19332d72013-03-28 09:55:38 -07001410static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1411 enum pipe pipe)
1412{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001413 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001414 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001415
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001416 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001417 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001418 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001419 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001420 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1421 sprite, pipe_name(pipe));
1422 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001423 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001424 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001425 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001426 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001427 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001428 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001429 }
1430 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001431 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001432 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001433 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001434 plane_name(pipe), pipe_name(pipe));
1435 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001436 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001437 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001438 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1439 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001440 }
1441}
1442
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001443static void assert_vblank_disabled(struct drm_crtc *crtc)
1444{
Rob Clarke2c719b2014-12-15 13:56:32 -05001445 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001446 drm_crtc_vblank_put(crtc);
1447}
1448
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001449static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001450{
1451 u32 val;
1452 bool enabled;
1453
Rob Clarke2c719b2014-12-15 13:56:32 -05001454 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001455
Jesse Barnes92f25842011-01-04 15:09:34 -08001456 val = I915_READ(PCH_DREF_CONTROL);
1457 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1458 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001459 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001460}
1461
Daniel Vetterab9412b2013-05-03 11:49:46 +02001462static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1463 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001464{
Jesse Barnes92f25842011-01-04 15:09:34 -08001465 u32 val;
1466 bool enabled;
1467
Ville Syrjälä649636e2015-09-22 19:50:01 +03001468 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001469 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001470 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001471 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1472 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001473}
1474
Keith Packard4e634382011-08-06 10:39:45 -07001475static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1476 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001477{
1478 if ((val & DP_PORT_EN) == 0)
1479 return false;
1480
1481 if (HAS_PCH_CPT(dev_priv->dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001482 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001483 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1484 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001485 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1486 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1487 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001488 } else {
1489 if ((val & DP_PIPE_MASK) != (pipe << 30))
1490 return false;
1491 }
1492 return true;
1493}
1494
Keith Packard1519b992011-08-06 10:35:34 -07001495static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1496 enum pipe pipe, u32 val)
1497{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001498 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001499 return false;
1500
1501 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001502 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001503 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001504 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1505 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1506 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001507 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001508 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001509 return false;
1510 }
1511 return true;
1512}
1513
1514static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1515 enum pipe pipe, u32 val)
1516{
1517 if ((val & LVDS_PORT_EN) == 0)
1518 return false;
1519
1520 if (HAS_PCH_CPT(dev_priv->dev)) {
1521 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1522 return false;
1523 } else {
1524 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1525 return false;
1526 }
1527 return true;
1528}
1529
1530static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1531 enum pipe pipe, u32 val)
1532{
1533 if ((val & ADPA_DAC_ENABLE) == 0)
1534 return false;
1535 if (HAS_PCH_CPT(dev_priv->dev)) {
1536 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1537 return false;
1538 } else {
1539 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1540 return false;
1541 }
1542 return true;
1543}
1544
Jesse Barnes291906f2011-02-02 12:28:03 -08001545static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001546 enum pipe pipe, i915_reg_t reg,
1547 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001548{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001549 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001550 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001551 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001552 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001553
Rob Clarke2c719b2014-12-15 13:56:32 -05001554 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001555 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001556 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001557}
1558
1559static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001560 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001561{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001562 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001563 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001564 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001565 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001566
Rob Clarke2c719b2014-12-15 13:56:32 -05001567 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001568 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001569 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001570}
1571
1572static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1573 enum pipe pipe)
1574{
Jesse Barnes291906f2011-02-02 12:28:03 -08001575 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001576
Keith Packardf0575e92011-07-25 22:12:43 -07001577 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1578 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1579 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001580
Ville Syrjälä649636e2015-09-22 19:50:01 +03001581 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001582 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001583 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001584 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001585
Ville Syrjälä649636e2015-09-22 19:50:01 +03001586 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001587 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001588 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001589 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001590
Paulo Zanonie2debe92013-02-18 19:00:27 -03001591 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1592 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1593 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001594}
1595
Ville Syrjäläd288f652014-10-28 13:20:22 +02001596static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001597 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001598{
Daniel Vetter426115c2013-07-11 22:13:42 +02001599 struct drm_device *dev = crtc->base.dev;
1600 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001601 i915_reg_t reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001602 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001603
Daniel Vetter426115c2013-07-11 22:13:42 +02001604 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001605
Daniel Vetter87442f72013-06-06 00:52:17 +02001606 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001607 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001608 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001609
Daniel Vetter426115c2013-07-11 22:13:42 +02001610 I915_WRITE(reg, dpll);
1611 POSTING_READ(reg);
1612 udelay(150);
1613
1614 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1615 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1616
Ville Syrjäläd288f652014-10-28 13:20:22 +02001617 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001618 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001619
1620 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001621 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001622 POSTING_READ(reg);
1623 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001624 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001625 POSTING_READ(reg);
1626 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001627 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001628 POSTING_READ(reg);
1629 udelay(150); /* wait for warmup */
1630}
1631
Ville Syrjäläd288f652014-10-28 13:20:22 +02001632static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001633 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001634{
1635 struct drm_device *dev = crtc->base.dev;
1636 struct drm_i915_private *dev_priv = dev->dev_private;
1637 int pipe = crtc->pipe;
1638 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001639 u32 tmp;
1640
1641 assert_pipe_disabled(dev_priv, crtc->pipe);
1642
Ville Syrjäläa5805162015-05-26 20:42:30 +03001643 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001644
1645 /* Enable back the 10bit clock to display controller */
1646 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1647 tmp |= DPIO_DCLKP_EN;
1648 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1649
Ville Syrjälä54433e92015-05-26 20:42:31 +03001650 mutex_unlock(&dev_priv->sb_lock);
1651
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001652 /*
1653 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1654 */
1655 udelay(1);
1656
1657 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001658 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001659
1660 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001661 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001662 DRM_ERROR("PLL %d failed to lock\n", pipe);
1663
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001664 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001665 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001666 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001667}
1668
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001669static int intel_num_dvo_pipes(struct drm_device *dev)
1670{
1671 struct intel_crtc *crtc;
1672 int count = 0;
1673
1674 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001675 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001676 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001677
1678 return count;
1679}
1680
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001681static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001682{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001683 struct drm_device *dev = crtc->base.dev;
1684 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001685 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001686 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001687
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001688 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001689
1690 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001691 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001692
1693 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001694 if (IS_MOBILE(dev) && !IS_I830(dev))
1695 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001696
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001697 /* Enable DVO 2x clock on both PLLs if necessary */
1698 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1699 /*
1700 * It appears to be important that we don't enable this
1701 * for the current pipe before otherwise configuring the
1702 * PLL. No idea how this should be handled if multiple
1703 * DVO outputs are enabled simultaneosly.
1704 */
1705 dpll |= DPLL_DVO_2X_MODE;
1706 I915_WRITE(DPLL(!crtc->pipe),
1707 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1708 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001709
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001710 /*
1711 * Apparently we need to have VGA mode enabled prior to changing
1712 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1713 * dividers, even though the register value does change.
1714 */
1715 I915_WRITE(reg, 0);
1716
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001717 I915_WRITE(reg, dpll);
1718
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001719 /* Wait for the clocks to stabilize. */
1720 POSTING_READ(reg);
1721 udelay(150);
1722
1723 if (INTEL_INFO(dev)->gen >= 4) {
1724 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001725 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001726 } else {
1727 /* The pixel multiplier can only be updated once the
1728 * DPLL is enabled and the clocks are stable.
1729 *
1730 * So write it again.
1731 */
1732 I915_WRITE(reg, dpll);
1733 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001734
1735 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001736 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001737 POSTING_READ(reg);
1738 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001739 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001740 POSTING_READ(reg);
1741 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001742 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001743 POSTING_READ(reg);
1744 udelay(150); /* wait for warmup */
1745}
1746
1747/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001748 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001749 * @dev_priv: i915 private structure
1750 * @pipe: pipe PLL to disable
1751 *
1752 * Disable the PLL for @pipe, making sure the pipe is off first.
1753 *
1754 * Note! This is for pre-ILK only.
1755 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001756static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001757{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001758 struct drm_device *dev = crtc->base.dev;
1759 struct drm_i915_private *dev_priv = dev->dev_private;
1760 enum pipe pipe = crtc->pipe;
1761
1762 /* Disable DVO 2x clock on both PLLs if necessary */
1763 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001764 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001765 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001766 I915_WRITE(DPLL(PIPE_B),
1767 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1768 I915_WRITE(DPLL(PIPE_A),
1769 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1770 }
1771
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001772 /* Don't disable pipe or pipe PLLs if needed */
1773 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1774 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001775 return;
1776
1777 /* Make sure the pipe isn't still relying on us */
1778 assert_pipe_disabled(dev_priv, pipe);
1779
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001780 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001781 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001782}
1783
Jesse Barnesf6071162013-10-01 10:41:38 -07001784static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1785{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001786 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001787
1788 /* Make sure the pipe isn't still relying on us */
1789 assert_pipe_disabled(dev_priv, pipe);
1790
Imre Deake5cbfbf2014-01-09 17:08:16 +02001791 /*
1792 * Leave integrated clock source and reference clock enabled for pipe B.
1793 * The latter is needed for VGA hotplug / manual detection.
1794 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001795 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001796 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001797 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001798 I915_WRITE(DPLL(pipe), val);
1799 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001800
1801}
1802
1803static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1804{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001805 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001806 u32 val;
1807
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001808 /* Make sure the pipe isn't still relying on us */
1809 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001810
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001811 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001812 val = DPLL_SSC_REF_CLK_CHV |
1813 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001814 if (pipe != PIPE_A)
1815 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1816 I915_WRITE(DPLL(pipe), val);
1817 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001818
Ville Syrjäläa5805162015-05-26 20:42:30 +03001819 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001820
1821 /* Disable 10bit clock to display controller */
1822 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1823 val &= ~DPIO_DCLKP_EN;
1824 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1825
Ville Syrjäläa5805162015-05-26 20:42:30 +03001826 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001827}
1828
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001829void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001830 struct intel_digital_port *dport,
1831 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001832{
1833 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001834 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001835
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001836 switch (dport->port) {
1837 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001838 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001839 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001840 break;
1841 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001842 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001843 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001844 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001845 break;
1846 case PORT_D:
1847 port_mask = DPLL_PORTD_READY_MASK;
1848 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001849 break;
1850 default:
1851 BUG();
1852 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001853
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001854 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1855 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1856 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001857}
1858
Daniel Vetterb14b1052014-04-24 23:55:13 +02001859static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1860{
1861 struct drm_device *dev = crtc->base.dev;
1862 struct drm_i915_private *dev_priv = dev->dev_private;
1863 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1864
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001865 if (WARN_ON(pll == NULL))
1866 return;
1867
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001868 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001869 if (pll->active == 0) {
1870 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1871 WARN_ON(pll->on);
1872 assert_shared_dpll_disabled(dev_priv, pll);
1873
1874 pll->mode_set(dev_priv, pll);
1875 }
1876}
1877
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001878/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001879 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001880 * @dev_priv: i915 private structure
1881 * @pipe: pipe PLL to enable
1882 *
1883 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1884 * drives the transcoder clock.
1885 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001886static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001887{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001888 struct drm_device *dev = crtc->base.dev;
1889 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001890 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001891
Daniel Vetter87a875b2013-06-05 13:34:19 +02001892 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001893 return;
1894
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001895 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001896 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001897
Damien Lespiau74dd6922014-07-29 18:06:17 +01001898 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001899 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001900 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001901
Daniel Vettercdbd2312013-06-05 13:34:03 +02001902 if (pll->active++) {
1903 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001904 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001905 return;
1906 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001907 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001908
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001909 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1910
Daniel Vetter46edb022013-06-05 13:34:12 +02001911 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001912 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001913 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001914}
1915
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001916static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001917{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001918 struct drm_device *dev = crtc->base.dev;
1919 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001920 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001921
Jesse Barnes92f25842011-01-04 15:09:34 -08001922 /* PCH only available on ILK+ */
Jesse Barnes80aa9312015-08-03 13:09:11 -07001923 if (INTEL_INFO(dev)->gen < 5)
1924 return;
1925
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001926 if (pll == NULL)
1927 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001928
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001929 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
Chris Wilson48da64a2012-05-13 20:16:12 +01001930 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001931
Daniel Vetter46edb022013-06-05 13:34:12 +02001932 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1933 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001934 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001935
Chris Wilson48da64a2012-05-13 20:16:12 +01001936 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001937 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001938 return;
1939 }
1940
Daniel Vettere9d69442013-06-05 13:34:15 +02001941 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001942 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001943 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001944 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001945
Daniel Vetter46edb022013-06-05 13:34:12 +02001946 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001947 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001948 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001949
1950 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001951}
1952
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001953static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1954 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001955{
Daniel Vetter23670b322012-11-01 09:15:30 +01001956 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001957 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001958 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001959 i915_reg_t reg;
1960 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001961
1962 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001963 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001964
1965 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001966 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001967 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001968
1969 /* FDI must be feeding us bits for PCH ports */
1970 assert_fdi_tx_enabled(dev_priv, pipe);
1971 assert_fdi_rx_enabled(dev_priv, pipe);
1972
Daniel Vetter23670b322012-11-01 09:15:30 +01001973 if (HAS_PCH_CPT(dev)) {
1974 /* Workaround: Set the timing override bit before enabling the
1975 * pch transcoder. */
1976 reg = TRANS_CHICKEN2(pipe);
1977 val = I915_READ(reg);
1978 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1979 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001980 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001981
Daniel Vetterab9412b2013-05-03 11:49:46 +02001982 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001983 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001984 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001985
1986 if (HAS_PCH_IBX(dev_priv->dev)) {
1987 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001988 * Make the BPC in transcoder be consistent with
1989 * that in pipeconf reg. For HDMI we must use 8bpc
1990 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001991 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001992 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001993 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1994 val |= PIPECONF_8BPC;
1995 else
1996 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001997 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001998
1999 val &= ~TRANS_INTERLACE_MASK;
2000 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002001 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002002 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002003 val |= TRANS_LEGACY_INTERLACED_ILK;
2004 else
2005 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002006 else
2007 val |= TRANS_PROGRESSIVE;
2008
Jesse Barnes040484a2011-01-03 12:14:26 -08002009 I915_WRITE(reg, val | TRANS_ENABLE);
2010 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002011 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002012}
2013
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002014static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002015 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002016{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002017 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002018
2019 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002020 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002021
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002022 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002023 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002024 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002025
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002026 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002027 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01002028 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002029 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002030
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002031 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002032 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002033
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002034 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2035 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002036 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002037 else
2038 val |= TRANS_PROGRESSIVE;
2039
Daniel Vetterab9412b2013-05-03 11:49:46 +02002040 I915_WRITE(LPT_TRANSCONF, val);
2041 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002042 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002043}
2044
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002045static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2046 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002047{
Daniel Vetter23670b322012-11-01 09:15:30 +01002048 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002049 i915_reg_t reg;
2050 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002051
2052 /* FDI relies on the transcoder */
2053 assert_fdi_tx_disabled(dev_priv, pipe);
2054 assert_fdi_rx_disabled(dev_priv, pipe);
2055
Jesse Barnes291906f2011-02-02 12:28:03 -08002056 /* Ports must be off as well */
2057 assert_pch_ports_disabled(dev_priv, pipe);
2058
Daniel Vetterab9412b2013-05-03 11:49:46 +02002059 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002060 val = I915_READ(reg);
2061 val &= ~TRANS_ENABLE;
2062 I915_WRITE(reg, val);
2063 /* wait for PCH transcoder off, transcoder state */
2064 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002065 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002066
Ville Syrjäläc4656132015-10-29 21:25:56 +02002067 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01002068 /* Workaround: Clear the timing override chicken bit again. */
2069 reg = TRANS_CHICKEN2(pipe);
2070 val = I915_READ(reg);
2071 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2072 I915_WRITE(reg, val);
2073 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002074}
2075
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002076static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002077{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002078 u32 val;
2079
Daniel Vetterab9412b2013-05-03 11:49:46 +02002080 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002081 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002082 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002083 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002084 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002085 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002086
2087 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002088 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01002089 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002090 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002091}
2092
2093/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002094 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002095 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002096 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002097 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002098 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002099 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002100static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002101{
Paulo Zanoni03722642014-01-17 13:51:09 -02002102 struct drm_device *dev = crtc->base.dev;
2103 struct drm_i915_private *dev_priv = dev->dev_private;
2104 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02002105 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01002106 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002107 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002108 u32 val;
2109
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002110 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
2111
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002112 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002113 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002114 assert_sprites_disabled(dev_priv, pipe);
2115
Paulo Zanoni681e5812012-12-06 11:12:38 -02002116 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002117 pch_transcoder = TRANSCODER_A;
2118 else
2119 pch_transcoder = pipe;
2120
Jesse Barnesb24e7172011-01-04 15:09:30 -08002121 /*
2122 * A pipe without a PLL won't actually be able to drive bits from
2123 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2124 * need the check.
2125 */
Imre Deak50360402015-01-16 00:55:16 -08002126 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Jani Nikulaa65347b2015-11-27 12:21:46 +02002127 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03002128 assert_dsi_pll_enabled(dev_priv);
2129 else
2130 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002131 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002132 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002133 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002134 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002135 assert_fdi_tx_pll_enabled(dev_priv,
2136 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002137 }
2138 /* FIXME: assert CPU port conditions for SNB+ */
2139 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002140
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002141 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002142 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002143 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002144 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2145 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002146 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002147 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002148
2149 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002150 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002151
2152 /*
2153 * Until the pipe starts DSL will read as 0, which would cause
2154 * an apparent vblank timestamp jump, which messes up also the
2155 * frame count when it's derived from the timestamps. So let's
2156 * wait for the pipe to start properly before we call
2157 * drm_crtc_vblank_on()
2158 */
2159 if (dev->max_vblank_count == 0 &&
2160 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2161 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002162}
2163
2164/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002165 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002166 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002167 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002168 * Disable the pipe of @crtc, making sure that various hardware
2169 * specific requirements are met, if applicable, e.g. plane
2170 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002171 *
2172 * Will wait until the pipe has shut down before returning.
2173 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002174static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002175{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002176 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002177 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002178 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002179 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002180 u32 val;
2181
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002182 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2183
Jesse Barnesb24e7172011-01-04 15:09:30 -08002184 /*
2185 * Make sure planes won't keep trying to pump pixels to us,
2186 * or we might hang the display.
2187 */
2188 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002189 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002190 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002191
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002192 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002193 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002194 if ((val & PIPECONF_ENABLE) == 0)
2195 return;
2196
Ville Syrjälä67adc642014-08-15 01:21:57 +03002197 /*
2198 * Double wide has implications for planes
2199 * so best keep it disabled when not needed.
2200 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002201 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002202 val &= ~PIPECONF_DOUBLE_WIDE;
2203
2204 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002205 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2206 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002207 val &= ~PIPECONF_ENABLE;
2208
2209 I915_WRITE(reg, val);
2210 if ((val & PIPECONF_ENABLE) == 0)
2211 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002212}
2213
Chris Wilson693db182013-03-05 14:52:39 +00002214static bool need_vtd_wa(struct drm_device *dev)
2215{
2216#ifdef CONFIG_INTEL_IOMMU
2217 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2218 return true;
2219#endif
2220 return false;
2221}
2222
Ville Syrjälä832be822016-01-12 21:08:33 +02002223static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2224{
2225 return IS_GEN2(dev_priv) ? 2048 : 4096;
2226}
2227
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002228static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2229 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002230{
2231 switch (fb_modifier) {
2232 case DRM_FORMAT_MOD_NONE:
2233 return cpp;
2234 case I915_FORMAT_MOD_X_TILED:
2235 if (IS_GEN2(dev_priv))
2236 return 128;
2237 else
2238 return 512;
2239 case I915_FORMAT_MOD_Y_TILED:
2240 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2241 return 128;
2242 else
2243 return 512;
2244 case I915_FORMAT_MOD_Yf_TILED:
2245 switch (cpp) {
2246 case 1:
2247 return 64;
2248 case 2:
2249 case 4:
2250 return 128;
2251 case 8:
2252 case 16:
2253 return 256;
2254 default:
2255 MISSING_CASE(cpp);
2256 return cpp;
2257 }
2258 break;
2259 default:
2260 MISSING_CASE(fb_modifier);
2261 return cpp;
2262 }
2263}
2264
Ville Syrjälä832be822016-01-12 21:08:33 +02002265unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2266 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002267{
Ville Syrjälä832be822016-01-12 21:08:33 +02002268 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2269 return 1;
2270 else
2271 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002272 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002273}
2274
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002275/* Return the tile dimensions in pixel units */
2276static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2277 unsigned int *tile_width,
2278 unsigned int *tile_height,
2279 uint64_t fb_modifier,
2280 unsigned int cpp)
2281{
2282 unsigned int tile_width_bytes =
2283 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2284
2285 *tile_width = tile_width_bytes / cpp;
2286 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2287}
2288
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002289unsigned int
2290intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002291 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002292{
Ville Syrjälä832be822016-01-12 21:08:33 +02002293 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2294 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2295
2296 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002297}
2298
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002299unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2300{
2301 unsigned int size = 0;
2302 int i;
2303
2304 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2305 size += rot_info->plane[i].width * rot_info->plane[i].height;
2306
2307 return size;
2308}
2309
Daniel Vetter75c82a52015-10-14 16:51:04 +02002310static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002311intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2312 const struct drm_framebuffer *fb,
2313 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002314{
Ville Syrjälä832be822016-01-12 21:08:33 +02002315 struct drm_i915_private *dev_priv = to_i915(fb->dev);
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002316 struct intel_rotation_info *info = &view->params.rotated;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002317 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002318
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002319 *view = i915_ggtt_view_normal;
2320
Ville Syrjälä3465c582016-02-15 22:54:43 +02002321 if (!intel_rotation_90_or_270(rotation))
Daniel Vetter75c82a52015-10-14 16:51:04 +02002322 return;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002323
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002324 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002325
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002326 info->uv_offset = fb->offsets[1];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002327
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002328 tile_size = intel_tile_size(dev_priv);
2329
2330 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002331 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2332 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002333
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002334 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2335 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002336
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002337 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002338 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002339 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2340 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002341
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002342 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2343 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002344 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002345}
2346
Ville Syrjälä603525d2016-01-12 21:08:37 +02002347static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002348{
2349 if (INTEL_INFO(dev_priv)->gen >= 9)
2350 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002351 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002352 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002353 return 128 * 1024;
2354 else if (INTEL_INFO(dev_priv)->gen >= 4)
2355 return 4 * 1024;
2356 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002357 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002358}
2359
Ville Syrjälä603525d2016-01-12 21:08:37 +02002360static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2361 uint64_t fb_modifier)
2362{
2363 switch (fb_modifier) {
2364 case DRM_FORMAT_MOD_NONE:
2365 return intel_linear_alignment(dev_priv);
2366 case I915_FORMAT_MOD_X_TILED:
2367 if (INTEL_INFO(dev_priv)->gen >= 9)
2368 return 256 * 1024;
2369 return 0;
2370 case I915_FORMAT_MOD_Y_TILED:
2371 case I915_FORMAT_MOD_Yf_TILED:
2372 return 1 * 1024 * 1024;
2373 default:
2374 MISSING_CASE(fb_modifier);
2375 return 0;
2376 }
2377}
2378
Chris Wilson127bd2a2010-07-23 23:32:05 +01002379int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002380intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2381 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002382{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002383 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002384 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002385 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002386 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002387 u32 alignment;
2388 int ret;
2389
Matt Roperebcdd392014-07-09 16:22:11 -07002390 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2391
Ville Syrjälä603525d2016-01-12 21:08:37 +02002392 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002393
Ville Syrjälä3465c582016-02-15 22:54:43 +02002394 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002395
Chris Wilson693db182013-03-05 14:52:39 +00002396 /* Note that the w/a also requires 64 PTE of padding following the
2397 * bo. We currently fill all unused PTE with the shadow page and so
2398 * we should always have valid PTE following the scanout preventing
2399 * the VT-d warning.
2400 */
2401 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2402 alignment = 256 * 1024;
2403
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002404 /*
2405 * Global gtt pte registers are special registers which actually forward
2406 * writes to a chunk of system memory. Which means that there is no risk
2407 * that the register values disappear as soon as we call
2408 * intel_runtime_pm_put(), so it is correct to wrap only the
2409 * pin/unpin/fence and not more.
2410 */
2411 intel_runtime_pm_get(dev_priv);
2412
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002413 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2414 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002415 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002416 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002417
2418 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2419 * fence, whereas 965+ only requires a fence if using
2420 * framebuffer compression. For simplicity, we always install
2421 * a fence as the cost is not that onerous.
2422 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002423 if (view.type == I915_GGTT_VIEW_NORMAL) {
2424 ret = i915_gem_object_get_fence(obj);
2425 if (ret == -EDEADLK) {
2426 /*
2427 * -EDEADLK means there are no free fences
2428 * no pending flips.
2429 *
2430 * This is propagated to atomic, but it uses
2431 * -EDEADLK to force a locking recovery, so
2432 * change the returned error to -EBUSY.
2433 */
2434 ret = -EBUSY;
2435 goto err_unpin;
2436 } else if (ret)
2437 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002438
Vivek Kasireddy98072162015-10-29 18:54:38 -07002439 i915_gem_object_pin_fence(obj);
2440 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002441
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002442 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002443 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002444
2445err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002446 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002447err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002448 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002449 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002450}
2451
Ville Syrjälä3465c582016-02-15 22:54:43 +02002452static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002453{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002454 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002455 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002456
Matt Roperebcdd392014-07-09 16:22:11 -07002457 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2458
Ville Syrjälä3465c582016-02-15 22:54:43 +02002459 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002460
Vivek Kasireddy98072162015-10-29 18:54:38 -07002461 if (view.type == I915_GGTT_VIEW_NORMAL)
2462 i915_gem_object_unpin_fence(obj);
2463
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002464 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002465}
2466
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002467/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002468 * Adjust the tile offset by moving the difference into
2469 * the x/y offsets.
2470 *
2471 * Input tile dimensions and pitch must already be
2472 * rotated to match x and y, and in pixel units.
2473 */
2474static u32 intel_adjust_tile_offset(int *x, int *y,
2475 unsigned int tile_width,
2476 unsigned int tile_height,
2477 unsigned int tile_size,
2478 unsigned int pitch_tiles,
2479 u32 old_offset,
2480 u32 new_offset)
2481{
2482 unsigned int tiles;
2483
2484 WARN_ON(old_offset & (tile_size - 1));
2485 WARN_ON(new_offset & (tile_size - 1));
2486 WARN_ON(new_offset > old_offset);
2487
2488 tiles = (old_offset - new_offset) / tile_size;
2489
2490 *y += tiles / pitch_tiles * tile_height;
2491 *x += tiles % pitch_tiles * tile_width;
2492
2493 return new_offset;
2494}
2495
2496/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002497 * Computes the linear offset to the base tile and adjusts
2498 * x, y. bytes per pixel is assumed to be a power-of-two.
2499 *
2500 * In the 90/270 rotated case, x and y are assumed
2501 * to be already rotated to match the rotated GTT view, and
2502 * pitch is the tile_height aligned framebuffer height.
2503 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002504u32 intel_compute_tile_offset(int *x, int *y,
2505 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002506 unsigned int pitch,
2507 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002508{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002509 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2510 uint64_t fb_modifier = fb->modifier[plane];
2511 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002512 u32 offset, offset_aligned, alignment;
2513
2514 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2515 if (alignment)
2516 alignment--;
2517
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002518 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002519 unsigned int tile_size, tile_width, tile_height;
2520 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002521
Ville Syrjäläd8433102016-01-12 21:08:35 +02002522 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002523 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2524 fb_modifier, cpp);
2525
2526 if (intel_rotation_90_or_270(rotation)) {
2527 pitch_tiles = pitch / tile_height;
2528 swap(tile_width, tile_height);
2529 } else {
2530 pitch_tiles = pitch / (tile_width * cpp);
2531 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002532
Ville Syrjäläd8433102016-01-12 21:08:35 +02002533 tile_rows = *y / tile_height;
2534 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002535
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002536 tiles = *x / tile_width;
2537 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002538
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002539 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2540 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002541
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002542 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2543 tile_size, pitch_tiles,
2544 offset, offset_aligned);
2545 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002546 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002547 offset_aligned = offset & ~alignment;
2548
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002549 *y = (offset & alignment) / pitch;
2550 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002551 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002552
2553 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002554}
2555
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002556static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002557{
2558 switch (format) {
2559 case DISPPLANE_8BPP:
2560 return DRM_FORMAT_C8;
2561 case DISPPLANE_BGRX555:
2562 return DRM_FORMAT_XRGB1555;
2563 case DISPPLANE_BGRX565:
2564 return DRM_FORMAT_RGB565;
2565 default:
2566 case DISPPLANE_BGRX888:
2567 return DRM_FORMAT_XRGB8888;
2568 case DISPPLANE_RGBX888:
2569 return DRM_FORMAT_XBGR8888;
2570 case DISPPLANE_BGRX101010:
2571 return DRM_FORMAT_XRGB2101010;
2572 case DISPPLANE_RGBX101010:
2573 return DRM_FORMAT_XBGR2101010;
2574 }
2575}
2576
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002577static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2578{
2579 switch (format) {
2580 case PLANE_CTL_FORMAT_RGB_565:
2581 return DRM_FORMAT_RGB565;
2582 default:
2583 case PLANE_CTL_FORMAT_XRGB_8888:
2584 if (rgb_order) {
2585 if (alpha)
2586 return DRM_FORMAT_ABGR8888;
2587 else
2588 return DRM_FORMAT_XBGR8888;
2589 } else {
2590 if (alpha)
2591 return DRM_FORMAT_ARGB8888;
2592 else
2593 return DRM_FORMAT_XRGB8888;
2594 }
2595 case PLANE_CTL_FORMAT_XRGB_2101010:
2596 if (rgb_order)
2597 return DRM_FORMAT_XBGR2101010;
2598 else
2599 return DRM_FORMAT_XRGB2101010;
2600 }
2601}
2602
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002603static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002604intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2605 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002606{
2607 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002608 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002609 struct drm_i915_gem_object *obj = NULL;
2610 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002611 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002612 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2613 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2614 PAGE_SIZE);
2615
2616 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002617
Chris Wilsonff2652e2014-03-10 08:07:02 +00002618 if (plane_config->size == 0)
2619 return false;
2620
Paulo Zanoni3badb492015-09-23 12:52:23 -03002621 /* If the FB is too big, just don't use it since fbdev is not very
2622 * important and we should probably use that space with FBC or other
2623 * features. */
2624 if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
2625 return false;
2626
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002627 mutex_lock(&dev->struct_mutex);
2628
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002629 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2630 base_aligned,
2631 base_aligned,
2632 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002633 if (!obj) {
2634 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002635 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002636 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002637
Damien Lespiau49af4492015-01-20 12:51:44 +00002638 obj->tiling_mode = plane_config->tiling;
2639 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002640 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002641
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002642 mode_cmd.pixel_format = fb->pixel_format;
2643 mode_cmd.width = fb->width;
2644 mode_cmd.height = fb->height;
2645 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002646 mode_cmd.modifier[0] = fb->modifier[0];
2647 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002648
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002649 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002650 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002651 DRM_DEBUG_KMS("intel fb init failed\n");
2652 goto out_unref_obj;
2653 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002654
Jesse Barnes46f297f2014-03-07 08:57:48 -08002655 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002656
Daniel Vetterf6936e22015-03-26 12:17:05 +01002657 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002658 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002659
2660out_unref_obj:
2661 drm_gem_object_unreference(&obj->base);
2662 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002663 return false;
2664}
2665
Matt Roperafd65eb2015-02-03 13:10:04 -08002666/* Update plane->state->fb to match plane->fb after driver-internal updates */
2667static void
2668update_state_fb(struct drm_plane *plane)
2669{
2670 if (plane->fb == plane->state->fb)
2671 return;
2672
2673 if (plane->state->fb)
2674 drm_framebuffer_unreference(plane->state->fb);
2675 plane->state->fb = plane->fb;
2676 if (plane->state->fb)
2677 drm_framebuffer_reference(plane->state->fb);
2678}
2679
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002680static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002681intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2682 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002683{
2684 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002685 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002686 struct drm_crtc *c;
2687 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002688 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002689 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002690 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002691 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2692 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002693 struct intel_plane_state *intel_state =
2694 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002695 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002696
Damien Lespiau2d140302015-02-05 17:22:18 +00002697 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002698 return;
2699
Daniel Vetterf6936e22015-03-26 12:17:05 +01002700 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002701 fb = &plane_config->fb->base;
2702 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002703 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002704
Damien Lespiau2d140302015-02-05 17:22:18 +00002705 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002706
2707 /*
2708 * Failed to alloc the obj, check to see if we should share
2709 * an fb with another CRTC instead
2710 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002711 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002712 i = to_intel_crtc(c);
2713
2714 if (c == &intel_crtc->base)
2715 continue;
2716
Matt Roper2ff8fde2014-07-08 07:50:07 -07002717 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002718 continue;
2719
Daniel Vetter88595ac2015-03-26 12:42:24 +01002720 fb = c->primary->fb;
2721 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002722 continue;
2723
Daniel Vetter88595ac2015-03-26 12:42:24 +01002724 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002725 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002726 drm_framebuffer_reference(fb);
2727 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002728 }
2729 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002730
Matt Roper200757f2015-12-03 11:37:36 -08002731 /*
2732 * We've failed to reconstruct the BIOS FB. Current display state
2733 * indicates that the primary plane is visible, but has a NULL FB,
2734 * which will lead to problems later if we don't fix it up. The
2735 * simplest solution is to just disable the primary plane now and
2736 * pretend the BIOS never had it enabled.
2737 */
2738 to_intel_plane_state(plane_state)->visible = false;
2739 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2740 intel_pre_disable_primary(&intel_crtc->base);
2741 intel_plane->disable_plane(primary, &intel_crtc->base);
2742
Daniel Vetter88595ac2015-03-26 12:42:24 +01002743 return;
2744
2745valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002746 plane_state->src_x = 0;
2747 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002748 plane_state->src_w = fb->width << 16;
2749 plane_state->src_h = fb->height << 16;
2750
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002751 plane_state->crtc_x = 0;
2752 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002753 plane_state->crtc_w = fb->width;
2754 plane_state->crtc_h = fb->height;
2755
Matt Roper0a8d8a82015-12-03 11:37:38 -08002756 intel_state->src.x1 = plane_state->src_x;
2757 intel_state->src.y1 = plane_state->src_y;
2758 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2759 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2760 intel_state->dst.x1 = plane_state->crtc_x;
2761 intel_state->dst.y1 = plane_state->crtc_y;
2762 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2763 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2764
Daniel Vetter88595ac2015-03-26 12:42:24 +01002765 obj = intel_fb_obj(fb);
2766 if (obj->tiling_mode != I915_TILING_NONE)
2767 dev_priv->preserve_bios_swizzle = true;
2768
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002769 drm_framebuffer_reference(fb);
2770 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002771 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002772 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002773 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002774}
2775
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002776static void i9xx_update_primary_plane(struct drm_plane *primary,
2777 const struct intel_crtc_state *crtc_state,
2778 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002779{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002780 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002781 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002782 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2783 struct drm_framebuffer *fb = plane_state->base.fb;
2784 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002785 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002786 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002787 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002788 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002789 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002790 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002791 int x = plane_state->src.x1 >> 16;
2792 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002793
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002794 dspcntr = DISPPLANE_GAMMA_ENABLE;
2795
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002796 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002797
2798 if (INTEL_INFO(dev)->gen < 4) {
2799 if (intel_crtc->pipe == PIPE_B)
2800 dspcntr |= DISPPLANE_SEL_PIPE_B;
2801
2802 /* pipesrc and dspsize control the size that is scaled from,
2803 * which should always be the user's requested size.
2804 */
2805 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002806 ((crtc_state->pipe_src_h - 1) << 16) |
2807 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002808 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002809 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2810 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002811 ((crtc_state->pipe_src_h - 1) << 16) |
2812 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002813 I915_WRITE(PRIMPOS(plane), 0);
2814 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002815 }
2816
Ville Syrjälä57779d02012-10-31 17:50:14 +02002817 switch (fb->pixel_format) {
2818 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002819 dspcntr |= DISPPLANE_8BPP;
2820 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002821 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002822 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002823 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002824 case DRM_FORMAT_RGB565:
2825 dspcntr |= DISPPLANE_BGRX565;
2826 break;
2827 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002828 dspcntr |= DISPPLANE_BGRX888;
2829 break;
2830 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002831 dspcntr |= DISPPLANE_RGBX888;
2832 break;
2833 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002834 dspcntr |= DISPPLANE_BGRX101010;
2835 break;
2836 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002837 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002838 break;
2839 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002840 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002841 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002842
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002843 if (INTEL_INFO(dev)->gen >= 4 &&
2844 obj->tiling_mode != I915_TILING_NONE)
2845 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002846
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002847 if (IS_G4X(dev))
2848 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2849
Ville Syrjäläac484962016-01-20 21:05:26 +02002850 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002851
Daniel Vetterc2c75132012-07-05 12:17:30 +02002852 if (INTEL_INFO(dev)->gen >= 4) {
2853 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002854 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002855 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002856 linear_offset -= intel_crtc->dspaddr_offset;
2857 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002858 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002859 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002860
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002861 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302862 dspcntr |= DISPPLANE_ROTATE_180;
2863
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002864 x += (crtc_state->pipe_src_w - 1);
2865 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302866
2867 /* Finding the last pixel of the last line of the display
2868 data and adding to linear_offset*/
2869 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002870 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002871 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302872 }
2873
Paulo Zanoni2db33662015-09-14 15:20:03 -03002874 intel_crtc->adjusted_x = x;
2875 intel_crtc->adjusted_y = y;
2876
Sonika Jindal48404c12014-08-22 14:06:04 +05302877 I915_WRITE(reg, dspcntr);
2878
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002879 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002880 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002881 I915_WRITE(DSPSURF(plane),
2882 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002883 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002884 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002885 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002886 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002887 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002888}
2889
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002890static void i9xx_disable_primary_plane(struct drm_plane *primary,
2891 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002892{
2893 struct drm_device *dev = crtc->dev;
2894 struct drm_i915_private *dev_priv = dev->dev_private;
2895 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002896 int plane = intel_crtc->plane;
2897
2898 I915_WRITE(DSPCNTR(plane), 0);
2899 if (INTEL_INFO(dev_priv)->gen >= 4)
2900 I915_WRITE(DSPSURF(plane), 0);
2901 else
2902 I915_WRITE(DSPADDR(plane), 0);
2903 POSTING_READ(DSPCNTR(plane));
2904}
2905
2906static void ironlake_update_primary_plane(struct drm_plane *primary,
2907 const struct intel_crtc_state *crtc_state,
2908 const struct intel_plane_state *plane_state)
2909{
2910 struct drm_device *dev = primary->dev;
2911 struct drm_i915_private *dev_priv = dev->dev_private;
2912 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2913 struct drm_framebuffer *fb = plane_state->base.fb;
2914 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002915 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002916 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002917 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002918 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002919 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002920 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002921 int x = plane_state->src.x1 >> 16;
2922 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002923
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002924 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002925 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002926
2927 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2928 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2929
Ville Syrjälä57779d02012-10-31 17:50:14 +02002930 switch (fb->pixel_format) {
2931 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002932 dspcntr |= DISPPLANE_8BPP;
2933 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002934 case DRM_FORMAT_RGB565:
2935 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002936 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002937 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002938 dspcntr |= DISPPLANE_BGRX888;
2939 break;
2940 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002941 dspcntr |= DISPPLANE_RGBX888;
2942 break;
2943 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002944 dspcntr |= DISPPLANE_BGRX101010;
2945 break;
2946 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002947 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002948 break;
2949 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002950 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002951 }
2952
2953 if (obj->tiling_mode != I915_TILING_NONE)
2954 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002955
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002956 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002957 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002958
Ville Syrjäläac484962016-01-20 21:05:26 +02002959 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002960 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002961 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002962 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002963 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002964 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302965 dspcntr |= DISPPLANE_ROTATE_180;
2966
2967 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002968 x += (crtc_state->pipe_src_w - 1);
2969 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302970
2971 /* Finding the last pixel of the last line of the display
2972 data and adding to linear_offset*/
2973 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002974 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002975 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302976 }
2977 }
2978
Paulo Zanoni2db33662015-09-14 15:20:03 -03002979 intel_crtc->adjusted_x = x;
2980 intel_crtc->adjusted_y = y;
2981
Sonika Jindal48404c12014-08-22 14:06:04 +05302982 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002983
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002984 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002985 I915_WRITE(DSPSURF(plane),
2986 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002987 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002988 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2989 } else {
2990 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2991 I915_WRITE(DSPLINOFF(plane), linear_offset);
2992 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002993 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002994}
2995
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002996u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2997 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002998{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002999 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
3000 return 64;
3001 } else {
3002 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00003003
Ville Syrjälä27ba3912016-02-15 22:54:40 +02003004 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00003005 }
3006}
3007
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003008u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
3009 struct drm_i915_gem_object *obj,
3010 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003011{
Daniel Vetterce7f1722015-10-14 16:51:06 +02003012 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003013 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003014 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003015
Ville Syrjäläe7941292016-01-19 18:23:17 +02003016 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02003017 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003018
Daniel Vetterce7f1722015-10-14 16:51:06 +02003019 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003020 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02003021 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003022 return -1;
3023
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003024 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003025
3026 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02003027 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003028 PAGE_SIZE;
3029 }
3030
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003031 WARN_ON(upper_32_bits(offset));
3032
3033 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003034}
3035
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003036static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3037{
3038 struct drm_device *dev = intel_crtc->base.dev;
3039 struct drm_i915_private *dev_priv = dev->dev_private;
3040
3041 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3042 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3043 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003044}
3045
Chandra Kondurua1b22782015-04-07 15:28:45 -07003046/*
3047 * This function detaches (aka. unbinds) unused scalers in hardware
3048 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02003049static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07003050{
Chandra Kondurua1b22782015-04-07 15:28:45 -07003051 struct intel_crtc_scaler_state *scaler_state;
3052 int i;
3053
Chandra Kondurua1b22782015-04-07 15:28:45 -07003054 scaler_state = &intel_crtc->config->scaler_state;
3055
3056 /* loop through and disable scalers that aren't in use */
3057 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003058 if (!scaler_state->scalers[i].in_use)
3059 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07003060 }
3061}
3062
Chandra Konduru6156a452015-04-27 13:48:39 -07003063u32 skl_plane_ctl_format(uint32_t pixel_format)
3064{
Chandra Konduru6156a452015-04-27 13:48:39 -07003065 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01003066 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003067 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07003068 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003069 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07003070 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003071 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003072 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003073 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003074 /*
3075 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3076 * to be already pre-multiplied. We need to add a knob (or a different
3077 * DRM_FORMAT) for user-space to configure that.
3078 */
3079 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003080 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07003081 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003082 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003083 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07003084 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003085 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003086 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003087 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003088 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003089 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003090 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003091 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003092 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003093 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003094 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003095 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003096 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003097 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003098 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003099 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003100
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003101 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003102}
3103
3104u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3105{
Chandra Konduru6156a452015-04-27 13:48:39 -07003106 switch (fb_modifier) {
3107 case DRM_FORMAT_MOD_NONE:
3108 break;
3109 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003110 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003111 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003112 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003113 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003114 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003115 default:
3116 MISSING_CASE(fb_modifier);
3117 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003118
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003119 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003120}
3121
3122u32 skl_plane_ctl_rotation(unsigned int rotation)
3123{
Chandra Konduru6156a452015-04-27 13:48:39 -07003124 switch (rotation) {
3125 case BIT(DRM_ROTATE_0):
3126 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303127 /*
3128 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3129 * while i915 HW rotation is clockwise, thats why this swapping.
3130 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003131 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303132 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003133 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003134 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003135 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303136 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003137 default:
3138 MISSING_CASE(rotation);
3139 }
3140
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003141 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003142}
3143
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003144static void skylake_update_primary_plane(struct drm_plane *plane,
3145 const struct intel_crtc_state *crtc_state,
3146 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003147{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003148 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003149 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003150 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3151 struct drm_framebuffer *fb = plane_state->base.fb;
3152 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003153 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303154 u32 plane_ctl, stride_div, stride;
3155 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003156 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303157 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003158 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003159 int scaler_id = plane_state->scaler_id;
3160 int src_x = plane_state->src.x1 >> 16;
3161 int src_y = plane_state->src.y1 >> 16;
3162 int src_w = drm_rect_width(&plane_state->src) >> 16;
3163 int src_h = drm_rect_height(&plane_state->src) >> 16;
3164 int dst_x = plane_state->dst.x1;
3165 int dst_y = plane_state->dst.y1;
3166 int dst_w = drm_rect_width(&plane_state->dst);
3167 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003168
3169 plane_ctl = PLANE_CTL_ENABLE |
3170 PLANE_CTL_PIPE_GAMMA_ENABLE |
3171 PLANE_CTL_PIPE_CSC_ENABLE;
3172
Chandra Konduru6156a452015-04-27 13:48:39 -07003173 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3174 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003175 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003176 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003177
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003178 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003179 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003180 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303181
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003182 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003183
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303184 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003185 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3186
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303187 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003188 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303189 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003190 x_offset = stride * tile_height - src_y - src_h;
3191 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003192 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303193 } else {
3194 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003195 x_offset = src_x;
3196 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003197 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303198 }
3199 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003200
Paulo Zanoni2db33662015-09-14 15:20:03 -03003201 intel_crtc->adjusted_x = x_offset;
3202 intel_crtc->adjusted_y = y_offset;
3203
Damien Lespiau70d21f02013-07-03 21:06:04 +01003204 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303205 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3206 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3207 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003208
3209 if (scaler_id >= 0) {
3210 uint32_t ps_ctrl = 0;
3211
3212 WARN_ON(!dst_w || !dst_h);
3213 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3214 crtc_state->scaler_state.scalers[scaler_id].mode;
3215 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3216 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3217 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3218 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3219 I915_WRITE(PLANE_POS(pipe, 0), 0);
3220 } else {
3221 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3222 }
3223
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003224 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003225
3226 POSTING_READ(PLANE_SURF(pipe, 0));
3227}
3228
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003229static void skylake_disable_primary_plane(struct drm_plane *primary,
3230 struct drm_crtc *crtc)
3231{
3232 struct drm_device *dev = crtc->dev;
3233 struct drm_i915_private *dev_priv = dev->dev_private;
3234 int pipe = to_intel_crtc(crtc)->pipe;
3235
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003236 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3237 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3238 POSTING_READ(PLANE_SURF(pipe, 0));
3239}
3240
Jesse Barnes17638cd2011-06-24 12:19:23 -07003241/* Assume fb object is pinned & idle & fenced and just update base pointers */
3242static int
3243intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3244 int x, int y, enum mode_set_atomic state)
3245{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003246 /* Support for kgdboc is disabled, this needs a major rework. */
3247 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003248
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003249 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003250}
3251
Ville Syrjälä75147472014-11-24 18:28:11 +02003252static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003253{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003254 struct drm_crtc *crtc;
3255
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003256 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003257 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3258 enum plane plane = intel_crtc->plane;
3259
3260 intel_prepare_page_flip(dev, plane);
3261 intel_finish_page_flip_plane(dev, plane);
3262 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003263}
3264
3265static void intel_update_primary_planes(struct drm_device *dev)
3266{
Ville Syrjälä75147472014-11-24 18:28:11 +02003267 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003268
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003269 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003270 struct intel_plane *plane = to_intel_plane(crtc->primary);
3271 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003272
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003273 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003274 plane_state = to_intel_plane_state(plane->base.state);
3275
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003276 if (plane_state->visible)
3277 plane->update_plane(&plane->base,
3278 to_intel_crtc_state(crtc->state),
3279 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003280
3281 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003282 }
3283}
3284
Ville Syrjälä75147472014-11-24 18:28:11 +02003285void intel_prepare_reset(struct drm_device *dev)
3286{
3287 /* no reset support for gen2 */
3288 if (IS_GEN2(dev))
3289 return;
3290
3291 /* reset doesn't touch the display */
3292 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3293 return;
3294
3295 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003296 /*
3297 * Disabling the crtcs gracefully seems nicer. Also the
3298 * g33 docs say we should at least disable all the planes.
3299 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003300 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003301}
3302
3303void intel_finish_reset(struct drm_device *dev)
3304{
3305 struct drm_i915_private *dev_priv = to_i915(dev);
3306
3307 /*
3308 * Flips in the rings will be nuked by the reset,
3309 * so complete all pending flips so that user space
3310 * will get its events and not get stuck.
3311 */
3312 intel_complete_page_flips(dev);
3313
3314 /* no reset support for gen2 */
3315 if (IS_GEN2(dev))
3316 return;
3317
3318 /* reset doesn't touch the display */
3319 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3320 /*
3321 * Flips in the rings have been nuked by the reset,
3322 * so update the base address of all primary
3323 * planes to the the last fb to make sure we're
3324 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003325 *
3326 * FIXME: Atomic will make this obsolete since we won't schedule
3327 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003328 */
3329 intel_update_primary_planes(dev);
3330 return;
3331 }
3332
3333 /*
3334 * The display has been reset as well,
3335 * so need a full re-initialization.
3336 */
3337 intel_runtime_pm_disable_interrupts(dev_priv);
3338 intel_runtime_pm_enable_interrupts(dev_priv);
3339
3340 intel_modeset_init_hw(dev);
3341
3342 spin_lock_irq(&dev_priv->irq_lock);
3343 if (dev_priv->display.hpd_irq_setup)
3344 dev_priv->display.hpd_irq_setup(dev);
3345 spin_unlock_irq(&dev_priv->irq_lock);
3346
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003347 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003348
3349 intel_hpd_init(dev_priv);
3350
3351 drm_modeset_unlock_all(dev);
3352}
3353
Chris Wilson7d5e3792014-03-04 13:15:08 +00003354static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3355{
3356 struct drm_device *dev = crtc->dev;
3357 struct drm_i915_private *dev_priv = dev->dev_private;
3358 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003359 bool pending;
3360
3361 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3362 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3363 return false;
3364
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003365 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003366 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003367 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003368
3369 return pending;
3370}
3371
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003372static void intel_update_pipe_config(struct intel_crtc *crtc,
3373 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003374{
3375 struct drm_device *dev = crtc->base.dev;
3376 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003377 struct intel_crtc_state *pipe_config =
3378 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003379
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003380 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3381 crtc->base.mode = crtc->base.state->mode;
3382
3383 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3384 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3385 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003386
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003387 if (HAS_DDI(dev))
3388 intel_set_pipe_csc(&crtc->base);
3389
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003390 /*
3391 * Update pipe size and adjust fitter if needed: the reason for this is
3392 * that in compute_mode_changes we check the native mode (not the pfit
3393 * mode) to see if we can flip rather than do a full mode set. In the
3394 * fastboot case, we'll flip, but if we don't update the pipesrc and
3395 * pfit state, we'll end up with a big fb scanned out into the wrong
3396 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003397 */
3398
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003399 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003400 ((pipe_config->pipe_src_w - 1) << 16) |
3401 (pipe_config->pipe_src_h - 1));
3402
3403 /* on skylake this is done by detaching scalers */
3404 if (INTEL_INFO(dev)->gen >= 9) {
3405 skl_detach_scalers(crtc);
3406
3407 if (pipe_config->pch_pfit.enabled)
3408 skylake_pfit_enable(crtc);
3409 } else if (HAS_PCH_SPLIT(dev)) {
3410 if (pipe_config->pch_pfit.enabled)
3411 ironlake_pfit_enable(crtc);
3412 else if (old_crtc_state->pch_pfit.enabled)
3413 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003414 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003415}
3416
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003417static void intel_fdi_normal_train(struct drm_crtc *crtc)
3418{
3419 struct drm_device *dev = crtc->dev;
3420 struct drm_i915_private *dev_priv = dev->dev_private;
3421 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3422 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003423 i915_reg_t reg;
3424 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003425
3426 /* enable normal train */
3427 reg = FDI_TX_CTL(pipe);
3428 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003429 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003430 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3431 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003432 } else {
3433 temp &= ~FDI_LINK_TRAIN_NONE;
3434 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003435 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003436 I915_WRITE(reg, temp);
3437
3438 reg = FDI_RX_CTL(pipe);
3439 temp = I915_READ(reg);
3440 if (HAS_PCH_CPT(dev)) {
3441 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3442 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3443 } else {
3444 temp &= ~FDI_LINK_TRAIN_NONE;
3445 temp |= FDI_LINK_TRAIN_NONE;
3446 }
3447 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3448
3449 /* wait one idle pattern time */
3450 POSTING_READ(reg);
3451 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003452
3453 /* IVB wants error correction enabled */
3454 if (IS_IVYBRIDGE(dev))
3455 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3456 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003457}
3458
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003459/* The FDI link training functions for ILK/Ibexpeak. */
3460static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3461{
3462 struct drm_device *dev = crtc->dev;
3463 struct drm_i915_private *dev_priv = dev->dev_private;
3464 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3465 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003466 i915_reg_t reg;
3467 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003468
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003469 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003470 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003471
Adam Jacksone1a44742010-06-25 15:32:14 -04003472 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3473 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003474 reg = FDI_RX_IMR(pipe);
3475 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003476 temp &= ~FDI_RX_SYMBOL_LOCK;
3477 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003478 I915_WRITE(reg, temp);
3479 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003480 udelay(150);
3481
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003482 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003483 reg = FDI_TX_CTL(pipe);
3484 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003485 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003486 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003487 temp &= ~FDI_LINK_TRAIN_NONE;
3488 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003489 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490
Chris Wilson5eddb702010-09-11 13:48:45 +01003491 reg = FDI_RX_CTL(pipe);
3492 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003493 temp &= ~FDI_LINK_TRAIN_NONE;
3494 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003495 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3496
3497 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003498 udelay(150);
3499
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003500 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003501 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3502 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3503 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003504
Chris Wilson5eddb702010-09-11 13:48:45 +01003505 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003506 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003507 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003508 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3509
3510 if ((temp & FDI_RX_BIT_LOCK)) {
3511 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003512 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003513 break;
3514 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003515 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003516 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003517 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003518
3519 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003520 reg = FDI_TX_CTL(pipe);
3521 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003522 temp &= ~FDI_LINK_TRAIN_NONE;
3523 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003524 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003525
Chris Wilson5eddb702010-09-11 13:48:45 +01003526 reg = FDI_RX_CTL(pipe);
3527 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003528 temp &= ~FDI_LINK_TRAIN_NONE;
3529 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003530 I915_WRITE(reg, temp);
3531
3532 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003533 udelay(150);
3534
Chris Wilson5eddb702010-09-11 13:48:45 +01003535 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003536 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003537 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003538 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3539
3540 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003541 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003542 DRM_DEBUG_KMS("FDI train 2 done.\n");
3543 break;
3544 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003545 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003546 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003547 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003548
3549 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003550
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003551}
3552
Akshay Joshi0206e352011-08-16 15:34:10 -04003553static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003554 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3555 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3556 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3557 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3558};
3559
3560/* The FDI link training functions for SNB/Cougarpoint. */
3561static void gen6_fdi_link_train(struct drm_crtc *crtc)
3562{
3563 struct drm_device *dev = crtc->dev;
3564 struct drm_i915_private *dev_priv = dev->dev_private;
3565 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3566 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003567 i915_reg_t reg;
3568 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003569
Adam Jacksone1a44742010-06-25 15:32:14 -04003570 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3571 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003572 reg = FDI_RX_IMR(pipe);
3573 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003574 temp &= ~FDI_RX_SYMBOL_LOCK;
3575 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003576 I915_WRITE(reg, temp);
3577
3578 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003579 udelay(150);
3580
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003581 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003582 reg = FDI_TX_CTL(pipe);
3583 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003584 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003585 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003586 temp &= ~FDI_LINK_TRAIN_NONE;
3587 temp |= FDI_LINK_TRAIN_PATTERN_1;
3588 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3589 /* SNB-B */
3590 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003591 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003592
Daniel Vetterd74cf322012-10-26 10:58:13 +02003593 I915_WRITE(FDI_RX_MISC(pipe),
3594 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3595
Chris Wilson5eddb702010-09-11 13:48:45 +01003596 reg = FDI_RX_CTL(pipe);
3597 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003598 if (HAS_PCH_CPT(dev)) {
3599 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3600 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3601 } else {
3602 temp &= ~FDI_LINK_TRAIN_NONE;
3603 temp |= FDI_LINK_TRAIN_PATTERN_1;
3604 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003605 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3606
3607 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003608 udelay(150);
3609
Akshay Joshi0206e352011-08-16 15:34:10 -04003610 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003611 reg = FDI_TX_CTL(pipe);
3612 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003613 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3614 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003615 I915_WRITE(reg, temp);
3616
3617 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003618 udelay(500);
3619
Sean Paulfa37d392012-03-02 12:53:39 -05003620 for (retry = 0; retry < 5; retry++) {
3621 reg = FDI_RX_IIR(pipe);
3622 temp = I915_READ(reg);
3623 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3624 if (temp & FDI_RX_BIT_LOCK) {
3625 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3626 DRM_DEBUG_KMS("FDI train 1 done.\n");
3627 break;
3628 }
3629 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003630 }
Sean Paulfa37d392012-03-02 12:53:39 -05003631 if (retry < 5)
3632 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003633 }
3634 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003635 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003636
3637 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003638 reg = FDI_TX_CTL(pipe);
3639 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003640 temp &= ~FDI_LINK_TRAIN_NONE;
3641 temp |= FDI_LINK_TRAIN_PATTERN_2;
3642 if (IS_GEN6(dev)) {
3643 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3644 /* SNB-B */
3645 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3646 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003647 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003648
Chris Wilson5eddb702010-09-11 13:48:45 +01003649 reg = FDI_RX_CTL(pipe);
3650 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003651 if (HAS_PCH_CPT(dev)) {
3652 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3653 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3654 } else {
3655 temp &= ~FDI_LINK_TRAIN_NONE;
3656 temp |= FDI_LINK_TRAIN_PATTERN_2;
3657 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003658 I915_WRITE(reg, temp);
3659
3660 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003661 udelay(150);
3662
Akshay Joshi0206e352011-08-16 15:34:10 -04003663 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003664 reg = FDI_TX_CTL(pipe);
3665 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003666 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3667 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003668 I915_WRITE(reg, temp);
3669
3670 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003671 udelay(500);
3672
Sean Paulfa37d392012-03-02 12:53:39 -05003673 for (retry = 0; retry < 5; retry++) {
3674 reg = FDI_RX_IIR(pipe);
3675 temp = I915_READ(reg);
3676 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3677 if (temp & FDI_RX_SYMBOL_LOCK) {
3678 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3679 DRM_DEBUG_KMS("FDI train 2 done.\n");
3680 break;
3681 }
3682 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003683 }
Sean Paulfa37d392012-03-02 12:53:39 -05003684 if (retry < 5)
3685 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003686 }
3687 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003688 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003689
3690 DRM_DEBUG_KMS("FDI train done.\n");
3691}
3692
Jesse Barnes357555c2011-04-28 15:09:55 -07003693/* Manual link training for Ivy Bridge A0 parts */
3694static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3695{
3696 struct drm_device *dev = crtc->dev;
3697 struct drm_i915_private *dev_priv = dev->dev_private;
3698 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3699 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003700 i915_reg_t reg;
3701 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003702
3703 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3704 for train result */
3705 reg = FDI_RX_IMR(pipe);
3706 temp = I915_READ(reg);
3707 temp &= ~FDI_RX_SYMBOL_LOCK;
3708 temp &= ~FDI_RX_BIT_LOCK;
3709 I915_WRITE(reg, temp);
3710
3711 POSTING_READ(reg);
3712 udelay(150);
3713
Daniel Vetter01a415f2012-10-27 15:58:40 +02003714 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3715 I915_READ(FDI_RX_IIR(pipe)));
3716
Jesse Barnes139ccd32013-08-19 11:04:55 -07003717 /* Try each vswing and preemphasis setting twice before moving on */
3718 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3719 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003720 reg = FDI_TX_CTL(pipe);
3721 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003722 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3723 temp &= ~FDI_TX_ENABLE;
3724 I915_WRITE(reg, temp);
3725
3726 reg = FDI_RX_CTL(pipe);
3727 temp = I915_READ(reg);
3728 temp &= ~FDI_LINK_TRAIN_AUTO;
3729 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3730 temp &= ~FDI_RX_ENABLE;
3731 I915_WRITE(reg, temp);
3732
3733 /* enable CPU FDI TX and PCH FDI RX */
3734 reg = FDI_TX_CTL(pipe);
3735 temp = I915_READ(reg);
3736 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003737 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003738 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003739 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003740 temp |= snb_b_fdi_train_param[j/2];
3741 temp |= FDI_COMPOSITE_SYNC;
3742 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3743
3744 I915_WRITE(FDI_RX_MISC(pipe),
3745 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3746
3747 reg = FDI_RX_CTL(pipe);
3748 temp = I915_READ(reg);
3749 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3750 temp |= FDI_COMPOSITE_SYNC;
3751 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3752
3753 POSTING_READ(reg);
3754 udelay(1); /* should be 0.5us */
3755
3756 for (i = 0; i < 4; i++) {
3757 reg = FDI_RX_IIR(pipe);
3758 temp = I915_READ(reg);
3759 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3760
3761 if (temp & FDI_RX_BIT_LOCK ||
3762 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3763 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3764 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3765 i);
3766 break;
3767 }
3768 udelay(1); /* should be 0.5us */
3769 }
3770 if (i == 4) {
3771 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3772 continue;
3773 }
3774
3775 /* Train 2 */
3776 reg = FDI_TX_CTL(pipe);
3777 temp = I915_READ(reg);
3778 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3779 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3780 I915_WRITE(reg, temp);
3781
3782 reg = FDI_RX_CTL(pipe);
3783 temp = I915_READ(reg);
3784 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3785 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003786 I915_WRITE(reg, temp);
3787
3788 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003789 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003790
Jesse Barnes139ccd32013-08-19 11:04:55 -07003791 for (i = 0; i < 4; i++) {
3792 reg = FDI_RX_IIR(pipe);
3793 temp = I915_READ(reg);
3794 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003795
Jesse Barnes139ccd32013-08-19 11:04:55 -07003796 if (temp & FDI_RX_SYMBOL_LOCK ||
3797 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3798 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3799 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3800 i);
3801 goto train_done;
3802 }
3803 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003804 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003805 if (i == 4)
3806 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003807 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003808
Jesse Barnes139ccd32013-08-19 11:04:55 -07003809train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003810 DRM_DEBUG_KMS("FDI train done.\n");
3811}
3812
Daniel Vetter88cefb62012-08-12 19:27:14 +02003813static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003814{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003815 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003816 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003817 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003818 i915_reg_t reg;
3819 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003820
Jesse Barnes0e23b992010-09-10 11:10:00 -07003821 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003822 reg = FDI_RX_CTL(pipe);
3823 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003824 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003825 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003826 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003827 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3828
3829 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003830 udelay(200);
3831
3832 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003833 temp = I915_READ(reg);
3834 I915_WRITE(reg, temp | FDI_PCDCLK);
3835
3836 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003837 udelay(200);
3838
Paulo Zanoni20749732012-11-23 15:30:38 -02003839 /* Enable CPU FDI TX PLL, always on for Ironlake */
3840 reg = FDI_TX_CTL(pipe);
3841 temp = I915_READ(reg);
3842 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3843 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003844
Paulo Zanoni20749732012-11-23 15:30:38 -02003845 POSTING_READ(reg);
3846 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003847 }
3848}
3849
Daniel Vetter88cefb62012-08-12 19:27:14 +02003850static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3851{
3852 struct drm_device *dev = intel_crtc->base.dev;
3853 struct drm_i915_private *dev_priv = dev->dev_private;
3854 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003855 i915_reg_t reg;
3856 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003857
3858 /* Switch from PCDclk to Rawclk */
3859 reg = FDI_RX_CTL(pipe);
3860 temp = I915_READ(reg);
3861 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3862
3863 /* Disable CPU FDI TX PLL */
3864 reg = FDI_TX_CTL(pipe);
3865 temp = I915_READ(reg);
3866 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3867
3868 POSTING_READ(reg);
3869 udelay(100);
3870
3871 reg = FDI_RX_CTL(pipe);
3872 temp = I915_READ(reg);
3873 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3874
3875 /* Wait for the clocks to turn off. */
3876 POSTING_READ(reg);
3877 udelay(100);
3878}
3879
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003880static void ironlake_fdi_disable(struct drm_crtc *crtc)
3881{
3882 struct drm_device *dev = crtc->dev;
3883 struct drm_i915_private *dev_priv = dev->dev_private;
3884 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3885 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003886 i915_reg_t reg;
3887 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003888
3889 /* disable CPU FDI tx and PCH FDI rx */
3890 reg = FDI_TX_CTL(pipe);
3891 temp = I915_READ(reg);
3892 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3893 POSTING_READ(reg);
3894
3895 reg = FDI_RX_CTL(pipe);
3896 temp = I915_READ(reg);
3897 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003898 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003899 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3900
3901 POSTING_READ(reg);
3902 udelay(100);
3903
3904 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003905 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003906 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003907
3908 /* still set train pattern 1 */
3909 reg = FDI_TX_CTL(pipe);
3910 temp = I915_READ(reg);
3911 temp &= ~FDI_LINK_TRAIN_NONE;
3912 temp |= FDI_LINK_TRAIN_PATTERN_1;
3913 I915_WRITE(reg, temp);
3914
3915 reg = FDI_RX_CTL(pipe);
3916 temp = I915_READ(reg);
3917 if (HAS_PCH_CPT(dev)) {
3918 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3919 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3920 } else {
3921 temp &= ~FDI_LINK_TRAIN_NONE;
3922 temp |= FDI_LINK_TRAIN_PATTERN_1;
3923 }
3924 /* BPC in FDI rx is consistent with that in PIPECONF */
3925 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003926 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003927 I915_WRITE(reg, temp);
3928
3929 POSTING_READ(reg);
3930 udelay(100);
3931}
3932
Chris Wilson5dce5b932014-01-20 10:17:36 +00003933bool intel_has_pending_fb_unpin(struct drm_device *dev)
3934{
3935 struct intel_crtc *crtc;
3936
3937 /* Note that we don't need to be called with mode_config.lock here
3938 * as our list of CRTC objects is static for the lifetime of the
3939 * device and so cannot disappear as we iterate. Similarly, we can
3940 * happily treat the predicates as racy, atomic checks as userspace
3941 * cannot claim and pin a new fb without at least acquring the
3942 * struct_mutex and so serialising with us.
3943 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003944 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003945 if (atomic_read(&crtc->unpin_work_count) == 0)
3946 continue;
3947
3948 if (crtc->unpin_work)
3949 intel_wait_for_vblank(dev, crtc->pipe);
3950
3951 return true;
3952 }
3953
3954 return false;
3955}
3956
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003957static void page_flip_completed(struct intel_crtc *intel_crtc)
3958{
3959 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3960 struct intel_unpin_work *work = intel_crtc->unpin_work;
3961
3962 /* ensure that the unpin work is consistent wrt ->pending. */
3963 smp_rmb();
3964 intel_crtc->unpin_work = NULL;
3965
3966 if (work->event)
3967 drm_send_vblank_event(intel_crtc->base.dev,
3968 intel_crtc->pipe,
3969 work->event);
3970
3971 drm_crtc_vblank_put(&intel_crtc->base);
3972
3973 wake_up_all(&dev_priv->pending_flip_queue);
3974 queue_work(dev_priv->wq, &work->work);
3975
3976 trace_i915_flip_complete(intel_crtc->plane,
3977 work->pending_flip_obj);
3978}
3979
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003980static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003981{
Chris Wilson0f911282012-04-17 10:05:38 +01003982 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003983 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003984 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003985
Daniel Vetter2c10d572012-12-20 21:24:07 +01003986 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003987
3988 ret = wait_event_interruptible_timeout(
3989 dev_priv->pending_flip_queue,
3990 !intel_crtc_has_pending_flip(crtc),
3991 60*HZ);
3992
3993 if (ret < 0)
3994 return ret;
3995
3996 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003998
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003999 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01004000 if (intel_crtc->unpin_work) {
4001 WARN_ONCE(1, "Removing stuck page flip\n");
4002 page_flip_completed(intel_crtc);
4003 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02004004 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01004005 }
Chris Wilson5bb61642012-09-27 21:25:58 +01004006
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004007 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004008}
4009
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004010static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
4011{
4012 u32 temp;
4013
4014 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4015
4016 mutex_lock(&dev_priv->sb_lock);
4017
4018 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4019 temp |= SBI_SSCCTL_DISABLE;
4020 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4021
4022 mutex_unlock(&dev_priv->sb_lock);
4023}
4024
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004025/* Program iCLKIP clock to the desired frequency */
4026static void lpt_program_iclkip(struct drm_crtc *crtc)
4027{
4028 struct drm_device *dev = crtc->dev;
4029 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004030 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004031 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4032 u32 temp;
4033
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004034 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004035
4036 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03004037 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004038 auxdiv = 1;
4039 divsel = 0x41;
4040 phaseinc = 0x20;
4041 } else {
4042 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01004043 * but the adjusted_mode->crtc_clock in in KHz. To get the
4044 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004045 * convert the virtual clock precision to KHz here for higher
4046 * precision.
4047 */
4048 u32 iclk_virtual_root_freq = 172800 * 1000;
4049 u32 iclk_pi_range = 64;
4050 u32 desired_divisor, msb_divisor_value, pi_value;
4051
Ville Syrjäläa2572f52015-12-04 22:20:21 +02004052 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq, clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004053 msb_divisor_value = desired_divisor / iclk_pi_range;
4054 pi_value = desired_divisor % iclk_pi_range;
4055
4056 auxdiv = 0;
4057 divsel = msb_divisor_value - 2;
4058 phaseinc = pi_value;
4059 }
4060
4061 /* This should not happen with any sane values */
4062 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4063 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4064 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4065 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4066
4067 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 +03004068 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004069 auxdiv,
4070 divsel,
4071 phasedir,
4072 phaseinc);
4073
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004074 mutex_lock(&dev_priv->sb_lock);
4075
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004076 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004077 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004078 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4079 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4080 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4081 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4082 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4083 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004084 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004085
4086 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004087 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004088 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4089 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004090 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004091
4092 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004093 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004094 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004095 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004096
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004097 mutex_unlock(&dev_priv->sb_lock);
4098
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004099 /* Wait for initialization time */
4100 udelay(24);
4101
4102 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4103}
4104
Daniel Vetter275f01b22013-05-03 11:49:47 +02004105static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4106 enum pipe pch_transcoder)
4107{
4108 struct drm_device *dev = crtc->base.dev;
4109 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004110 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004111
4112 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4113 I915_READ(HTOTAL(cpu_transcoder)));
4114 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4115 I915_READ(HBLANK(cpu_transcoder)));
4116 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4117 I915_READ(HSYNC(cpu_transcoder)));
4118
4119 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4120 I915_READ(VTOTAL(cpu_transcoder)));
4121 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4122 I915_READ(VBLANK(cpu_transcoder)));
4123 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4124 I915_READ(VSYNC(cpu_transcoder)));
4125 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4126 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4127}
4128
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004129static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004130{
4131 struct drm_i915_private *dev_priv = dev->dev_private;
4132 uint32_t temp;
4133
4134 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004135 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004136 return;
4137
4138 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4139 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4140
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004141 temp &= ~FDI_BC_BIFURCATION_SELECT;
4142 if (enable)
4143 temp |= FDI_BC_BIFURCATION_SELECT;
4144
4145 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004146 I915_WRITE(SOUTH_CHICKEN1, temp);
4147 POSTING_READ(SOUTH_CHICKEN1);
4148}
4149
4150static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4151{
4152 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004153
4154 switch (intel_crtc->pipe) {
4155 case PIPE_A:
4156 break;
4157 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004158 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004159 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004160 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004161 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004162
4163 break;
4164 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004165 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004166
4167 break;
4168 default:
4169 BUG();
4170 }
4171}
4172
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004173/* Return which DP Port should be selected for Transcoder DP control */
4174static enum port
4175intel_trans_dp_port_sel(struct drm_crtc *crtc)
4176{
4177 struct drm_device *dev = crtc->dev;
4178 struct intel_encoder *encoder;
4179
4180 for_each_encoder_on_crtc(dev, crtc, encoder) {
4181 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4182 encoder->type == INTEL_OUTPUT_EDP)
4183 return enc_to_dig_port(&encoder->base)->port;
4184 }
4185
4186 return -1;
4187}
4188
Jesse Barnesf67a5592011-01-05 10:31:48 -08004189/*
4190 * Enable PCH resources required for PCH ports:
4191 * - PCH PLLs
4192 * - FDI training & RX/TX
4193 * - update transcoder timings
4194 * - DP transcoding bits
4195 * - transcoder
4196 */
4197static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004198{
4199 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004200 struct drm_i915_private *dev_priv = dev->dev_private;
4201 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4202 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004203 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004204
Daniel Vetterab9412b2013-05-03 11:49:46 +02004205 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004206
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004207 if (IS_IVYBRIDGE(dev))
4208 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4209
Daniel Vettercd986ab2012-10-26 10:58:12 +02004210 /* Write the TU size bits before fdi link training, so that error
4211 * detection works. */
4212 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4213 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4214
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004215 /*
4216 * Sometimes spurious CPU pipe underruns happen during FDI
4217 * training, at least with VGA+HDMI cloning. Suppress them.
4218 */
4219 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4220
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004221 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004222 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004223
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004224 /* We need to program the right clock selection before writing the pixel
4225 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004226 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004227 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004228
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004229 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004230 temp |= TRANS_DPLL_ENABLE(pipe);
4231 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004232 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004233 temp |= sel;
4234 else
4235 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004236 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004237 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004238
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004239 /* XXX: pch pll's can be enabled any time before we enable the PCH
4240 * transcoder, and we actually should do this to not upset any PCH
4241 * transcoder that already use the clock when we share it.
4242 *
4243 * Note that enable_shared_dpll tries to do the right thing, but
4244 * get_shared_dpll unconditionally resets the pll - we need that to have
4245 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004246 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004247
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004248 /* set transcoder timing, panel must allow it */
4249 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004250 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004251
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004252 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004253
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004254 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4255
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004256 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004257 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004258 const struct drm_display_mode *adjusted_mode =
4259 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004260 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004261 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004262 temp = I915_READ(reg);
4263 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004264 TRANS_DP_SYNC_MASK |
4265 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004266 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004267 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004268
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004269 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004270 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004271 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004272 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004273
4274 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004275 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004276 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004277 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004278 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004279 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004280 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004281 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004282 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004283 break;
4284 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004285 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004286 }
4287
Chris Wilson5eddb702010-09-11 13:48:45 +01004288 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004289 }
4290
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004291 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004292}
4293
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004294static void lpt_pch_enable(struct drm_crtc *crtc)
4295{
4296 struct drm_device *dev = crtc->dev;
4297 struct drm_i915_private *dev_priv = dev->dev_private;
4298 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004299 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004300
Daniel Vetterab9412b2013-05-03 11:49:46 +02004301 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004302
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004303 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004304
Paulo Zanoni0540e482012-10-31 18:12:40 -02004305 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004306 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004307
Paulo Zanoni937bb612012-10-31 18:12:47 -02004308 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004309}
4310
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004311struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4312 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004313{
Daniel Vettere2b78262013-06-07 23:10:03 +02004314 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004315 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004316 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004317 enum intel_dpll_id i;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01004318 int max = dev_priv->num_shared_dpll;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004319
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004320 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4321
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004322 if (HAS_PCH_IBX(dev_priv->dev)) {
4323 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004324 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004325 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004326
Daniel Vetter46edb022013-06-05 13:34:12 +02004327 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4328 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004329
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004330 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004331
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004332 goto found;
4333 }
4334
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304335 if (IS_BROXTON(dev_priv->dev)) {
4336 /* PLL is attached to port in bxt */
4337 struct intel_encoder *encoder;
4338 struct intel_digital_port *intel_dig_port;
4339
4340 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4341 if (WARN_ON(!encoder))
4342 return NULL;
4343
4344 intel_dig_port = enc_to_dig_port(&encoder->base);
4345 /* 1:1 mapping between ports and PLLs */
4346 i = (enum intel_dpll_id)intel_dig_port->port;
4347 pll = &dev_priv->shared_dplls[i];
4348 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4349 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004350 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304351
4352 goto found;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01004353 } else if (INTEL_INFO(dev_priv)->gen < 9 && HAS_DDI(dev_priv))
4354 /* Do not consider SPLL */
4355 max = 2;
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304356
Maarten Lankhorst00490c22015-11-16 14:42:12 +01004357 for (i = 0; i < max; i++) {
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004358 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004359
4360 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004361 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004362 continue;
4363
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004364 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004365 &shared_dpll[i].hw_state,
4366 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004367 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004368 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004369 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004370 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004371 goto found;
4372 }
4373 }
4374
4375 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004376 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4377 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004378 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004379 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4380 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004381 goto found;
4382 }
4383 }
4384
4385 return NULL;
4386
4387found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004388 if (shared_dpll[i].crtc_mask == 0)
4389 shared_dpll[i].hw_state =
4390 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004391
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004392 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004393 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4394 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004395
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004396 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004397
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004398 return pll;
4399}
4400
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004401static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004402{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004403 struct drm_i915_private *dev_priv = to_i915(state->dev);
4404 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004405 struct intel_shared_dpll *pll;
4406 enum intel_dpll_id i;
4407
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004408 if (!to_intel_atomic_state(state)->dpll_set)
4409 return;
4410
4411 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004412 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4413 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004414 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004415 }
4416}
4417
Daniel Vettera1520312013-05-03 11:49:50 +02004418static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004419{
4420 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004421 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004422 u32 temp;
4423
4424 temp = I915_READ(dslreg);
4425 udelay(500);
4426 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004427 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004428 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004429 }
4430}
4431
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004432static int
4433skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4434 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4435 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004436{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004437 struct intel_crtc_scaler_state *scaler_state =
4438 &crtc_state->scaler_state;
4439 struct intel_crtc *intel_crtc =
4440 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004441 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004442
4443 need_scaling = intel_rotation_90_or_270(rotation) ?
4444 (src_h != dst_w || src_w != dst_h):
4445 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004446
4447 /*
4448 * if plane is being disabled or scaler is no more required or force detach
4449 * - free scaler binded to this plane/crtc
4450 * - in order to do this, update crtc->scaler_usage
4451 *
4452 * Here scaler state in crtc_state is set free so that
4453 * scaler can be assigned to other user. Actual register
4454 * update to free the scaler is done in plane/panel-fit programming.
4455 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4456 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004457 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004458 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004459 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004460 scaler_state->scalers[*scaler_id].in_use = 0;
4461
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004462 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4463 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4464 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004465 scaler_state->scaler_users);
4466 *scaler_id = -1;
4467 }
4468 return 0;
4469 }
4470
4471 /* range checks */
4472 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4473 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4474
4475 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4476 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004477 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004478 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004479 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004480 return -EINVAL;
4481 }
4482
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004483 /* mark this plane as a scaler user in crtc_state */
4484 scaler_state->scaler_users |= (1 << scaler_user);
4485 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4486 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4487 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4488 scaler_state->scaler_users);
4489
4490 return 0;
4491}
4492
4493/**
4494 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4495 *
4496 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004497 *
4498 * Return
4499 * 0 - scaler_usage updated successfully
4500 * error - requested scaling cannot be supported or other error condition
4501 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004502int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004503{
4504 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004505 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004506
4507 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4508 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4509
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004510 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004511 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004512 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004513 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004514}
4515
4516/**
4517 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4518 *
4519 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004520 * @plane_state: atomic plane state to update
4521 *
4522 * Return
4523 * 0 - scaler_usage updated successfully
4524 * error - requested scaling cannot be supported or other error condition
4525 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004526static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4527 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004528{
4529
4530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004531 struct intel_plane *intel_plane =
4532 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004533 struct drm_framebuffer *fb = plane_state->base.fb;
4534 int ret;
4535
4536 bool force_detach = !fb || !plane_state->visible;
4537
4538 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4539 intel_plane->base.base.id, intel_crtc->pipe,
4540 drm_plane_index(&intel_plane->base));
4541
4542 ret = skl_update_scaler(crtc_state, force_detach,
4543 drm_plane_index(&intel_plane->base),
4544 &plane_state->scaler_id,
4545 plane_state->base.rotation,
4546 drm_rect_width(&plane_state->src) >> 16,
4547 drm_rect_height(&plane_state->src) >> 16,
4548 drm_rect_width(&plane_state->dst),
4549 drm_rect_height(&plane_state->dst));
4550
4551 if (ret || plane_state->scaler_id < 0)
4552 return ret;
4553
Chandra Kondurua1b22782015-04-07 15:28:45 -07004554 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004555 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004556 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004557 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004558 return -EINVAL;
4559 }
4560
4561 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004562 switch (fb->pixel_format) {
4563 case DRM_FORMAT_RGB565:
4564 case DRM_FORMAT_XBGR8888:
4565 case DRM_FORMAT_XRGB8888:
4566 case DRM_FORMAT_ABGR8888:
4567 case DRM_FORMAT_ARGB8888:
4568 case DRM_FORMAT_XRGB2101010:
4569 case DRM_FORMAT_XBGR2101010:
4570 case DRM_FORMAT_YUYV:
4571 case DRM_FORMAT_YVYU:
4572 case DRM_FORMAT_UYVY:
4573 case DRM_FORMAT_VYUY:
4574 break;
4575 default:
4576 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4577 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4578 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004579 }
4580
Chandra Kondurua1b22782015-04-07 15:28:45 -07004581 return 0;
4582}
4583
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004584static void skylake_scaler_disable(struct intel_crtc *crtc)
4585{
4586 int i;
4587
4588 for (i = 0; i < crtc->num_scalers; i++)
4589 skl_detach_scaler(crtc, i);
4590}
4591
4592static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004593{
4594 struct drm_device *dev = crtc->base.dev;
4595 struct drm_i915_private *dev_priv = dev->dev_private;
4596 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004597 struct intel_crtc_scaler_state *scaler_state =
4598 &crtc->config->scaler_state;
4599
4600 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4601
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004602 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004603 int id;
4604
4605 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4606 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4607 return;
4608 }
4609
4610 id = scaler_state->scaler_id;
4611 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4612 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4613 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4614 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4615
4616 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004617 }
4618}
4619
Jesse Barnesb074cec2013-04-25 12:55:02 -07004620static void ironlake_pfit_enable(struct intel_crtc *crtc)
4621{
4622 struct drm_device *dev = crtc->base.dev;
4623 struct drm_i915_private *dev_priv = dev->dev_private;
4624 int pipe = crtc->pipe;
4625
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004626 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004627 /* Force use of hard-coded filter coefficients
4628 * as some pre-programmed values are broken,
4629 * e.g. x201.
4630 */
4631 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4632 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4633 PF_PIPE_SEL_IVB(pipe));
4634 else
4635 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004636 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4637 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004638 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004639}
4640
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004641void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004642{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004643 struct drm_device *dev = crtc->base.dev;
4644 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004645
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004646 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004647 return;
4648
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004649 /* We can only enable IPS after we enable a plane and wait for a vblank */
4650 intel_wait_for_vblank(dev, crtc->pipe);
4651
Paulo Zanonid77e4532013-09-24 13:52:55 -03004652 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004653 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004654 mutex_lock(&dev_priv->rps.hw_lock);
4655 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4656 mutex_unlock(&dev_priv->rps.hw_lock);
4657 /* Quoting Art Runyan: "its not safe to expect any particular
4658 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004659 * mailbox." Moreover, the mailbox may return a bogus state,
4660 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004661 */
4662 } else {
4663 I915_WRITE(IPS_CTL, IPS_ENABLE);
4664 /* The bit only becomes 1 in the next vblank, so this wait here
4665 * is essentially intel_wait_for_vblank. If we don't have this
4666 * and don't wait for vblanks until the end of crtc_enable, then
4667 * the HW state readout code will complain that the expected
4668 * IPS_CTL value is not the one we read. */
4669 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4670 DRM_ERROR("Timed out waiting for IPS enable\n");
4671 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004672}
4673
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004674void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004675{
4676 struct drm_device *dev = crtc->base.dev;
4677 struct drm_i915_private *dev_priv = dev->dev_private;
4678
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004679 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004680 return;
4681
4682 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004683 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004684 mutex_lock(&dev_priv->rps.hw_lock);
4685 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4686 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004687 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4688 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4689 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004690 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004691 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004692 POSTING_READ(IPS_CTL);
4693 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004694
4695 /* We need to wait for a vblank before we can disable the plane. */
4696 intel_wait_for_vblank(dev, crtc->pipe);
4697}
4698
4699/** Loads the palette/gamma unit for the CRTC with the prepared values */
4700static void intel_crtc_load_lut(struct drm_crtc *crtc)
4701{
4702 struct drm_device *dev = crtc->dev;
4703 struct drm_i915_private *dev_priv = dev->dev_private;
4704 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4705 enum pipe pipe = intel_crtc->pipe;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004706 int i;
4707 bool reenable_ips = false;
4708
4709 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004710 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004711 return;
4712
Imre Deak50360402015-01-16 00:55:16 -08004713 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Jani Nikulaa65347b2015-11-27 12:21:46 +02004714 if (intel_crtc->config->has_dsi_encoder)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004715 assert_dsi_pll_enabled(dev_priv);
4716 else
4717 assert_pll_enabled(dev_priv, pipe);
4718 }
4719
Paulo Zanonid77e4532013-09-24 13:52:55 -03004720 /* Workaround : Do not read or write the pipe palette/gamma data while
4721 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4722 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004723 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004724 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4725 GAMMA_MODE_MODE_SPLIT)) {
4726 hsw_disable_ips(intel_crtc);
4727 reenable_ips = true;
4728 }
4729
4730 for (i = 0; i < 256; i++) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004731 i915_reg_t palreg;
Ville Syrjäläf65a9c52015-09-18 20:03:28 +03004732
4733 if (HAS_GMCH_DISPLAY(dev))
4734 palreg = PALETTE(pipe, i);
4735 else
4736 palreg = LGC_PALETTE(pipe, i);
4737
4738 I915_WRITE(palreg,
Paulo Zanonid77e4532013-09-24 13:52:55 -03004739 (intel_crtc->lut_r[i] << 16) |
4740 (intel_crtc->lut_g[i] << 8) |
4741 intel_crtc->lut_b[i]);
4742 }
4743
4744 if (reenable_ips)
4745 hsw_enable_ips(intel_crtc);
4746}
4747
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004748static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004749{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004750 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004751 struct drm_device *dev = intel_crtc->base.dev;
4752 struct drm_i915_private *dev_priv = dev->dev_private;
4753
4754 mutex_lock(&dev->struct_mutex);
4755 dev_priv->mm.interruptible = false;
4756 (void) intel_overlay_switch_off(intel_crtc->overlay);
4757 dev_priv->mm.interruptible = true;
4758 mutex_unlock(&dev->struct_mutex);
4759 }
4760
4761 /* Let userspace switch the overlay on again. In most cases userspace
4762 * has to recompute where to put it anyway.
4763 */
4764}
4765
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004766/**
4767 * intel_post_enable_primary - Perform operations after enabling primary plane
4768 * @crtc: the CRTC whose primary plane was just enabled
4769 *
4770 * Performs potentially sleeping operations that must be done after the primary
4771 * plane is enabled, such as updating FBC and IPS. Note that this may be
4772 * called due to an explicit primary plane update, or due to an implicit
4773 * re-enable that is caused when a sprite plane is updated to no longer
4774 * completely hide the primary plane.
4775 */
4776static void
4777intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004778{
4779 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004780 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4782 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004783
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004784 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004785 * FIXME IPS should be fine as long as one plane is
4786 * enabled, but in practice it seems to have problems
4787 * when going from primary only to sprite only and vice
4788 * versa.
4789 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004790 hsw_enable_ips(intel_crtc);
4791
Daniel Vetterf99d7062014-06-19 16:01:59 +02004792 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004793 * Gen2 reports pipe underruns whenever all planes are disabled.
4794 * So don't enable underrun reporting before at least some planes
4795 * are enabled.
4796 * FIXME: Need to fix the logic to work when we turn off all planes
4797 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004798 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004799 if (IS_GEN2(dev))
4800 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4801
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004802 /* Underruns don't always raise interrupts, so check manually. */
4803 intel_check_cpu_fifo_underruns(dev_priv);
4804 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004805}
4806
4807/**
4808 * intel_pre_disable_primary - Perform operations before disabling primary plane
4809 * @crtc: the CRTC whose primary plane is to be disabled
4810 *
4811 * Performs potentially sleeping operations that must be done before the
4812 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4813 * be called due to an explicit primary plane update, or due to an implicit
4814 * disable that is caused when a sprite plane completely hides the primary
4815 * plane.
4816 */
4817static void
4818intel_pre_disable_primary(struct drm_crtc *crtc)
4819{
4820 struct drm_device *dev = crtc->dev;
4821 struct drm_i915_private *dev_priv = dev->dev_private;
4822 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4823 int pipe = intel_crtc->pipe;
4824
4825 /*
4826 * Gen2 reports pipe underruns whenever all planes are disabled.
4827 * So diasble underrun reporting before all the planes get disabled.
4828 * FIXME: Need to fix the logic to work when we turn off all planes
4829 * but leave the pipe running.
4830 */
4831 if (IS_GEN2(dev))
4832 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4833
4834 /*
4835 * Vblank time updates from the shadow to live plane control register
4836 * are blocked if the memory self-refresh mode is active at that
4837 * moment. So to make sure the plane gets truly disabled, disable
4838 * first the self-refresh mode. The self-refresh enable bit in turn
4839 * will be checked/applied by the HW only at the next frame start
4840 * event which is after the vblank start event, so we need to have a
4841 * wait-for-vblank between disabling the plane and the pipe.
4842 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004843 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004844 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004845 dev_priv->wm.vlv.cxsr = false;
4846 intel_wait_for_vblank(dev, pipe);
4847 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004848
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004849 /*
4850 * FIXME IPS should be fine as long as one plane is
4851 * enabled, but in practice it seems to have problems
4852 * when going from primary only to sprite only and vice
4853 * versa.
4854 */
4855 hsw_disable_ips(intel_crtc);
4856}
4857
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004858static void intel_post_plane_update(struct intel_crtc *crtc)
4859{
4860 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004861 struct intel_crtc_state *pipe_config =
4862 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004863 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004864
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004865 intel_frontbuffer_flip(dev, atomic->fb_bits);
4866
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004867 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004868
Maarten Lankhorstb9001112015-11-19 16:07:16 +01004869 if (pipe_config->wm_changed && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004870 intel_update_watermarks(&crtc->base);
4871
Paulo Zanonic80ac852015-07-02 19:25:13 -03004872 if (atomic->update_fbc)
Paulo Zanoni1eb52232016-01-19 11:35:44 -02004873 intel_fbc_post_update(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004874
4875 if (atomic->post_enable_primary)
4876 intel_post_enable_primary(&crtc->base);
4877
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004878 memset(atomic, 0, sizeof(*atomic));
4879}
4880
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004881static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004882{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004883 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004884 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004885 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004886 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004887 struct intel_crtc_state *pipe_config =
4888 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004889 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4890 struct drm_plane *primary = crtc->base.primary;
4891 struct drm_plane_state *old_pri_state =
4892 drm_atomic_get_existing_plane_state(old_state, primary);
4893 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004894
Paulo Zanoni1eb52232016-01-19 11:35:44 -02004895 if (atomic->update_fbc)
4896 intel_fbc_pre_update(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004897
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004898 if (old_pri_state) {
4899 struct intel_plane_state *primary_state =
4900 to_intel_plane_state(primary->state);
4901 struct intel_plane_state *old_primary_state =
4902 to_intel_plane_state(old_pri_state);
4903
4904 if (old_primary_state->visible &&
4905 (modeset || !primary_state->visible))
4906 intel_pre_disable_primary(&crtc->base);
4907 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004908
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004909 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004910 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004911
4912 if (old_crtc_state->base.active)
4913 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä852eb002015-06-24 22:00:07 +03004914 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004915
Matt Ropered4a6a72016-02-23 17:20:13 -08004916 /*
4917 * IVB workaround: must disable low power watermarks for at least
4918 * one frame before enabling scaling. LP watermarks can be re-enabled
4919 * when scaling is disabled.
4920 *
4921 * WaCxSRDisabledForSpriteScaling:ivb
4922 */
4923 if (pipe_config->disable_lp_wm) {
4924 ilk_disable_lp_wm(dev);
4925 intel_wait_for_vblank(dev, crtc->pipe);
4926 }
4927
4928 /*
4929 * If we're doing a modeset, we're done. No need to do any pre-vblank
4930 * watermark programming here.
4931 */
4932 if (needs_modeset(&pipe_config->base))
4933 return;
4934
4935 /*
4936 * For platforms that support atomic watermarks, program the
4937 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4938 * will be the intermediate values that are safe for both pre- and
4939 * post- vblank; when vblank happens, the 'active' values will be set
4940 * to the final 'target' values and we'll do this again to get the
4941 * optimal watermarks. For gen9+ platforms, the values we program here
4942 * will be the final target values which will get automatically latched
4943 * at vblank time; no further programming will be necessary.
4944 *
4945 * If a platform hasn't been transitioned to atomic watermarks yet,
4946 * we'll continue to update watermarks the old way, if flags tell
4947 * us to.
4948 */
4949 if (dev_priv->display.initial_watermarks != NULL)
4950 dev_priv->display.initial_watermarks(pipe_config);
4951 else if (pipe_config->wm_changed)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004952 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004953}
4954
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004955static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004956{
4957 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004958 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004959 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004960 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004961
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004962 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004963
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004964 drm_for_each_plane_mask(p, dev, plane_mask)
4965 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004966
Daniel Vetterf99d7062014-06-19 16:01:59 +02004967 /*
4968 * FIXME: Once we grow proper nuclear flip support out of this we need
4969 * to compute the mask of flip planes precisely. For the time being
4970 * consider this a flip to a NULL plane.
4971 */
4972 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004973}
4974
Jesse Barnesf67a5592011-01-05 10:31:48 -08004975static void ironlake_crtc_enable(struct drm_crtc *crtc)
4976{
4977 struct drm_device *dev = crtc->dev;
4978 struct drm_i915_private *dev_priv = dev->dev_private;
4979 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004980 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004981 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004982
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004983 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004984 return;
4985
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004986 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004987 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4988
4989 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004990 intel_prepare_shared_dpll(intel_crtc);
4991
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004992 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304993 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004994
4995 intel_set_pipe_timings(intel_crtc);
4996
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004997 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004998 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004999 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005000 }
5001
5002 ironlake_set_pipeconf(crtc);
5003
Jesse Barnesf67a5592011-01-05 10:31:48 -08005004 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005005
Daniel Vettera72e4c92014-09-30 10:56:47 +02005006 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03005007
Daniel Vetterf6736a12013-06-05 13:34:30 +02005008 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02005009 if (encoder->pre_enable)
5010 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005011
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005012 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02005013 /* Note: FDI PLL enabling _must_ be done before we enable the
5014 * cpu pipes, hence this is separate from all the other fdi/pch
5015 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02005016 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02005017 } else {
5018 assert_fdi_tx_disabled(dev_priv, pipe);
5019 assert_fdi_rx_disabled(dev_priv, pipe);
5020 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005021
Jesse Barnesb074cec2013-04-25 12:55:02 -07005022 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005023
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005024 /*
5025 * On ILK+ LUT must be loaded before the pipe is running but with
5026 * clocks enabled
5027 */
5028 intel_crtc_load_lut(crtc);
5029
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005030 if (dev_priv->display.initial_watermarks != NULL)
5031 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005032 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005033
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005034 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005035 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005036
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005037 assert_vblank_disabled(crtc);
5038 drm_crtc_vblank_on(crtc);
5039
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02005040 for_each_encoder_on_crtc(dev, crtc, encoder)
5041 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02005042
5043 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02005044 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005045
5046 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5047 if (intel_crtc->config->has_pch_encoder)
5048 intel_wait_for_vblank(dev, pipe);
5049 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005050}
5051
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005052/* IPS only exists on ULT machines and is tied to pipe A. */
5053static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5054{
Damien Lespiauf5adf942013-06-24 18:29:34 +01005055 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005056}
5057
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005058static void haswell_crtc_enable(struct drm_crtc *crtc)
5059{
5060 struct drm_device *dev = crtc->dev;
5061 struct drm_i915_private *dev_priv = dev->dev_private;
5062 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5063 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005064 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
5065 struct intel_crtc_state *pipe_config =
5066 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005067
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005068 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005069 return;
5070
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005071 if (intel_crtc->config->has_pch_encoder)
5072 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5073 false);
5074
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005075 if (intel_crtc_to_shared_dpll(intel_crtc))
5076 intel_enable_shared_dpll(intel_crtc);
5077
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005078 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305079 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005080
5081 intel_set_pipe_timings(intel_crtc);
5082
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005083 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5084 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5085 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005086 }
5087
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005088 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005089 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005090 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005091 }
5092
5093 haswell_set_pipeconf(crtc);
5094
5095 intel_set_pipe_csc(crtc);
5096
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005097 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005098
Daniel Vetter6b698512015-11-28 11:05:39 +01005099 if (intel_crtc->config->has_pch_encoder)
5100 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5101 else
5102 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5103
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305104 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005105 if (encoder->pre_enable)
5106 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305107 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005108
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005109 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03005110 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03005111
Jani Nikulaa65347b2015-11-27 12:21:46 +02005112 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305113 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005114
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005115 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005116 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005117 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005118 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005119
5120 /*
5121 * On ILK+ LUT must be loaded before the pipe is running but with
5122 * clocks enabled
5123 */
5124 intel_crtc_load_lut(crtc);
5125
Paulo Zanoni1f544382012-10-24 11:32:00 -02005126 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02005127 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305128 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005129
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005130 if (dev_priv->display.initial_watermarks != NULL)
5131 dev_priv->display.initial_watermarks(pipe_config);
5132 else
5133 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005134 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005135
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005136 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005137 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005138
Jani Nikulaa65347b2015-11-27 12:21:46 +02005139 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005140 intel_ddi_set_vc_payload_alloc(crtc, true);
5141
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005142 assert_vblank_disabled(crtc);
5143 drm_crtc_vblank_on(crtc);
5144
Jani Nikula8807e552013-08-30 19:40:32 +03005145 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005146 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005147 intel_opregion_notify_encoder(encoder, true);
5148 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005149
Daniel Vetter6b698512015-11-28 11:05:39 +01005150 if (intel_crtc->config->has_pch_encoder) {
5151 intel_wait_for_vblank(dev, pipe);
5152 intel_wait_for_vblank(dev, pipe);
5153 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005154 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5155 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01005156 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005157
Paulo Zanonie4916942013-09-20 16:21:19 -03005158 /* If we change the relative order between pipe/planes enabling, we need
5159 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005160 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5161 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5162 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5163 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5164 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005165}
5166
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005167static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005168{
5169 struct drm_device *dev = crtc->base.dev;
5170 struct drm_i915_private *dev_priv = dev->dev_private;
5171 int pipe = crtc->pipe;
5172
5173 /* To avoid upsetting the power well on haswell only disable the pfit if
5174 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005175 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005176 I915_WRITE(PF_CTL(pipe), 0);
5177 I915_WRITE(PF_WIN_POS(pipe), 0);
5178 I915_WRITE(PF_WIN_SZ(pipe), 0);
5179 }
5180}
5181
Jesse Barnes6be4a602010-09-10 10:26:01 -07005182static void ironlake_crtc_disable(struct drm_crtc *crtc)
5183{
5184 struct drm_device *dev = crtc->dev;
5185 struct drm_i915_private *dev_priv = dev->dev_private;
5186 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005187 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005188 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005189
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005190 if (intel_crtc->config->has_pch_encoder)
5191 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5192
Daniel Vetterea9d7582012-07-10 10:42:52 +02005193 for_each_encoder_on_crtc(dev, crtc, encoder)
5194 encoder->disable(encoder);
5195
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005196 drm_crtc_vblank_off(crtc);
5197 assert_vblank_disabled(crtc);
5198
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005199 /*
5200 * Sometimes spurious CPU pipe underruns happen when the
5201 * pipe is already disabled, but FDI RX/TX is still enabled.
5202 * Happens at least with VGA+HDMI cloning. Suppress them.
5203 */
5204 if (intel_crtc->config->has_pch_encoder)
5205 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5206
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005207 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005208
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005209 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005210
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005211 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005212 ironlake_fdi_disable(crtc);
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005213 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5214 }
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005215
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005216 for_each_encoder_on_crtc(dev, crtc, encoder)
5217 if (encoder->post_disable)
5218 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005219
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005220 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005221 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005222
Daniel Vetterd925c592013-06-05 13:34:04 +02005223 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005224 i915_reg_t reg;
5225 u32 temp;
5226
Daniel Vetterd925c592013-06-05 13:34:04 +02005227 /* disable TRANS_DP_CTL */
5228 reg = TRANS_DP_CTL(pipe);
5229 temp = I915_READ(reg);
5230 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5231 TRANS_DP_PORT_SEL_MASK);
5232 temp |= TRANS_DP_PORT_SEL_NONE;
5233 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005234
Daniel Vetterd925c592013-06-05 13:34:04 +02005235 /* disable DPLL_SEL */
5236 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005237 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005238 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005239 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005240
Daniel Vetterd925c592013-06-05 13:34:04 +02005241 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005242 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005243
5244 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005245}
5246
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005247static void haswell_crtc_disable(struct drm_crtc *crtc)
5248{
5249 struct drm_device *dev = crtc->dev;
5250 struct drm_i915_private *dev_priv = dev->dev_private;
5251 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5252 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005253 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005254
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005255 if (intel_crtc->config->has_pch_encoder)
5256 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5257 false);
5258
Jani Nikula8807e552013-08-30 19:40:32 +03005259 for_each_encoder_on_crtc(dev, crtc, encoder) {
5260 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005261 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005262 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005263
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005264 drm_crtc_vblank_off(crtc);
5265 assert_vblank_disabled(crtc);
5266
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005267 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005268
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005269 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005270 intel_ddi_set_vc_payload_alloc(crtc, false);
5271
Jani Nikulaa65347b2015-11-27 12:21:46 +02005272 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305273 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005274
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005275 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005276 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005277 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005278 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005279
Jani Nikulaa65347b2015-11-27 12:21:46 +02005280 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305281 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005282
Imre Deak97b040a2014-06-25 22:01:50 +03005283 for_each_encoder_on_crtc(dev, crtc, encoder)
5284 if (encoder->post_disable)
5285 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005286
Ville Syrjälä92966a32015-12-08 16:05:48 +02005287 if (intel_crtc->config->has_pch_encoder) {
5288 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005289 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005290 intel_ddi_fdi_disable(crtc);
5291
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005292 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5293 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005294 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005295}
5296
Jesse Barnes2dd24552013-04-25 12:55:01 -07005297static void i9xx_pfit_enable(struct intel_crtc *crtc)
5298{
5299 struct drm_device *dev = crtc->base.dev;
5300 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005301 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005302
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005303 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005304 return;
5305
Daniel Vetterc0b03412013-05-28 12:05:54 +02005306 /*
5307 * The panel fitter should only be adjusted whilst the pipe is disabled,
5308 * according to register description and PRM.
5309 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005310 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5311 assert_pipe_disabled(dev_priv, crtc->pipe);
5312
Jesse Barnesb074cec2013-04-25 12:55:02 -07005313 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5314 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005315
5316 /* Border color in case we don't scale up to the full screen. Black by
5317 * default, change to something else for debugging. */
5318 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005319}
5320
Dave Airlied05410f2014-06-05 13:22:59 +10005321static enum intel_display_power_domain port_to_power_domain(enum port port)
5322{
5323 switch (port) {
5324 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005325 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005326 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005327 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005328 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005329 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005330 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005331 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005332 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005333 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005334 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005335 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005336 return POWER_DOMAIN_PORT_OTHER;
5337 }
5338}
5339
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005340static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5341{
5342 switch (port) {
5343 case PORT_A:
5344 return POWER_DOMAIN_AUX_A;
5345 case PORT_B:
5346 return POWER_DOMAIN_AUX_B;
5347 case PORT_C:
5348 return POWER_DOMAIN_AUX_C;
5349 case PORT_D:
5350 return POWER_DOMAIN_AUX_D;
5351 case PORT_E:
5352 /* FIXME: Check VBT for actual wiring of PORT E */
5353 return POWER_DOMAIN_AUX_D;
5354 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005355 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005356 return POWER_DOMAIN_AUX_A;
5357 }
5358}
5359
Imre Deak319be8a2014-03-04 19:22:57 +02005360enum intel_display_power_domain
5361intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005362{
Imre Deak319be8a2014-03-04 19:22:57 +02005363 struct drm_device *dev = intel_encoder->base.dev;
5364 struct intel_digital_port *intel_dig_port;
5365
5366 switch (intel_encoder->type) {
5367 case INTEL_OUTPUT_UNKNOWN:
5368 /* Only DDI platforms should ever use this output type */
5369 WARN_ON_ONCE(!HAS_DDI(dev));
5370 case INTEL_OUTPUT_DISPLAYPORT:
5371 case INTEL_OUTPUT_HDMI:
5372 case INTEL_OUTPUT_EDP:
5373 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005374 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005375 case INTEL_OUTPUT_DP_MST:
5376 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5377 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005378 case INTEL_OUTPUT_ANALOG:
5379 return POWER_DOMAIN_PORT_CRT;
5380 case INTEL_OUTPUT_DSI:
5381 return POWER_DOMAIN_PORT_DSI;
5382 default:
5383 return POWER_DOMAIN_PORT_OTHER;
5384 }
5385}
5386
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005387enum intel_display_power_domain
5388intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5389{
5390 struct drm_device *dev = intel_encoder->base.dev;
5391 struct intel_digital_port *intel_dig_port;
5392
5393 switch (intel_encoder->type) {
5394 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005395 case INTEL_OUTPUT_HDMI:
5396 /*
5397 * Only DDI platforms should ever use these output types.
5398 * We can get here after the HDMI detect code has already set
5399 * the type of the shared encoder. Since we can't be sure
5400 * what's the status of the given connectors, play safe and
5401 * run the DP detection too.
5402 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005403 WARN_ON_ONCE(!HAS_DDI(dev));
5404 case INTEL_OUTPUT_DISPLAYPORT:
5405 case INTEL_OUTPUT_EDP:
5406 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5407 return port_to_aux_power_domain(intel_dig_port->port);
5408 case INTEL_OUTPUT_DP_MST:
5409 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5410 return port_to_aux_power_domain(intel_dig_port->port);
5411 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005412 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005413 return POWER_DOMAIN_AUX_A;
5414 }
5415}
5416
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005417static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5418 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005419{
5420 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005421 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005422 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5423 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005424 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005425 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005426
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005427 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005428 return 0;
5429
Imre Deak77d22dc2014-03-05 16:20:52 +02005430 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5431 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005432 if (crtc_state->pch_pfit.enabled ||
5433 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005434 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5435
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005436 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5437 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5438
Imre Deak319be8a2014-03-04 19:22:57 +02005439 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005440 }
Imre Deak319be8a2014-03-04 19:22:57 +02005441
Imre Deak77d22dc2014-03-05 16:20:52 +02005442 return mask;
5443}
5444
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005445static unsigned long
5446modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5447 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005448{
5449 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5451 enum intel_display_power_domain domain;
5452 unsigned long domains, new_domains, old_domains;
5453
5454 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005455 intel_crtc->enabled_power_domains = new_domains =
5456 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005457
5458 domains = new_domains & ~old_domains;
5459
5460 for_each_power_domain(domain, domains)
5461 intel_display_power_get(dev_priv, domain);
5462
5463 return old_domains & ~new_domains;
5464}
5465
5466static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5467 unsigned long domains)
5468{
5469 enum intel_display_power_domain domain;
5470
5471 for_each_power_domain(domain, domains)
5472 intel_display_power_put(dev_priv, domain);
5473}
5474
Mika Kaholaadafdc62015-08-18 14:36:59 +03005475static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5476{
5477 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5478
5479 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5480 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5481 return max_cdclk_freq;
5482 else if (IS_CHERRYVIEW(dev_priv))
5483 return max_cdclk_freq*95/100;
5484 else if (INTEL_INFO(dev_priv)->gen < 4)
5485 return 2*max_cdclk_freq*90/100;
5486 else
5487 return max_cdclk_freq*90/100;
5488}
5489
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005490static void intel_update_max_cdclk(struct drm_device *dev)
5491{
5492 struct drm_i915_private *dev_priv = dev->dev_private;
5493
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005494 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005495 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5496
5497 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5498 dev_priv->max_cdclk_freq = 675000;
5499 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5500 dev_priv->max_cdclk_freq = 540000;
5501 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5502 dev_priv->max_cdclk_freq = 450000;
5503 else
5504 dev_priv->max_cdclk_freq = 337500;
5505 } else if (IS_BROADWELL(dev)) {
5506 /*
5507 * FIXME with extra cooling we can allow
5508 * 540 MHz for ULX and 675 Mhz for ULT.
5509 * How can we know if extra cooling is
5510 * available? PCI ID, VTB, something else?
5511 */
5512 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5513 dev_priv->max_cdclk_freq = 450000;
5514 else if (IS_BDW_ULX(dev))
5515 dev_priv->max_cdclk_freq = 450000;
5516 else if (IS_BDW_ULT(dev))
5517 dev_priv->max_cdclk_freq = 540000;
5518 else
5519 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005520 } else if (IS_CHERRYVIEW(dev)) {
5521 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005522 } else if (IS_VALLEYVIEW(dev)) {
5523 dev_priv->max_cdclk_freq = 400000;
5524 } else {
5525 /* otherwise assume cdclk is fixed */
5526 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5527 }
5528
Mika Kaholaadafdc62015-08-18 14:36:59 +03005529 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5530
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005531 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5532 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005533
5534 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5535 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005536}
5537
5538static void intel_update_cdclk(struct drm_device *dev)
5539{
5540 struct drm_i915_private *dev_priv = dev->dev_private;
5541
5542 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5543 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5544 dev_priv->cdclk_freq);
5545
5546 /*
5547 * Program the gmbus_freq based on the cdclk frequency.
5548 * BSpec erroneously claims we should aim for 4MHz, but
5549 * in fact 1MHz is the correct frequency.
5550 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005551 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005552 /*
5553 * Program the gmbus_freq based on the cdclk frequency.
5554 * BSpec erroneously claims we should aim for 4MHz, but
5555 * in fact 1MHz is the correct frequency.
5556 */
5557 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5558 }
5559
5560 if (dev_priv->max_cdclk_freq == 0)
5561 intel_update_max_cdclk(dev);
5562}
5563
Damien Lespiau70d0c572015-06-04 18:21:29 +01005564static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305565{
5566 struct drm_i915_private *dev_priv = dev->dev_private;
5567 uint32_t divider;
5568 uint32_t ratio;
5569 uint32_t current_freq;
5570 int ret;
5571
5572 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5573 switch (frequency) {
5574 case 144000:
5575 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5576 ratio = BXT_DE_PLL_RATIO(60);
5577 break;
5578 case 288000:
5579 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5580 ratio = BXT_DE_PLL_RATIO(60);
5581 break;
5582 case 384000:
5583 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5584 ratio = BXT_DE_PLL_RATIO(60);
5585 break;
5586 case 576000:
5587 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5588 ratio = BXT_DE_PLL_RATIO(60);
5589 break;
5590 case 624000:
5591 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5592 ratio = BXT_DE_PLL_RATIO(65);
5593 break;
5594 case 19200:
5595 /*
5596 * Bypass frequency with DE PLL disabled. Init ratio, divider
5597 * to suppress GCC warning.
5598 */
5599 ratio = 0;
5600 divider = 0;
5601 break;
5602 default:
5603 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5604
5605 return;
5606 }
5607
5608 mutex_lock(&dev_priv->rps.hw_lock);
5609 /* Inform power controller of upcoming frequency change */
5610 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5611 0x80000000);
5612 mutex_unlock(&dev_priv->rps.hw_lock);
5613
5614 if (ret) {
5615 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5616 ret, frequency);
5617 return;
5618 }
5619
5620 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5621 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5622 current_freq = current_freq * 500 + 1000;
5623
5624 /*
5625 * DE PLL has to be disabled when
5626 * - setting to 19.2MHz (bypass, PLL isn't used)
5627 * - before setting to 624MHz (PLL needs toggling)
5628 * - before setting to any frequency from 624MHz (PLL needs toggling)
5629 */
5630 if (frequency == 19200 || frequency == 624000 ||
5631 current_freq == 624000) {
5632 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5633 /* Timeout 200us */
5634 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5635 1))
5636 DRM_ERROR("timout waiting for DE PLL unlock\n");
5637 }
5638
5639 if (frequency != 19200) {
5640 uint32_t val;
5641
5642 val = I915_READ(BXT_DE_PLL_CTL);
5643 val &= ~BXT_DE_PLL_RATIO_MASK;
5644 val |= ratio;
5645 I915_WRITE(BXT_DE_PLL_CTL, val);
5646
5647 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5648 /* Timeout 200us */
5649 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5650 DRM_ERROR("timeout waiting for DE PLL lock\n");
5651
5652 val = I915_READ(CDCLK_CTL);
5653 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5654 val |= divider;
5655 /*
5656 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5657 * enable otherwise.
5658 */
5659 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5660 if (frequency >= 500000)
5661 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5662
5663 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5664 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5665 val |= (frequency - 1000) / 500;
5666 I915_WRITE(CDCLK_CTL, val);
5667 }
5668
5669 mutex_lock(&dev_priv->rps.hw_lock);
5670 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5671 DIV_ROUND_UP(frequency, 25000));
5672 mutex_unlock(&dev_priv->rps.hw_lock);
5673
5674 if (ret) {
5675 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5676 ret, frequency);
5677 return;
5678 }
5679
Damien Lespiaua47871b2015-06-04 18:21:34 +01005680 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305681}
5682
5683void broxton_init_cdclk(struct drm_device *dev)
5684{
5685 struct drm_i915_private *dev_priv = dev->dev_private;
5686 uint32_t val;
5687
5688 /*
5689 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5690 * or else the reset will hang because there is no PCH to respond.
5691 * Move the handshake programming to initialization sequence.
5692 * Previously was left up to BIOS.
5693 */
5694 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5695 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5696 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5697
5698 /* Enable PG1 for cdclk */
5699 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5700
5701 /* check if cd clock is enabled */
5702 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5703 DRM_DEBUG_KMS("Display already initialized\n");
5704 return;
5705 }
5706
5707 /*
5708 * FIXME:
5709 * - The initial CDCLK needs to be read from VBT.
5710 * Need to make this change after VBT has changes for BXT.
5711 * - check if setting the max (or any) cdclk freq is really necessary
5712 * here, it belongs to modeset time
5713 */
5714 broxton_set_cdclk(dev, 624000);
5715
5716 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005717 POSTING_READ(DBUF_CTL);
5718
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305719 udelay(10);
5720
5721 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5722 DRM_ERROR("DBuf power enable timeout!\n");
5723}
5724
5725void broxton_uninit_cdclk(struct drm_device *dev)
5726{
5727 struct drm_i915_private *dev_priv = dev->dev_private;
5728
5729 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005730 POSTING_READ(DBUF_CTL);
5731
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305732 udelay(10);
5733
5734 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5735 DRM_ERROR("DBuf power disable timeout!\n");
5736
5737 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5738 broxton_set_cdclk(dev, 19200);
5739
5740 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5741}
5742
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005743static const struct skl_cdclk_entry {
5744 unsigned int freq;
5745 unsigned int vco;
5746} skl_cdclk_frequencies[] = {
5747 { .freq = 308570, .vco = 8640 },
5748 { .freq = 337500, .vco = 8100 },
5749 { .freq = 432000, .vco = 8640 },
5750 { .freq = 450000, .vco = 8100 },
5751 { .freq = 540000, .vco = 8100 },
5752 { .freq = 617140, .vco = 8640 },
5753 { .freq = 675000, .vco = 8100 },
5754};
5755
5756static unsigned int skl_cdclk_decimal(unsigned int freq)
5757{
5758 return (freq - 1000) / 500;
5759}
5760
5761static unsigned int skl_cdclk_get_vco(unsigned int freq)
5762{
5763 unsigned int i;
5764
5765 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5766 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5767
5768 if (e->freq == freq)
5769 return e->vco;
5770 }
5771
5772 return 8100;
5773}
5774
5775static void
5776skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5777{
5778 unsigned int min_freq;
5779 u32 val;
5780
5781 /* select the minimum CDCLK before enabling DPLL 0 */
5782 val = I915_READ(CDCLK_CTL);
5783 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5784 val |= CDCLK_FREQ_337_308;
5785
5786 if (required_vco == 8640)
5787 min_freq = 308570;
5788 else
5789 min_freq = 337500;
5790
5791 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5792
5793 I915_WRITE(CDCLK_CTL, val);
5794 POSTING_READ(CDCLK_CTL);
5795
5796 /*
5797 * We always enable DPLL0 with the lowest link rate possible, but still
5798 * taking into account the VCO required to operate the eDP panel at the
5799 * desired frequency. The usual DP link rates operate with a VCO of
5800 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5801 * The modeset code is responsible for the selection of the exact link
5802 * rate later on, with the constraint of choosing a frequency that
5803 * works with required_vco.
5804 */
5805 val = I915_READ(DPLL_CTRL1);
5806
5807 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5808 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5809 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5810 if (required_vco == 8640)
5811 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5812 SKL_DPLL0);
5813 else
5814 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5815 SKL_DPLL0);
5816
5817 I915_WRITE(DPLL_CTRL1, val);
5818 POSTING_READ(DPLL_CTRL1);
5819
5820 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5821
5822 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5823 DRM_ERROR("DPLL0 not locked\n");
5824}
5825
5826static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5827{
5828 int ret;
5829 u32 val;
5830
5831 /* inform PCU we want to change CDCLK */
5832 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5833 mutex_lock(&dev_priv->rps.hw_lock);
5834 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5835 mutex_unlock(&dev_priv->rps.hw_lock);
5836
5837 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5838}
5839
5840static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5841{
5842 unsigned int i;
5843
5844 for (i = 0; i < 15; i++) {
5845 if (skl_cdclk_pcu_ready(dev_priv))
5846 return true;
5847 udelay(10);
5848 }
5849
5850 return false;
5851}
5852
5853static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5854{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005855 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005856 u32 freq_select, pcu_ack;
5857
5858 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5859
5860 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5861 DRM_ERROR("failed to inform PCU about cdclk change\n");
5862 return;
5863 }
5864
5865 /* set CDCLK_CTL */
5866 switch(freq) {
5867 case 450000:
5868 case 432000:
5869 freq_select = CDCLK_FREQ_450_432;
5870 pcu_ack = 1;
5871 break;
5872 case 540000:
5873 freq_select = CDCLK_FREQ_540;
5874 pcu_ack = 2;
5875 break;
5876 case 308570:
5877 case 337500:
5878 default:
5879 freq_select = CDCLK_FREQ_337_308;
5880 pcu_ack = 0;
5881 break;
5882 case 617140:
5883 case 675000:
5884 freq_select = CDCLK_FREQ_675_617;
5885 pcu_ack = 3;
5886 break;
5887 }
5888
5889 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5890 POSTING_READ(CDCLK_CTL);
5891
5892 /* inform PCU of the change */
5893 mutex_lock(&dev_priv->rps.hw_lock);
5894 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5895 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005896
5897 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005898}
5899
5900void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5901{
5902 /* disable DBUF power */
5903 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5904 POSTING_READ(DBUF_CTL);
5905
5906 udelay(10);
5907
5908 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5909 DRM_ERROR("DBuf power disable timeout\n");
5910
Imre Deakab96c1ee2015-11-04 19:24:18 +02005911 /* disable DPLL0 */
5912 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5913 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5914 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005915}
5916
5917void skl_init_cdclk(struct drm_i915_private *dev_priv)
5918{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005919 unsigned int required_vco;
5920
Gary Wang39d9b852015-08-28 16:40:34 +08005921 /* DPLL0 not enabled (happens on early BIOS versions) */
5922 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5923 /* enable DPLL0 */
5924 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5925 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005926 }
5927
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005928 /* set CDCLK to the frequency the BIOS chose */
5929 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5930
5931 /* enable DBUF power */
5932 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5933 POSTING_READ(DBUF_CTL);
5934
5935 udelay(10);
5936
5937 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5938 DRM_ERROR("DBuf power enable timeout\n");
5939}
5940
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305941int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5942{
5943 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5944 uint32_t cdctl = I915_READ(CDCLK_CTL);
5945 int freq = dev_priv->skl_boot_cdclk;
5946
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305947 /*
5948 * check if the pre-os intialized the display
5949 * There is SWF18 scratchpad register defined which is set by the
5950 * pre-os which can be used by the OS drivers to check the status
5951 */
5952 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5953 goto sanitize;
5954
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305955 /* Is PLL enabled and locked ? */
5956 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5957 goto sanitize;
5958
5959 /* DPLL okay; verify the cdclock
5960 *
5961 * Noticed in some instances that the freq selection is correct but
5962 * decimal part is programmed wrong from BIOS where pre-os does not
5963 * enable display. Verify the same as well.
5964 */
5965 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5966 /* All well; nothing to sanitize */
5967 return false;
5968sanitize:
5969 /*
5970 * As of now initialize with max cdclk till
5971 * we get dynamic cdclk support
5972 * */
5973 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5974 skl_init_cdclk(dev_priv);
5975
5976 /* we did have to sanitize */
5977 return true;
5978}
5979
Jesse Barnes30a970c2013-11-04 13:48:12 -08005980/* Adjust CDclk dividers to allow high res or save power if possible */
5981static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5982{
5983 struct drm_i915_private *dev_priv = dev->dev_private;
5984 u32 val, cmd;
5985
Vandana Kannan164dfd22014-11-24 13:37:41 +05305986 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5987 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005988
Ville Syrjälädfcab172014-06-13 13:37:47 +03005989 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005990 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005991 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005992 cmd = 1;
5993 else
5994 cmd = 0;
5995
5996 mutex_lock(&dev_priv->rps.hw_lock);
5997 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5998 val &= ~DSPFREQGUAR_MASK;
5999 val |= (cmd << DSPFREQGUAR_SHIFT);
6000 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6001 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6002 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
6003 50)) {
6004 DRM_ERROR("timed out waiting for CDclk change\n");
6005 }
6006 mutex_unlock(&dev_priv->rps.hw_lock);
6007
Ville Syrjälä54433e92015-05-26 20:42:31 +03006008 mutex_lock(&dev_priv->sb_lock);
6009
Ville Syrjälädfcab172014-06-13 13:37:47 +03006010 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006011 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006012
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006013 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006014
Jesse Barnes30a970c2013-11-04 13:48:12 -08006015 /* adjust cdclk divider */
6016 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03006017 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006018 val |= divider;
6019 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03006020
6021 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03006022 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03006023 50))
6024 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08006025 }
6026
Jesse Barnes30a970c2013-11-04 13:48:12 -08006027 /* adjust self-refresh exit latency value */
6028 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
6029 val &= ~0x7f;
6030
6031 /*
6032 * For high bandwidth configs, we set a higher latency in the bunit
6033 * so that the core display fetch happens in time to avoid underruns.
6034 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03006035 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006036 val |= 4500 / 250; /* 4.5 usec */
6037 else
6038 val |= 3000 / 250; /* 3.0 usec */
6039 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03006040
Ville Syrjäläa5805162015-05-26 20:42:30 +03006041 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006042
Ville Syrjäläb6283052015-06-03 15:45:07 +03006043 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006044}
6045
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006046static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6047{
6048 struct drm_i915_private *dev_priv = dev->dev_private;
6049 u32 val, cmd;
6050
Vandana Kannan164dfd22014-11-24 13:37:41 +05306051 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6052 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006053
6054 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006055 case 333333:
6056 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006057 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006058 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006059 break;
6060 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01006061 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006062 return;
6063 }
6064
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02006065 /*
6066 * Specs are full of misinformation, but testing on actual
6067 * hardware has shown that we just need to write the desired
6068 * CCK divider into the Punit register.
6069 */
6070 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6071
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006072 mutex_lock(&dev_priv->rps.hw_lock);
6073 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6074 val &= ~DSPFREQGUAR_MASK_CHV;
6075 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6076 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6077 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6078 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6079 50)) {
6080 DRM_ERROR("timed out waiting for CDclk change\n");
6081 }
6082 mutex_unlock(&dev_priv->rps.hw_lock);
6083
Ville Syrjäläb6283052015-06-03 15:45:07 +03006084 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006085}
6086
Jesse Barnes30a970c2013-11-04 13:48:12 -08006087static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6088 int max_pixclk)
6089{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006090 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006091 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006092
Jesse Barnes30a970c2013-11-04 13:48:12 -08006093 /*
6094 * Really only a few cases to deal with, as only 4 CDclks are supported:
6095 * 200MHz
6096 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006097 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006098 * 400MHz (VLV only)
6099 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6100 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006101 *
6102 * We seem to get an unstable or solid color picture at 200MHz.
6103 * Not sure what's wrong. For now use 200MHz only when all pipes
6104 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08006105 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006106 if (!IS_CHERRYVIEW(dev_priv) &&
6107 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006108 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006109 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006110 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006111 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006112 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006113 else
6114 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006115}
6116
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306117static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
6118 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006119{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306120 /*
6121 * FIXME:
6122 * - remove the guardband, it's not needed on BXT
6123 * - set 19.2MHz bypass frequency if there are no active pipes
6124 */
6125 if (max_pixclk > 576000*9/10)
6126 return 624000;
6127 else if (max_pixclk > 384000*9/10)
6128 return 576000;
6129 else if (max_pixclk > 288000*9/10)
6130 return 384000;
6131 else if (max_pixclk > 144000*9/10)
6132 return 288000;
6133 else
6134 return 144000;
6135}
6136
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01006137/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006138static int intel_mode_max_pixclk(struct drm_device *dev,
6139 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006140{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006141 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
6142 struct drm_i915_private *dev_priv = dev->dev_private;
6143 struct drm_crtc *crtc;
6144 struct drm_crtc_state *crtc_state;
6145 unsigned max_pixclk = 0, i;
6146 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006147
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006148 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6149 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006150
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006151 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6152 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006153
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006154 if (crtc_state->enable)
6155 pixclk = crtc_state->adjusted_mode.crtc_clock;
6156
6157 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006158 }
6159
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006160 for_each_pipe(dev_priv, pipe)
6161 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6162
Jesse Barnes30a970c2013-11-04 13:48:12 -08006163 return max_pixclk;
6164}
6165
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006166static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006167{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006168 struct drm_device *dev = state->dev;
6169 struct drm_i915_private *dev_priv = dev->dev_private;
6170 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006171 struct intel_atomic_state *intel_state =
6172 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006173
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006174 if (max_pixclk < 0)
6175 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006176
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006177 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006178 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306179
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006180 if (!intel_state->active_crtcs)
6181 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6182
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006183 return 0;
6184}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006185
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006186static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
6187{
6188 struct drm_device *dev = state->dev;
6189 struct drm_i915_private *dev_priv = dev->dev_private;
6190 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006191 struct intel_atomic_state *intel_state =
6192 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006193
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006194 if (max_pixclk < 0)
6195 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006196
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006197 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006198 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006199
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006200 if (!intel_state->active_crtcs)
6201 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
6202
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006203 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006204}
6205
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006206static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6207{
6208 unsigned int credits, default_credits;
6209
6210 if (IS_CHERRYVIEW(dev_priv))
6211 default_credits = PFI_CREDIT(12);
6212 else
6213 default_credits = PFI_CREDIT(8);
6214
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006215 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006216 /* CHV suggested value is 31 or 63 */
6217 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006218 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006219 else
6220 credits = PFI_CREDIT(15);
6221 } else {
6222 credits = default_credits;
6223 }
6224
6225 /*
6226 * WA - write default credits before re-programming
6227 * FIXME: should we also set the resend bit here?
6228 */
6229 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6230 default_credits);
6231
6232 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6233 credits | PFI_CREDIT_RESEND);
6234
6235 /*
6236 * FIXME is this guaranteed to clear
6237 * immediately or should we poll for it?
6238 */
6239 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6240}
6241
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006242static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006243{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006244 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006245 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006246 struct intel_atomic_state *old_intel_state =
6247 to_intel_atomic_state(old_state);
6248 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006249
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006250 /*
6251 * FIXME: We can end up here with all power domains off, yet
6252 * with a CDCLK frequency other than the minimum. To account
6253 * for this take the PIPE-A power domain, which covers the HW
6254 * blocks needed for the following programming. This can be
6255 * removed once it's guaranteed that we get here either with
6256 * the minimum CDCLK set, or the required power domains
6257 * enabled.
6258 */
6259 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006260
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006261 if (IS_CHERRYVIEW(dev))
6262 cherryview_set_cdclk(dev, req_cdclk);
6263 else
6264 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006265
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006266 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006267
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006268 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006269}
6270
Jesse Barnes89b667f2013-04-18 14:51:36 -07006271static void valleyview_crtc_enable(struct drm_crtc *crtc)
6272{
6273 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006274 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006275 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6276 struct intel_encoder *encoder;
6277 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006278
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006279 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006280 return;
6281
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006282 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306283 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006284
6285 intel_set_pipe_timings(intel_crtc);
6286
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006287 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6288 struct drm_i915_private *dev_priv = dev->dev_private;
6289
6290 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6291 I915_WRITE(CHV_CANVAS(pipe), 0);
6292 }
6293
Daniel Vetter5b18e572014-04-24 23:55:06 +02006294 i9xx_set_pipeconf(intel_crtc);
6295
Jesse Barnes89b667f2013-04-18 14:51:36 -07006296 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006297
Daniel Vettera72e4c92014-09-30 10:56:47 +02006298 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006299
Jesse Barnes89b667f2013-04-18 14:51:36 -07006300 for_each_encoder_on_crtc(dev, crtc, encoder)
6301 if (encoder->pre_pll_enable)
6302 encoder->pre_pll_enable(encoder);
6303
Jani Nikulaa65347b2015-11-27 12:21:46 +02006304 if (!intel_crtc->config->has_dsi_encoder) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006305 if (IS_CHERRYVIEW(dev)) {
6306 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006307 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006308 } else {
6309 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006310 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006311 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006312 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006313
6314 for_each_encoder_on_crtc(dev, crtc, encoder)
6315 if (encoder->pre_enable)
6316 encoder->pre_enable(encoder);
6317
Jesse Barnes2dd24552013-04-25 12:55:01 -07006318 i9xx_pfit_enable(intel_crtc);
6319
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006320 intel_crtc_load_lut(crtc);
6321
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006322 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006323
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006324 assert_vblank_disabled(crtc);
6325 drm_crtc_vblank_on(crtc);
6326
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006327 for_each_encoder_on_crtc(dev, crtc, encoder)
6328 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006329}
6330
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006331static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6332{
6333 struct drm_device *dev = crtc->base.dev;
6334 struct drm_i915_private *dev_priv = dev->dev_private;
6335
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006336 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6337 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006338}
6339
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006340static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006341{
6342 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006343 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006344 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006345 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006346 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006347
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006348 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006349 return;
6350
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006351 i9xx_set_pll_dividers(intel_crtc);
6352
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006353 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306354 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006355
6356 intel_set_pipe_timings(intel_crtc);
6357
Daniel Vetter5b18e572014-04-24 23:55:06 +02006358 i9xx_set_pipeconf(intel_crtc);
6359
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006360 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006361
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006362 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006363 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006364
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006365 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006366 if (encoder->pre_enable)
6367 encoder->pre_enable(encoder);
6368
Daniel Vetterf6736a12013-06-05 13:34:30 +02006369 i9xx_enable_pll(intel_crtc);
6370
Jesse Barnes2dd24552013-04-25 12:55:01 -07006371 i9xx_pfit_enable(intel_crtc);
6372
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006373 intel_crtc_load_lut(crtc);
6374
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006375 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006376 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006377
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006378 assert_vblank_disabled(crtc);
6379 drm_crtc_vblank_on(crtc);
6380
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006381 for_each_encoder_on_crtc(dev, crtc, encoder)
6382 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006383}
6384
Daniel Vetter87476d62013-04-11 16:29:06 +02006385static void i9xx_pfit_disable(struct intel_crtc *crtc)
6386{
6387 struct drm_device *dev = crtc->base.dev;
6388 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006389
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006390 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006391 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006392
6393 assert_pipe_disabled(dev_priv, crtc->pipe);
6394
Daniel Vetter328d8e82013-05-08 10:36:31 +02006395 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6396 I915_READ(PFIT_CONTROL));
6397 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006398}
6399
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006400static void i9xx_crtc_disable(struct drm_crtc *crtc)
6401{
6402 struct drm_device *dev = crtc->dev;
6403 struct drm_i915_private *dev_priv = dev->dev_private;
6404 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006405 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006406 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006407
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006408 /*
6409 * On gen2 planes are double buffered but the pipe isn't, so we must
6410 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006411 * We also need to wait on all gmch platforms because of the
6412 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006413 */
Imre Deak564ed192014-06-13 14:54:21 +03006414 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006415
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006416 for_each_encoder_on_crtc(dev, crtc, encoder)
6417 encoder->disable(encoder);
6418
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006419 drm_crtc_vblank_off(crtc);
6420 assert_vblank_disabled(crtc);
6421
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006422 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006423
Daniel Vetter87476d62013-04-11 16:29:06 +02006424 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006425
Jesse Barnes89b667f2013-04-18 14:51:36 -07006426 for_each_encoder_on_crtc(dev, crtc, encoder)
6427 if (encoder->post_disable)
6428 encoder->post_disable(encoder);
6429
Jani Nikulaa65347b2015-11-27 12:21:46 +02006430 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006431 if (IS_CHERRYVIEW(dev))
6432 chv_disable_pll(dev_priv, pipe);
6433 else if (IS_VALLEYVIEW(dev))
6434 vlv_disable_pll(dev_priv, pipe);
6435 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006436 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006437 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006438
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006439 for_each_encoder_on_crtc(dev, crtc, encoder)
6440 if (encoder->post_pll_disable)
6441 encoder->post_pll_disable(encoder);
6442
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006443 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006444 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006445}
6446
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006447static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006448{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006449 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006450 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006451 enum intel_display_power_domain domain;
6452 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006453
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006454 if (!intel_crtc->active)
6455 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006456
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006457 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006458 WARN_ON(intel_crtc->unpin_work);
6459
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006460 intel_pre_disable_primary(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006461
6462 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6463 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006464 }
6465
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006466 dev_priv->display.crtc_disable(crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006467 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006468 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006469 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006470 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006471
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006472 domains = intel_crtc->enabled_power_domains;
6473 for_each_power_domain(domain, domains)
6474 intel_display_power_put(dev_priv, domain);
6475 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006476
6477 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6478 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006479}
6480
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006481/*
6482 * turn all crtc's off, but do not adjust state
6483 * This has to be paired with a call to intel_modeset_setup_hw_state.
6484 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006485int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006486{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006487 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006488 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006489 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006490
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006491 state = drm_atomic_helper_suspend(dev);
6492 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006493 if (ret)
6494 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006495 else
6496 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006497 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006498}
6499
Chris Wilsonea5b2132010-08-04 13:50:23 +01006500void intel_encoder_destroy(struct drm_encoder *encoder)
6501{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006502 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006503
Chris Wilsonea5b2132010-08-04 13:50:23 +01006504 drm_encoder_cleanup(encoder);
6505 kfree(intel_encoder);
6506}
6507
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006508/* Cross check the actual hw state with our own modeset state tracking (and it's
6509 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006510static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006511{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006512 struct drm_crtc *crtc = connector->base.state->crtc;
6513
6514 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6515 connector->base.base.id,
6516 connector->base.name);
6517
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006518 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006519 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006520 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006521
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006522 I915_STATE_WARN(!crtc,
6523 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006524
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006525 if (!crtc)
6526 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006527
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006528 I915_STATE_WARN(!crtc->state->active,
6529 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006530
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006531 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006532 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006533
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006534 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006535 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006536
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006537 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006538 "attached encoder crtc differs from connector crtc\n");
6539 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006540 I915_STATE_WARN(crtc && crtc->state->active,
6541 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006542 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6543 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006544 }
6545}
6546
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006547int intel_connector_init(struct intel_connector *connector)
6548{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006549 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006550
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006551 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006552 return -ENOMEM;
6553
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006554 return 0;
6555}
6556
6557struct intel_connector *intel_connector_alloc(void)
6558{
6559 struct intel_connector *connector;
6560
6561 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6562 if (!connector)
6563 return NULL;
6564
6565 if (intel_connector_init(connector) < 0) {
6566 kfree(connector);
6567 return NULL;
6568 }
6569
6570 return connector;
6571}
6572
Daniel Vetterf0947c32012-07-02 13:10:34 +02006573/* Simple connector->get_hw_state implementation for encoders that support only
6574 * one connector and no cloning and hence the encoder state determines the state
6575 * of the connector. */
6576bool intel_connector_get_hw_state(struct intel_connector *connector)
6577{
Daniel Vetter24929352012-07-02 20:28:59 +02006578 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006579 struct intel_encoder *encoder = connector->encoder;
6580
6581 return encoder->get_hw_state(encoder, &pipe);
6582}
6583
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006584static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006585{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006586 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6587 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006588
6589 return 0;
6590}
6591
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006592static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006593 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006594{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006595 struct drm_atomic_state *state = pipe_config->base.state;
6596 struct intel_crtc *other_crtc;
6597 struct intel_crtc_state *other_crtc_state;
6598
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006599 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6600 pipe_name(pipe), pipe_config->fdi_lanes);
6601 if (pipe_config->fdi_lanes > 4) {
6602 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6603 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006604 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006605 }
6606
Paulo Zanonibafb6552013-11-02 21:07:44 -07006607 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006608 if (pipe_config->fdi_lanes > 2) {
6609 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6610 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006611 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006612 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006613 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006614 }
6615 }
6616
6617 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006618 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006619
6620 /* Ivybridge 3 pipe is really complicated */
6621 switch (pipe) {
6622 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006623 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006624 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006625 if (pipe_config->fdi_lanes <= 2)
6626 return 0;
6627
6628 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6629 other_crtc_state =
6630 intel_atomic_get_crtc_state(state, other_crtc);
6631 if (IS_ERR(other_crtc_state))
6632 return PTR_ERR(other_crtc_state);
6633
6634 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006635 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6636 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006637 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006638 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006639 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006640 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006641 if (pipe_config->fdi_lanes > 2) {
6642 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6643 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006644 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006645 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006646
6647 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6648 other_crtc_state =
6649 intel_atomic_get_crtc_state(state, other_crtc);
6650 if (IS_ERR(other_crtc_state))
6651 return PTR_ERR(other_crtc_state);
6652
6653 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006654 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006655 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006656 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006657 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006658 default:
6659 BUG();
6660 }
6661}
6662
Daniel Vettere29c22c2013-02-21 00:00:16 +01006663#define RETRY 1
6664static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006665 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006666{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006667 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006668 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006669 int lane, link_bw, fdi_dotclock, ret;
6670 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006671
Daniel Vettere29c22c2013-02-21 00:00:16 +01006672retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006673 /* FDI is a binary signal running at ~2.7GHz, encoding
6674 * each output octet as 10 bits. The actual frequency
6675 * is stored as a divider into a 100MHz clock, and the
6676 * mode pixel clock is stored in units of 1KHz.
6677 * Hence the bw of each lane in terms of the mode signal
6678 * is:
6679 */
6680 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6681
Damien Lespiau241bfc32013-09-25 16:45:37 +01006682 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006683
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006684 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006685 pipe_config->pipe_bpp);
6686
6687 pipe_config->fdi_lanes = lane;
6688
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006689 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006690 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006691
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006692 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6693 intel_crtc->pipe, pipe_config);
6694 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006695 pipe_config->pipe_bpp -= 2*3;
6696 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6697 pipe_config->pipe_bpp);
6698 needs_recompute = true;
6699 pipe_config->bw_constrained = true;
6700
6701 goto retry;
6702 }
6703
6704 if (needs_recompute)
6705 return RETRY;
6706
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006707 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006708}
6709
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006710static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6711 struct intel_crtc_state *pipe_config)
6712{
6713 if (pipe_config->pipe_bpp > 24)
6714 return false;
6715
6716 /* HSW can handle pixel rate up to cdclk? */
6717 if (IS_HASWELL(dev_priv->dev))
6718 return true;
6719
6720 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006721 * We compare against max which means we must take
6722 * the increased cdclk requirement into account when
6723 * calculating the new cdclk.
6724 *
6725 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006726 */
6727 return ilk_pipe_pixel_rate(pipe_config) <=
6728 dev_priv->max_cdclk_freq * 95 / 100;
6729}
6730
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006731static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006732 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006733{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006734 struct drm_device *dev = crtc->base.dev;
6735 struct drm_i915_private *dev_priv = dev->dev_private;
6736
Jani Nikulad330a952014-01-21 11:24:25 +02006737 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006738 hsw_crtc_supports_ips(crtc) &&
6739 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006740}
6741
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006742static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6743{
6744 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6745
6746 /* GDG double wide on either pipe, otherwise pipe A only */
6747 return INTEL_INFO(dev_priv)->gen < 4 &&
6748 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6749}
6750
Daniel Vettera43f6e02013-06-07 23:10:32 +02006751static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006752 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006753{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006754 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006755 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006756 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006757
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006758 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006759 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006760 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006761
6762 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006763 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006764 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006765 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006766 if (intel_crtc_supports_double_wide(crtc) &&
6767 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006768 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006769 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006770 }
6771
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006772 if (adjusted_mode->crtc_clock > clock_limit) {
6773 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6774 adjusted_mode->crtc_clock, clock_limit,
6775 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006776 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006777 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006778 }
Chris Wilson89749352010-09-12 18:25:19 +01006779
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006780 /*
6781 * Pipe horizontal size must be even in:
6782 * - DVO ganged mode
6783 * - LVDS dual channel mode
6784 * - Double wide pipe
6785 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006786 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006787 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6788 pipe_config->pipe_src_w &= ~1;
6789
Damien Lespiau8693a822013-05-03 18:48:11 +01006790 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6791 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006792 */
6793 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006794 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006795 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006796
Damien Lespiauf5adf942013-06-24 18:29:34 +01006797 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006798 hsw_compute_ips_config(crtc, pipe_config);
6799
Daniel Vetter877d48d2013-04-19 11:24:43 +02006800 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006801 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006802
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006803 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006804}
6805
Ville Syrjälä1652d192015-03-31 14:12:01 +03006806static int skylake_get_display_clock_speed(struct drm_device *dev)
6807{
6808 struct drm_i915_private *dev_priv = to_i915(dev);
6809 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6810 uint32_t cdctl = I915_READ(CDCLK_CTL);
6811 uint32_t linkrate;
6812
Damien Lespiau414355a2015-06-04 18:21:31 +01006813 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006814 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006815
6816 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6817 return 540000;
6818
6819 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006820 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006821
Damien Lespiau71cd8422015-04-30 16:39:17 +01006822 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6823 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006824 /* vco 8640 */
6825 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6826 case CDCLK_FREQ_450_432:
6827 return 432000;
6828 case CDCLK_FREQ_337_308:
6829 return 308570;
6830 case CDCLK_FREQ_675_617:
6831 return 617140;
6832 default:
6833 WARN(1, "Unknown cd freq selection\n");
6834 }
6835 } else {
6836 /* vco 8100 */
6837 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6838 case CDCLK_FREQ_450_432:
6839 return 450000;
6840 case CDCLK_FREQ_337_308:
6841 return 337500;
6842 case CDCLK_FREQ_675_617:
6843 return 675000;
6844 default:
6845 WARN(1, "Unknown cd freq selection\n");
6846 }
6847 }
6848
6849 /* error case, do as if DPLL0 isn't enabled */
6850 return 24000;
6851}
6852
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006853static int broxton_get_display_clock_speed(struct drm_device *dev)
6854{
6855 struct drm_i915_private *dev_priv = to_i915(dev);
6856 uint32_t cdctl = I915_READ(CDCLK_CTL);
6857 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6858 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6859 int cdclk;
6860
6861 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6862 return 19200;
6863
6864 cdclk = 19200 * pll_ratio / 2;
6865
6866 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6867 case BXT_CDCLK_CD2X_DIV_SEL_1:
6868 return cdclk; /* 576MHz or 624MHz */
6869 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6870 return cdclk * 2 / 3; /* 384MHz */
6871 case BXT_CDCLK_CD2X_DIV_SEL_2:
6872 return cdclk / 2; /* 288MHz */
6873 case BXT_CDCLK_CD2X_DIV_SEL_4:
6874 return cdclk / 4; /* 144MHz */
6875 }
6876
6877 /* error case, do as if DE PLL isn't enabled */
6878 return 19200;
6879}
6880
Ville Syrjälä1652d192015-03-31 14:12:01 +03006881static int broadwell_get_display_clock_speed(struct drm_device *dev)
6882{
6883 struct drm_i915_private *dev_priv = dev->dev_private;
6884 uint32_t lcpll = I915_READ(LCPLL_CTL);
6885 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6886
6887 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6888 return 800000;
6889 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6890 return 450000;
6891 else if (freq == LCPLL_CLK_FREQ_450)
6892 return 450000;
6893 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6894 return 540000;
6895 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6896 return 337500;
6897 else
6898 return 675000;
6899}
6900
6901static int haswell_get_display_clock_speed(struct drm_device *dev)
6902{
6903 struct drm_i915_private *dev_priv = dev->dev_private;
6904 uint32_t lcpll = I915_READ(LCPLL_CTL);
6905 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6906
6907 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6908 return 800000;
6909 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6910 return 450000;
6911 else if (freq == LCPLL_CLK_FREQ_450)
6912 return 450000;
6913 else if (IS_HSW_ULT(dev))
6914 return 337500;
6915 else
6916 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006917}
6918
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006919static int valleyview_get_display_clock_speed(struct drm_device *dev)
6920{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006921 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6922 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006923}
6924
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006925static int ilk_get_display_clock_speed(struct drm_device *dev)
6926{
6927 return 450000;
6928}
6929
Jesse Barnese70236a2009-09-21 10:42:27 -07006930static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006931{
Jesse Barnese70236a2009-09-21 10:42:27 -07006932 return 400000;
6933}
Jesse Barnes79e53942008-11-07 14:24:08 -08006934
Jesse Barnese70236a2009-09-21 10:42:27 -07006935static int i915_get_display_clock_speed(struct drm_device *dev)
6936{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006937 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006938}
Jesse Barnes79e53942008-11-07 14:24:08 -08006939
Jesse Barnese70236a2009-09-21 10:42:27 -07006940static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6941{
6942 return 200000;
6943}
Jesse Barnes79e53942008-11-07 14:24:08 -08006944
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006945static int pnv_get_display_clock_speed(struct drm_device *dev)
6946{
6947 u16 gcfgc = 0;
6948
6949 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6950
6951 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6952 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006953 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006954 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006955 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006956 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006957 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006958 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6959 return 200000;
6960 default:
6961 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6962 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006963 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006964 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006965 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006966 }
6967}
6968
Jesse Barnese70236a2009-09-21 10:42:27 -07006969static int i915gm_get_display_clock_speed(struct drm_device *dev)
6970{
6971 u16 gcfgc = 0;
6972
6973 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6974
6975 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006976 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006977 else {
6978 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6979 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006980 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006981 default:
6982 case GC_DISPLAY_CLOCK_190_200_MHZ:
6983 return 190000;
6984 }
6985 }
6986}
Jesse Barnes79e53942008-11-07 14:24:08 -08006987
Jesse Barnese70236a2009-09-21 10:42:27 -07006988static int i865_get_display_clock_speed(struct drm_device *dev)
6989{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006990 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006991}
6992
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006993static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006994{
6995 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006996
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006997 /*
6998 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6999 * encoding is different :(
7000 * FIXME is this the right way to detect 852GM/852GMV?
7001 */
7002 if (dev->pdev->revision == 0x1)
7003 return 133333;
7004
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007005 pci_bus_read_config_word(dev->pdev->bus,
7006 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7007
Jesse Barnese70236a2009-09-21 10:42:27 -07007008 /* Assume that the hardware is in the high speed state. This
7009 * should be the default.
7010 */
7011 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7012 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007013 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07007014 case GC_CLOCK_100_200:
7015 return 200000;
7016 case GC_CLOCK_166_250:
7017 return 250000;
7018 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007019 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007020 case GC_CLOCK_133_266:
7021 case GC_CLOCK_133_266_2:
7022 case GC_CLOCK_166_266:
7023 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007024 }
7025
7026 /* Shouldn't happen */
7027 return 0;
7028}
7029
7030static int i830_get_display_clock_speed(struct drm_device *dev)
7031{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007032 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08007033}
7034
Ville Syrjälä34edce22015-05-22 11:22:33 +03007035static unsigned int intel_hpll_vco(struct drm_device *dev)
7036{
7037 struct drm_i915_private *dev_priv = dev->dev_private;
7038 static const unsigned int blb_vco[8] = {
7039 [0] = 3200000,
7040 [1] = 4000000,
7041 [2] = 5333333,
7042 [3] = 4800000,
7043 [4] = 6400000,
7044 };
7045 static const unsigned int pnv_vco[8] = {
7046 [0] = 3200000,
7047 [1] = 4000000,
7048 [2] = 5333333,
7049 [3] = 4800000,
7050 [4] = 2666667,
7051 };
7052 static const unsigned int cl_vco[8] = {
7053 [0] = 3200000,
7054 [1] = 4000000,
7055 [2] = 5333333,
7056 [3] = 6400000,
7057 [4] = 3333333,
7058 [5] = 3566667,
7059 [6] = 4266667,
7060 };
7061 static const unsigned int elk_vco[8] = {
7062 [0] = 3200000,
7063 [1] = 4000000,
7064 [2] = 5333333,
7065 [3] = 4800000,
7066 };
7067 static const unsigned int ctg_vco[8] = {
7068 [0] = 3200000,
7069 [1] = 4000000,
7070 [2] = 5333333,
7071 [3] = 6400000,
7072 [4] = 2666667,
7073 [5] = 4266667,
7074 };
7075 const unsigned int *vco_table;
7076 unsigned int vco;
7077 uint8_t tmp = 0;
7078
7079 /* FIXME other chipsets? */
7080 if (IS_GM45(dev))
7081 vco_table = ctg_vco;
7082 else if (IS_G4X(dev))
7083 vco_table = elk_vco;
7084 else if (IS_CRESTLINE(dev))
7085 vco_table = cl_vco;
7086 else if (IS_PINEVIEW(dev))
7087 vco_table = pnv_vco;
7088 else if (IS_G33(dev))
7089 vco_table = blb_vco;
7090 else
7091 return 0;
7092
7093 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7094
7095 vco = vco_table[tmp & 0x7];
7096 if (vco == 0)
7097 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7098 else
7099 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7100
7101 return vco;
7102}
7103
7104static int gm45_get_display_clock_speed(struct drm_device *dev)
7105{
7106 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7107 uint16_t tmp = 0;
7108
7109 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7110
7111 cdclk_sel = (tmp >> 12) & 0x1;
7112
7113 switch (vco) {
7114 case 2666667:
7115 case 4000000:
7116 case 5333333:
7117 return cdclk_sel ? 333333 : 222222;
7118 case 3200000:
7119 return cdclk_sel ? 320000 : 228571;
7120 default:
7121 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7122 return 222222;
7123 }
7124}
7125
7126static int i965gm_get_display_clock_speed(struct drm_device *dev)
7127{
7128 static const uint8_t div_3200[] = { 16, 10, 8 };
7129 static const uint8_t div_4000[] = { 20, 12, 10 };
7130 static const uint8_t div_5333[] = { 24, 16, 14 };
7131 const uint8_t *div_table;
7132 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7133 uint16_t tmp = 0;
7134
7135 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7136
7137 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7138
7139 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7140 goto fail;
7141
7142 switch (vco) {
7143 case 3200000:
7144 div_table = div_3200;
7145 break;
7146 case 4000000:
7147 div_table = div_4000;
7148 break;
7149 case 5333333:
7150 div_table = div_5333;
7151 break;
7152 default:
7153 goto fail;
7154 }
7155
7156 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7157
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007158fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007159 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7160 return 200000;
7161}
7162
7163static int g33_get_display_clock_speed(struct drm_device *dev)
7164{
7165 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7166 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7167 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7168 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7169 const uint8_t *div_table;
7170 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7171 uint16_t tmp = 0;
7172
7173 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7174
7175 cdclk_sel = (tmp >> 4) & 0x7;
7176
7177 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7178 goto fail;
7179
7180 switch (vco) {
7181 case 3200000:
7182 div_table = div_3200;
7183 break;
7184 case 4000000:
7185 div_table = div_4000;
7186 break;
7187 case 4800000:
7188 div_table = div_4800;
7189 break;
7190 case 5333333:
7191 div_table = div_5333;
7192 break;
7193 default:
7194 goto fail;
7195 }
7196
7197 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7198
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007199fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007200 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7201 return 190476;
7202}
7203
Zhenyu Wang2c072452009-06-05 15:38:42 +08007204static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007205intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007206{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007207 while (*num > DATA_LINK_M_N_MASK ||
7208 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007209 *num >>= 1;
7210 *den >>= 1;
7211 }
7212}
7213
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007214static void compute_m_n(unsigned int m, unsigned int n,
7215 uint32_t *ret_m, uint32_t *ret_n)
7216{
7217 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7218 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7219 intel_reduce_m_n_ratio(ret_m, ret_n);
7220}
7221
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007222void
7223intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7224 int pixel_clock, int link_clock,
7225 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007226{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007227 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007228
7229 compute_m_n(bits_per_pixel * pixel_clock,
7230 link_clock * nlanes * 8,
7231 &m_n->gmch_m, &m_n->gmch_n);
7232
7233 compute_m_n(pixel_clock, link_clock,
7234 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007235}
7236
Chris Wilsona7615032011-01-12 17:04:08 +00007237static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7238{
Jani Nikulad330a952014-01-21 11:24:25 +02007239 if (i915.panel_use_ssc >= 0)
7240 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007241 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007242 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007243}
7244
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007245static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7246 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007247{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007248 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007249 struct drm_i915_private *dev_priv = dev->dev_private;
7250 int refclk;
7251
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007252 WARN_ON(!crtc_state->base.state);
7253
Wayne Boyer666a4532015-12-09 12:29:35 -08007254 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007255 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007256 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007257 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007258 refclk = dev_priv->vbt.lvds_ssc_freq;
7259 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007260 } else if (!IS_GEN2(dev)) {
7261 refclk = 96000;
7262 } else {
7263 refclk = 48000;
7264 }
7265
7266 return refclk;
7267}
7268
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007269static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007270{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007271 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007272}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007273
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007274static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7275{
7276 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007277}
7278
Daniel Vetterf47709a2013-03-28 10:42:02 +01007279static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007280 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007281 intel_clock_t *reduced_clock)
7282{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007283 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007284 u32 fp, fp2 = 0;
7285
7286 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007287 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007288 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007289 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007290 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007291 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007292 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007293 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007294 }
7295
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007296 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007297
Daniel Vetterf47709a2013-03-28 10:42:02 +01007298 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007299 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007300 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007301 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007302 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007303 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007304 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007305 }
7306}
7307
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007308static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7309 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007310{
7311 u32 reg_val;
7312
7313 /*
7314 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7315 * and set it to a reasonable value instead.
7316 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007317 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007318 reg_val &= 0xffffff00;
7319 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007320 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007321
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007322 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007323 reg_val &= 0x8cffffff;
7324 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007325 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007326
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007327 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007328 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007329 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007330
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007331 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007332 reg_val &= 0x00ffffff;
7333 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007334 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007335}
7336
Daniel Vetterb5518422013-05-03 11:49:48 +02007337static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7338 struct intel_link_m_n *m_n)
7339{
7340 struct drm_device *dev = crtc->base.dev;
7341 struct drm_i915_private *dev_priv = dev->dev_private;
7342 int pipe = crtc->pipe;
7343
Daniel Vettere3b95f12013-05-03 11:49:49 +02007344 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7345 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7346 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7347 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007348}
7349
7350static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007351 struct intel_link_m_n *m_n,
7352 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007353{
7354 struct drm_device *dev = crtc->base.dev;
7355 struct drm_i915_private *dev_priv = dev->dev_private;
7356 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007357 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007358
7359 if (INTEL_INFO(dev)->gen >= 5) {
7360 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7361 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7362 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7363 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007364 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7365 * for gen < 8) and if DRRS is supported (to make sure the
7366 * registers are not unnecessarily accessed).
7367 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307368 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007369 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007370 I915_WRITE(PIPE_DATA_M2(transcoder),
7371 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7372 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7373 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7374 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7375 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007376 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007377 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7378 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7379 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7380 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007381 }
7382}
7383
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307384void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007385{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307386 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7387
7388 if (m_n == M1_N1) {
7389 dp_m_n = &crtc->config->dp_m_n;
7390 dp_m2_n2 = &crtc->config->dp_m2_n2;
7391 } else if (m_n == M2_N2) {
7392
7393 /*
7394 * M2_N2 registers are not supported. Hence m2_n2 divider value
7395 * needs to be programmed into M1_N1.
7396 */
7397 dp_m_n = &crtc->config->dp_m2_n2;
7398 } else {
7399 DRM_ERROR("Unsupported divider value\n");
7400 return;
7401 }
7402
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007403 if (crtc->config->has_pch_encoder)
7404 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007405 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307406 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007407}
7408
Daniel Vetter251ac862015-06-18 10:30:24 +02007409static void vlv_compute_dpll(struct intel_crtc *crtc,
7410 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007411{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007412 u32 dpll, dpll_md;
7413
7414 /*
7415 * Enable DPIO clock input. We should never disable the reference
7416 * clock for pipe B, since VGA hotplug / manual detection depends
7417 * on it.
7418 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007419 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7420 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007421 /* We should never disable this, set it here for state tracking */
7422 if (crtc->pipe == PIPE_B)
7423 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7424 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007425 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007426
Ville Syrjäläd288f652014-10-28 13:20:22 +02007427 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007428 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007429 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007430}
7431
Ville Syrjäläd288f652014-10-28 13:20:22 +02007432static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007433 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007434{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007435 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007436 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007437 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007438 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007439 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007440 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007441
Ville Syrjäläa5805162015-05-26 20:42:30 +03007442 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007443
Ville Syrjäläd288f652014-10-28 13:20:22 +02007444 bestn = pipe_config->dpll.n;
7445 bestm1 = pipe_config->dpll.m1;
7446 bestm2 = pipe_config->dpll.m2;
7447 bestp1 = pipe_config->dpll.p1;
7448 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007449
Jesse Barnes89b667f2013-04-18 14:51:36 -07007450 /* See eDP HDMI DPIO driver vbios notes doc */
7451
7452 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007453 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007454 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007455
7456 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007457 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007458
7459 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007460 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007461 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007462 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007463
7464 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007465 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007466
7467 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007468 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7469 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7470 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007471 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007472
7473 /*
7474 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7475 * but we don't support that).
7476 * Note: don't use the DAC post divider as it seems unstable.
7477 */
7478 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007479 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007480
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007481 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007482 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007483
Jesse Barnes89b667f2013-04-18 14:51:36 -07007484 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007485 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007486 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7487 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007488 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007489 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007490 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007491 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007492 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007493
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007494 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007495 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007496 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007497 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007498 0x0df40000);
7499 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007500 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007501 0x0df70000);
7502 } else { /* HDMI or VGA */
7503 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007504 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007505 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007506 0x0df70000);
7507 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007508 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007509 0x0df40000);
7510 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007511
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007512 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007513 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007514 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7515 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007516 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007517 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007518
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007519 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007520 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007521}
7522
Daniel Vetter251ac862015-06-18 10:30:24 +02007523static void chv_compute_dpll(struct intel_crtc *crtc,
7524 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007525{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007526 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7527 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007528 DPLL_VCO_ENABLE;
7529 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007530 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007531
Ville Syrjäläd288f652014-10-28 13:20:22 +02007532 pipe_config->dpll_hw_state.dpll_md =
7533 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007534}
7535
Ville Syrjäläd288f652014-10-28 13:20:22 +02007536static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007537 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007538{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007539 struct drm_device *dev = crtc->base.dev;
7540 struct drm_i915_private *dev_priv = dev->dev_private;
7541 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007542 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007543 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307544 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007545 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307546 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307547 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007548
Ville Syrjäläd288f652014-10-28 13:20:22 +02007549 bestn = pipe_config->dpll.n;
7550 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7551 bestm1 = pipe_config->dpll.m1;
7552 bestm2 = pipe_config->dpll.m2 >> 22;
7553 bestp1 = pipe_config->dpll.p1;
7554 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307555 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307556 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307557 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007558
7559 /*
7560 * Enable Refclk and SSC
7561 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007562 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007563 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007564
Ville Syrjäläa5805162015-05-26 20:42:30 +03007565 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007566
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007567 /* p1 and p2 divider */
7568 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7569 5 << DPIO_CHV_S1_DIV_SHIFT |
7570 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7571 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7572 1 << DPIO_CHV_K_DIV_SHIFT);
7573
7574 /* Feedback post-divider - m2 */
7575 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7576
7577 /* Feedback refclk divider - n and m1 */
7578 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7579 DPIO_CHV_M1_DIV_BY_2 |
7580 1 << DPIO_CHV_N_DIV_SHIFT);
7581
7582 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007583 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007584
7585 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307586 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7587 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7588 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7589 if (bestm2_frac)
7590 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7591 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007592
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307593 /* Program digital lock detect threshold */
7594 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7595 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7596 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7597 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7598 if (!bestm2_frac)
7599 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7600 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7601
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007602 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307603 if (vco == 5400000) {
7604 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7605 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7606 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7607 tribuf_calcntr = 0x9;
7608 } else if (vco <= 6200000) {
7609 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7610 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7611 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7612 tribuf_calcntr = 0x9;
7613 } else if (vco <= 6480000) {
7614 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7615 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7616 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7617 tribuf_calcntr = 0x8;
7618 } else {
7619 /* Not supported. Apply the same limits as in the max case */
7620 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7621 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7622 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7623 tribuf_calcntr = 0;
7624 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007625 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7626
Ville Syrjälä968040b2015-03-11 22:52:08 +02007627 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307628 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7629 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7630 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7631
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007632 /* AFC Recal */
7633 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7634 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7635 DPIO_AFC_RECAL);
7636
Ville Syrjäläa5805162015-05-26 20:42:30 +03007637 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007638}
7639
Ville Syrjäläd288f652014-10-28 13:20:22 +02007640/**
7641 * vlv_force_pll_on - forcibly enable just the PLL
7642 * @dev_priv: i915 private structure
7643 * @pipe: pipe PLL to enable
7644 * @dpll: PLL configuration
7645 *
7646 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7647 * in cases where we need the PLL enabled even when @pipe is not going to
7648 * be enabled.
7649 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007650int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7651 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007652{
7653 struct intel_crtc *crtc =
7654 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007655 struct intel_crtc_state *pipe_config;
7656
7657 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7658 if (!pipe_config)
7659 return -ENOMEM;
7660
7661 pipe_config->base.crtc = &crtc->base;
7662 pipe_config->pixel_multiplier = 1;
7663 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007664
7665 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007666 chv_compute_dpll(crtc, pipe_config);
7667 chv_prepare_pll(crtc, pipe_config);
7668 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007669 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007670 vlv_compute_dpll(crtc, pipe_config);
7671 vlv_prepare_pll(crtc, pipe_config);
7672 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007673 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007674
7675 kfree(pipe_config);
7676
7677 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007678}
7679
7680/**
7681 * vlv_force_pll_off - forcibly disable just the PLL
7682 * @dev_priv: i915 private structure
7683 * @pipe: pipe PLL to disable
7684 *
7685 * Disable the PLL for @pipe. To be used in cases where we need
7686 * the PLL enabled even when @pipe is not going to be enabled.
7687 */
7688void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7689{
7690 if (IS_CHERRYVIEW(dev))
7691 chv_disable_pll(to_i915(dev), pipe);
7692 else
7693 vlv_disable_pll(to_i915(dev), pipe);
7694}
7695
Daniel Vetter251ac862015-06-18 10:30:24 +02007696static void i9xx_compute_dpll(struct intel_crtc *crtc,
7697 struct intel_crtc_state *crtc_state,
7698 intel_clock_t *reduced_clock,
7699 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007700{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007701 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007702 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007703 u32 dpll;
7704 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007705 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007706
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007707 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307708
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007709 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7710 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007711
7712 dpll = DPLL_VGA_MODE_DIS;
7713
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007714 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007715 dpll |= DPLLB_MODE_LVDS;
7716 else
7717 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007718
Daniel Vetteref1b4602013-06-01 17:17:04 +02007719 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007720 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007721 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007722 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007723
7724 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007725 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007726
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007727 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007728 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007729
7730 /* compute bitmask from p1 value */
7731 if (IS_PINEVIEW(dev))
7732 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7733 else {
7734 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7735 if (IS_G4X(dev) && reduced_clock)
7736 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7737 }
7738 switch (clock->p2) {
7739 case 5:
7740 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7741 break;
7742 case 7:
7743 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7744 break;
7745 case 10:
7746 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7747 break;
7748 case 14:
7749 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7750 break;
7751 }
7752 if (INTEL_INFO(dev)->gen >= 4)
7753 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7754
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007755 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007756 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007757 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007758 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7759 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7760 else
7761 dpll |= PLL_REF_INPUT_DREFCLK;
7762
7763 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007764 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007765
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007766 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007767 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007768 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007769 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007770 }
7771}
7772
Daniel Vetter251ac862015-06-18 10:30:24 +02007773static void i8xx_compute_dpll(struct intel_crtc *crtc,
7774 struct intel_crtc_state *crtc_state,
7775 intel_clock_t *reduced_clock,
7776 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007777{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007778 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007779 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007780 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007781 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007782
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007783 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307784
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007785 dpll = DPLL_VGA_MODE_DIS;
7786
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007787 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007788 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7789 } else {
7790 if (clock->p1 == 2)
7791 dpll |= PLL_P1_DIVIDE_BY_TWO;
7792 else
7793 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7794 if (clock->p2 == 4)
7795 dpll |= PLL_P2_DIVIDE_BY_4;
7796 }
7797
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007798 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007799 dpll |= DPLL_DVO_2X_MODE;
7800
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007801 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007802 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7803 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7804 else
7805 dpll |= PLL_REF_INPUT_DREFCLK;
7806
7807 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007808 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007809}
7810
Daniel Vetter8a654f32013-06-01 17:16:22 +02007811static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007812{
7813 struct drm_device *dev = intel_crtc->base.dev;
7814 struct drm_i915_private *dev_priv = dev->dev_private;
7815 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007816 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007817 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007818 uint32_t crtc_vtotal, crtc_vblank_end;
7819 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007820
7821 /* We need to be careful not to changed the adjusted mode, for otherwise
7822 * the hw state checker will get angry at the mismatch. */
7823 crtc_vtotal = adjusted_mode->crtc_vtotal;
7824 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007825
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007826 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007827 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007828 crtc_vtotal -= 1;
7829 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007830
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007831 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007832 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7833 else
7834 vsyncshift = adjusted_mode->crtc_hsync_start -
7835 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007836 if (vsyncshift < 0)
7837 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007838 }
7839
7840 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007841 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007842
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007843 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007844 (adjusted_mode->crtc_hdisplay - 1) |
7845 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007846 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007847 (adjusted_mode->crtc_hblank_start - 1) |
7848 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007849 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007850 (adjusted_mode->crtc_hsync_start - 1) |
7851 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7852
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007853 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007854 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007855 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007856 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007857 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007858 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007859 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007860 (adjusted_mode->crtc_vsync_start - 1) |
7861 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7862
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007863 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7864 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7865 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7866 * bits. */
7867 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7868 (pipe == PIPE_B || pipe == PIPE_C))
7869 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7870
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007871 /* pipesrc controls the size that is scaled from, which should
7872 * always be the user's requested size.
7873 */
7874 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007875 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7876 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007877}
7878
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007879static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007880 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007881{
7882 struct drm_device *dev = crtc->base.dev;
7883 struct drm_i915_private *dev_priv = dev->dev_private;
7884 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7885 uint32_t tmp;
7886
7887 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007888 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7889 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007890 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007891 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7892 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007893 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007894 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7895 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007896
7897 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007898 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7899 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007900 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007901 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7902 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007903 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007904 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7905 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007906
7907 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007908 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7909 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7910 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007911 }
7912
7913 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007914 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7915 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7916
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007917 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7918 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007919}
7920
Daniel Vetterf6a83282014-02-11 15:28:57 -08007921void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007922 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007923{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007924 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7925 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7926 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7927 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007928
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007929 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7930 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7931 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7932 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007933
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007934 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007935 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007936
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007937 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7938 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007939
7940 mode->hsync = drm_mode_hsync(mode);
7941 mode->vrefresh = drm_mode_vrefresh(mode);
7942 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007943}
7944
Daniel Vetter84b046f2013-02-19 18:48:54 +01007945static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7946{
7947 struct drm_device *dev = intel_crtc->base.dev;
7948 struct drm_i915_private *dev_priv = dev->dev_private;
7949 uint32_t pipeconf;
7950
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007951 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007952
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007953 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7954 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7955 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007956
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007957 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007958 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007959
Daniel Vetterff9ce462013-04-24 14:57:17 +02007960 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007961 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007962 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007963 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007964 pipeconf |= PIPECONF_DITHER_EN |
7965 PIPECONF_DITHER_TYPE_SP;
7966
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007967 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007968 case 18:
7969 pipeconf |= PIPECONF_6BPC;
7970 break;
7971 case 24:
7972 pipeconf |= PIPECONF_8BPC;
7973 break;
7974 case 30:
7975 pipeconf |= PIPECONF_10BPC;
7976 break;
7977 default:
7978 /* Case prevented by intel_choose_pipe_bpp_dither. */
7979 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007980 }
7981 }
7982
7983 if (HAS_PIPE_CXSR(dev)) {
7984 if (intel_crtc->lowfreq_avail) {
7985 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7986 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7987 } else {
7988 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007989 }
7990 }
7991
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007992 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007993 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007994 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007995 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7996 else
7997 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7998 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007999 pipeconf |= PIPECONF_PROGRESSIVE;
8000
Wayne Boyer666a4532015-12-09 12:29:35 -08008001 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8002 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02008003 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03008004
Daniel Vetter84b046f2013-02-19 18:48:54 +01008005 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8006 POSTING_READ(PIPECONF(intel_crtc->pipe));
8007}
8008
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008009static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8010 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008011{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008012 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008013 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07008014 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008015 intel_clock_t clock;
8016 bool ok;
Ma Lingd4906092009-03-18 20:13:27 +08008017 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008018 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008019 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008020 struct drm_connector_state *connector_state;
8021 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008022
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008023 memset(&crtc_state->dpll_hw_state, 0,
8024 sizeof(crtc_state->dpll_hw_state));
8025
Jani Nikulaa65347b2015-11-27 12:21:46 +02008026 if (crtc_state->has_dsi_encoder)
Daniel Vetter5b18e572014-04-24 23:55:06 +02008027 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008028
Jani Nikulaa65347b2015-11-27 12:21:46 +02008029 for_each_connector_in_state(state, connector, connector_state, i) {
8030 if (connector_state->crtc == &crtc->base)
8031 num_connectors++;
8032 }
8033
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008034 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008035 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03008036
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008037 /*
8038 * Returns a set of divisors for the desired target clock with
8039 * the given refclk, or FALSE. The returned values represent
8040 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
8041 * 2) / p1 / p2.
8042 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008043 limit = intel_limit(crtc_state, refclk);
8044 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008045 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008046 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03008047 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008048 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8049 return -EINVAL;
8050 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008051
Jani Nikulaf2335332013-09-13 11:03:09 +03008052 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008053 crtc_state->dpll.n = clock.n;
8054 crtc_state->dpll.m1 = clock.m1;
8055 crtc_state->dpll.m2 = clock.m2;
8056 crtc_state->dpll.p1 = clock.p1;
8057 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008058 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008059
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008060 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008061 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02008062 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008063 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02008064 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008065 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02008066 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008067 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008068 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02008069 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008070 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008071
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008072 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008073}
8074
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008075static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008076 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008077{
8078 struct drm_device *dev = crtc->base.dev;
8079 struct drm_i915_private *dev_priv = dev->dev_private;
8080 uint32_t tmp;
8081
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008082 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8083 return;
8084
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008085 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008086 if (!(tmp & PFIT_ENABLE))
8087 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008088
Daniel Vetter06922822013-07-11 13:35:40 +02008089 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008090 if (INTEL_INFO(dev)->gen < 4) {
8091 if (crtc->pipe != PIPE_B)
8092 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008093 } else {
8094 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8095 return;
8096 }
8097
Daniel Vetter06922822013-07-11 13:35:40 +02008098 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008099 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
8100 if (INTEL_INFO(dev)->gen < 5)
8101 pipe_config->gmch_pfit.lvds_border_bits =
8102 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
8103}
8104
Jesse Barnesacbec812013-09-20 11:29:32 -07008105static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008106 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008107{
8108 struct drm_device *dev = crtc->base.dev;
8109 struct drm_i915_private *dev_priv = dev->dev_private;
8110 int pipe = pipe_config->cpu_transcoder;
8111 intel_clock_t clock;
8112 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008113 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008114
Shobhit Kumarf573de52014-07-30 20:32:37 +05308115 /* In case of MIPI DPLL will not even be used */
8116 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8117 return;
8118
Ville Syrjäläa5805162015-05-26 20:42:30 +03008119 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008120 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008121 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008122
8123 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8124 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8125 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8126 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8127 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8128
Imre Deakdccbea32015-06-22 23:35:51 +03008129 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008130}
8131
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008132static void
8133i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8134 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008135{
8136 struct drm_device *dev = crtc->base.dev;
8137 struct drm_i915_private *dev_priv = dev->dev_private;
8138 u32 val, base, offset;
8139 int pipe = crtc->pipe, plane = crtc->plane;
8140 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008141 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008142 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008143 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008144
Damien Lespiau42a7b082015-02-05 19:35:13 +00008145 val = I915_READ(DSPCNTR(plane));
8146 if (!(val & DISPLAY_PLANE_ENABLE))
8147 return;
8148
Damien Lespiaud9806c92015-01-21 14:07:19 +00008149 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008150 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008151 DRM_DEBUG_KMS("failed to alloc fb\n");
8152 return;
8153 }
8154
Damien Lespiau1b842c82015-01-21 13:50:54 +00008155 fb = &intel_fb->base;
8156
Daniel Vetter18c52472015-02-10 17:16:09 +00008157 if (INTEL_INFO(dev)->gen >= 4) {
8158 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008159 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008160 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8161 }
8162 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008163
8164 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008165 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008166 fb->pixel_format = fourcc;
8167 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008168
8169 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008170 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008171 offset = I915_READ(DSPTILEOFF(plane));
8172 else
8173 offset = I915_READ(DSPLINOFF(plane));
8174 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8175 } else {
8176 base = I915_READ(DSPADDR(plane));
8177 }
8178 plane_config->base = base;
8179
8180 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008181 fb->width = ((val >> 16) & 0xfff) + 1;
8182 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008183
8184 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008185 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008186
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008187 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008188 fb->pixel_format,
8189 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008190
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008191 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008192
Damien Lespiau2844a922015-01-20 12:51:48 +00008193 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8194 pipe_name(pipe), plane, fb->width, fb->height,
8195 fb->bits_per_pixel, base, fb->pitches[0],
8196 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008197
Damien Lespiau2d140302015-02-05 17:22:18 +00008198 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008199}
8200
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008201static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008202 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008203{
8204 struct drm_device *dev = crtc->base.dev;
8205 struct drm_i915_private *dev_priv = dev->dev_private;
8206 int pipe = pipe_config->cpu_transcoder;
8207 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8208 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008209 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008210 int refclk = 100000;
8211
Ville Syrjäläa5805162015-05-26 20:42:30 +03008212 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008213 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8214 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8215 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8216 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008217 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008218 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008219
8220 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008221 clock.m2 = (pll_dw0 & 0xff) << 22;
8222 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8223 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008224 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8225 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8226 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8227
Imre Deakdccbea32015-06-22 23:35:51 +03008228 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008229}
8230
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008231static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008232 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008233{
8234 struct drm_device *dev = crtc->base.dev;
8235 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008236 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008237 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008238 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008239
Imre Deak17290502016-02-12 18:55:11 +02008240 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8241 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008242 return false;
8243
Daniel Vettere143a212013-07-04 12:01:15 +02008244 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008245 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008246
Imre Deak17290502016-02-12 18:55:11 +02008247 ret = false;
8248
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008249 tmp = I915_READ(PIPECONF(crtc->pipe));
8250 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008251 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008252
Wayne Boyer666a4532015-12-09 12:29:35 -08008253 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008254 switch (tmp & PIPECONF_BPC_MASK) {
8255 case PIPECONF_6BPC:
8256 pipe_config->pipe_bpp = 18;
8257 break;
8258 case PIPECONF_8BPC:
8259 pipe_config->pipe_bpp = 24;
8260 break;
8261 case PIPECONF_10BPC:
8262 pipe_config->pipe_bpp = 30;
8263 break;
8264 default:
8265 break;
8266 }
8267 }
8268
Wayne Boyer666a4532015-12-09 12:29:35 -08008269 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8270 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008271 pipe_config->limited_color_range = true;
8272
Ville Syrjälä282740f2013-09-04 18:30:03 +03008273 if (INTEL_INFO(dev)->gen < 4)
8274 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8275
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008276 intel_get_pipe_timings(crtc, pipe_config);
8277
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008278 i9xx_get_pfit_config(crtc, pipe_config);
8279
Daniel Vetter6c49f242013-06-06 12:45:25 +02008280 if (INTEL_INFO(dev)->gen >= 4) {
8281 tmp = I915_READ(DPLL_MD(crtc->pipe));
8282 pipe_config->pixel_multiplier =
8283 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8284 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008285 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008286 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8287 tmp = I915_READ(DPLL(crtc->pipe));
8288 pipe_config->pixel_multiplier =
8289 ((tmp & SDVO_MULTIPLIER_MASK)
8290 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8291 } else {
8292 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8293 * port and will be fixed up in the encoder->get_config
8294 * function. */
8295 pipe_config->pixel_multiplier = 1;
8296 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008297 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008298 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008299 /*
8300 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8301 * on 830. Filter it out here so that we don't
8302 * report errors due to that.
8303 */
8304 if (IS_I830(dev))
8305 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8306
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008307 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8308 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008309 } else {
8310 /* Mask out read-only status bits. */
8311 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8312 DPLL_PORTC_READY_MASK |
8313 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008314 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008315
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008316 if (IS_CHERRYVIEW(dev))
8317 chv_crtc_clock_get(crtc, pipe_config);
8318 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008319 vlv_crtc_clock_get(crtc, pipe_config);
8320 else
8321 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008322
Ville Syrjälä0f646142015-08-26 19:39:18 +03008323 /*
8324 * Normally the dotclock is filled in by the encoder .get_config()
8325 * but in case the pipe is enabled w/o any ports we need a sane
8326 * default.
8327 */
8328 pipe_config->base.adjusted_mode.crtc_clock =
8329 pipe_config->port_clock / pipe_config->pixel_multiplier;
8330
Imre Deak17290502016-02-12 18:55:11 +02008331 ret = true;
8332
8333out:
8334 intel_display_power_put(dev_priv, power_domain);
8335
8336 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008337}
8338
Paulo Zanonidde86e22012-12-01 12:04:25 -02008339static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008340{
8341 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008342 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008343 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008344 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008345 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008346 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008347 bool has_ck505 = false;
8348 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008349
8350 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008351 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008352 switch (encoder->type) {
8353 case INTEL_OUTPUT_LVDS:
8354 has_panel = true;
8355 has_lvds = true;
8356 break;
8357 case INTEL_OUTPUT_EDP:
8358 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008359 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008360 has_cpu_edp = true;
8361 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008362 default:
8363 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008364 }
8365 }
8366
Keith Packard99eb6a02011-09-26 14:29:12 -07008367 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008368 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008369 can_ssc = has_ck505;
8370 } else {
8371 has_ck505 = false;
8372 can_ssc = true;
8373 }
8374
Imre Deak2de69052013-05-08 13:14:04 +03008375 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8376 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008377
8378 /* Ironlake: try to setup display ref clock before DPLL
8379 * enabling. This is only under driver's control after
8380 * PCH B stepping, previous chipset stepping should be
8381 * ignoring this setting.
8382 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008383 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008384
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008385 /* As we must carefully and slowly disable/enable each source in turn,
8386 * compute the final state we want first and check if we need to
8387 * make any changes at all.
8388 */
8389 final = val;
8390 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008391 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008392 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008393 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008394 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8395
8396 final &= ~DREF_SSC_SOURCE_MASK;
8397 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8398 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008399
Keith Packard199e5d72011-09-22 12:01:57 -07008400 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008401 final |= DREF_SSC_SOURCE_ENABLE;
8402
8403 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8404 final |= DREF_SSC1_ENABLE;
8405
8406 if (has_cpu_edp) {
8407 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8408 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8409 else
8410 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8411 } else
8412 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8413 } else {
8414 final |= DREF_SSC_SOURCE_DISABLE;
8415 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8416 }
8417
8418 if (final == val)
8419 return;
8420
8421 /* Always enable nonspread source */
8422 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8423
8424 if (has_ck505)
8425 val |= DREF_NONSPREAD_CK505_ENABLE;
8426 else
8427 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8428
8429 if (has_panel) {
8430 val &= ~DREF_SSC_SOURCE_MASK;
8431 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008432
Keith Packard199e5d72011-09-22 12:01:57 -07008433 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008434 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008435 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008436 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008437 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008438 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008439
8440 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008441 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008442 POSTING_READ(PCH_DREF_CONTROL);
8443 udelay(200);
8444
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008445 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008446
8447 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008448 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008449 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008450 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008451 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008452 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008453 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008454 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008455 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008456
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008457 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008458 POSTING_READ(PCH_DREF_CONTROL);
8459 udelay(200);
8460 } else {
8461 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8462
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008463 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008464
8465 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008466 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008467
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008468 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008469 POSTING_READ(PCH_DREF_CONTROL);
8470 udelay(200);
8471
8472 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008473 val &= ~DREF_SSC_SOURCE_MASK;
8474 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008475
8476 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008477 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008478
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008479 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008480 POSTING_READ(PCH_DREF_CONTROL);
8481 udelay(200);
8482 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008483
8484 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008485}
8486
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008487static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008488{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008489 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008490
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008491 tmp = I915_READ(SOUTH_CHICKEN2);
8492 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8493 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008494
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008495 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8496 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8497 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008498
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008499 tmp = I915_READ(SOUTH_CHICKEN2);
8500 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8501 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008502
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008503 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8504 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8505 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008506}
8507
8508/* WaMPhyProgramming:hsw */
8509static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8510{
8511 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008512
8513 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8514 tmp &= ~(0xFF << 24);
8515 tmp |= (0x12 << 24);
8516 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8517
Paulo Zanonidde86e22012-12-01 12:04:25 -02008518 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8519 tmp |= (1 << 11);
8520 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8521
8522 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8523 tmp |= (1 << 11);
8524 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8525
Paulo Zanonidde86e22012-12-01 12:04:25 -02008526 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8527 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8528 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8529
8530 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8531 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8532 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8533
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008534 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8535 tmp &= ~(7 << 13);
8536 tmp |= (5 << 13);
8537 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008538
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008539 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8540 tmp &= ~(7 << 13);
8541 tmp |= (5 << 13);
8542 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008543
8544 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8545 tmp &= ~0xFF;
8546 tmp |= 0x1C;
8547 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8548
8549 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8550 tmp &= ~0xFF;
8551 tmp |= 0x1C;
8552 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8553
8554 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8555 tmp &= ~(0xFF << 16);
8556 tmp |= (0x1C << 16);
8557 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8558
8559 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8560 tmp &= ~(0xFF << 16);
8561 tmp |= (0x1C << 16);
8562 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8563
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008564 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8565 tmp |= (1 << 27);
8566 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008567
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008568 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8569 tmp |= (1 << 27);
8570 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008571
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008572 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8573 tmp &= ~(0xF << 28);
8574 tmp |= (4 << 28);
8575 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008576
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008577 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8578 tmp &= ~(0xF << 28);
8579 tmp |= (4 << 28);
8580 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008581}
8582
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008583/* Implements 3 different sequences from BSpec chapter "Display iCLK
8584 * Programming" based on the parameters passed:
8585 * - Sequence to enable CLKOUT_DP
8586 * - Sequence to enable CLKOUT_DP without spread
8587 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8588 */
8589static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8590 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008591{
8592 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008593 uint32_t reg, tmp;
8594
8595 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8596 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008597 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008598 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008599
Ville Syrjäläa5805162015-05-26 20:42:30 +03008600 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008601
8602 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8603 tmp &= ~SBI_SSCCTL_DISABLE;
8604 tmp |= SBI_SSCCTL_PATHALT;
8605 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8606
8607 udelay(24);
8608
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008609 if (with_spread) {
8610 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8611 tmp &= ~SBI_SSCCTL_PATHALT;
8612 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008613
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008614 if (with_fdi) {
8615 lpt_reset_fdi_mphy(dev_priv);
8616 lpt_program_fdi_mphy(dev_priv);
8617 }
8618 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008619
Ville Syrjäläc2699522015-08-27 23:55:59 +03008620 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008621 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8622 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8623 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008624
Ville Syrjäläa5805162015-05-26 20:42:30 +03008625 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008626}
8627
Paulo Zanoni47701c32013-07-23 11:19:25 -03008628/* Sequence to disable CLKOUT_DP */
8629static void lpt_disable_clkout_dp(struct drm_device *dev)
8630{
8631 struct drm_i915_private *dev_priv = dev->dev_private;
8632 uint32_t reg, tmp;
8633
Ville Syrjäläa5805162015-05-26 20:42:30 +03008634 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008635
Ville Syrjäläc2699522015-08-27 23:55:59 +03008636 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008637 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8638 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8639 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8640
8641 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8642 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8643 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8644 tmp |= SBI_SSCCTL_PATHALT;
8645 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8646 udelay(32);
8647 }
8648 tmp |= SBI_SSCCTL_DISABLE;
8649 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8650 }
8651
Ville Syrjäläa5805162015-05-26 20:42:30 +03008652 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008653}
8654
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008655#define BEND_IDX(steps) ((50 + (steps)) / 5)
8656
8657static const uint16_t sscdivintphase[] = {
8658 [BEND_IDX( 50)] = 0x3B23,
8659 [BEND_IDX( 45)] = 0x3B23,
8660 [BEND_IDX( 40)] = 0x3C23,
8661 [BEND_IDX( 35)] = 0x3C23,
8662 [BEND_IDX( 30)] = 0x3D23,
8663 [BEND_IDX( 25)] = 0x3D23,
8664 [BEND_IDX( 20)] = 0x3E23,
8665 [BEND_IDX( 15)] = 0x3E23,
8666 [BEND_IDX( 10)] = 0x3F23,
8667 [BEND_IDX( 5)] = 0x3F23,
8668 [BEND_IDX( 0)] = 0x0025,
8669 [BEND_IDX( -5)] = 0x0025,
8670 [BEND_IDX(-10)] = 0x0125,
8671 [BEND_IDX(-15)] = 0x0125,
8672 [BEND_IDX(-20)] = 0x0225,
8673 [BEND_IDX(-25)] = 0x0225,
8674 [BEND_IDX(-30)] = 0x0325,
8675 [BEND_IDX(-35)] = 0x0325,
8676 [BEND_IDX(-40)] = 0x0425,
8677 [BEND_IDX(-45)] = 0x0425,
8678 [BEND_IDX(-50)] = 0x0525,
8679};
8680
8681/*
8682 * Bend CLKOUT_DP
8683 * steps -50 to 50 inclusive, in steps of 5
8684 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8685 * change in clock period = -(steps / 10) * 5.787 ps
8686 */
8687static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8688{
8689 uint32_t tmp;
8690 int idx = BEND_IDX(steps);
8691
8692 if (WARN_ON(steps % 5 != 0))
8693 return;
8694
8695 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8696 return;
8697
8698 mutex_lock(&dev_priv->sb_lock);
8699
8700 if (steps % 10 != 0)
8701 tmp = 0xAAAAAAAB;
8702 else
8703 tmp = 0x00000000;
8704 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8705
8706 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8707 tmp &= 0xffff0000;
8708 tmp |= sscdivintphase[idx];
8709 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8710
8711 mutex_unlock(&dev_priv->sb_lock);
8712}
8713
8714#undef BEND_IDX
8715
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008716static void lpt_init_pch_refclk(struct drm_device *dev)
8717{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008718 struct intel_encoder *encoder;
8719 bool has_vga = false;
8720
Damien Lespiaub2784e12014-08-05 11:29:37 +01008721 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008722 switch (encoder->type) {
8723 case INTEL_OUTPUT_ANALOG:
8724 has_vga = true;
8725 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008726 default:
8727 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008728 }
8729 }
8730
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008731 if (has_vga) {
8732 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008733 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008734 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008735 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008736 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008737}
8738
Paulo Zanonidde86e22012-12-01 12:04:25 -02008739/*
8740 * Initialize reference clocks when the driver loads
8741 */
8742void intel_init_pch_refclk(struct drm_device *dev)
8743{
8744 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8745 ironlake_init_pch_refclk(dev);
8746 else if (HAS_PCH_LPT(dev))
8747 lpt_init_pch_refclk(dev);
8748}
8749
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008750static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008751{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008752 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008753 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008754 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008755 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008756 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008757 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008758 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008759 bool is_lvds = false;
8760
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008761 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008762 if (connector_state->crtc != crtc_state->base.crtc)
8763 continue;
8764
8765 encoder = to_intel_encoder(connector_state->best_encoder);
8766
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008767 switch (encoder->type) {
8768 case INTEL_OUTPUT_LVDS:
8769 is_lvds = true;
8770 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008771 default:
8772 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008773 }
8774 num_connectors++;
8775 }
8776
8777 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008778 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008779 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008780 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008781 }
8782
8783 return 120000;
8784}
8785
Daniel Vetter6ff93602013-04-19 11:24:36 +02008786static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008787{
8788 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8789 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8790 int pipe = intel_crtc->pipe;
8791 uint32_t val;
8792
Daniel Vetter78114072013-06-13 00:54:57 +02008793 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008794
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008795 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008796 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008797 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008798 break;
8799 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008800 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008801 break;
8802 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008803 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008804 break;
8805 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008806 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008807 break;
8808 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008809 /* Case prevented by intel_choose_pipe_bpp_dither. */
8810 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008811 }
8812
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008813 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008814 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8815
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008816 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008817 val |= PIPECONF_INTERLACED_ILK;
8818 else
8819 val |= PIPECONF_PROGRESSIVE;
8820
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008821 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008822 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008823
Paulo Zanonic8203562012-09-12 10:06:29 -03008824 I915_WRITE(PIPECONF(pipe), val);
8825 POSTING_READ(PIPECONF(pipe));
8826}
8827
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008828/*
8829 * Set up the pipe CSC unit.
8830 *
8831 * Currently only full range RGB to limited range RGB conversion
8832 * is supported, but eventually this should handle various
8833 * RGB<->YCbCr scenarios as well.
8834 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008835static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008836{
8837 struct drm_device *dev = crtc->dev;
8838 struct drm_i915_private *dev_priv = dev->dev_private;
8839 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8840 int pipe = intel_crtc->pipe;
8841 uint16_t coeff = 0x7800; /* 1.0 */
8842
8843 /*
8844 * TODO: Check what kind of values actually come out of the pipe
8845 * with these coeff/postoff values and adjust to get the best
8846 * accuracy. Perhaps we even need to take the bpc value into
8847 * consideration.
8848 */
8849
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008850 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008851 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8852
8853 /*
8854 * GY/GU and RY/RU should be the other way around according
8855 * to BSpec, but reality doesn't agree. Just set them up in
8856 * a way that results in the correct picture.
8857 */
8858 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8859 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8860
8861 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8862 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8863
8864 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8865 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8866
8867 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8868 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8869 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8870
8871 if (INTEL_INFO(dev)->gen > 6) {
8872 uint16_t postoff = 0;
8873
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008874 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008875 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008876
8877 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8878 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8879 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8880
8881 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8882 } else {
8883 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8884
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008885 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008886 mode |= CSC_BLACK_SCREEN_OFFSET;
8887
8888 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8889 }
8890}
8891
Daniel Vetter6ff93602013-04-19 11:24:36 +02008892static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008893{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008894 struct drm_device *dev = crtc->dev;
8895 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008896 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008897 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008898 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008899 uint32_t val;
8900
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008901 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008902
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008903 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008904 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8905
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008906 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008907 val |= PIPECONF_INTERLACED_ILK;
8908 else
8909 val |= PIPECONF_PROGRESSIVE;
8910
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008911 I915_WRITE(PIPECONF(cpu_transcoder), val);
8912 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008913
8914 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8915 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008916
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308917 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008918 val = 0;
8919
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008920 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008921 case 18:
8922 val |= PIPEMISC_DITHER_6_BPC;
8923 break;
8924 case 24:
8925 val |= PIPEMISC_DITHER_8_BPC;
8926 break;
8927 case 30:
8928 val |= PIPEMISC_DITHER_10_BPC;
8929 break;
8930 case 36:
8931 val |= PIPEMISC_DITHER_12_BPC;
8932 break;
8933 default:
8934 /* Case prevented by pipe_config_set_bpp. */
8935 BUG();
8936 }
8937
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008938 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008939 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8940
8941 I915_WRITE(PIPEMISC(pipe), val);
8942 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008943}
8944
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008945static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008946 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008947 intel_clock_t *clock,
8948 bool *has_reduced_clock,
8949 intel_clock_t *reduced_clock)
8950{
8951 struct drm_device *dev = crtc->dev;
8952 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008953 int refclk;
8954 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008955 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008956
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008957 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008958
8959 /*
8960 * Returns a set of divisors for the desired target clock with the given
8961 * refclk, or FALSE. The returned values represent the clock equation:
8962 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8963 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008964 limit = intel_limit(crtc_state, refclk);
8965 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008966 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008967 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008968 if (!ret)
8969 return false;
8970
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008971 return true;
8972}
8973
Paulo Zanonid4b19312012-11-29 11:29:32 -02008974int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8975{
8976 /*
8977 * Account for spread spectrum to avoid
8978 * oversubscribing the link. Max center spread
8979 * is 2.5%; use 5% for safety's sake.
8980 */
8981 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008982 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008983}
8984
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008985static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008986{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008987 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008988}
8989
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008990static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008991 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008992 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008993 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008994{
8995 struct drm_crtc *crtc = &intel_crtc->base;
8996 struct drm_device *dev = crtc->dev;
8997 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008998 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008999 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02009000 struct drm_connector_state *connector_state;
9001 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009002 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02009003 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02009004 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009005
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03009006 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02009007 if (connector_state->crtc != crtc_state->base.crtc)
9008 continue;
9009
9010 encoder = to_intel_encoder(connector_state->best_encoder);
9011
9012 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009013 case INTEL_OUTPUT_LVDS:
9014 is_lvds = true;
9015 break;
9016 case INTEL_OUTPUT_SDVO:
9017 case INTEL_OUTPUT_HDMI:
9018 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009019 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02009020 default:
9021 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009022 }
9023
9024 num_connectors++;
9025 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009026
Chris Wilsonc1858122010-12-03 21:35:48 +00009027 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07009028 factor = 21;
9029 if (is_lvds) {
9030 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009031 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02009032 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07009033 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009034 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07009035 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00009036
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009037 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02009038 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00009039
Daniel Vetter9a7c7892013-04-04 22:20:34 +02009040 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
9041 *fp2 |= FP_CB_TUNE;
9042
Chris Wilson5eddb702010-09-11 13:48:45 +01009043 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08009044
Eric Anholta07d6782011-03-30 13:01:08 -07009045 if (is_lvds)
9046 dpll |= DPLLB_MODE_LVDS;
9047 else
9048 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009049
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009050 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02009051 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009052
9053 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02009054 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009055 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02009056 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08009057
Eric Anholta07d6782011-03-30 13:01:08 -07009058 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009059 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009060 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009061 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009062
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009063 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07009064 case 5:
9065 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9066 break;
9067 case 7:
9068 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9069 break;
9070 case 10:
9071 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9072 break;
9073 case 14:
9074 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9075 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009076 }
9077
Daniel Vetterb4c09f32013-04-30 14:01:42 +02009078 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05009079 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08009080 else
9081 dpll |= PLL_REF_INPUT_DREFCLK;
9082
Daniel Vetter959e16d2013-06-05 13:34:21 +02009083 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009084}
9085
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009086static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9087 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08009088{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009089 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009090 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009091 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03009092 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01009093 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009094 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009095
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03009096 memset(&crtc_state->dpll_hw_state, 0,
9097 sizeof(crtc_state->dpll_hw_state));
9098
Ville Syrjälä7905df22015-11-25 16:35:30 +02009099 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08009100
Paulo Zanoni5dc52982012-10-05 12:05:56 -03009101 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
9102 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
9103
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009104 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03009105 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009106 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009107 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9108 return -EINVAL;
9109 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01009110 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009111 if (!crtc_state->clock_set) {
9112 crtc_state->dpll.n = clock.n;
9113 crtc_state->dpll.m1 = clock.m1;
9114 crtc_state->dpll.m2 = clock.m2;
9115 crtc_state->dpll.p1 = clock.p1;
9116 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009117 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009118
Paulo Zanoni5dc52982012-10-05 12:05:56 -03009119 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009120 if (crtc_state->has_pch_encoder) {
9121 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009122 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009123 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009124
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009125 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009126 &fp, &reduced_clock,
9127 has_reduced_clock ? &fp2 : NULL);
9128
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009129 crtc_state->dpll_hw_state.dpll = dpll;
9130 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009131 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009132 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009133 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009134 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009135
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009136 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009137 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03009138 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009139 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07009140 return -EINVAL;
9141 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009142 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009143
Rodrigo Viviab585de2015-03-24 12:40:09 -07009144 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009145 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02009146 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009147 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009148
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009149 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009150}
9151
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009152static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9153 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009154{
9155 struct drm_device *dev = crtc->base.dev;
9156 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009157 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009158
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009159 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9160 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9161 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9162 & ~TU_SIZE_MASK;
9163 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9164 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9165 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9166}
9167
9168static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9169 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009170 struct intel_link_m_n *m_n,
9171 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009172{
9173 struct drm_device *dev = crtc->base.dev;
9174 struct drm_i915_private *dev_priv = dev->dev_private;
9175 enum pipe pipe = crtc->pipe;
9176
9177 if (INTEL_INFO(dev)->gen >= 5) {
9178 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9179 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9180 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9181 & ~TU_SIZE_MASK;
9182 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9183 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9184 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009185 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9186 * gen < 8) and if DRRS is supported (to make sure the
9187 * registers are not unnecessarily read).
9188 */
9189 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009190 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009191 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9192 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9193 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9194 & ~TU_SIZE_MASK;
9195 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9196 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9197 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9198 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009199 } else {
9200 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9201 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9202 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9203 & ~TU_SIZE_MASK;
9204 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9205 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9206 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9207 }
9208}
9209
9210void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009211 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009212{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009213 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009214 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9215 else
9216 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009217 &pipe_config->dp_m_n,
9218 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009219}
9220
Daniel Vetter72419202013-04-04 13:28:53 +02009221static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009222 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009223{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009224 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009225 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009226}
9227
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009228static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009229 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009230{
9231 struct drm_device *dev = crtc->base.dev;
9232 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009233 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9234 uint32_t ps_ctrl = 0;
9235 int id = -1;
9236 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009237
Chandra Kondurua1b22782015-04-07 15:28:45 -07009238 /* find scaler attached to this pipe */
9239 for (i = 0; i < crtc->num_scalers; i++) {
9240 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9241 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9242 id = i;
9243 pipe_config->pch_pfit.enabled = true;
9244 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9245 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9246 break;
9247 }
9248 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009249
Chandra Kondurua1b22782015-04-07 15:28:45 -07009250 scaler_state->scaler_id = id;
9251 if (id >= 0) {
9252 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9253 } else {
9254 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009255 }
9256}
9257
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009258static void
9259skylake_get_initial_plane_config(struct intel_crtc *crtc,
9260 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009261{
9262 struct drm_device *dev = crtc->base.dev;
9263 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009264 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009265 int pipe = crtc->pipe;
9266 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009267 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009268 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009269 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009270
Damien Lespiaud9806c92015-01-21 14:07:19 +00009271 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009272 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009273 DRM_DEBUG_KMS("failed to alloc fb\n");
9274 return;
9275 }
9276
Damien Lespiau1b842c82015-01-21 13:50:54 +00009277 fb = &intel_fb->base;
9278
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009279 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009280 if (!(val & PLANE_CTL_ENABLE))
9281 goto error;
9282
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009283 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9284 fourcc = skl_format_to_fourcc(pixel_format,
9285 val & PLANE_CTL_ORDER_RGBX,
9286 val & PLANE_CTL_ALPHA_MASK);
9287 fb->pixel_format = fourcc;
9288 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9289
Damien Lespiau40f46282015-02-27 11:15:21 +00009290 tiling = val & PLANE_CTL_TILED_MASK;
9291 switch (tiling) {
9292 case PLANE_CTL_TILED_LINEAR:
9293 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9294 break;
9295 case PLANE_CTL_TILED_X:
9296 plane_config->tiling = I915_TILING_X;
9297 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9298 break;
9299 case PLANE_CTL_TILED_Y:
9300 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9301 break;
9302 case PLANE_CTL_TILED_YF:
9303 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9304 break;
9305 default:
9306 MISSING_CASE(tiling);
9307 goto error;
9308 }
9309
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009310 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9311 plane_config->base = base;
9312
9313 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9314
9315 val = I915_READ(PLANE_SIZE(pipe, 0));
9316 fb->height = ((val >> 16) & 0xfff) + 1;
9317 fb->width = ((val >> 0) & 0x1fff) + 1;
9318
9319 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009320 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009321 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009322 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9323
9324 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009325 fb->pixel_format,
9326 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009327
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009328 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009329
9330 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9331 pipe_name(pipe), fb->width, fb->height,
9332 fb->bits_per_pixel, base, fb->pitches[0],
9333 plane_config->size);
9334
Damien Lespiau2d140302015-02-05 17:22:18 +00009335 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009336 return;
9337
9338error:
9339 kfree(fb);
9340}
9341
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009342static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009343 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009344{
9345 struct drm_device *dev = crtc->base.dev;
9346 struct drm_i915_private *dev_priv = dev->dev_private;
9347 uint32_t tmp;
9348
9349 tmp = I915_READ(PF_CTL(crtc->pipe));
9350
9351 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009352 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009353 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9354 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009355
9356 /* We currently do not free assignements of panel fitters on
9357 * ivb/hsw (since we don't use the higher upscaling modes which
9358 * differentiates them) so just WARN about this case for now. */
9359 if (IS_GEN7(dev)) {
9360 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9361 PF_PIPE_SEL_IVB(crtc->pipe));
9362 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009363 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009364}
9365
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009366static void
9367ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9368 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009369{
9370 struct drm_device *dev = crtc->base.dev;
9371 struct drm_i915_private *dev_priv = dev->dev_private;
9372 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009373 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009374 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009375 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009376 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009377 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009378
Damien Lespiau42a7b082015-02-05 19:35:13 +00009379 val = I915_READ(DSPCNTR(pipe));
9380 if (!(val & DISPLAY_PLANE_ENABLE))
9381 return;
9382
Damien Lespiaud9806c92015-01-21 14:07:19 +00009383 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009384 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009385 DRM_DEBUG_KMS("failed to alloc fb\n");
9386 return;
9387 }
9388
Damien Lespiau1b842c82015-01-21 13:50:54 +00009389 fb = &intel_fb->base;
9390
Daniel Vetter18c52472015-02-10 17:16:09 +00009391 if (INTEL_INFO(dev)->gen >= 4) {
9392 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009393 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009394 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9395 }
9396 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009397
9398 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009399 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009400 fb->pixel_format = fourcc;
9401 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009402
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009403 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009404 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009405 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009406 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009407 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009408 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009409 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009410 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009411 }
9412 plane_config->base = base;
9413
9414 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009415 fb->width = ((val >> 16) & 0xfff) + 1;
9416 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009417
9418 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009419 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009420
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009421 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009422 fb->pixel_format,
9423 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009424
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009425 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009426
Damien Lespiau2844a922015-01-20 12:51:48 +00009427 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9428 pipe_name(pipe), fb->width, fb->height,
9429 fb->bits_per_pixel, base, fb->pitches[0],
9430 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009431
Damien Lespiau2d140302015-02-05 17:22:18 +00009432 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009433}
9434
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009435static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009436 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009437{
9438 struct drm_device *dev = crtc->base.dev;
9439 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009440 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009441 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009442 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009443
Imre Deak17290502016-02-12 18:55:11 +02009444 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9445 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009446 return false;
9447
Daniel Vettere143a212013-07-04 12:01:15 +02009448 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009449 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009450
Imre Deak17290502016-02-12 18:55:11 +02009451 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009452 tmp = I915_READ(PIPECONF(crtc->pipe));
9453 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009454 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009455
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009456 switch (tmp & PIPECONF_BPC_MASK) {
9457 case PIPECONF_6BPC:
9458 pipe_config->pipe_bpp = 18;
9459 break;
9460 case PIPECONF_8BPC:
9461 pipe_config->pipe_bpp = 24;
9462 break;
9463 case PIPECONF_10BPC:
9464 pipe_config->pipe_bpp = 30;
9465 break;
9466 case PIPECONF_12BPC:
9467 pipe_config->pipe_bpp = 36;
9468 break;
9469 default:
9470 break;
9471 }
9472
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009473 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9474 pipe_config->limited_color_range = true;
9475
Daniel Vetterab9412b2013-05-03 11:49:46 +02009476 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009477 struct intel_shared_dpll *pll;
9478
Daniel Vetter88adfff2013-03-28 10:42:01 +01009479 pipe_config->has_pch_encoder = true;
9480
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009481 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9482 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9483 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009484
9485 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009486
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009487 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009488 pipe_config->shared_dpll =
9489 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009490 } else {
9491 tmp = I915_READ(PCH_DPLL_SEL);
9492 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9493 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9494 else
9495 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9496 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009497
9498 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9499
9500 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9501 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009502
9503 tmp = pipe_config->dpll_hw_state.dpll;
9504 pipe_config->pixel_multiplier =
9505 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9506 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009507
9508 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009509 } else {
9510 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009511 }
9512
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009513 intel_get_pipe_timings(crtc, pipe_config);
9514
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009515 ironlake_get_pfit_config(crtc, pipe_config);
9516
Imre Deak17290502016-02-12 18:55:11 +02009517 ret = true;
9518
9519out:
9520 intel_display_power_put(dev_priv, power_domain);
9521
9522 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009523}
9524
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009525static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9526{
9527 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009528 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009529
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009530 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009531 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009532 pipe_name(crtc->pipe));
9533
Rob Clarke2c719b2014-12-15 13:56:32 -05009534 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9535 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009536 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9537 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009538 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9539 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009540 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009541 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009542 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009543 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009544 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009545 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009546 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009547 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009548 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009549
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009550 /*
9551 * In theory we can still leave IRQs enabled, as long as only the HPD
9552 * interrupts remain enabled. We used to check for that, but since it's
9553 * gen-specific and since we only disable LCPLL after we fully disable
9554 * the interrupts, the check below should be enough.
9555 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009556 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009557}
9558
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009559static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9560{
9561 struct drm_device *dev = dev_priv->dev;
9562
9563 if (IS_HASWELL(dev))
9564 return I915_READ(D_COMP_HSW);
9565 else
9566 return I915_READ(D_COMP_BDW);
9567}
9568
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009569static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9570{
9571 struct drm_device *dev = dev_priv->dev;
9572
9573 if (IS_HASWELL(dev)) {
9574 mutex_lock(&dev_priv->rps.hw_lock);
9575 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9576 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009577 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009578 mutex_unlock(&dev_priv->rps.hw_lock);
9579 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009580 I915_WRITE(D_COMP_BDW, val);
9581 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009582 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009583}
9584
9585/*
9586 * This function implements pieces of two sequences from BSpec:
9587 * - Sequence for display software to disable LCPLL
9588 * - Sequence for display software to allow package C8+
9589 * The steps implemented here are just the steps that actually touch the LCPLL
9590 * register. Callers should take care of disabling all the display engine
9591 * functions, doing the mode unset, fixing interrupts, etc.
9592 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009593static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9594 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009595{
9596 uint32_t val;
9597
9598 assert_can_disable_lcpll(dev_priv);
9599
9600 val = I915_READ(LCPLL_CTL);
9601
9602 if (switch_to_fclk) {
9603 val |= LCPLL_CD_SOURCE_FCLK;
9604 I915_WRITE(LCPLL_CTL, val);
9605
9606 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9607 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9608 DRM_ERROR("Switching to FCLK failed\n");
9609
9610 val = I915_READ(LCPLL_CTL);
9611 }
9612
9613 val |= LCPLL_PLL_DISABLE;
9614 I915_WRITE(LCPLL_CTL, val);
9615 POSTING_READ(LCPLL_CTL);
9616
9617 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9618 DRM_ERROR("LCPLL still locked\n");
9619
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009620 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009621 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009622 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009623 ndelay(100);
9624
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009625 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9626 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009627 DRM_ERROR("D_COMP RCOMP still in progress\n");
9628
9629 if (allow_power_down) {
9630 val = I915_READ(LCPLL_CTL);
9631 val |= LCPLL_POWER_DOWN_ALLOW;
9632 I915_WRITE(LCPLL_CTL, val);
9633 POSTING_READ(LCPLL_CTL);
9634 }
9635}
9636
9637/*
9638 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9639 * source.
9640 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009641static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009642{
9643 uint32_t val;
9644
9645 val = I915_READ(LCPLL_CTL);
9646
9647 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9648 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9649 return;
9650
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009651 /*
9652 * Make sure we're not on PC8 state before disabling PC8, otherwise
9653 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009654 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009655 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009656
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009657 if (val & LCPLL_POWER_DOWN_ALLOW) {
9658 val &= ~LCPLL_POWER_DOWN_ALLOW;
9659 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009660 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009661 }
9662
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009663 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009664 val |= D_COMP_COMP_FORCE;
9665 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009666 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009667
9668 val = I915_READ(LCPLL_CTL);
9669 val &= ~LCPLL_PLL_DISABLE;
9670 I915_WRITE(LCPLL_CTL, val);
9671
9672 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9673 DRM_ERROR("LCPLL not locked yet\n");
9674
9675 if (val & LCPLL_CD_SOURCE_FCLK) {
9676 val = I915_READ(LCPLL_CTL);
9677 val &= ~LCPLL_CD_SOURCE_FCLK;
9678 I915_WRITE(LCPLL_CTL, val);
9679
9680 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9681 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9682 DRM_ERROR("Switching back to LCPLL failed\n");
9683 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009684
Mika Kuoppala59bad942015-01-16 11:34:40 +02009685 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009686 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009687}
9688
Paulo Zanoni765dab672014-03-07 20:08:18 -03009689/*
9690 * Package states C8 and deeper are really deep PC states that can only be
9691 * reached when all the devices on the system allow it, so even if the graphics
9692 * device allows PC8+, it doesn't mean the system will actually get to these
9693 * states. Our driver only allows PC8+ when going into runtime PM.
9694 *
9695 * The requirements for PC8+ are that all the outputs are disabled, the power
9696 * well is disabled and most interrupts are disabled, and these are also
9697 * requirements for runtime PM. When these conditions are met, we manually do
9698 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9699 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9700 * hang the machine.
9701 *
9702 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9703 * the state of some registers, so when we come back from PC8+ we need to
9704 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9705 * need to take care of the registers kept by RC6. Notice that this happens even
9706 * if we don't put the device in PCI D3 state (which is what currently happens
9707 * because of the runtime PM support).
9708 *
9709 * For more, read "Display Sequences for Package C8" on the hardware
9710 * documentation.
9711 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009712void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009713{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009714 struct drm_device *dev = dev_priv->dev;
9715 uint32_t val;
9716
Paulo Zanonic67a4702013-08-19 13:18:09 -03009717 DRM_DEBUG_KMS("Enabling package C8+\n");
9718
Ville Syrjäläc2699522015-08-27 23:55:59 +03009719 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009720 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9721 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9722 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9723 }
9724
9725 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009726 hsw_disable_lcpll(dev_priv, true, true);
9727}
9728
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009729void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009730{
9731 struct drm_device *dev = dev_priv->dev;
9732 uint32_t val;
9733
Paulo Zanonic67a4702013-08-19 13:18:09 -03009734 DRM_DEBUG_KMS("Disabling package C8+\n");
9735
9736 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009737 lpt_init_pch_refclk(dev);
9738
Ville Syrjäläc2699522015-08-27 23:55:59 +03009739 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009740 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9741 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9742 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9743 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009744}
9745
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009746static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309747{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009748 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009749 struct intel_atomic_state *old_intel_state =
9750 to_intel_atomic_state(old_state);
9751 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309752
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009753 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309754}
9755
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009756/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009757static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009758{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009759 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9760 struct drm_i915_private *dev_priv = state->dev->dev_private;
9761 struct drm_crtc *crtc;
9762 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009763 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009764 unsigned max_pixel_rate = 0, i;
9765 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009766
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009767 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9768 sizeof(intel_state->min_pixclk));
9769
9770 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009771 int pixel_rate;
9772
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009773 crtc_state = to_intel_crtc_state(cstate);
9774 if (!crtc_state->base.enable) {
9775 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009776 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009777 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009778
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009779 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009780
9781 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009782 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009783 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9784
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009785 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009786 }
9787
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009788 for_each_pipe(dev_priv, pipe)
9789 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9790
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009791 return max_pixel_rate;
9792}
9793
9794static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9795{
9796 struct drm_i915_private *dev_priv = dev->dev_private;
9797 uint32_t val, data;
9798 int ret;
9799
9800 if (WARN((I915_READ(LCPLL_CTL) &
9801 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9802 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9803 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9804 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9805 "trying to change cdclk frequency with cdclk not enabled\n"))
9806 return;
9807
9808 mutex_lock(&dev_priv->rps.hw_lock);
9809 ret = sandybridge_pcode_write(dev_priv,
9810 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9811 mutex_unlock(&dev_priv->rps.hw_lock);
9812 if (ret) {
9813 DRM_ERROR("failed to inform pcode about cdclk change\n");
9814 return;
9815 }
9816
9817 val = I915_READ(LCPLL_CTL);
9818 val |= LCPLL_CD_SOURCE_FCLK;
9819 I915_WRITE(LCPLL_CTL, val);
9820
9821 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9822 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9823 DRM_ERROR("Switching to FCLK failed\n");
9824
9825 val = I915_READ(LCPLL_CTL);
9826 val &= ~LCPLL_CLK_FREQ_MASK;
9827
9828 switch (cdclk) {
9829 case 450000:
9830 val |= LCPLL_CLK_FREQ_450;
9831 data = 0;
9832 break;
9833 case 540000:
9834 val |= LCPLL_CLK_FREQ_54O_BDW;
9835 data = 1;
9836 break;
9837 case 337500:
9838 val |= LCPLL_CLK_FREQ_337_5_BDW;
9839 data = 2;
9840 break;
9841 case 675000:
9842 val |= LCPLL_CLK_FREQ_675_BDW;
9843 data = 3;
9844 break;
9845 default:
9846 WARN(1, "invalid cdclk frequency\n");
9847 return;
9848 }
9849
9850 I915_WRITE(LCPLL_CTL, val);
9851
9852 val = I915_READ(LCPLL_CTL);
9853 val &= ~LCPLL_CD_SOURCE_FCLK;
9854 I915_WRITE(LCPLL_CTL, val);
9855
9856 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9857 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9858 DRM_ERROR("Switching back to LCPLL failed\n");
9859
9860 mutex_lock(&dev_priv->rps.hw_lock);
9861 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9862 mutex_unlock(&dev_priv->rps.hw_lock);
9863
9864 intel_update_cdclk(dev);
9865
9866 WARN(cdclk != dev_priv->cdclk_freq,
9867 "cdclk requested %d kHz but got %d kHz\n",
9868 cdclk, dev_priv->cdclk_freq);
9869}
9870
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009871static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009872{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009873 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009874 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009875 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009876 int cdclk;
9877
9878 /*
9879 * FIXME should also account for plane ratio
9880 * once 64bpp pixel formats are supported.
9881 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009882 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009883 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009884 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009885 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009886 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009887 cdclk = 450000;
9888 else
9889 cdclk = 337500;
9890
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009891 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009892 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9893 cdclk, dev_priv->max_cdclk_freq);
9894 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009895 }
9896
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009897 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9898 if (!intel_state->active_crtcs)
9899 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009900
9901 return 0;
9902}
9903
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009904static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009905{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009906 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009907 struct intel_atomic_state *old_intel_state =
9908 to_intel_atomic_state(old_state);
9909 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009910
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009911 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009912}
9913
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009914static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9915 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009916{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009917 struct intel_encoder *intel_encoder =
9918 intel_ddi_get_crtc_new_encoder(crtc_state);
9919
9920 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9921 if (!intel_ddi_pll_select(crtc, crtc_state))
9922 return -EINVAL;
9923 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009924
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009925 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009926
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009927 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009928}
9929
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309930static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9931 enum port port,
9932 struct intel_crtc_state *pipe_config)
9933{
9934 switch (port) {
9935 case PORT_A:
9936 pipe_config->ddi_pll_sel = SKL_DPLL0;
9937 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9938 break;
9939 case PORT_B:
9940 pipe_config->ddi_pll_sel = SKL_DPLL1;
9941 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9942 break;
9943 case PORT_C:
9944 pipe_config->ddi_pll_sel = SKL_DPLL2;
9945 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9946 break;
9947 default:
9948 DRM_ERROR("Incorrect port type\n");
9949 }
9950}
9951
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009952static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9953 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009954 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009955{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009956 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009957
9958 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9959 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9960
9961 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009962 case SKL_DPLL0:
9963 /*
9964 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9965 * of the shared DPLL framework and thus needs to be read out
9966 * separately
9967 */
9968 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9969 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9970 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009971 case SKL_DPLL1:
9972 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9973 break;
9974 case SKL_DPLL2:
9975 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9976 break;
9977 case SKL_DPLL3:
9978 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9979 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009980 }
9981}
9982
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009983static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9984 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009985 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009986{
9987 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9988
9989 switch (pipe_config->ddi_pll_sel) {
9990 case PORT_CLK_SEL_WRPLL1:
9991 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9992 break;
9993 case PORT_CLK_SEL_WRPLL2:
9994 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9995 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009996 case PORT_CLK_SEL_SPLL:
9997 pipe_config->shared_dpll = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009998 break;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009999 }
10000}
10001
Daniel Vetter26804af2014-06-25 22:01:55 +030010002static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010003 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010004{
10005 struct drm_device *dev = crtc->base.dev;
10006 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010007 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010008 enum port port;
10009 uint32_t tmp;
10010
10011 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10012
10013 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10014
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070010015 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010016 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010017 else if (IS_BROXTON(dev))
10018 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010019 else
10020 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010021
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010022 if (pipe_config->shared_dpll >= 0) {
10023 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
10024
10025 WARN_ON(!pll->get_hw_state(dev_priv, pll,
10026 &pipe_config->dpll_hw_state));
10027 }
10028
Daniel Vetter26804af2014-06-25 22:01:55 +030010029 /*
10030 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10031 * DDI E. So just check whether this pipe is wired to DDI E and whether
10032 * the PCH transcoder is on.
10033 */
Damien Lespiauca370452013-12-03 13:56:24 +000010034 if (INTEL_INFO(dev)->gen < 9 &&
10035 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010036 pipe_config->has_pch_encoder = true;
10037
10038 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10039 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10040 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10041
10042 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10043 }
10044}
10045
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010046static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010047 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010048{
10049 struct drm_device *dev = crtc->base.dev;
10050 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +020010051 enum intel_display_power_domain power_domain;
10052 unsigned long power_domain_mask;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010053 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +020010054 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010055
Imre Deak17290502016-02-12 18:55:11 +020010056 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10057 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010058 return false;
Imre Deak17290502016-02-12 18:55:11 +020010059 power_domain_mask = BIT(power_domain);
10060
10061 ret = false;
Imre Deakb5482bd2014-03-05 16:20:55 +020010062
Daniel Vettere143a212013-07-04 12:01:15 +020010063 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010064 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
10065
Daniel Vettereccb1402013-05-22 00:50:22 +020010066 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10067 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10068 enum pipe trans_edp_pipe;
10069 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10070 default:
10071 WARN(1, "unknown pipe linked to edp transcoder\n");
10072 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10073 case TRANS_DDI_EDP_INPUT_A_ON:
10074 trans_edp_pipe = PIPE_A;
10075 break;
10076 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10077 trans_edp_pipe = PIPE_B;
10078 break;
10079 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10080 trans_edp_pipe = PIPE_C;
10081 break;
10082 }
10083
10084 if (trans_edp_pipe == crtc->pipe)
10085 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10086 }
10087
Imre Deak17290502016-02-12 18:55:11 +020010088 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10089 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10090 goto out;
10091 power_domain_mask |= BIT(power_domain);
Paulo Zanoni2bfce952013-04-18 16:35:40 -030010092
Daniel Vettereccb1402013-05-22 00:50:22 +020010093 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010094 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +020010095 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010096
Daniel Vetter26804af2014-06-25 22:01:55 +030010097 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010098
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010099 intel_get_pipe_timings(crtc, pipe_config);
10100
Chandra Kondurua1b22782015-04-07 15:28:45 -070010101 if (INTEL_INFO(dev)->gen >= 9) {
10102 skl_init_scalers(dev, crtc, pipe_config);
10103 }
10104
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010105 if (INTEL_INFO(dev)->gen >= 9) {
10106 pipe_config->scaler_state.scaler_id = -1;
10107 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10108 }
10109
Imre Deak17290502016-02-12 18:55:11 +020010110 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10111 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10112 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010113 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010114 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010115 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010116 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010117 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010118
Jesse Barnese59150d2014-01-07 13:30:45 -080010119 if (IS_HASWELL(dev))
10120 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10121 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010122
Clint Taylorebb69c92014-09-30 10:30:22 -070010123 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
10124 pipe_config->pixel_multiplier =
10125 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10126 } else {
10127 pipe_config->pixel_multiplier = 1;
10128 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010129
Imre Deak17290502016-02-12 18:55:11 +020010130 ret = true;
10131
10132out:
10133 for_each_power_domain(power_domain, power_domain_mask)
10134 intel_display_power_put(dev_priv, power_domain);
10135
10136 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010137}
10138
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010139static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10140 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010141{
10142 struct drm_device *dev = crtc->dev;
10143 struct drm_i915_private *dev_priv = dev->dev_private;
10144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010145 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010146
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010147 if (plane_state && plane_state->visible) {
10148 unsigned int width = plane_state->base.crtc_w;
10149 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010150 unsigned int stride = roundup_pow_of_two(width) * 4;
10151
10152 switch (stride) {
10153 default:
10154 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10155 width, stride);
10156 stride = 256;
10157 /* fallthrough */
10158 case 256:
10159 case 512:
10160 case 1024:
10161 case 2048:
10162 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010163 }
10164
Ville Syrjälädc41c152014-08-13 11:57:05 +030010165 cntl |= CURSOR_ENABLE |
10166 CURSOR_GAMMA_ENABLE |
10167 CURSOR_FORMAT_ARGB |
10168 CURSOR_STRIDE(stride);
10169
10170 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010171 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010172
Ville Syrjälädc41c152014-08-13 11:57:05 +030010173 if (intel_crtc->cursor_cntl != 0 &&
10174 (intel_crtc->cursor_base != base ||
10175 intel_crtc->cursor_size != size ||
10176 intel_crtc->cursor_cntl != cntl)) {
10177 /* On these chipsets we can only modify the base/size/stride
10178 * whilst the cursor is disabled.
10179 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010180 I915_WRITE(CURCNTR(PIPE_A), 0);
10181 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010182 intel_crtc->cursor_cntl = 0;
10183 }
10184
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010185 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010186 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010187 intel_crtc->cursor_base = base;
10188 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010189
10190 if (intel_crtc->cursor_size != size) {
10191 I915_WRITE(CURSIZE, size);
10192 intel_crtc->cursor_size = size;
10193 }
10194
Chris Wilson4b0e3332014-05-30 16:35:26 +030010195 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010196 I915_WRITE(CURCNTR(PIPE_A), cntl);
10197 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010198 intel_crtc->cursor_cntl = cntl;
10199 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010200}
10201
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010202static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10203 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010204{
10205 struct drm_device *dev = crtc->dev;
10206 struct drm_i915_private *dev_priv = dev->dev_private;
10207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10208 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010209 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010210
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010211 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010212 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010213 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010214 case 64:
10215 cntl |= CURSOR_MODE_64_ARGB_AX;
10216 break;
10217 case 128:
10218 cntl |= CURSOR_MODE_128_ARGB_AX;
10219 break;
10220 case 256:
10221 cntl |= CURSOR_MODE_256_ARGB_AX;
10222 break;
10223 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010224 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010225 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010226 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010227 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010228
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010229 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010230 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010231
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010232 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10233 cntl |= CURSOR_ROTATE_180;
10234 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010235
Chris Wilson4b0e3332014-05-30 16:35:26 +030010236 if (intel_crtc->cursor_cntl != cntl) {
10237 I915_WRITE(CURCNTR(pipe), cntl);
10238 POSTING_READ(CURCNTR(pipe));
10239 intel_crtc->cursor_cntl = cntl;
10240 }
10241
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010242 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010243 I915_WRITE(CURBASE(pipe), base);
10244 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010245
10246 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010247}
10248
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010249/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010250static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010251 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010252{
10253 struct drm_device *dev = crtc->dev;
10254 struct drm_i915_private *dev_priv = dev->dev_private;
10255 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10256 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010257 u32 base = intel_crtc->cursor_addr;
10258 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010259
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010260 if (plane_state) {
10261 int x = plane_state->base.crtc_x;
10262 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010263
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010264 if (x < 0) {
10265 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10266 x = -x;
10267 }
10268 pos |= x << CURSOR_X_SHIFT;
10269
10270 if (y < 0) {
10271 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10272 y = -y;
10273 }
10274 pos |= y << CURSOR_Y_SHIFT;
10275
10276 /* ILK+ do this automagically */
10277 if (HAS_GMCH_DISPLAY(dev) &&
10278 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10279 base += (plane_state->base.crtc_h *
10280 plane_state->base.crtc_w - 1) * 4;
10281 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010282 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010283
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010284 I915_WRITE(CURPOS(pipe), pos);
10285
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010286 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010287 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010288 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010289 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010290}
10291
Ville Syrjälädc41c152014-08-13 11:57:05 +030010292static bool cursor_size_ok(struct drm_device *dev,
10293 uint32_t width, uint32_t height)
10294{
10295 if (width == 0 || height == 0)
10296 return false;
10297
10298 /*
10299 * 845g/865g are special in that they are only limited by
10300 * the width of their cursors, the height is arbitrary up to
10301 * the precision of the register. Everything else requires
10302 * square cursors, limited to a few power-of-two sizes.
10303 */
10304 if (IS_845G(dev) || IS_I865G(dev)) {
10305 if ((width & 63) != 0)
10306 return false;
10307
10308 if (width > (IS_845G(dev) ? 64 : 512))
10309 return false;
10310
10311 if (height > 1023)
10312 return false;
10313 } else {
10314 switch (width | height) {
10315 case 256:
10316 case 128:
10317 if (IS_GEN2(dev))
10318 return false;
10319 case 64:
10320 break;
10321 default:
10322 return false;
10323 }
10324 }
10325
10326 return true;
10327}
10328
Jesse Barnes79e53942008-11-07 14:24:08 -080010329static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010330 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010331{
James Simmons72034252010-08-03 01:33:19 +010010332 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010333 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010334
James Simmons72034252010-08-03 01:33:19 +010010335 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010336 intel_crtc->lut_r[i] = red[i] >> 8;
10337 intel_crtc->lut_g[i] = green[i] >> 8;
10338 intel_crtc->lut_b[i] = blue[i] >> 8;
10339 }
10340
10341 intel_crtc_load_lut(crtc);
10342}
10343
Jesse Barnes79e53942008-11-07 14:24:08 -080010344/* VESA 640x480x72Hz mode to set on the pipe */
10345static struct drm_display_mode load_detect_mode = {
10346 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10347 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10348};
10349
Daniel Vettera8bb6812014-02-10 18:00:39 +010010350struct drm_framebuffer *
10351__intel_framebuffer_create(struct drm_device *dev,
10352 struct drm_mode_fb_cmd2 *mode_cmd,
10353 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010354{
10355 struct intel_framebuffer *intel_fb;
10356 int ret;
10357
10358 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010359 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010360 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010361
10362 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010363 if (ret)
10364 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010365
10366 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010367
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010368err:
10369 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010370 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010371}
10372
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010373static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010374intel_framebuffer_create(struct drm_device *dev,
10375 struct drm_mode_fb_cmd2 *mode_cmd,
10376 struct drm_i915_gem_object *obj)
10377{
10378 struct drm_framebuffer *fb;
10379 int ret;
10380
10381 ret = i915_mutex_lock_interruptible(dev);
10382 if (ret)
10383 return ERR_PTR(ret);
10384 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10385 mutex_unlock(&dev->struct_mutex);
10386
10387 return fb;
10388}
10389
Chris Wilsond2dff872011-04-19 08:36:26 +010010390static u32
10391intel_framebuffer_pitch_for_width(int width, int bpp)
10392{
10393 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10394 return ALIGN(pitch, 64);
10395}
10396
10397static u32
10398intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10399{
10400 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010401 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010402}
10403
10404static struct drm_framebuffer *
10405intel_framebuffer_create_for_mode(struct drm_device *dev,
10406 struct drm_display_mode *mode,
10407 int depth, int bpp)
10408{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010409 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010410 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010411 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010412
10413 obj = i915_gem_alloc_object(dev,
10414 intel_framebuffer_size_for_mode(mode, bpp));
10415 if (obj == NULL)
10416 return ERR_PTR(-ENOMEM);
10417
10418 mode_cmd.width = mode->hdisplay;
10419 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010420 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10421 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010422 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010423
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010424 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10425 if (IS_ERR(fb))
10426 drm_gem_object_unreference_unlocked(&obj->base);
10427
10428 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010429}
10430
10431static struct drm_framebuffer *
10432mode_fits_in_fbdev(struct drm_device *dev,
10433 struct drm_display_mode *mode)
10434{
Daniel Vetter06957262015-08-10 13:34:08 +020010435#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010436 struct drm_i915_private *dev_priv = dev->dev_private;
10437 struct drm_i915_gem_object *obj;
10438 struct drm_framebuffer *fb;
10439
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010440 if (!dev_priv->fbdev)
10441 return NULL;
10442
10443 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010444 return NULL;
10445
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010446 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010447 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010448
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010449 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010450 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10451 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010452 return NULL;
10453
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010454 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010455 return NULL;
10456
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010457 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010458 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010459#else
10460 return NULL;
10461#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010462}
10463
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010464static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10465 struct drm_crtc *crtc,
10466 struct drm_display_mode *mode,
10467 struct drm_framebuffer *fb,
10468 int x, int y)
10469{
10470 struct drm_plane_state *plane_state;
10471 int hdisplay, vdisplay;
10472 int ret;
10473
10474 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10475 if (IS_ERR(plane_state))
10476 return PTR_ERR(plane_state);
10477
10478 if (mode)
10479 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10480 else
10481 hdisplay = vdisplay = 0;
10482
10483 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10484 if (ret)
10485 return ret;
10486 drm_atomic_set_fb_for_plane(plane_state, fb);
10487 plane_state->crtc_x = 0;
10488 plane_state->crtc_y = 0;
10489 plane_state->crtc_w = hdisplay;
10490 plane_state->crtc_h = vdisplay;
10491 plane_state->src_x = x << 16;
10492 plane_state->src_y = y << 16;
10493 plane_state->src_w = hdisplay << 16;
10494 plane_state->src_h = vdisplay << 16;
10495
10496 return 0;
10497}
10498
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010499bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010500 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010501 struct intel_load_detect_pipe *old,
10502 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010503{
10504 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010505 struct intel_encoder *intel_encoder =
10506 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010507 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010508 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010509 struct drm_crtc *crtc = NULL;
10510 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010511 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010512 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010513 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010514 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010515 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010516 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010517
Chris Wilsond2dff872011-04-19 08:36:26 +010010518 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010519 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010520 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010521
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010522 old->restore_state = NULL;
10523
Rob Clark51fd3712013-11-19 12:10:12 -050010524retry:
10525 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10526 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010527 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010528
Jesse Barnes79e53942008-11-07 14:24:08 -080010529 /*
10530 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010531 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010532 * - if the connector already has an assigned crtc, use it (but make
10533 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010534 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010535 * - try to find the first unused crtc that can drive this connector,
10536 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010537 */
10538
10539 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010540 if (connector->state->crtc) {
10541 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010542
Rob Clark51fd3712013-11-19 12:10:12 -050010543 ret = drm_modeset_lock(&crtc->mutex, ctx);
10544 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010545 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010546
10547 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010548 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010549 }
10550
10551 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010552 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010553 i++;
10554 if (!(encoder->possible_crtcs & (1 << i)))
10555 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010556
10557 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10558 if (ret)
10559 goto fail;
10560
10561 if (possible_crtc->state->enable) {
10562 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010563 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010564 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010565
10566 crtc = possible_crtc;
10567 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010568 }
10569
10570 /*
10571 * If we didn't find an unused CRTC, don't use any.
10572 */
10573 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010574 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010575 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010576 }
10577
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010578found:
10579 intel_crtc = to_intel_crtc(crtc);
10580
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010581 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10582 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010583 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010584
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010585 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010586 restore_state = drm_atomic_state_alloc(dev);
10587 if (!state || !restore_state) {
10588 ret = -ENOMEM;
10589 goto fail;
10590 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010591
10592 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010593 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010594
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010595 connector_state = drm_atomic_get_connector_state(state, connector);
10596 if (IS_ERR(connector_state)) {
10597 ret = PTR_ERR(connector_state);
10598 goto fail;
10599 }
10600
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010601 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10602 if (ret)
10603 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010604
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010605 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10606 if (IS_ERR(crtc_state)) {
10607 ret = PTR_ERR(crtc_state);
10608 goto fail;
10609 }
10610
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010611 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010612
Chris Wilson64927112011-04-20 07:25:26 +010010613 if (!mode)
10614 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010615
Chris Wilsond2dff872011-04-19 08:36:26 +010010616 /* We need a framebuffer large enough to accommodate all accesses
10617 * that the plane may generate whilst we perform load detection.
10618 * We can not rely on the fbcon either being present (we get called
10619 * during its initialisation to detect all boot displays, or it may
10620 * not even exist) or that it is large enough to satisfy the
10621 * requested mode.
10622 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010623 fb = mode_fits_in_fbdev(dev, mode);
10624 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010625 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010626 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010627 } else
10628 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010629 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010630 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010631 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010632 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010633
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010634 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10635 if (ret)
10636 goto fail;
10637
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010638 drm_framebuffer_unreference(fb);
10639
10640 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10641 if (ret)
10642 goto fail;
10643
10644 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10645 if (!ret)
10646 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10647 if (!ret)
10648 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10649 if (ret) {
10650 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10651 goto fail;
10652 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010653
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010654 ret = drm_atomic_commit(state);
10655 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010656 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010657 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010658 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010659
10660 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010661
Jesse Barnes79e53942008-11-07 14:24:08 -080010662 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010663 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010664 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010665
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010666fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010667 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010668 drm_atomic_state_free(restore_state);
10669 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010670
Rob Clark51fd3712013-11-19 12:10:12 -050010671 if (ret == -EDEADLK) {
10672 drm_modeset_backoff(ctx);
10673 goto retry;
10674 }
10675
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010676 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010677}
10678
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010679void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010680 struct intel_load_detect_pipe *old,
10681 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010682{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010683 struct intel_encoder *intel_encoder =
10684 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010685 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010686 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010687 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010688
Chris Wilsond2dff872011-04-19 08:36:26 +010010689 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010690 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010691 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010692
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010693 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010694 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010695
10696 ret = drm_atomic_commit(state);
10697 if (ret) {
10698 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10699 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010700 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010701}
10702
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010703static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010704 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010705{
10706 struct drm_i915_private *dev_priv = dev->dev_private;
10707 u32 dpll = pipe_config->dpll_hw_state.dpll;
10708
10709 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010710 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010711 else if (HAS_PCH_SPLIT(dev))
10712 return 120000;
10713 else if (!IS_GEN2(dev))
10714 return 96000;
10715 else
10716 return 48000;
10717}
10718
Jesse Barnes79e53942008-11-07 14:24:08 -080010719/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010720static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010721 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010722{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010723 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010724 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010725 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010726 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010727 u32 fp;
10728 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010729 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010730 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010731
10732 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010733 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010734 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010735 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010736
10737 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010738 if (IS_PINEVIEW(dev)) {
10739 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10740 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010741 } else {
10742 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10743 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10744 }
10745
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010746 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010747 if (IS_PINEVIEW(dev))
10748 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10749 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010750 else
10751 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010752 DPLL_FPA01_P1_POST_DIV_SHIFT);
10753
10754 switch (dpll & DPLL_MODE_MASK) {
10755 case DPLLB_MODE_DAC_SERIAL:
10756 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10757 5 : 10;
10758 break;
10759 case DPLLB_MODE_LVDS:
10760 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10761 7 : 14;
10762 break;
10763 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010764 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010765 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010766 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010767 }
10768
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010769 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010770 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010771 else
Imre Deakdccbea32015-06-22 23:35:51 +030010772 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010773 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010774 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010775 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010776
10777 if (is_lvds) {
10778 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10779 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010780
10781 if (lvds & LVDS_CLKB_POWER_UP)
10782 clock.p2 = 7;
10783 else
10784 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010785 } else {
10786 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10787 clock.p1 = 2;
10788 else {
10789 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10790 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10791 }
10792 if (dpll & PLL_P2_DIVIDE_BY_4)
10793 clock.p2 = 4;
10794 else
10795 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010796 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010797
Imre Deakdccbea32015-06-22 23:35:51 +030010798 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010799 }
10800
Ville Syrjälä18442d02013-09-13 16:00:08 +030010801 /*
10802 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010803 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010804 * encoder's get_config() function.
10805 */
Imre Deakdccbea32015-06-22 23:35:51 +030010806 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010807}
10808
Ville Syrjälä6878da02013-09-13 15:59:11 +030010809int intel_dotclock_calculate(int link_freq,
10810 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010811{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010812 /*
10813 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010814 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010815 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010816 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010817 *
10818 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010819 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010820 */
10821
Ville Syrjälä6878da02013-09-13 15:59:11 +030010822 if (!m_n->link_n)
10823 return 0;
10824
10825 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10826}
10827
Ville Syrjälä18442d02013-09-13 16:00:08 +030010828static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010829 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010830{
10831 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010832
10833 /* read out port_clock from the DPLL */
10834 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010835
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010836 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010837 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010838 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010839 * agree once we know their relationship in the encoder's
10840 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010841 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010842 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010843 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10844 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010845}
10846
10847/** Returns the currently programmed mode of the given pipe. */
10848struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10849 struct drm_crtc *crtc)
10850{
Jesse Barnes548f2452011-02-17 10:40:53 -080010851 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010852 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010853 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010854 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010855 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010856 int htot = I915_READ(HTOTAL(cpu_transcoder));
10857 int hsync = I915_READ(HSYNC(cpu_transcoder));
10858 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10859 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010860 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010861
10862 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10863 if (!mode)
10864 return NULL;
10865
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010866 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10867 if (!pipe_config) {
10868 kfree(mode);
10869 return NULL;
10870 }
10871
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010872 /*
10873 * Construct a pipe_config sufficient for getting the clock info
10874 * back out of crtc_clock_get.
10875 *
10876 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10877 * to use a real value here instead.
10878 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010879 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10880 pipe_config->pixel_multiplier = 1;
10881 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10882 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10883 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10884 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010885
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010886 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010887 mode->hdisplay = (htot & 0xffff) + 1;
10888 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10889 mode->hsync_start = (hsync & 0xffff) + 1;
10890 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10891 mode->vdisplay = (vtot & 0xffff) + 1;
10892 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10893 mode->vsync_start = (vsync & 0xffff) + 1;
10894 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10895
10896 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010897
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010898 kfree(pipe_config);
10899
Jesse Barnes79e53942008-11-07 14:24:08 -080010900 return mode;
10901}
10902
Chris Wilsonf047e392012-07-21 12:31:41 +010010903void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010904{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010905 struct drm_i915_private *dev_priv = dev->dev_private;
10906
Chris Wilsonf62a0072014-02-21 17:55:39 +000010907 if (dev_priv->mm.busy)
10908 return;
10909
Paulo Zanoni43694d62014-03-07 20:08:08 -030010910 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010911 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010912 if (INTEL_INFO(dev)->gen >= 6)
10913 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010914 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010915}
10916
10917void intel_mark_idle(struct drm_device *dev)
10918{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010919 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010920
Chris Wilsonf62a0072014-02-21 17:55:39 +000010921 if (!dev_priv->mm.busy)
10922 return;
10923
10924 dev_priv->mm.busy = false;
10925
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010926 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010927 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010928
Paulo Zanoni43694d62014-03-07 20:08:08 -030010929 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010930}
10931
Jesse Barnes79e53942008-11-07 14:24:08 -080010932static void intel_crtc_destroy(struct drm_crtc *crtc)
10933{
10934 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010935 struct drm_device *dev = crtc->dev;
10936 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010937
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010938 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010939 work = intel_crtc->unpin_work;
10940 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010941 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010942
10943 if (work) {
10944 cancel_work_sync(&work->work);
10945 kfree(work);
10946 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010947
10948 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010949
Jesse Barnes79e53942008-11-07 14:24:08 -080010950 kfree(intel_crtc);
10951}
10952
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010953static void intel_unpin_work_fn(struct work_struct *__work)
10954{
10955 struct intel_unpin_work *work =
10956 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010957 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10958 struct drm_device *dev = crtc->base.dev;
10959 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010960
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010961 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010962 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010963 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010964
John Harrisonf06cc1b2014-11-24 18:49:37 +000010965 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010966 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010967 mutex_unlock(&dev->struct_mutex);
10968
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010969 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010970 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010971 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010972
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010973 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10974 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010975
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010976 kfree(work);
10977}
10978
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010979static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010980 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010981{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10983 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010984 unsigned long flags;
10985
10986 /* Ignore early vblank irqs */
10987 if (intel_crtc == NULL)
10988 return;
10989
Daniel Vetterf3260382014-09-15 14:55:23 +020010990 /*
10991 * This is called both by irq handlers and the reset code (to complete
10992 * lost pageflips) so needs the full irqsave spinlocks.
10993 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010994 spin_lock_irqsave(&dev->event_lock, flags);
10995 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010996
10997 /* Ensure we don't miss a work->pending update ... */
10998 smp_rmb();
10999
11000 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011001 spin_unlock_irqrestore(&dev->event_lock, flags);
11002 return;
11003 }
11004
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011005 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010011006
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011007 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011008}
11009
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011010void intel_finish_page_flip(struct drm_device *dev, int pipe)
11011{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011012 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011013 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11014
Mario Kleiner49b14a52010-12-09 07:00:07 +010011015 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011016}
11017
11018void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
11019{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011020 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011021 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
11022
Mario Kleiner49b14a52010-12-09 07:00:07 +010011023 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011024}
11025
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011026/* Is 'a' after or equal to 'b'? */
11027static bool g4x_flip_count_after_eq(u32 a, u32 b)
11028{
11029 return !((a - b) & 0x80000000);
11030}
11031
11032static bool page_flip_finished(struct intel_crtc *crtc)
11033{
11034 struct drm_device *dev = crtc->base.dev;
11035 struct drm_i915_private *dev_priv = dev->dev_private;
11036
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030011037 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
11038 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
11039 return true;
11040
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011041 /*
11042 * The relevant registers doen't exist on pre-ctg.
11043 * As the flip done interrupt doesn't trigger for mmio
11044 * flips on gmch platforms, a flip count check isn't
11045 * really needed there. But since ctg has the registers,
11046 * include it in the check anyway.
11047 */
11048 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11049 return true;
11050
11051 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010011052 * BDW signals flip done immediately if the plane
11053 * is disabled, even if the plane enable is already
11054 * armed to occur at the next vblank :(
11055 */
11056
11057 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011058 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11059 * used the same base address. In that case the mmio flip might
11060 * have completed, but the CS hasn't even executed the flip yet.
11061 *
11062 * A flip count check isn't enough as the CS might have updated
11063 * the base address just after start of vblank, but before we
11064 * managed to process the interrupt. This means we'd complete the
11065 * CS flip too soon.
11066 *
11067 * Combining both checks should get us a good enough result. It may
11068 * still happen that the CS flip has been executed, but has not
11069 * yet actually completed. But in case the base address is the same
11070 * anyway, we don't really care.
11071 */
11072 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11073 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030011074 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011075 crtc->unpin_work->flip_count);
11076}
11077
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011078void intel_prepare_page_flip(struct drm_device *dev, int plane)
11079{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011080 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011081 struct intel_crtc *intel_crtc =
11082 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
11083 unsigned long flags;
11084
Daniel Vetterf3260382014-09-15 14:55:23 +020011085
11086 /*
11087 * This is called both by irq handlers and the reset code (to complete
11088 * lost pageflips) so needs the full irqsave spinlocks.
11089 *
11090 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000011091 * generate a page-flip completion irq, i.e. every modeset
11092 * is also accompanied by a spurious intel_prepare_page_flip().
11093 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011094 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011095 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000011096 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011097 spin_unlock_irqrestore(&dev->event_lock, flags);
11098}
11099
Chris Wilson60426392015-10-10 10:44:32 +010011100static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011101{
11102 /* Ensure that the work item is consistent when activating it ... */
11103 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010011104 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011105 /* and that it is marked active as soon as the irq could fire. */
11106 smp_wmb();
11107}
11108
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011109static int intel_gen2_queue_flip(struct drm_device *dev,
11110 struct drm_crtc *crtc,
11111 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011112 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011113 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011114 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011115{
John Harrison6258fbe2015-05-29 17:43:48 +010011116 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011117 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011118 u32 flip_mask;
11119 int ret;
11120
John Harrison5fb9de12015-05-29 17:44:07 +010011121 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011122 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011123 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011124
11125 /* Can't queue multiple flips, so wait for the previous
11126 * one to finish before executing the next.
11127 */
11128 if (intel_crtc->plane)
11129 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11130 else
11131 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011132 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11133 intel_ring_emit(ring, MI_NOOP);
11134 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11135 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11136 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011137 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011138 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011139
Chris Wilson60426392015-10-10 10:44:32 +010011140 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011141 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011142}
11143
11144static int intel_gen3_queue_flip(struct drm_device *dev,
11145 struct drm_crtc *crtc,
11146 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011147 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011148 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011149 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011150{
John Harrison6258fbe2015-05-29 17:43:48 +010011151 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011152 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011153 u32 flip_mask;
11154 int ret;
11155
John Harrison5fb9de12015-05-29 17:44:07 +010011156 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011157 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011158 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011159
11160 if (intel_crtc->plane)
11161 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11162 else
11163 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011164 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11165 intel_ring_emit(ring, MI_NOOP);
11166 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11167 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11168 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011169 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011170 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011171
Chris Wilson60426392015-10-10 10:44:32 +010011172 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011173 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011174}
11175
11176static int intel_gen4_queue_flip(struct drm_device *dev,
11177 struct drm_crtc *crtc,
11178 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011179 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011180 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011181 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011182{
John Harrison6258fbe2015-05-29 17:43:48 +010011183 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011184 struct drm_i915_private *dev_priv = dev->dev_private;
11185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11186 uint32_t pf, pipesrc;
11187 int ret;
11188
John Harrison5fb9de12015-05-29 17:44:07 +010011189 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011190 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011191 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011192
11193 /* i965+ uses the linear or tiled offsets from the
11194 * Display Registers (which do not change across a page-flip)
11195 * so we need only reprogram the base address.
11196 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020011197 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11198 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11199 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011200 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011201 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011202
11203 /* XXX Enabling the panel-fitter across page-flip is so far
11204 * untested on non-native modes, so ignore it for now.
11205 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11206 */
11207 pf = 0;
11208 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011209 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011210
Chris Wilson60426392015-10-10 10:44:32 +010011211 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011212 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011213}
11214
11215static int intel_gen6_queue_flip(struct drm_device *dev,
11216 struct drm_crtc *crtc,
11217 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011218 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011219 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011220 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011221{
John Harrison6258fbe2015-05-29 17:43:48 +010011222 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011223 struct drm_i915_private *dev_priv = dev->dev_private;
11224 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11225 uint32_t pf, pipesrc;
11226 int ret;
11227
John Harrison5fb9de12015-05-29 17:44:07 +010011228 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011229 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011230 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011231
Daniel Vetter6d90c952012-04-26 23:28:05 +020011232 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11233 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11234 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011235 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011236
Chris Wilson99d9acd2012-04-17 20:37:00 +010011237 /* Contrary to the suggestions in the documentation,
11238 * "Enable Panel Fitter" does not seem to be required when page
11239 * flipping with a non-native mode, and worse causes a normal
11240 * modeset to fail.
11241 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11242 */
11243 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011244 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011245 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011246
Chris Wilson60426392015-10-10 10:44:32 +010011247 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011248 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011249}
11250
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011251static int intel_gen7_queue_flip(struct drm_device *dev,
11252 struct drm_crtc *crtc,
11253 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011254 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011255 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011256 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011257{
John Harrison6258fbe2015-05-29 17:43:48 +010011258 struct intel_engine_cs *ring = req->ring;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011259 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011260 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011261 int len, ret;
11262
Robin Schroereba905b2014-05-18 02:24:50 +020011263 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011264 case PLANE_A:
11265 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11266 break;
11267 case PLANE_B:
11268 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11269 break;
11270 case PLANE_C:
11271 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11272 break;
11273 default:
11274 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011275 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011276 }
11277
Chris Wilsonffe74d72013-08-26 20:58:12 +010011278 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011279 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011280 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011281 /*
11282 * On Gen 8, SRM is now taking an extra dword to accommodate
11283 * 48bits addresses, and we need a NOOP for the batch size to
11284 * stay even.
11285 */
11286 if (IS_GEN8(dev))
11287 len += 2;
11288 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011289
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011290 /*
11291 * BSpec MI_DISPLAY_FLIP for IVB:
11292 * "The full packet must be contained within the same cache line."
11293 *
11294 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11295 * cacheline, if we ever start emitting more commands before
11296 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11297 * then do the cacheline alignment, and finally emit the
11298 * MI_DISPLAY_FLIP.
11299 */
John Harrisonbba09b12015-05-29 17:44:06 +010011300 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011301 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011302 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011303
John Harrison5fb9de12015-05-29 17:44:07 +010011304 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011305 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011306 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011307
Chris Wilsonffe74d72013-08-26 20:58:12 +010011308 /* Unmask the flip-done completion message. Note that the bspec says that
11309 * we should do this for both the BCS and RCS, and that we must not unmask
11310 * more than one flip event at any time (or ensure that one flip message
11311 * can be sent by waiting for flip-done prior to queueing new flips).
11312 * Experimentation says that BCS works despite DERRMR masking all
11313 * flip-done completion events and that unmasking all planes at once
11314 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11315 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11316 */
11317 if (ring->id == RCS) {
11318 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
Ville Syrjäläf92a9162015-11-04 23:20:07 +020011319 intel_ring_emit_reg(ring, DERRMR);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011320 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11321 DERRMR_PIPEB_PRI_FLIP_DONE |
11322 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011323 if (IS_GEN8(dev))
Arun Siluveryf1afe242015-08-04 16:22:20 +010011324 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011325 MI_SRM_LRM_GLOBAL_GTT);
11326 else
Arun Siluveryf1afe242015-08-04 16:22:20 +010011327 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011328 MI_SRM_LRM_GLOBAL_GTT);
Ville Syrjäläf92a9162015-11-04 23:20:07 +020011329 intel_ring_emit_reg(ring, DERRMR);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011330 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011331 if (IS_GEN8(dev)) {
11332 intel_ring_emit(ring, 0);
11333 intel_ring_emit(ring, MI_NOOP);
11334 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011335 }
11336
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011337 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011338 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011339 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011340 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011341
Chris Wilson60426392015-10-10 10:44:32 +010011342 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011343 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011344}
11345
Sourab Gupta84c33a62014-06-02 16:47:17 +053011346static bool use_mmio_flip(struct intel_engine_cs *ring,
11347 struct drm_i915_gem_object *obj)
11348{
11349 /*
11350 * This is not being used for older platforms, because
11351 * non-availability of flip done interrupt forces us to use
11352 * CS flips. Older platforms derive flip done using some clever
11353 * tricks involving the flip_pending status bits and vblank irqs.
11354 * So using MMIO flips there would disrupt this mechanism.
11355 */
11356
Chris Wilson8e09bf82014-07-08 10:40:30 +010011357 if (ring == NULL)
11358 return true;
11359
Sourab Gupta84c33a62014-06-02 16:47:17 +053011360 if (INTEL_INFO(ring->dev)->gen < 5)
11361 return false;
11362
11363 if (i915.use_mmio_flip < 0)
11364 return false;
11365 else if (i915.use_mmio_flip > 0)
11366 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011367 else if (i915.enable_execlists)
11368 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011369 else if (obj->base.dma_buf &&
11370 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11371 false))
11372 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011373 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011374 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011375}
11376
Chris Wilson60426392015-10-10 10:44:32 +010011377static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011378 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011379 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011380{
11381 struct drm_device *dev = intel_crtc->base.dev;
11382 struct drm_i915_private *dev_priv = dev->dev_private;
11383 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011384 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011385 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011386
11387 ctl = I915_READ(PLANE_CTL(pipe, 0));
11388 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011389 switch (fb->modifier[0]) {
11390 case DRM_FORMAT_MOD_NONE:
11391 break;
11392 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011393 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011394 break;
11395 case I915_FORMAT_MOD_Y_TILED:
11396 ctl |= PLANE_CTL_TILED_Y;
11397 break;
11398 case I915_FORMAT_MOD_Yf_TILED:
11399 ctl |= PLANE_CTL_TILED_YF;
11400 break;
11401 default:
11402 MISSING_CASE(fb->modifier[0]);
11403 }
Damien Lespiauff944562014-11-20 14:58:16 +000011404
11405 /*
11406 * The stride is either expressed as a multiple of 64 bytes chunks for
11407 * linear buffers or in number of tiles for tiled buffers.
11408 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011409 if (intel_rotation_90_or_270(rotation)) {
11410 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011411 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011412 stride = DIV_ROUND_UP(fb->height, tile_height);
11413 } else {
11414 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011415 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11416 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011417 }
Damien Lespiauff944562014-11-20 14:58:16 +000011418
11419 /*
11420 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11421 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11422 */
11423 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11424 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11425
Chris Wilson60426392015-10-10 10:44:32 +010011426 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011427 POSTING_READ(PLANE_SURF(pipe, 0));
11428}
11429
Chris Wilson60426392015-10-10 10:44:32 +010011430static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11431 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011432{
11433 struct drm_device *dev = intel_crtc->base.dev;
11434 struct drm_i915_private *dev_priv = dev->dev_private;
11435 struct intel_framebuffer *intel_fb =
11436 to_intel_framebuffer(intel_crtc->base.primary->fb);
11437 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011438 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011439 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011440
Sourab Gupta84c33a62014-06-02 16:47:17 +053011441 dspcntr = I915_READ(reg);
11442
Damien Lespiauc5d97472014-10-25 00:11:11 +010011443 if (obj->tiling_mode != I915_TILING_NONE)
11444 dspcntr |= DISPPLANE_TILED;
11445 else
11446 dspcntr &= ~DISPPLANE_TILED;
11447
Sourab Gupta84c33a62014-06-02 16:47:17 +053011448 I915_WRITE(reg, dspcntr);
11449
Chris Wilson60426392015-10-10 10:44:32 +010011450 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011451 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011452}
11453
11454/*
11455 * XXX: This is the temporary way to update the plane registers until we get
11456 * around to using the usual plane update functions for MMIO flips
11457 */
Chris Wilson60426392015-10-10 10:44:32 +010011458static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011459{
Chris Wilson60426392015-10-10 10:44:32 +010011460 struct intel_crtc *crtc = mmio_flip->crtc;
11461 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011462
Chris Wilson60426392015-10-10 10:44:32 +010011463 spin_lock_irq(&crtc->base.dev->event_lock);
11464 work = crtc->unpin_work;
11465 spin_unlock_irq(&crtc->base.dev->event_lock);
11466 if (work == NULL)
11467 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011468
Chris Wilson60426392015-10-10 10:44:32 +010011469 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011470
Chris Wilson60426392015-10-10 10:44:32 +010011471 intel_pipe_update_start(crtc);
11472
11473 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011474 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011475 else
11476 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011477 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011478
Chris Wilson60426392015-10-10 10:44:32 +010011479 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011480}
11481
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011482static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011483{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011484 struct intel_mmio_flip *mmio_flip =
11485 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011486 struct intel_framebuffer *intel_fb =
11487 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11488 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011489
Chris Wilson60426392015-10-10 10:44:32 +010011490 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011491 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011492 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011493 false, NULL,
11494 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011495 i915_gem_request_unreference__unlocked(mmio_flip->req);
11496 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011497
Alex Goinsfd8e0582015-11-25 18:43:38 -080011498 /* For framebuffer backed by dmabuf, wait for fence */
11499 if (obj->base.dma_buf)
11500 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11501 false, false,
11502 MAX_SCHEDULE_TIMEOUT) < 0);
11503
Chris Wilson60426392015-10-10 10:44:32 +010011504 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011505 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011506}
11507
11508static int intel_queue_mmio_flip(struct drm_device *dev,
11509 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011510 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011511{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011512 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011513
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011514 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11515 if (mmio_flip == NULL)
11516 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011517
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011518 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011519 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011520 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011521 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011522
11523 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11524 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011525
Sourab Gupta84c33a62014-06-02 16:47:17 +053011526 return 0;
11527}
11528
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011529static int intel_default_queue_flip(struct drm_device *dev,
11530 struct drm_crtc *crtc,
11531 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011532 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011533 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011534 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011535{
11536 return -ENODEV;
11537}
11538
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011539static bool __intel_pageflip_stall_check(struct drm_device *dev,
11540 struct drm_crtc *crtc)
11541{
11542 struct drm_i915_private *dev_priv = dev->dev_private;
11543 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11544 struct intel_unpin_work *work = intel_crtc->unpin_work;
11545 u32 addr;
11546
11547 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11548 return true;
11549
Chris Wilson908565c2015-08-12 13:08:22 +010011550 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11551 return false;
11552
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011553 if (!work->enable_stall_check)
11554 return false;
11555
11556 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011557 if (work->flip_queued_req &&
11558 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011559 return false;
11560
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011561 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011562 }
11563
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011564 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011565 return false;
11566
11567 /* Potential stall - if we see that the flip has happened,
11568 * assume a missed interrupt. */
11569 if (INTEL_INFO(dev)->gen >= 4)
11570 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11571 else
11572 addr = I915_READ(DSPADDR(intel_crtc->plane));
11573
11574 /* There is a potential issue here with a false positive after a flip
11575 * to the same address. We could address this by checking for a
11576 * non-incrementing frame counter.
11577 */
11578 return addr == work->gtt_offset;
11579}
11580
11581void intel_check_page_flip(struct drm_device *dev, int pipe)
11582{
11583 struct drm_i915_private *dev_priv = dev->dev_private;
11584 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11585 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011586 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011587
Dave Gordon6c51d462015-03-06 15:34:26 +000011588 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011589
11590 if (crtc == NULL)
11591 return;
11592
Daniel Vetterf3260382014-09-15 14:55:23 +020011593 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011594 work = intel_crtc->unpin_work;
11595 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011596 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011597 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011598 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011599 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011600 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011601 if (work != NULL &&
11602 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11603 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011604 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011605}
11606
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011607static int intel_crtc_page_flip(struct drm_crtc *crtc,
11608 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011609 struct drm_pending_vblank_event *event,
11610 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011611{
11612 struct drm_device *dev = crtc->dev;
11613 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011614 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011615 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011616 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011617 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011618 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011619 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011620 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011621 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011622 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011623 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011624
Matt Roper2ff8fde2014-07-08 07:50:07 -070011625 /*
11626 * drm_mode_page_flip_ioctl() should already catch this, but double
11627 * check to be safe. In the future we may enable pageflipping from
11628 * a disabled primary plane.
11629 */
11630 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11631 return -EBUSY;
11632
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011633 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011634 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011635 return -EINVAL;
11636
11637 /*
11638 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11639 * Note that pitch changes could also affect these register.
11640 */
11641 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011642 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11643 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011644 return -EINVAL;
11645
Chris Wilsonf900db42014-02-20 09:26:13 +000011646 if (i915_terminally_wedged(&dev_priv->gpu_error))
11647 goto out_hang;
11648
Daniel Vetterb14c5672013-09-19 12:18:32 +020011649 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011650 if (work == NULL)
11651 return -ENOMEM;
11652
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011653 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011654 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011655 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011656 INIT_WORK(&work->work, intel_unpin_work_fn);
11657
Daniel Vetter87b6b102014-05-15 15:33:46 +020011658 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011659 if (ret)
11660 goto free_work;
11661
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011662 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011663 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011664 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011665 /* Before declaring the flip queue wedged, check if
11666 * the hardware completed the operation behind our backs.
11667 */
11668 if (__intel_pageflip_stall_check(dev, crtc)) {
11669 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11670 page_flip_completed(intel_crtc);
11671 } else {
11672 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011673 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011674
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011675 drm_crtc_vblank_put(crtc);
11676 kfree(work);
11677 return -EBUSY;
11678 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011679 }
11680 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011681 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011682
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011683 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11684 flush_workqueue(dev_priv->wq);
11685
Jesse Barnes75dfca82010-02-10 15:09:44 -080011686 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011687 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011688 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011689
Matt Roperf4510a22014-04-01 15:22:40 -070011690 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011691 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011692 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011693
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011694 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011695
Chris Wilson89ed88b2015-02-16 14:31:49 +000011696 ret = i915_mutex_lock_interruptible(dev);
11697 if (ret)
11698 goto cleanup;
11699
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011700 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011701 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011702
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011703 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030011704 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011705
Wayne Boyer666a4532015-12-09 12:29:35 -080011706 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011707 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011708 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011709 /* vlv: DISPLAY_FLIP fails to change tiling */
11710 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011711 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011712 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011713 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011714 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011715 if (ring == NULL || ring->id != RCS)
11716 ring = &dev_priv->ring[BCS];
11717 } else {
11718 ring = &dev_priv->ring[RCS];
11719 }
11720
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011721 mmio_flip = use_mmio_flip(ring, obj);
11722
11723 /* When using CS flips, we want to emit semaphores between rings.
11724 * However, when using mmio flips we will create a task to do the
11725 * synchronisation, so all we want here is to pin the framebuffer
11726 * into the display plane and skip any waits.
11727 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011728 if (!mmio_flip) {
11729 ret = i915_gem_object_sync(obj, ring, &request);
11730 if (ret)
11731 goto cleanup_pending;
11732 }
11733
Ville Syrjälä3465c582016-02-15 22:54:43 +020011734 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011735 if (ret)
11736 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011737
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011738 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11739 obj, 0);
11740 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011741
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011742 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011743 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011744 if (ret)
11745 goto cleanup_unpin;
11746
John Harrisonf06cc1b2014-11-24 18:49:37 +000011747 i915_gem_request_assign(&work->flip_queued_req,
11748 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011749 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011750 if (!request) {
Dave Gordon26827082016-01-19 19:02:53 +000011751 request = i915_gem_request_alloc(ring, NULL);
11752 if (IS_ERR(request)) {
11753 ret = PTR_ERR(request);
John Harrison6258fbe2015-05-29 17:43:48 +010011754 goto cleanup_unpin;
Dave Gordon26827082016-01-19 19:02:53 +000011755 }
John Harrison6258fbe2015-05-29 17:43:48 +010011756 }
11757
11758 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011759 page_flip_flags);
11760 if (ret)
11761 goto cleanup_unpin;
11762
John Harrison6258fbe2015-05-29 17:43:48 +010011763 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011764 }
11765
John Harrison91af1272015-06-18 13:14:56 +010011766 if (request)
John Harrison75289872015-05-29 17:43:49 +010011767 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011768
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011769 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011770 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011771
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011772 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011773 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011774 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011775
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011776 intel_frontbuffer_flip_prepare(dev,
11777 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011778
Jesse Barnese5510fa2010-07-01 16:48:37 -070011779 trace_i915_flip_request(intel_crtc->plane, obj);
11780
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011781 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011782
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011783cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011784 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011785cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011786 if (!IS_ERR_OR_NULL(request))
John Harrison91af1272015-06-18 13:14:56 +010011787 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011788 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011789 mutex_unlock(&dev->struct_mutex);
11790cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011791 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011792 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011793
Chris Wilson89ed88b2015-02-16 14:31:49 +000011794 drm_gem_object_unreference_unlocked(&obj->base);
11795 drm_framebuffer_unreference(work->old_fb);
11796
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011797 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011798 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011799 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011800
Daniel Vetter87b6b102014-05-15 15:33:46 +020011801 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011802free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011803 kfree(work);
11804
Chris Wilsonf900db42014-02-20 09:26:13 +000011805 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011806 struct drm_atomic_state *state;
11807 struct drm_plane_state *plane_state;
11808
Chris Wilsonf900db42014-02-20 09:26:13 +000011809out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011810 state = drm_atomic_state_alloc(dev);
11811 if (!state)
11812 return -ENOMEM;
11813 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11814
11815retry:
11816 plane_state = drm_atomic_get_plane_state(state, primary);
11817 ret = PTR_ERR_OR_ZERO(plane_state);
11818 if (!ret) {
11819 drm_atomic_set_fb_for_plane(plane_state, fb);
11820
11821 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11822 if (!ret)
11823 ret = drm_atomic_commit(state);
11824 }
11825
11826 if (ret == -EDEADLK) {
11827 drm_modeset_backoff(state->acquire_ctx);
11828 drm_atomic_state_clear(state);
11829 goto retry;
11830 }
11831
11832 if (ret)
11833 drm_atomic_state_free(state);
11834
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011835 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011836 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011837 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011838 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011839 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011840 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011841 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011842}
11843
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011844
11845/**
11846 * intel_wm_need_update - Check whether watermarks need updating
11847 * @plane: drm plane
11848 * @state: new plane state
11849 *
11850 * Check current plane state versus the new one to determine whether
11851 * watermarks need to be recalculated.
11852 *
11853 * Returns true or false.
11854 */
11855static bool intel_wm_need_update(struct drm_plane *plane,
11856 struct drm_plane_state *state)
11857{
Matt Roperd21fbe82015-09-24 15:53:12 -070011858 struct intel_plane_state *new = to_intel_plane_state(state);
11859 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11860
11861 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011862 if (new->visible != cur->visible)
11863 return true;
11864
11865 if (!cur->base.fb || !new->base.fb)
11866 return false;
11867
11868 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11869 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011870 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11871 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11872 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11873 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011874 return true;
11875
11876 return false;
11877}
11878
Matt Roperd21fbe82015-09-24 15:53:12 -070011879static bool needs_scaling(struct intel_plane_state *state)
11880{
11881 int src_w = drm_rect_width(&state->src) >> 16;
11882 int src_h = drm_rect_height(&state->src) >> 16;
11883 int dst_w = drm_rect_width(&state->dst);
11884 int dst_h = drm_rect_height(&state->dst);
11885
11886 return (src_w != dst_w || src_h != dst_h);
11887}
11888
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011889int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11890 struct drm_plane_state *plane_state)
11891{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011892 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011893 struct drm_crtc *crtc = crtc_state->crtc;
11894 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11895 struct drm_plane *plane = plane_state->plane;
11896 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011897 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011898 struct intel_plane_state *old_plane_state =
11899 to_intel_plane_state(plane->state);
11900 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011901 bool mode_changed = needs_modeset(crtc_state);
11902 bool was_crtc_enabled = crtc->state->active;
11903 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011904 bool turn_off, turn_on, visible, was_visible;
11905 struct drm_framebuffer *fb = plane_state->fb;
11906
11907 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11908 plane->type != DRM_PLANE_TYPE_CURSOR) {
11909 ret = skl_update_scaler_plane(
11910 to_intel_crtc_state(crtc_state),
11911 to_intel_plane_state(plane_state));
11912 if (ret)
11913 return ret;
11914 }
11915
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011916 was_visible = old_plane_state->visible;
11917 visible = to_intel_plane_state(plane_state)->visible;
11918
11919 if (!was_crtc_enabled && WARN_ON(was_visible))
11920 was_visible = false;
11921
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011922 /*
11923 * Visibility is calculated as if the crtc was on, but
11924 * after scaler setup everything depends on it being off
11925 * when the crtc isn't active.
11926 */
11927 if (!is_crtc_enabled)
11928 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011929
11930 if (!was_visible && !visible)
11931 return 0;
11932
Maarten Lankhorste8861672016-02-24 11:24:26 +010011933 if (fb != old_plane_state->base.fb)
11934 pipe_config->fb_changed = true;
11935
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011936 turn_off = was_visible && (!visible || mode_changed);
11937 turn_on = visible && (!was_visible || mode_changed);
11938
11939 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11940 plane->base.id, fb ? fb->base.id : -1);
11941
11942 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11943 plane->base.id, was_visible, visible,
11944 turn_off, turn_on, mode_changed);
11945
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011946 if (turn_on || turn_off) {
11947 pipe_config->wm_changed = true;
11948
Ville Syrjälä852eb002015-06-24 22:00:07 +030011949 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011950 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011951 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011952 } else if (intel_wm_need_update(plane, plane_state)) {
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011953 pipe_config->wm_changed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011954 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011955
Matt Ropered4a6a72016-02-23 17:20:13 -080011956 /* Pre-gen9 platforms need two-step watermark updates */
11957 if (pipe_config->wm_changed && INTEL_INFO(dev)->gen < 9 &&
11958 dev_priv->display.optimize_watermarks)
11959 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11960
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011961 if (visible || was_visible)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011962 intel_crtc->atomic.fb_bits |=
11963 to_intel_plane(plane)->frontbuffer_bit;
11964
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011965 switch (plane->type) {
11966 case DRM_PLANE_TYPE_PRIMARY:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011967 intel_crtc->atomic.post_enable_primary = turn_on;
Paulo Zanonifcf38d12016-01-21 18:07:17 -020011968 intel_crtc->atomic.update_fbc = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011969
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011970 break;
11971 case DRM_PLANE_TYPE_CURSOR:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011972 break;
11973 case DRM_PLANE_TYPE_OVERLAY:
Matt Roperd21fbe82015-09-24 15:53:12 -070011974 /*
11975 * WaCxSRDisabledForSpriteScaling:ivb
11976 *
11977 * cstate->update_wm was already set above, so this flag will
11978 * take effect when we commit and program watermarks.
11979 */
11980 if (IS_IVYBRIDGE(dev) &&
11981 needs_scaling(to_intel_plane_state(plane_state)) &&
Maarten Lankhorste8861672016-02-24 11:24:26 +010011982 !needs_scaling(old_plane_state))
11983 pipe_config->disable_lp_wm = true;
Matt Roperd21fbe82015-09-24 15:53:12 -070011984
11985 break;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011986 }
11987 return 0;
11988}
11989
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011990static bool encoders_cloneable(const struct intel_encoder *a,
11991 const struct intel_encoder *b)
11992{
11993 /* masks could be asymmetric, so check both ways */
11994 return a == b || (a->cloneable & (1 << b->type) &&
11995 b->cloneable & (1 << a->type));
11996}
11997
11998static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11999 struct intel_crtc *crtc,
12000 struct intel_encoder *encoder)
12001{
12002 struct intel_encoder *source_encoder;
12003 struct drm_connector *connector;
12004 struct drm_connector_state *connector_state;
12005 int i;
12006
12007 for_each_connector_in_state(state, connector, connector_state, i) {
12008 if (connector_state->crtc != &crtc->base)
12009 continue;
12010
12011 source_encoder =
12012 to_intel_encoder(connector_state->best_encoder);
12013 if (!encoders_cloneable(encoder, source_encoder))
12014 return false;
12015 }
12016
12017 return true;
12018}
12019
12020static bool check_encoder_cloning(struct drm_atomic_state *state,
12021 struct intel_crtc *crtc)
12022{
12023 struct intel_encoder *encoder;
12024 struct drm_connector *connector;
12025 struct drm_connector_state *connector_state;
12026 int i;
12027
12028 for_each_connector_in_state(state, connector, connector_state, i) {
12029 if (connector_state->crtc != &crtc->base)
12030 continue;
12031
12032 encoder = to_intel_encoder(connector_state->best_encoder);
12033 if (!check_single_encoder_cloning(state, crtc, encoder))
12034 return false;
12035 }
12036
12037 return true;
12038}
12039
12040static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12041 struct drm_crtc_state *crtc_state)
12042{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012043 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012044 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012045 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012046 struct intel_crtc_state *pipe_config =
12047 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012048 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012049 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012050 bool mode_changed = needs_modeset(crtc_state);
12051
12052 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12053 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12054 return -EINVAL;
12055 }
12056
Ville Syrjälä852eb002015-06-24 22:00:07 +030012057 if (mode_changed && !crtc_state->active)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012058 pipe_config->wm_changed = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012059
Maarten Lankhorstad421372015-06-15 12:33:42 +020012060 if (mode_changed && crtc_state->enable &&
12061 dev_priv->display.crtc_compute_clock &&
12062 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
12063 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12064 pipe_config);
12065 if (ret)
12066 return ret;
12067 }
12068
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012069 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012070 if (dev_priv->display.compute_pipe_wm) {
12071 ret = dev_priv->display.compute_pipe_wm(intel_crtc, state);
Matt Ropered4a6a72016-02-23 17:20:13 -080012072 if (ret) {
12073 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012074 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012075 }
12076 }
12077
12078 if (dev_priv->display.compute_intermediate_wm &&
12079 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12080 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12081 return 0;
12082
12083 /*
12084 * Calculate 'intermediate' watermarks that satisfy both the
12085 * old state and the new state. We can program these
12086 * immediately.
12087 */
12088 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12089 intel_crtc,
12090 pipe_config);
12091 if (ret) {
12092 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12093 return ret;
12094 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070012095 }
12096
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012097 if (INTEL_INFO(dev)->gen >= 9) {
12098 if (mode_changed)
12099 ret = skl_update_scaler_crtc(pipe_config);
12100
12101 if (!ret)
12102 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12103 pipe_config);
12104 }
12105
12106 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012107}
12108
Jani Nikula65b38e02015-04-13 11:26:56 +030012109static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012110 .mode_set_base_atomic = intel_pipe_set_base_atomic,
12111 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080012112 .atomic_begin = intel_begin_crtc_commit,
12113 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012114 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012115};
12116
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012117static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12118{
12119 struct intel_connector *connector;
12120
12121 for_each_intel_connector(dev, connector) {
12122 if (connector->base.encoder) {
12123 connector->base.state->best_encoder =
12124 connector->base.encoder;
12125 connector->base.state->crtc =
12126 connector->base.encoder->crtc;
12127 } else {
12128 connector->base.state->best_encoder = NULL;
12129 connector->base.state->crtc = NULL;
12130 }
12131 }
12132}
12133
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012134static void
Robin Schroereba905b2014-05-18 02:24:50 +020012135connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012136 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012137{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012138 int bpp = pipe_config->pipe_bpp;
12139
12140 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12141 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012142 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012143
12144 /* Don't use an invalid EDID bpc value */
12145 if (connector->base.display_info.bpc &&
12146 connector->base.display_info.bpc * 3 < bpp) {
12147 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12148 bpp, connector->base.display_info.bpc*3);
12149 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12150 }
12151
Jani Nikula013dd9e2016-01-13 16:35:20 +020012152 /* Clamp bpp to default limit on screens without EDID 1.4 */
12153 if (connector->base.display_info.bpc == 0) {
12154 int type = connector->base.connector_type;
12155 int clamp_bpp = 24;
12156
12157 /* Fall back to 18 bpp when DP sink capability is unknown. */
12158 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12159 type == DRM_MODE_CONNECTOR_eDP)
12160 clamp_bpp = 18;
12161
12162 if (bpp > clamp_bpp) {
12163 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12164 bpp, clamp_bpp);
12165 pipe_config->pipe_bpp = clamp_bpp;
12166 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012167 }
12168}
12169
12170static int
12171compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012172 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012173{
12174 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012175 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012176 struct drm_connector *connector;
12177 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012178 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012179
Wayne Boyer666a4532015-12-09 12:29:35 -080012180 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012181 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012182 else if (INTEL_INFO(dev)->gen >= 5)
12183 bpp = 12*3;
12184 else
12185 bpp = 8*3;
12186
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012187
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012188 pipe_config->pipe_bpp = bpp;
12189
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012190 state = pipe_config->base.state;
12191
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012192 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012193 for_each_connector_in_state(state, connector, connector_state, i) {
12194 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012195 continue;
12196
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012197 connected_sink_compute_bpp(to_intel_connector(connector),
12198 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012199 }
12200
12201 return bpp;
12202}
12203
Daniel Vetter644db712013-09-19 14:53:58 +020012204static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12205{
12206 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12207 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012208 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012209 mode->crtc_hdisplay, mode->crtc_hsync_start,
12210 mode->crtc_hsync_end, mode->crtc_htotal,
12211 mode->crtc_vdisplay, mode->crtc_vsync_start,
12212 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12213}
12214
Daniel Vetterc0b03412013-05-28 12:05:54 +020012215static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012216 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012217 const char *context)
12218{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012219 struct drm_device *dev = crtc->base.dev;
12220 struct drm_plane *plane;
12221 struct intel_plane *intel_plane;
12222 struct intel_plane_state *state;
12223 struct drm_framebuffer *fb;
12224
12225 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12226 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012227
12228 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
12229 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12230 pipe_config->pipe_bpp, pipe_config->dither);
12231 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12232 pipe_config->has_pch_encoder,
12233 pipe_config->fdi_lanes,
12234 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12235 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12236 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012237 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 +030012238 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012239 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012240 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12241 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12242 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012243
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012244 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 -070012245 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012246 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012247 pipe_config->dp_m2_n2.gmch_m,
12248 pipe_config->dp_m2_n2.gmch_n,
12249 pipe_config->dp_m2_n2.link_m,
12250 pipe_config->dp_m2_n2.link_n,
12251 pipe_config->dp_m2_n2.tu);
12252
Daniel Vetter55072d12014-11-20 16:10:28 +010012253 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12254 pipe_config->has_audio,
12255 pipe_config->has_infoframe);
12256
Daniel Vetterc0b03412013-05-28 12:05:54 +020012257 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012258 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012259 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012260 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12261 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012262 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012263 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12264 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012265 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12266 crtc->num_scalers,
12267 pipe_config->scaler_state.scaler_users,
12268 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012269 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12270 pipe_config->gmch_pfit.control,
12271 pipe_config->gmch_pfit.pgm_ratios,
12272 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012273 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012274 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012275 pipe_config->pch_pfit.size,
12276 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012277 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012278 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012279
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012280 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012281 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012282 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012283 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012284 pipe_config->ddi_pll_sel,
12285 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012286 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012287 pipe_config->dpll_hw_state.pll0,
12288 pipe_config->dpll_hw_state.pll1,
12289 pipe_config->dpll_hw_state.pll2,
12290 pipe_config->dpll_hw_state.pll3,
12291 pipe_config->dpll_hw_state.pll6,
12292 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012293 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012294 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012295 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012296 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012297 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12298 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12299 pipe_config->ddi_pll_sel,
12300 pipe_config->dpll_hw_state.ctrl1,
12301 pipe_config->dpll_hw_state.cfgcr1,
12302 pipe_config->dpll_hw_state.cfgcr2);
12303 } else if (HAS_DDI(dev)) {
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012304 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012305 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012306 pipe_config->dpll_hw_state.wrpll,
12307 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012308 } else {
12309 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12310 "fp0: 0x%x, fp1: 0x%x\n",
12311 pipe_config->dpll_hw_state.dpll,
12312 pipe_config->dpll_hw_state.dpll_md,
12313 pipe_config->dpll_hw_state.fp0,
12314 pipe_config->dpll_hw_state.fp1);
12315 }
12316
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012317 DRM_DEBUG_KMS("planes on this crtc\n");
12318 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12319 intel_plane = to_intel_plane(plane);
12320 if (intel_plane->pipe != crtc->pipe)
12321 continue;
12322
12323 state = to_intel_plane_state(plane->state);
12324 fb = state->base.fb;
12325 if (!fb) {
12326 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12327 "disabled, scaler_id = %d\n",
12328 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12329 plane->base.id, intel_plane->pipe,
12330 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12331 drm_plane_index(plane), state->scaler_id);
12332 continue;
12333 }
12334
12335 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12336 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12337 plane->base.id, intel_plane->pipe,
12338 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12339 drm_plane_index(plane));
12340 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12341 fb->base.id, fb->width, fb->height, fb->pixel_format);
12342 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12343 state->scaler_id,
12344 state->src.x1 >> 16, state->src.y1 >> 16,
12345 drm_rect_width(&state->src) >> 16,
12346 drm_rect_height(&state->src) >> 16,
12347 state->dst.x1, state->dst.y1,
12348 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12349 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012350}
12351
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012352static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012353{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012354 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012355 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012356 unsigned int used_ports = 0;
12357
12358 /*
12359 * Walk the connector list instead of the encoder
12360 * list to detect the problem on ddi platforms
12361 * where there's just one encoder per digital port.
12362 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012363 drm_for_each_connector(connector, dev) {
12364 struct drm_connector_state *connector_state;
12365 struct intel_encoder *encoder;
12366
12367 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12368 if (!connector_state)
12369 connector_state = connector->state;
12370
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012371 if (!connector_state->best_encoder)
12372 continue;
12373
12374 encoder = to_intel_encoder(connector_state->best_encoder);
12375
12376 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012377
12378 switch (encoder->type) {
12379 unsigned int port_mask;
12380 case INTEL_OUTPUT_UNKNOWN:
12381 if (WARN_ON(!HAS_DDI(dev)))
12382 break;
12383 case INTEL_OUTPUT_DISPLAYPORT:
12384 case INTEL_OUTPUT_HDMI:
12385 case INTEL_OUTPUT_EDP:
12386 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12387
12388 /* the same port mustn't appear more than once */
12389 if (used_ports & port_mask)
12390 return false;
12391
12392 used_ports |= port_mask;
12393 default:
12394 break;
12395 }
12396 }
12397
12398 return true;
12399}
12400
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012401static void
12402clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12403{
12404 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012405 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012406 struct intel_dpll_hw_state dpll_hw_state;
12407 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012408 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012409 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012410
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012411 /* FIXME: before the switch to atomic started, a new pipe_config was
12412 * kzalloc'd. Code that depends on any field being zero should be
12413 * fixed, so that the crtc_state can be safely duplicated. For now,
12414 * only fields that are know to not cause problems are preserved. */
12415
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012416 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012417 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012418 shared_dpll = crtc_state->shared_dpll;
12419 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012420 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012421 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012422
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012423 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012424
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012425 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012426 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012427 crtc_state->shared_dpll = shared_dpll;
12428 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012429 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012430 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012431}
12432
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012433static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012434intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012435 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012436{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012437 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012438 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012439 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012440 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012441 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012442 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012443 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012444
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012445 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012446
Daniel Vettere143a212013-07-04 12:01:15 +020012447 pipe_config->cpu_transcoder =
12448 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012449
Imre Deak2960bc92013-07-30 13:36:32 +030012450 /*
12451 * Sanitize sync polarity flags based on requested ones. If neither
12452 * positive or negative polarity is requested, treat this as meaning
12453 * negative polarity.
12454 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012455 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012456 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012457 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012458
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012459 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012460 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012461 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012462
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012463 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12464 pipe_config);
12465 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012466 goto fail;
12467
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012468 /*
12469 * Determine the real pipe dimensions. Note that stereo modes can
12470 * increase the actual pipe size due to the frame doubling and
12471 * insertion of additional space for blanks between the frame. This
12472 * is stored in the crtc timings. We use the requested mode to do this
12473 * computation to clearly distinguish it from the adjusted mode, which
12474 * can be changed by the connectors in the below retry loop.
12475 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012476 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012477 &pipe_config->pipe_src_w,
12478 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012479
Daniel Vettere29c22c2013-02-21 00:00:16 +010012480encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012481 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012482 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012483 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012484
Daniel Vetter135c81b2013-07-21 21:37:09 +020012485 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012486 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12487 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012488
Daniel Vetter7758a112012-07-08 19:40:39 +020012489 /* Pass our mode to the connectors and the CRTC to give them a chance to
12490 * adjust it according to limitations or connector properties, and also
12491 * a chance to reject the mode entirely.
12492 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012493 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012494 if (connector_state->crtc != crtc)
12495 continue;
12496
12497 encoder = to_intel_encoder(connector_state->best_encoder);
12498
Daniel Vetterefea6e82013-07-21 21:36:59 +020012499 if (!(encoder->compute_config(encoder, pipe_config))) {
12500 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012501 goto fail;
12502 }
12503 }
12504
Daniel Vetterff9a6752013-06-01 17:16:21 +020012505 /* Set default port clock if not overwritten by the encoder. Needs to be
12506 * done afterwards in case the encoder adjusts the mode. */
12507 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012508 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012509 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012510
Daniel Vettera43f6e02013-06-07 23:10:32 +020012511 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012512 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012513 DRM_DEBUG_KMS("CRTC fixup failed\n");
12514 goto fail;
12515 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012516
12517 if (ret == RETRY) {
12518 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12519 ret = -EINVAL;
12520 goto fail;
12521 }
12522
12523 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12524 retry = false;
12525 goto encoder_retry;
12526 }
12527
Daniel Vettere8fa4272015-08-12 11:43:34 +020012528 /* Dithering seems to not pass-through bits correctly when it should, so
12529 * only enable it on 6bpc panels. */
12530 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012531 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012532 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012533
Daniel Vetter7758a112012-07-08 19:40:39 +020012534fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012535 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012536}
12537
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012538static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012539intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012540{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012541 struct drm_crtc *crtc;
12542 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012543 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012544
Ville Syrjälä76688512014-01-10 11:28:06 +020012545 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012546 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012547 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012548
12549 /* Update hwmode for vblank functions */
12550 if (crtc->state->active)
12551 crtc->hwmode = crtc->state->adjusted_mode;
12552 else
12553 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012554
12555 /*
12556 * Update legacy state to satisfy fbc code. This can
12557 * be removed when fbc uses the atomic state.
12558 */
12559 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12560 struct drm_plane_state *plane_state = crtc->primary->state;
12561
12562 crtc->primary->fb = plane_state->fb;
12563 crtc->x = plane_state->src_x >> 16;
12564 crtc->y = plane_state->src_y >> 16;
12565 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012566 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012567}
12568
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012569static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012570{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012571 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012572
12573 if (clock1 == clock2)
12574 return true;
12575
12576 if (!clock1 || !clock2)
12577 return false;
12578
12579 diff = abs(clock1 - clock2);
12580
12581 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12582 return true;
12583
12584 return false;
12585}
12586
Daniel Vetter25c5b262012-07-08 22:08:04 +020012587#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12588 list_for_each_entry((intel_crtc), \
12589 &(dev)->mode_config.crtc_list, \
12590 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012591 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012592
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012593static bool
12594intel_compare_m_n(unsigned int m, unsigned int n,
12595 unsigned int m2, unsigned int n2,
12596 bool exact)
12597{
12598 if (m == m2 && n == n2)
12599 return true;
12600
12601 if (exact || !m || !n || !m2 || !n2)
12602 return false;
12603
12604 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12605
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012606 if (n > n2) {
12607 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012608 m2 <<= 1;
12609 n2 <<= 1;
12610 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012611 } else if (n < n2) {
12612 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012613 m <<= 1;
12614 n <<= 1;
12615 }
12616 }
12617
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012618 if (n != n2)
12619 return false;
12620
12621 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012622}
12623
12624static bool
12625intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12626 struct intel_link_m_n *m2_n2,
12627 bool adjust)
12628{
12629 if (m_n->tu == m2_n2->tu &&
12630 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12631 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12632 intel_compare_m_n(m_n->link_m, m_n->link_n,
12633 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12634 if (adjust)
12635 *m2_n2 = *m_n;
12636
12637 return true;
12638 }
12639
12640 return false;
12641}
12642
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012643static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012644intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012645 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012646 struct intel_crtc_state *pipe_config,
12647 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012648{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012649 bool ret = true;
12650
12651#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12652 do { \
12653 if (!adjust) \
12654 DRM_ERROR(fmt, ##__VA_ARGS__); \
12655 else \
12656 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12657 } while (0)
12658
Daniel Vetter66e985c2013-06-05 13:34:20 +020012659#define PIPE_CONF_CHECK_X(name) \
12660 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012661 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012662 "(expected 0x%08x, found 0x%08x)\n", \
12663 current_config->name, \
12664 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012665 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012666 }
12667
Daniel Vetter08a24032013-04-19 11:25:34 +020012668#define PIPE_CONF_CHECK_I(name) \
12669 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012670 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012671 "(expected %i, found %i)\n", \
12672 current_config->name, \
12673 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012674 ret = false; \
12675 }
12676
12677#define PIPE_CONF_CHECK_M_N(name) \
12678 if (!intel_compare_link_m_n(&current_config->name, \
12679 &pipe_config->name,\
12680 adjust)) { \
12681 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12682 "(expected tu %i gmch %i/%i link %i/%i, " \
12683 "found tu %i, gmch %i/%i link %i/%i)\n", \
12684 current_config->name.tu, \
12685 current_config->name.gmch_m, \
12686 current_config->name.gmch_n, \
12687 current_config->name.link_m, \
12688 current_config->name.link_n, \
12689 pipe_config->name.tu, \
12690 pipe_config->name.gmch_m, \
12691 pipe_config->name.gmch_n, \
12692 pipe_config->name.link_m, \
12693 pipe_config->name.link_n); \
12694 ret = false; \
12695 }
12696
12697#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12698 if (!intel_compare_link_m_n(&current_config->name, \
12699 &pipe_config->name, adjust) && \
12700 !intel_compare_link_m_n(&current_config->alt_name, \
12701 &pipe_config->name, adjust)) { \
12702 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12703 "(expected tu %i gmch %i/%i link %i/%i, " \
12704 "or tu %i gmch %i/%i link %i/%i, " \
12705 "found tu %i, gmch %i/%i link %i/%i)\n", \
12706 current_config->name.tu, \
12707 current_config->name.gmch_m, \
12708 current_config->name.gmch_n, \
12709 current_config->name.link_m, \
12710 current_config->name.link_n, \
12711 current_config->alt_name.tu, \
12712 current_config->alt_name.gmch_m, \
12713 current_config->alt_name.gmch_n, \
12714 current_config->alt_name.link_m, \
12715 current_config->alt_name.link_n, \
12716 pipe_config->name.tu, \
12717 pipe_config->name.gmch_m, \
12718 pipe_config->name.gmch_n, \
12719 pipe_config->name.link_m, \
12720 pipe_config->name.link_n); \
12721 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012722 }
12723
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012724/* This is required for BDW+ where there is only one set of registers for
12725 * switching between high and low RR.
12726 * This macro can be used whenever a comparison has to be made between one
12727 * hw state and multiple sw state variables.
12728 */
12729#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12730 if ((current_config->name != pipe_config->name) && \
12731 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012732 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012733 "(expected %i or %i, found %i)\n", \
12734 current_config->name, \
12735 current_config->alt_name, \
12736 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012737 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012738 }
12739
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012740#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12741 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012742 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012743 "(expected %i, found %i)\n", \
12744 current_config->name & (mask), \
12745 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012746 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012747 }
12748
Ville Syrjälä5e550652013-09-06 23:29:07 +030012749#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12750 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012751 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012752 "(expected %i, found %i)\n", \
12753 current_config->name, \
12754 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012755 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012756 }
12757
Daniel Vetterbb760062013-06-06 14:55:52 +020012758#define PIPE_CONF_QUIRK(quirk) \
12759 ((current_config->quirks | pipe_config->quirks) & (quirk))
12760
Daniel Vettereccb1402013-05-22 00:50:22 +020012761 PIPE_CONF_CHECK_I(cpu_transcoder);
12762
Daniel Vetter08a24032013-04-19 11:25:34 +020012763 PIPE_CONF_CHECK_I(has_pch_encoder);
12764 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012765 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012766
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012767 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012768 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012769
12770 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012771 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012772
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012773 if (current_config->has_drrs)
12774 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12775 } else
12776 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012777
Jani Nikulaa65347b2015-11-27 12:21:46 +020012778 PIPE_CONF_CHECK_I(has_dsi_encoder);
12779
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012780 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12781 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12782 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12783 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12784 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12785 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012786
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012787 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12788 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12789 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12790 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12791 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12792 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012793
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012794 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012795 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012796 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012797 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012798 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012799 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012800
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012801 PIPE_CONF_CHECK_I(has_audio);
12802
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012803 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012804 DRM_MODE_FLAG_INTERLACE);
12805
Daniel Vetterbb760062013-06-06 14:55:52 +020012806 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012807 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012808 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012809 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012810 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012811 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012812 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012813 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012814 DRM_MODE_FLAG_NVSYNC);
12815 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012816
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012817 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012818 /* pfit ratios are autocomputed by the hw on gen4+ */
12819 if (INTEL_INFO(dev)->gen < 4)
12820 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012821 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012822
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012823 if (!adjust) {
12824 PIPE_CONF_CHECK_I(pipe_src_w);
12825 PIPE_CONF_CHECK_I(pipe_src_h);
12826
12827 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12828 if (current_config->pch_pfit.enabled) {
12829 PIPE_CONF_CHECK_X(pch_pfit.pos);
12830 PIPE_CONF_CHECK_X(pch_pfit.size);
12831 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012832
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012833 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12834 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012835
Jesse Barnese59150d2014-01-07 13:30:45 -080012836 /* BDW+ don't expose a synchronous way to read the state */
12837 if (IS_HASWELL(dev))
12838 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012839
Ville Syrjälä282740f2013-09-04 18:30:03 +030012840 PIPE_CONF_CHECK_I(double_wide);
12841
Daniel Vetter26804af2014-06-25 22:01:55 +030012842 PIPE_CONF_CHECK_X(ddi_pll_sel);
12843
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012844 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012845 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012846 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012847 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12848 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012849 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012850 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012851 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12852 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12853 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012854
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012855 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12856 PIPE_CONF_CHECK_I(pipe_bpp);
12857
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012858 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012859 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012860
Daniel Vetter66e985c2013-06-05 13:34:20 +020012861#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012862#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012863#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012864#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012865#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012866#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012867#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012868
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012869 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012870}
12871
Damien Lespiau08db6652014-11-04 17:06:52 +000012872static void check_wm_state(struct drm_device *dev)
12873{
12874 struct drm_i915_private *dev_priv = dev->dev_private;
12875 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12876 struct intel_crtc *intel_crtc;
12877 int plane;
12878
12879 if (INTEL_INFO(dev)->gen < 9)
12880 return;
12881
12882 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12883 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12884
12885 for_each_intel_crtc(dev, intel_crtc) {
12886 struct skl_ddb_entry *hw_entry, *sw_entry;
12887 const enum pipe pipe = intel_crtc->pipe;
12888
12889 if (!intel_crtc->active)
12890 continue;
12891
12892 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012893 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012894 hw_entry = &hw_ddb.plane[pipe][plane];
12895 sw_entry = &sw_ddb->plane[pipe][plane];
12896
12897 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12898 continue;
12899
12900 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12901 "(expected (%u,%u), found (%u,%u))\n",
12902 pipe_name(pipe), plane + 1,
12903 sw_entry->start, sw_entry->end,
12904 hw_entry->start, hw_entry->end);
12905 }
12906
12907 /* cursor */
Matt Roper4969d332015-09-24 15:53:10 -070012908 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12909 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
Damien Lespiau08db6652014-11-04 17:06:52 +000012910
12911 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12912 continue;
12913
12914 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12915 "(expected (%u,%u), found (%u,%u))\n",
12916 pipe_name(pipe),
12917 sw_entry->start, sw_entry->end,
12918 hw_entry->start, hw_entry->end);
12919 }
12920}
12921
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012922static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012923check_connector_state(struct drm_device *dev,
12924 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012925{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012926 struct drm_connector_state *old_conn_state;
12927 struct drm_connector *connector;
12928 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012929
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012930 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12931 struct drm_encoder *encoder = connector->encoder;
12932 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012933
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012934 /* This also checks the encoder/connector hw state with the
12935 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012936 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012937
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012938 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012939 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012940 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012941}
12942
12943static void
12944check_encoder_state(struct drm_device *dev)
12945{
12946 struct intel_encoder *encoder;
12947 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012948
Damien Lespiaub2784e12014-08-05 11:29:37 +010012949 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012950 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012951 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012952
12953 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12954 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012955 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012956
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012957 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012958 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012959 continue;
12960 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012961
12962 I915_STATE_WARN(connector->base.state->crtc !=
12963 encoder->base.crtc,
12964 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012965 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012966
Rob Clarke2c719b2014-12-15 13:56:32 -050012967 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012968 "encoder's enabled state mismatch "
12969 "(expected %i, found %i)\n",
12970 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012971
12972 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012973 bool active;
12974
12975 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012976 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012977 "encoder detached but still enabled on pipe %c.\n",
12978 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012979 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012980 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012981}
12982
12983static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012984check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012985{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012986 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012987 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012988 struct drm_crtc_state *old_crtc_state;
12989 struct drm_crtc *crtc;
12990 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012991
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012992 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12994 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020012995 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012996
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012997 if (!needs_modeset(crtc->state) &&
12998 !to_intel_crtc_state(crtc->state)->update_pipe)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012999 continue;
13000
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013001 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
13002 pipe_config = to_intel_crtc_state(old_crtc_state);
13003 memset(pipe_config, 0, sizeof(*pipe_config));
13004 pipe_config->base.crtc = crtc;
13005 pipe_config->base.state = old_state;
13006
13007 DRM_DEBUG_KMS("[CRTC:%d]\n",
13008 crtc->base.id);
13009
13010 active = dev_priv->display.get_pipe_config(intel_crtc,
13011 pipe_config);
13012
13013 /* hw state is inconsistent with the pipe quirk */
13014 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13015 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13016 active = crtc->state->active;
13017
13018 I915_STATE_WARN(crtc->state->active != active,
13019 "crtc active state doesn't match with hw state "
13020 "(expected %i, found %i)\n", crtc->state->active, active);
13021
13022 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
13023 "transitional active state does not match atomic hw state "
13024 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
13025
13026 for_each_encoder_on_crtc(dev, crtc, encoder) {
13027 enum pipe pipe;
13028
13029 active = encoder->get_hw_state(encoder, &pipe);
13030 I915_STATE_WARN(active != crtc->state->active,
13031 "[ENCODER:%i] active %i with crtc active %i\n",
13032 encoder->base.base.id, active, crtc->state->active);
13033
13034 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13035 "Encoder connected to wrong pipe %c\n",
13036 pipe_name(pipe));
13037
13038 if (active)
13039 encoder->get_config(encoder, pipe_config);
13040 }
13041
13042 if (!crtc->state->active)
13043 continue;
13044
13045 sw_config = to_intel_crtc_state(crtc->state);
13046 if (!intel_pipe_config_compare(dev, sw_config,
13047 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050013048 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013049 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020013050 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013051 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020013052 "[sw state]");
13053 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013054 }
13055}
13056
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013057static void
13058check_shared_dpll_state(struct drm_device *dev)
13059{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013060 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013061 struct intel_crtc *crtc;
13062 struct intel_dpll_hw_state dpll_hw_state;
13063 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013064
13065 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13066 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13067 int enabled_crtcs = 0, active_crtcs = 0;
13068 bool active;
13069
13070 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13071
13072 DRM_DEBUG_KMS("%s\n", pll->name);
13073
13074 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
13075
Rob Clarke2c719b2014-12-15 13:56:32 -050013076 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020013077 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013078 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050013079 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020013080 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050013081 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020013082 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050013083 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020013084 "pll on state mismatch (expected %i, found %i)\n",
13085 pll->on, active);
13086
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013087 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080013088 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020013089 enabled_crtcs++;
13090 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13091 active_crtcs++;
13092 }
Rob Clarke2c719b2014-12-15 13:56:32 -050013093 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020013094 "pll active crtcs mismatch (expected %i, found %i)\n",
13095 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050013096 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020013097 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013098 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013099
Rob Clarke2c719b2014-12-15 13:56:32 -050013100 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020013101 sizeof(dpll_hw_state)),
13102 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020013103 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013104}
13105
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013106static void
13107intel_modeset_check_state(struct drm_device *dev,
13108 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013109{
Damien Lespiau08db6652014-11-04 17:06:52 +000013110 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013111 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013112 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013113 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013114 check_shared_dpll_state(dev);
13115}
13116
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020013117void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030013118 int dotclock)
13119{
13120 /*
13121 * FDI already provided one idea for the dotclock.
13122 * Yell if the encoder disagrees.
13123 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013124 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030013125 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013126 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030013127}
13128
Ville Syrjälä80715b22014-05-15 20:23:23 +030013129static void update_scanline_offset(struct intel_crtc *crtc)
13130{
13131 struct drm_device *dev = crtc->base.dev;
13132
13133 /*
13134 * The scanline counter increments at the leading edge of hsync.
13135 *
13136 * On most platforms it starts counting from vtotal-1 on the
13137 * first active line. That means the scanline counter value is
13138 * always one less than what we would expect. Ie. just after
13139 * start of vblank, which also occurs at start of hsync (on the
13140 * last active line), the scanline counter will read vblank_start-1.
13141 *
13142 * On gen2 the scanline counter starts counting from 1 instead
13143 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13144 * to keep the value positive), instead of adding one.
13145 *
13146 * On HSW+ the behaviour of the scanline counter depends on the output
13147 * type. For DP ports it behaves like most other platforms, but on HDMI
13148 * there's an extra 1 line difference. So we need to add two instead of
13149 * one to the value.
13150 */
13151 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013152 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013153 int vtotal;
13154
Ville Syrjälä124abe02015-09-08 13:40:45 +030013155 vtotal = adjusted_mode->crtc_vtotal;
13156 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013157 vtotal /= 2;
13158
13159 crtc->scanline_offset = vtotal - 1;
13160 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013161 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013162 crtc->scanline_offset = 2;
13163 } else
13164 crtc->scanline_offset = 1;
13165}
13166
Maarten Lankhorstad421372015-06-15 12:33:42 +020013167static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013168{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013169 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013170 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013171 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013172 struct drm_crtc *crtc;
13173 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013174 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013175
13176 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013177 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013178
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013179 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13181 int old_dpll = to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013182
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013183 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013184 continue;
13185
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013186 to_intel_crtc_state(crtc_state)->shared_dpll = DPLL_ID_PRIVATE;
13187
13188 if (old_dpll == DPLL_ID_PRIVATE)
13189 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013190
Maarten Lankhorstad421372015-06-15 12:33:42 +020013191 if (!shared_dpll)
13192 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13193
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013194 shared_dpll[old_dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013195 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013196}
13197
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013198/*
13199 * This implements the workaround described in the "notes" section of the mode
13200 * set sequence documentation. When going from no pipes or single pipe to
13201 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13202 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13203 */
13204static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13205{
13206 struct drm_crtc_state *crtc_state;
13207 struct intel_crtc *intel_crtc;
13208 struct drm_crtc *crtc;
13209 struct intel_crtc_state *first_crtc_state = NULL;
13210 struct intel_crtc_state *other_crtc_state = NULL;
13211 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13212 int i;
13213
13214 /* look at all crtc's that are going to be enabled in during modeset */
13215 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13216 intel_crtc = to_intel_crtc(crtc);
13217
13218 if (!crtc_state->active || !needs_modeset(crtc_state))
13219 continue;
13220
13221 if (first_crtc_state) {
13222 other_crtc_state = to_intel_crtc_state(crtc_state);
13223 break;
13224 } else {
13225 first_crtc_state = to_intel_crtc_state(crtc_state);
13226 first_pipe = intel_crtc->pipe;
13227 }
13228 }
13229
13230 /* No workaround needed? */
13231 if (!first_crtc_state)
13232 return 0;
13233
13234 /* w/a possibly needed, check how many crtc's are already enabled. */
13235 for_each_intel_crtc(state->dev, intel_crtc) {
13236 struct intel_crtc_state *pipe_config;
13237
13238 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13239 if (IS_ERR(pipe_config))
13240 return PTR_ERR(pipe_config);
13241
13242 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13243
13244 if (!pipe_config->base.active ||
13245 needs_modeset(&pipe_config->base))
13246 continue;
13247
13248 /* 2 or more enabled crtcs means no need for w/a */
13249 if (enabled_pipe != INVALID_PIPE)
13250 return 0;
13251
13252 enabled_pipe = intel_crtc->pipe;
13253 }
13254
13255 if (enabled_pipe != INVALID_PIPE)
13256 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13257 else if (other_crtc_state)
13258 other_crtc_state->hsw_workaround_pipe = first_pipe;
13259
13260 return 0;
13261}
13262
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013263static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13264{
13265 struct drm_crtc *crtc;
13266 struct drm_crtc_state *crtc_state;
13267 int ret = 0;
13268
13269 /* add all active pipes to the state */
13270 for_each_crtc(state->dev, crtc) {
13271 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13272 if (IS_ERR(crtc_state))
13273 return PTR_ERR(crtc_state);
13274
13275 if (!crtc_state->active || needs_modeset(crtc_state))
13276 continue;
13277
13278 crtc_state->mode_changed = true;
13279
13280 ret = drm_atomic_add_affected_connectors(state, crtc);
13281 if (ret)
13282 break;
13283
13284 ret = drm_atomic_add_affected_planes(state, crtc);
13285 if (ret)
13286 break;
13287 }
13288
13289 return ret;
13290}
13291
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013292static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013293{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013294 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13295 struct drm_i915_private *dev_priv = state->dev->dev_private;
13296 struct drm_crtc *crtc;
13297 struct drm_crtc_state *crtc_state;
13298 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013299
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013300 if (!check_digital_port_conflicts(state)) {
13301 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13302 return -EINVAL;
13303 }
13304
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013305 intel_state->modeset = true;
13306 intel_state->active_crtcs = dev_priv->active_crtcs;
13307
13308 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13309 if (crtc_state->active)
13310 intel_state->active_crtcs |= 1 << i;
13311 else
13312 intel_state->active_crtcs &= ~(1 << i);
13313 }
13314
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013315 /*
13316 * See if the config requires any additional preparation, e.g.
13317 * to adjust global state with pipes off. We need to do this
13318 * here so we can get the modeset_pipe updated config for the new
13319 * mode set on this crtc. For other crtcs we need to use the
13320 * adjusted_mode bits in the crtc directly.
13321 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013322 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013323 ret = dev_priv->display.modeset_calc_cdclk(state);
13324
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013325 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013326 ret = intel_modeset_all_pipes(state);
13327
13328 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013329 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013330
13331 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13332 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013333 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013334 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013335
Maarten Lankhorstad421372015-06-15 12:33:42 +020013336 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013337
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013338 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013339 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013340
Maarten Lankhorstad421372015-06-15 12:33:42 +020013341 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013342}
13343
Matt Roperaa363132015-09-24 15:53:18 -070013344/*
13345 * Handle calculation of various watermark data at the end of the atomic check
13346 * phase. The code here should be run after the per-crtc and per-plane 'check'
13347 * handlers to ensure that all derived state has been updated.
13348 */
13349static void calc_watermark_data(struct drm_atomic_state *state)
13350{
13351 struct drm_device *dev = state->dev;
13352 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13353 struct drm_crtc *crtc;
13354 struct drm_crtc_state *cstate;
13355 struct drm_plane *plane;
13356 struct drm_plane_state *pstate;
13357
13358 /*
13359 * Calculate watermark configuration details now that derived
13360 * plane/crtc state is all properly updated.
13361 */
13362 drm_for_each_crtc(crtc, dev) {
13363 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13364 crtc->state;
13365
13366 if (cstate->active)
13367 intel_state->wm_config.num_pipes_active++;
13368 }
13369 drm_for_each_legacy_plane(plane, dev) {
13370 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13371 plane->state;
13372
13373 if (!to_intel_plane_state(pstate)->visible)
13374 continue;
13375
13376 intel_state->wm_config.sprites_enabled = true;
13377 if (pstate->crtc_w != pstate->src_w >> 16 ||
13378 pstate->crtc_h != pstate->src_h >> 16)
13379 intel_state->wm_config.sprites_scaled = true;
13380 }
13381}
13382
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013383/**
13384 * intel_atomic_check - validate state object
13385 * @dev: drm device
13386 * @state: state to validate
13387 */
13388static int intel_atomic_check(struct drm_device *dev,
13389 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013390{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013391 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013392 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013393 struct drm_crtc *crtc;
13394 struct drm_crtc_state *crtc_state;
13395 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013396 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013397
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013398 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013399 if (ret)
13400 return ret;
13401
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013402 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013403 struct intel_crtc_state *pipe_config =
13404 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013405
Maarten Lankhorstba8af3e2015-11-16 12:49:14 +010013406 memset(&to_intel_crtc(crtc)->atomic, 0,
13407 sizeof(struct intel_crtc_atomic_commit));
13408
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013409 /* Catch I915_MODE_FLAG_INHERITED */
13410 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13411 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013412
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013413 if (!crtc_state->enable) {
13414 if (needs_modeset(crtc_state))
13415 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013416 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013417 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013418
Daniel Vetter26495482015-07-15 14:15:52 +020013419 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013420 continue;
13421
Daniel Vetter26495482015-07-15 14:15:52 +020013422 /* FIXME: For only active_changed we shouldn't need to do any
13423 * state recomputation at all. */
13424
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013425 ret = drm_atomic_add_affected_connectors(state, crtc);
13426 if (ret)
13427 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013428
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013429 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013430 if (ret)
13431 return ret;
13432
Jani Nikula73831232015-11-19 10:26:30 +020013433 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013434 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013435 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013436 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013437 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013438 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013439 }
13440
13441 if (needs_modeset(crtc_state)) {
13442 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013443
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013444 ret = drm_atomic_add_affected_planes(state, crtc);
13445 if (ret)
13446 return ret;
13447 }
13448
Daniel Vetter26495482015-07-15 14:15:52 +020013449 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13450 needs_modeset(crtc_state) ?
13451 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013452 }
13453
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013454 if (any_ms) {
13455 ret = intel_modeset_checks(state);
13456
13457 if (ret)
13458 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013459 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013460 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013461
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013462 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013463 if (ret)
13464 return ret;
13465
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013466 intel_fbc_choose_crtc(dev_priv, state);
Matt Roperaa363132015-09-24 15:53:18 -070013467 calc_watermark_data(state);
13468
13469 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013470}
13471
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013472static int intel_atomic_prepare_commit(struct drm_device *dev,
13473 struct drm_atomic_state *state,
13474 bool async)
13475{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013476 struct drm_i915_private *dev_priv = dev->dev_private;
13477 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013478 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013479 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013480 struct drm_crtc *crtc;
13481 int i, ret;
13482
13483 if (async) {
13484 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13485 return -EINVAL;
13486 }
13487
13488 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13489 ret = intel_crtc_wait_for_pending_flips(crtc);
13490 if (ret)
13491 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013492
13493 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13494 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013495 }
13496
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013497 ret = mutex_lock_interruptible(&dev->struct_mutex);
13498 if (ret)
13499 return ret;
13500
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013501 ret = drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013502 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13503 u32 reset_counter;
13504
13505 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13506 mutex_unlock(&dev->struct_mutex);
13507
13508 for_each_plane_in_state(state, plane, plane_state, i) {
13509 struct intel_plane_state *intel_plane_state =
13510 to_intel_plane_state(plane_state);
13511
13512 if (!intel_plane_state->wait_req)
13513 continue;
13514
13515 ret = __i915_wait_request(intel_plane_state->wait_req,
13516 reset_counter, true,
13517 NULL, NULL);
13518
13519 /* Swallow -EIO errors to allow updates during hw lockup. */
13520 if (ret == -EIO)
13521 ret = 0;
13522
13523 if (ret)
13524 break;
13525 }
13526
13527 if (!ret)
13528 return 0;
13529
13530 mutex_lock(&dev->struct_mutex);
13531 drm_atomic_helper_cleanup_planes(dev, state);
13532 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013533
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013534 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013535 return ret;
13536}
13537
Maarten Lankhorste8861672016-02-24 11:24:26 +010013538static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13539 struct drm_i915_private *dev_priv,
13540 unsigned crtc_mask)
13541{
13542 unsigned last_vblank_count[I915_MAX_PIPES];
13543 enum pipe pipe;
13544 int ret;
13545
13546 if (!crtc_mask)
13547 return;
13548
13549 for_each_pipe(dev_priv, pipe) {
13550 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13551
13552 if (!((1 << pipe) & crtc_mask))
13553 continue;
13554
13555 ret = drm_crtc_vblank_get(crtc);
13556 if (WARN_ON(ret != 0)) {
13557 crtc_mask &= ~(1 << pipe);
13558 continue;
13559 }
13560
13561 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13562 }
13563
13564 for_each_pipe(dev_priv, pipe) {
13565 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13566 long lret;
13567
13568 if (!((1 << pipe) & crtc_mask))
13569 continue;
13570
13571 lret = wait_event_timeout(dev->vblank[pipe].queue,
13572 last_vblank_count[pipe] !=
13573 drm_crtc_vblank_count(crtc),
13574 msecs_to_jiffies(50));
13575
13576 WARN_ON(!lret);
13577
13578 drm_crtc_vblank_put(crtc);
13579 }
13580}
13581
13582static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13583{
13584 /* fb updated, need to unpin old fb */
13585 if (crtc_state->fb_changed)
13586 return true;
13587
13588 /* wm changes, need vblank before final wm's */
13589 if (crtc_state->wm_changed)
13590 return true;
13591
13592 /*
13593 * cxsr is re-enabled after vblank.
13594 * This is already handled by crtc_state->wm_changed,
13595 * but added for clarity.
13596 */
13597 if (crtc_state->disable_cxsr)
13598 return true;
13599
13600 return false;
13601}
13602
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013603/**
13604 * intel_atomic_commit - commit validated state object
13605 * @dev: DRM device
13606 * @state: the top-level driver state object
13607 * @async: asynchronous commit
13608 *
13609 * This function commits a top-level state object that has been validated
13610 * with drm_atomic_helper_check().
13611 *
13612 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13613 * we can only handle plane-related operations and do not yet support
13614 * asynchronous commit.
13615 *
13616 * RETURNS
13617 * Zero for success or -errno.
13618 */
13619static int intel_atomic_commit(struct drm_device *dev,
13620 struct drm_atomic_state *state,
13621 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013622{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013623 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013624 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013625 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013626 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013627 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013628 int ret = 0, i;
13629 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013630 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013631 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013632
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013633 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013634 if (ret) {
13635 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013636 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013637 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013638
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013639 drm_atomic_helper_swap_state(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013640 dev_priv->wm.config = to_intel_atomic_state(state)->wm_config;
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013641
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013642 if (intel_state->modeset) {
13643 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13644 sizeof(intel_state->min_pixclk));
13645 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013646 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013647
13648 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013649 }
13650
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013651 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13653
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013654 if (needs_modeset(crtc->state) ||
13655 to_intel_crtc_state(crtc->state)->update_pipe) {
13656 hw_check = true;
13657
13658 put_domains[to_intel_crtc(crtc)->pipe] =
13659 modeset_get_crtc_power_domains(crtc,
13660 to_intel_crtc_state(crtc->state));
13661 }
13662
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013663 if (!needs_modeset(crtc->state))
13664 continue;
13665
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +010013666 intel_pre_plane_update(to_intel_crtc_state(crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013667
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013668 if (crtc_state->active) {
13669 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13670 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013671 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013672 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013673 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013674
13675 /*
13676 * Underruns don't always raise
13677 * interrupts, so check manually.
13678 */
13679 intel_check_cpu_fifo_underruns(dev_priv);
13680 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013681
13682 if (!crtc->state->active)
13683 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013684 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013685 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013686
Daniel Vetterea9d7582012-07-10 10:42:52 +020013687 /* Only after disabling all output pipelines that will be changed can we
13688 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013689 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013690
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013691 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013692 intel_shared_dpll_commit(state);
13693
13694 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013695
13696 if (dev_priv->display.modeset_commit_cdclk &&
13697 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13698 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013699 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013700
Daniel Vettera6778b32012-07-02 09:56:42 +020013701 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013702 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013703 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13704 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013705 struct intel_crtc_state *pipe_config =
13706 to_intel_crtc_state(crtc->state);
13707 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013708
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013709 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013710 update_scanline_offset(to_intel_crtc(crtc));
13711 dev_priv->display.crtc_enable(crtc);
13712 }
13713
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013714 if (!modeset)
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +010013715 intel_pre_plane_update(to_intel_crtc_state(crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013716
Paulo Zanoni49227c42016-01-19 11:35:52 -020013717 if (crtc->state->active && intel_crtc->atomic.update_fbc)
13718 intel_fbc_enable(intel_crtc);
13719
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013720 if (crtc->state->active &&
13721 (crtc->state->planes_changed || update_pipe))
Maarten Lankhorst62852622015-09-23 16:29:38 +020013722 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013723
Maarten Lankhorste8861672016-02-24 11:24:26 +010013724 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13725 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013726 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013727
Daniel Vettera6778b32012-07-02 09:56:42 +020013728 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013729
Maarten Lankhorste8861672016-02-24 11:24:26 +010013730 if (!state->legacy_cursor_update)
13731 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013732
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013733 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorste8861672016-02-24 11:24:26 +010013734 intel_post_plane_update(to_intel_crtc(crtc));
13735
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013736 if (put_domains[i])
13737 modeset_put_power_domains(dev_priv, put_domains[i]);
13738 }
13739
13740 if (intel_state->modeset)
13741 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13742
Matt Ropered4a6a72016-02-23 17:20:13 -080013743 /*
13744 * Now that the vblank has passed, we can go ahead and program the
13745 * optimal watermarks on platforms that need two-step watermark
13746 * programming.
13747 *
13748 * TODO: Move this (and other cleanup) to an async worker eventually.
13749 */
13750 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13751 intel_cstate = to_intel_crtc_state(crtc->state);
13752
13753 if (dev_priv->display.optimize_watermarks)
13754 dev_priv->display.optimize_watermarks(intel_cstate);
13755 }
13756
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013757 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013758 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013759 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013760
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013761 if (hw_check)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013762 intel_modeset_check_state(dev, state);
13763
13764 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013765
Mika Kuoppala75714942015-12-16 09:26:48 +020013766 /* As one of the primary mmio accessors, KMS has a high likelihood
13767 * of triggering bugs in unclaimed access. After we finish
13768 * modesetting, see if an error has been flagged, and if so
13769 * enable debugging for the next modeset - and hope we catch
13770 * the culprit.
13771 *
13772 * XXX note that we assume display power is on at this point.
13773 * This might hold true now but we need to add pm helper to check
13774 * unclaimed only when the hardware is on, as atomic commits
13775 * can happen also when the device is completely off.
13776 */
13777 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13778
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013779 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013780}
13781
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013782void intel_crtc_restore_mode(struct drm_crtc *crtc)
13783{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013784 struct drm_device *dev = crtc->dev;
13785 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013786 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013787 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013788
13789 state = drm_atomic_state_alloc(dev);
13790 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013791 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013792 crtc->base.id);
13793 return;
13794 }
13795
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013796 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013797
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013798retry:
13799 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13800 ret = PTR_ERR_OR_ZERO(crtc_state);
13801 if (!ret) {
13802 if (!crtc_state->active)
13803 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013804
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013805 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013806 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013807 }
13808
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013809 if (ret == -EDEADLK) {
13810 drm_atomic_state_clear(state);
13811 drm_modeset_backoff(state->acquire_ctx);
13812 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013813 }
13814
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013815 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013816out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013817 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013818}
13819
Daniel Vetter25c5b262012-07-08 22:08:04 +020013820#undef for_each_intel_crtc_masked
13821
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013822static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013823 .gamma_set = intel_crtc_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013824 .set_config = drm_atomic_helper_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013825 .destroy = intel_crtc_destroy,
13826 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013827 .atomic_duplicate_state = intel_crtc_duplicate_state,
13828 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013829};
13830
Daniel Vetter53589012013-06-05 13:34:16 +020013831static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13832 struct intel_shared_dpll *pll,
13833 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013834{
Daniel Vetter53589012013-06-05 13:34:16 +020013835 uint32_t val;
13836
Imre Deak12fda382016-02-12 18:55:12 +020013837 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013838 return false;
13839
Daniel Vetter53589012013-06-05 13:34:16 +020013840 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013841 hw_state->dpll = val;
13842 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13843 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013844
Imre Deak12fda382016-02-12 18:55:12 +020013845 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
13846
Daniel Vetter53589012013-06-05 13:34:16 +020013847 return val & DPLL_VCO_ENABLE;
13848}
13849
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013850static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13851 struct intel_shared_dpll *pll)
13852{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013853 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13854 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013855}
13856
Daniel Vettere7b903d2013-06-05 13:34:14 +020013857static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13858 struct intel_shared_dpll *pll)
13859{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013860 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013861 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013862
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013863 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013864
13865 /* Wait for the clocks to stabilize. */
13866 POSTING_READ(PCH_DPLL(pll->id));
13867 udelay(150);
13868
13869 /* The pixel multiplier can only be updated once the
13870 * DPLL is enabled and the clocks are stable.
13871 *
13872 * So write it again.
13873 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013874 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013875 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013876 udelay(200);
13877}
13878
13879static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13880 struct intel_shared_dpll *pll)
13881{
13882 struct drm_device *dev = dev_priv->dev;
13883 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013884
13885 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013886 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013887 if (intel_crtc_to_shared_dpll(crtc) == pll)
13888 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13889 }
13890
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013891 I915_WRITE(PCH_DPLL(pll->id), 0);
13892 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013893 udelay(200);
13894}
13895
Daniel Vetter46edb022013-06-05 13:34:12 +020013896static char *ibx_pch_dpll_names[] = {
13897 "PCH DPLL A",
13898 "PCH DPLL B",
13899};
13900
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013901static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013902{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013903 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013904 int i;
13905
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013906 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013907
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013908 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013909 dev_priv->shared_dplls[i].id = i;
13910 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013911 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013912 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13913 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013914 dev_priv->shared_dplls[i].get_hw_state =
13915 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013916 }
13917}
13918
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013919static void intel_shared_dpll_init(struct drm_device *dev)
13920{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013921 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013922
Daniel Vetter9cd86932014-06-25 22:01:57 +030013923 if (HAS_DDI(dev))
13924 intel_ddi_pll_init(dev);
13925 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013926 ibx_pch_dpll_init(dev);
13927 else
13928 dev_priv->num_shared_dpll = 0;
13929
13930 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013931}
13932
Matt Roper6beb8c232014-12-01 15:40:14 -080013933/**
13934 * intel_prepare_plane_fb - Prepare fb for usage on plane
13935 * @plane: drm plane to prepare for
13936 * @fb: framebuffer to prepare for presentation
13937 *
13938 * Prepares a framebuffer for usage on a display plane. Generally this
13939 * involves pinning the underlying object and updating the frontbuffer tracking
13940 * bits. Some older platforms need special physical address handling for
13941 * cursor planes.
13942 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013943 * Must be called with struct_mutex held.
13944 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013945 * Returns 0 on success, negative error code on failure.
13946 */
13947int
13948intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013949 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013950{
13951 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013952 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013953 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013954 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013955 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013956 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013957
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013958 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013959 return 0;
13960
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013961 if (old_obj) {
13962 struct drm_crtc_state *crtc_state =
13963 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13964
13965 /* Big Hammer, we also need to ensure that any pending
13966 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13967 * current scanout is retired before unpinning the old
13968 * framebuffer. Note that we rely on userspace rendering
13969 * into the buffer attached to the pipe they are waiting
13970 * on. If not, userspace generates a GPU hang with IPEHR
13971 * point to the MI_WAIT_FOR_EVENT.
13972 *
13973 * This should only fail upon a hung GPU, in which case we
13974 * can safely continue.
13975 */
13976 if (needs_modeset(crtc_state))
13977 ret = i915_gem_object_wait_rendering(old_obj, true);
13978
13979 /* Swallow -EIO errors to allow updates during hw lockup. */
13980 if (ret && ret != -EIO)
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013981 return ret;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013982 }
13983
Alex Goins3c28ff22015-11-25 18:43:39 -080013984 /* For framebuffer backed by dmabuf, wait for fence */
13985 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013986 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013987
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013988 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13989 false, true,
13990 MAX_SCHEDULE_TIMEOUT);
13991 if (lret == -ERESTARTSYS)
13992 return lret;
13993
13994 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013995 }
13996
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013997 if (!obj) {
13998 ret = 0;
13999 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080014000 INTEL_INFO(dev)->cursor_needs_physical) {
14001 int align = IS_I830(dev) ? 16 * 1024 : 256;
14002 ret = i915_gem_object_attach_phys(obj, align);
14003 if (ret)
14004 DRM_DEBUG_KMS("failed to attach phys object\n");
14005 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020014006 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080014007 }
14008
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014009 if (ret == 0) {
14010 if (obj) {
14011 struct intel_plane_state *plane_state =
14012 to_intel_plane_state(new_state);
14013
14014 i915_gem_request_assign(&plane_state->wait_req,
14015 obj->last_write_req);
14016 }
14017
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014018 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014019 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014020
Matt Roper6beb8c232014-12-01 15:40:14 -080014021 return ret;
14022}
14023
Matt Roper38f3ce32014-12-02 07:45:25 -080014024/**
14025 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14026 * @plane: drm plane to clean up for
14027 * @fb: old framebuffer that was on plane
14028 *
14029 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014030 *
14031 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014032 */
14033void
14034intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014035 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014036{
14037 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014038 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014039 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014040 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14041 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014042
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014043 old_intel_state = to_intel_plane_state(old_state);
14044
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014045 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014046 return;
14047
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014048 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14049 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014050 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014051
14052 /* prepare_fb aborted? */
14053 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
14054 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
14055 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014056
14057 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070014058}
14059
Chandra Konduru6156a452015-04-27 13:48:39 -070014060int
14061skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14062{
14063 int max_scale;
14064 struct drm_device *dev;
14065 struct drm_i915_private *dev_priv;
14066 int crtc_clock, cdclk;
14067
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014068 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014069 return DRM_PLANE_HELPER_NO_SCALING;
14070
14071 dev = intel_crtc->base.dev;
14072 dev_priv = dev->dev_private;
14073 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014074 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014075
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014076 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014077 return DRM_PLANE_HELPER_NO_SCALING;
14078
14079 /*
14080 * skl max scale is lower of:
14081 * close to 3 but not 3, -1 is for that purpose
14082 * or
14083 * cdclk/crtc_clock
14084 */
14085 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14086
14087 return max_scale;
14088}
14089
Matt Roper465c1202014-05-29 08:06:54 -070014090static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014091intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014092 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014093 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014094{
Matt Roper2b875c22014-12-01 15:40:13 -080014095 struct drm_crtc *crtc = state->base.crtc;
14096 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014097 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014098 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14099 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014100
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014101 if (INTEL_INFO(plane->dev)->gen >= 9) {
14102 /* use scaler when colorkey is not required */
14103 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14104 min_scale = 1;
14105 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14106 }
Sonika Jindald8106362015-04-10 14:37:28 +053014107 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014108 }
Sonika Jindald8106362015-04-10 14:37:28 +053014109
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014110 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14111 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014112 min_scale, max_scale,
14113 can_position, true,
14114 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014115}
14116
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020014117static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14118 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080014119{
14120 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080014121 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020014122 struct intel_crtc_state *old_intel_state =
14123 to_intel_crtc_state(old_crtc_state);
14124 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030014125
Matt Roperc34c9ee2014-12-23 10:41:50 -080014126 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020014127 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020014128
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020014129 if (modeset)
14130 return;
14131
14132 if (to_intel_crtc_state(crtc->state)->update_pipe)
14133 intel_update_pipe_config(intel_crtc, old_intel_state);
14134 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020014135 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080014136}
14137
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020014138static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14139 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080014140{
Matt Roper32b7eee2014-12-24 07:59:06 -080014141 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080014142
Maarten Lankhorst62852622015-09-23 16:29:38 +020014143 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014144}
14145
Matt Ropercf4c7c12014-12-04 10:27:42 -080014146/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014147 * intel_plane_destroy - destroy a plane
14148 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014149 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014150 * Common destruction function for all types of planes (primary, cursor,
14151 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014152 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014153void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014154{
14155 struct intel_plane *intel_plane = to_intel_plane(plane);
14156 drm_plane_cleanup(plane);
14157 kfree(intel_plane);
14158}
14159
Matt Roper65a3fea2015-01-21 16:35:42 -080014160const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014161 .update_plane = drm_atomic_helper_update_plane,
14162 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014163 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014164 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014165 .atomic_get_property = intel_plane_atomic_get_property,
14166 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014167 .atomic_duplicate_state = intel_plane_duplicate_state,
14168 .atomic_destroy_state = intel_plane_destroy_state,
14169
Matt Roper465c1202014-05-29 08:06:54 -070014170};
14171
14172static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14173 int pipe)
14174{
14175 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080014176 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070014177 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014178 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070014179
14180 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
14181 if (primary == NULL)
14182 return NULL;
14183
Matt Roper8e7d6882015-01-21 16:35:41 -080014184 state = intel_create_plane_state(&primary->base);
14185 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014186 kfree(primary);
14187 return NULL;
14188 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014189 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014190
Matt Roper465c1202014-05-29 08:06:54 -070014191 primary->can_scale = false;
14192 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014193 if (INTEL_INFO(dev)->gen >= 9) {
14194 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014195 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014196 }
Matt Roper465c1202014-05-29 08:06:54 -070014197 primary->pipe = pipe;
14198 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014199 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014200 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014201 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14202 primary->plane = !pipe;
14203
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014204 if (INTEL_INFO(dev)->gen >= 9) {
14205 intel_primary_formats = skl_primary_formats;
14206 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014207
14208 primary->update_plane = skylake_update_primary_plane;
14209 primary->disable_plane = skylake_disable_primary_plane;
14210 } else if (HAS_PCH_SPLIT(dev)) {
14211 intel_primary_formats = i965_primary_formats;
14212 num_formats = ARRAY_SIZE(i965_primary_formats);
14213
14214 primary->update_plane = ironlake_update_primary_plane;
14215 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014216 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014217 intel_primary_formats = i965_primary_formats;
14218 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014219
14220 primary->update_plane = i9xx_update_primary_plane;
14221 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014222 } else {
14223 intel_primary_formats = i8xx_primary_formats;
14224 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014225
14226 primary->update_plane = i9xx_update_primary_plane;
14227 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014228 }
14229
14230 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014231 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070014232 intel_primary_formats, num_formats,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014233 DRM_PLANE_TYPE_PRIMARY, NULL);
Sonika Jindal48404c12014-08-22 14:06:04 +053014234
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014235 if (INTEL_INFO(dev)->gen >= 4)
14236 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014237
Matt Roperea2c67b2014-12-23 10:41:52 -080014238 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14239
Matt Roper465c1202014-05-29 08:06:54 -070014240 return &primary->base;
14241}
14242
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014243void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14244{
14245 if (!dev->mode_config.rotation_property) {
14246 unsigned long flags = BIT(DRM_ROTATE_0) |
14247 BIT(DRM_ROTATE_180);
14248
14249 if (INTEL_INFO(dev)->gen >= 9)
14250 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14251
14252 dev->mode_config.rotation_property =
14253 drm_mode_create_rotation_property(dev, flags);
14254 }
14255 if (dev->mode_config.rotation_property)
14256 drm_object_attach_property(&plane->base.base,
14257 dev->mode_config.rotation_property,
14258 plane->base.state->rotation);
14259}
14260
Matt Roper3d7d6512014-06-10 08:28:13 -070014261static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014262intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014263 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014264 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014265{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014266 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014267 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014268 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014269 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014270 unsigned stride;
14271 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014272
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014273 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14274 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014275 DRM_PLANE_HELPER_NO_SCALING,
14276 DRM_PLANE_HELPER_NO_SCALING,
14277 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014278 if (ret)
14279 return ret;
14280
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014281 /* if we want to turn off the cursor ignore width and height */
14282 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014283 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014284
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014285 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014286 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014287 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14288 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014289 return -EINVAL;
14290 }
14291
Matt Roperea2c67b2014-12-23 10:41:52 -080014292 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14293 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014294 DRM_DEBUG_KMS("buffer is too small\n");
14295 return -ENOMEM;
14296 }
14297
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014298 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014299 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014300 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014301 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014302
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014303 /*
14304 * There's something wrong with the cursor on CHV pipe C.
14305 * If it straddles the left edge of the screen then
14306 * moving it away from the edge or disabling it often
14307 * results in a pipe underrun, and often that can lead to
14308 * dead pipe (constant underrun reported, and it scans
14309 * out just a solid color). To recover from that, the
14310 * display power well must be turned off and on again.
14311 * Refuse the put the cursor into that compromised position.
14312 */
14313 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14314 state->visible && state->base.crtc_x < 0) {
14315 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14316 return -EINVAL;
14317 }
14318
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014319 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014320}
14321
Matt Roperf4a2cf22014-12-01 15:40:12 -080014322static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014323intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014324 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014325{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014326 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14327
14328 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014329 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014330}
14331
14332static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014333intel_update_cursor_plane(struct drm_plane *plane,
14334 const struct intel_crtc_state *crtc_state,
14335 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014336{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014337 struct drm_crtc *crtc = crtc_state->base.crtc;
14338 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014339 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014340 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014341 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014342
Matt Roperf4a2cf22014-12-01 15:40:12 -080014343 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014344 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014345 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014346 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014347 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014348 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014349
Gustavo Padovana912f122014-12-01 15:40:10 -080014350 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014351 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014352}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014353
Matt Roper3d7d6512014-06-10 08:28:13 -070014354static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14355 int pipe)
14356{
14357 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014358 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014359
14360 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14361 if (cursor == NULL)
14362 return NULL;
14363
Matt Roper8e7d6882015-01-21 16:35:41 -080014364 state = intel_create_plane_state(&cursor->base);
14365 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014366 kfree(cursor);
14367 return NULL;
14368 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014369 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014370
Matt Roper3d7d6512014-06-10 08:28:13 -070014371 cursor->can_scale = false;
14372 cursor->max_downscale = 1;
14373 cursor->pipe = pipe;
14374 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014375 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014376 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014377 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014378 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014379
14380 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014381 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014382 intel_cursor_formats,
14383 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014384 DRM_PLANE_TYPE_CURSOR, NULL);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014385
14386 if (INTEL_INFO(dev)->gen >= 4) {
14387 if (!dev->mode_config.rotation_property)
14388 dev->mode_config.rotation_property =
14389 drm_mode_create_rotation_property(dev,
14390 BIT(DRM_ROTATE_0) |
14391 BIT(DRM_ROTATE_180));
14392 if (dev->mode_config.rotation_property)
14393 drm_object_attach_property(&cursor->base.base,
14394 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014395 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014396 }
14397
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014398 if (INTEL_INFO(dev)->gen >=9)
14399 state->scaler_id = -1;
14400
Matt Roperea2c67b2014-12-23 10:41:52 -080014401 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14402
Matt Roper3d7d6512014-06-10 08:28:13 -070014403 return &cursor->base;
14404}
14405
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014406static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14407 struct intel_crtc_state *crtc_state)
14408{
14409 int i;
14410 struct intel_scaler *intel_scaler;
14411 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14412
14413 for (i = 0; i < intel_crtc->num_scalers; i++) {
14414 intel_scaler = &scaler_state->scalers[i];
14415 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014416 intel_scaler->mode = PS_SCALER_MODE_DYN;
14417 }
14418
14419 scaler_state->scaler_id = -1;
14420}
14421
Hannes Ederb358d0a2008-12-18 21:18:47 +010014422static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014423{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014424 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014425 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014426 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014427 struct drm_plane *primary = NULL;
14428 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014429 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014430
Daniel Vetter955382f2013-09-19 14:05:45 +020014431 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014432 if (intel_crtc == NULL)
14433 return;
14434
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014435 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14436 if (!crtc_state)
14437 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014438 intel_crtc->config = crtc_state;
14439 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014440 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014441
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014442 /* initialize shared scalers */
14443 if (INTEL_INFO(dev)->gen >= 9) {
14444 if (pipe == PIPE_C)
14445 intel_crtc->num_scalers = 1;
14446 else
14447 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14448
14449 skl_init_scalers(dev, intel_crtc, crtc_state);
14450 }
14451
Matt Roper465c1202014-05-29 08:06:54 -070014452 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014453 if (!primary)
14454 goto fail;
14455
14456 cursor = intel_cursor_plane_create(dev, pipe);
14457 if (!cursor)
14458 goto fail;
14459
Matt Roper465c1202014-05-29 08:06:54 -070014460 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014461 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014462 if (ret)
14463 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014464
14465 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014466 for (i = 0; i < 256; i++) {
14467 intel_crtc->lut_r[i] = i;
14468 intel_crtc->lut_g[i] = i;
14469 intel_crtc->lut_b[i] = i;
14470 }
14471
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014472 /*
14473 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014474 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014475 */
Jesse Barnes80824002009-09-10 15:28:06 -070014476 intel_crtc->pipe = pipe;
14477 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014478 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014479 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014480 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014481 }
14482
Chris Wilson4b0e3332014-05-30 16:35:26 +030014483 intel_crtc->cursor_base = ~0;
14484 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014485 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014486
Ville Syrjälä852eb002015-06-24 22:00:07 +030014487 intel_crtc->wm.cxsr_allowed = true;
14488
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014489 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14490 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14491 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14492 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14493
Jesse Barnes79e53942008-11-07 14:24:08 -080014494 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014495
14496 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014497 return;
14498
14499fail:
14500 if (primary)
14501 drm_plane_cleanup(primary);
14502 if (cursor)
14503 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014504 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014505 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014506}
14507
Jesse Barnes752aa882013-10-31 18:55:49 +020014508enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14509{
14510 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014511 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014512
Rob Clark51fd3712013-11-19 12:10:12 -050014513 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014514
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014515 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014516 return INVALID_PIPE;
14517
14518 return to_intel_crtc(encoder->crtc)->pipe;
14519}
14520
Carl Worth08d7b3d2009-04-29 14:43:54 -070014521int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014522 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014523{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014524 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014525 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014526 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014527
Rob Clark7707e652014-07-17 23:30:04 -040014528 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014529
Rob Clark7707e652014-07-17 23:30:04 -040014530 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014531 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014532 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014533 }
14534
Rob Clark7707e652014-07-17 23:30:04 -040014535 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014536 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014537
Daniel Vetterc05422d2009-08-11 16:05:30 +020014538 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014539}
14540
Daniel Vetter66a92782012-07-12 20:08:18 +020014541static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014542{
Daniel Vetter66a92782012-07-12 20:08:18 +020014543 struct drm_device *dev = encoder->base.dev;
14544 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014545 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014546 int entry = 0;
14547
Damien Lespiaub2784e12014-08-05 11:29:37 +010014548 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014549 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014550 index_mask |= (1 << entry);
14551
Jesse Barnes79e53942008-11-07 14:24:08 -080014552 entry++;
14553 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014554
Jesse Barnes79e53942008-11-07 14:24:08 -080014555 return index_mask;
14556}
14557
Chris Wilson4d302442010-12-14 19:21:29 +000014558static bool has_edp_a(struct drm_device *dev)
14559{
14560 struct drm_i915_private *dev_priv = dev->dev_private;
14561
14562 if (!IS_MOBILE(dev))
14563 return false;
14564
14565 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14566 return false;
14567
Damien Lespiaue3589902014-02-07 19:12:50 +000014568 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014569 return false;
14570
14571 return true;
14572}
14573
Jesse Barnes84b4e042014-06-25 08:24:29 -070014574static bool intel_crt_present(struct drm_device *dev)
14575{
14576 struct drm_i915_private *dev_priv = dev->dev_private;
14577
Damien Lespiau884497e2013-12-03 13:56:23 +000014578 if (INTEL_INFO(dev)->gen >= 9)
14579 return false;
14580
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014581 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014582 return false;
14583
14584 if (IS_CHERRYVIEW(dev))
14585 return false;
14586
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014587 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14588 return false;
14589
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014590 /* DDI E can't be used if DDI A requires 4 lanes */
14591 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14592 return false;
14593
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014594 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014595 return false;
14596
14597 return true;
14598}
14599
Jesse Barnes79e53942008-11-07 14:24:08 -080014600static void intel_setup_outputs(struct drm_device *dev)
14601{
Eric Anholt725e30a2009-01-22 13:01:02 -080014602 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014603 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014604 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014605
Daniel Vetterc9093352013-06-06 22:22:47 +020014606 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014607
Jesse Barnes84b4e042014-06-25 08:24:29 -070014608 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014609 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014610
Vandana Kannanc776eb22014-08-19 12:05:01 +053014611 if (IS_BROXTON(dev)) {
14612 /*
14613 * FIXME: Broxton doesn't support port detection via the
14614 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14615 * detect the ports.
14616 */
14617 intel_ddi_init(dev, PORT_A);
14618 intel_ddi_init(dev, PORT_B);
14619 intel_ddi_init(dev, PORT_C);
14620 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014621 int found;
14622
Jesse Barnesde31fac2015-03-06 15:53:32 -080014623 /*
14624 * Haswell uses DDI functions to detect digital outputs.
14625 * On SKL pre-D0 the strap isn't connected, so we assume
14626 * it's there.
14627 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014628 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014629 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014630 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014631 intel_ddi_init(dev, PORT_A);
14632
14633 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14634 * register */
14635 found = I915_READ(SFUSE_STRAP);
14636
14637 if (found & SFUSE_STRAP_DDIB_DETECTED)
14638 intel_ddi_init(dev, PORT_B);
14639 if (found & SFUSE_STRAP_DDIC_DETECTED)
14640 intel_ddi_init(dev, PORT_C);
14641 if (found & SFUSE_STRAP_DDID_DETECTED)
14642 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014643 /*
14644 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14645 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014646 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014647 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14648 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14649 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14650 intel_ddi_init(dev, PORT_E);
14651
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014652 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014653 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014654 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014655
14656 if (has_edp_a(dev))
14657 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014658
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014659 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014660 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014661 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014662 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014663 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014664 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014665 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014666 }
14667
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014668 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014669 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014670
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014671 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014672 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014673
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014674 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014675 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014676
Daniel Vetter270b3042012-10-27 15:52:05 +020014677 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014678 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014679 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014680 /*
14681 * The DP_DETECTED bit is the latched state of the DDC
14682 * SDA pin at boot. However since eDP doesn't require DDC
14683 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14684 * eDP ports may have been muxed to an alternate function.
14685 * Thus we can't rely on the DP_DETECTED bit alone to detect
14686 * eDP ports. Consult the VBT as well as DP_DETECTED to
14687 * detect eDP ports.
14688 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014689 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014690 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014691 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14692 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014693 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014694 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014695
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014696 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014697 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014698 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14699 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014700 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014701 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014702
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014703 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014704 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014705 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14706 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14707 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14708 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014709 }
14710
Jani Nikula3cfca972013-08-27 15:12:26 +030014711 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014712 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014713 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014714
Paulo Zanonie2debe92013-02-18 19:00:27 -030014715 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014716 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014717 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014718 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014719 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014720 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014721 }
Ma Ling27185ae2009-08-24 13:50:23 +080014722
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014723 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014724 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014725 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014726
14727 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014728
Paulo Zanonie2debe92013-02-18 19:00:27 -030014729 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014730 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014731 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014732 }
Ma Ling27185ae2009-08-24 13:50:23 +080014733
Paulo Zanonie2debe92013-02-18 19:00:27 -030014734 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014735
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014736 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014737 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014738 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014739 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014740 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014741 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014742 }
Ma Ling27185ae2009-08-24 13:50:23 +080014743
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014744 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014745 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014746 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014747 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014748 intel_dvo_init(dev);
14749
Zhenyu Wang103a1962009-11-27 11:44:36 +080014750 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014751 intel_tv_init(dev);
14752
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014753 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014754
Damien Lespiaub2784e12014-08-05 11:29:37 +010014755 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014756 encoder->base.possible_crtcs = encoder->crtc_mask;
14757 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014758 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014759 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014760
Paulo Zanonidde86e22012-12-01 12:04:25 -020014761 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014762
14763 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014764}
14765
14766static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14767{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014768 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014769 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014770
Daniel Vetteref2d6332014-02-10 18:00:38 +010014771 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014772 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014773 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014774 drm_gem_object_unreference(&intel_fb->obj->base);
14775 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014776 kfree(intel_fb);
14777}
14778
14779static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014780 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014781 unsigned int *handle)
14782{
14783 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014784 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014785
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014786 if (obj->userptr.mm) {
14787 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14788 return -EINVAL;
14789 }
14790
Chris Wilson05394f32010-11-08 19:18:58 +000014791 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014792}
14793
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014794static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14795 struct drm_file *file,
14796 unsigned flags, unsigned color,
14797 struct drm_clip_rect *clips,
14798 unsigned num_clips)
14799{
14800 struct drm_device *dev = fb->dev;
14801 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14802 struct drm_i915_gem_object *obj = intel_fb->obj;
14803
14804 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014805 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014806 mutex_unlock(&dev->struct_mutex);
14807
14808 return 0;
14809}
14810
Jesse Barnes79e53942008-11-07 14:24:08 -080014811static const struct drm_framebuffer_funcs intel_fb_funcs = {
14812 .destroy = intel_user_framebuffer_destroy,
14813 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014814 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014815};
14816
Damien Lespiaub3218032015-02-27 11:15:18 +000014817static
14818u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14819 uint32_t pixel_format)
14820{
14821 u32 gen = INTEL_INFO(dev)->gen;
14822
14823 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014824 int cpp = drm_format_plane_cpp(pixel_format, 0);
14825
Damien Lespiaub3218032015-02-27 11:15:18 +000014826 /* "The stride in bytes must not exceed the of the size of 8K
14827 * pixels and 32K bytes."
14828 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014829 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014830 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014831 return 32*1024;
14832 } else if (gen >= 4) {
14833 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14834 return 16*1024;
14835 else
14836 return 32*1024;
14837 } else if (gen >= 3) {
14838 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14839 return 8*1024;
14840 else
14841 return 16*1024;
14842 } else {
14843 /* XXX DSPC is limited to 4k tiled */
14844 return 8*1024;
14845 }
14846}
14847
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014848static int intel_framebuffer_init(struct drm_device *dev,
14849 struct intel_framebuffer *intel_fb,
14850 struct drm_mode_fb_cmd2 *mode_cmd,
14851 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014852{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014853 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014854 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014855 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014856 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014857
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014858 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14859
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014860 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14861 /* Enforce that fb modifier and tiling mode match, but only for
14862 * X-tiled. This is needed for FBC. */
14863 if (!!(obj->tiling_mode == I915_TILING_X) !=
14864 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14865 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14866 return -EINVAL;
14867 }
14868 } else {
14869 if (obj->tiling_mode == I915_TILING_X)
14870 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14871 else if (obj->tiling_mode == I915_TILING_Y) {
14872 DRM_DEBUG("No Y tiling for legacy addfb\n");
14873 return -EINVAL;
14874 }
14875 }
14876
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014877 /* Passed in modifier sanity checking. */
14878 switch (mode_cmd->modifier[0]) {
14879 case I915_FORMAT_MOD_Y_TILED:
14880 case I915_FORMAT_MOD_Yf_TILED:
14881 if (INTEL_INFO(dev)->gen < 9) {
14882 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14883 mode_cmd->modifier[0]);
14884 return -EINVAL;
14885 }
14886 case DRM_FORMAT_MOD_NONE:
14887 case I915_FORMAT_MOD_X_TILED:
14888 break;
14889 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014890 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14891 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014892 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014893 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014894
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014895 stride_alignment = intel_fb_stride_alignment(dev_priv,
14896 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014897 mode_cmd->pixel_format);
14898 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14899 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14900 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014901 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014902 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014903
Damien Lespiaub3218032015-02-27 11:15:18 +000014904 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14905 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014906 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014907 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14908 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014909 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014910 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014911 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014912 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014913
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014914 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014915 mode_cmd->pitches[0] != obj->stride) {
14916 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14917 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014918 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014919 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014920
Ville Syrjälä57779d02012-10-31 17:50:14 +020014921 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014922 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014923 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014924 case DRM_FORMAT_RGB565:
14925 case DRM_FORMAT_XRGB8888:
14926 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014927 break;
14928 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014929 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014930 DRM_DEBUG("unsupported pixel format: %s\n",
14931 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014932 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014933 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014934 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014935 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014936 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14937 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014938 DRM_DEBUG("unsupported pixel format: %s\n",
14939 drm_get_format_name(mode_cmd->pixel_format));
14940 return -EINVAL;
14941 }
14942 break;
14943 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014944 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014945 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014946 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014947 DRM_DEBUG("unsupported pixel format: %s\n",
14948 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014949 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014950 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014951 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014952 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014953 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014954 DRM_DEBUG("unsupported pixel format: %s\n",
14955 drm_get_format_name(mode_cmd->pixel_format));
14956 return -EINVAL;
14957 }
14958 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014959 case DRM_FORMAT_YUYV:
14960 case DRM_FORMAT_UYVY:
14961 case DRM_FORMAT_YVYU:
14962 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014963 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014964 DRM_DEBUG("unsupported pixel format: %s\n",
14965 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014966 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014967 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014968 break;
14969 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014970 DRM_DEBUG("unsupported pixel format: %s\n",
14971 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014972 return -EINVAL;
14973 }
14974
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014975 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14976 if (mode_cmd->offsets[0] != 0)
14977 return -EINVAL;
14978
Damien Lespiauec2c9812015-01-20 12:51:45 +000014979 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014980 mode_cmd->pixel_format,
14981 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014982 /* FIXME drm helper for size checks (especially planar formats)? */
14983 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14984 return -EINVAL;
14985
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014986 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14987 intel_fb->obj = obj;
14988
Jesse Barnes79e53942008-11-07 14:24:08 -080014989 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14990 if (ret) {
14991 DRM_ERROR("framebuffer init failed %d\n", ret);
14992 return ret;
14993 }
14994
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014995 intel_fb->obj->framebuffer_references++;
14996
Jesse Barnes79e53942008-11-07 14:24:08 -080014997 return 0;
14998}
14999
Jesse Barnes79e53942008-11-07 14:24:08 -080015000static struct drm_framebuffer *
15001intel_user_framebuffer_create(struct drm_device *dev,
15002 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020015003 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015004{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015005 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015006 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015007 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015008
Jesse Barnes308e5bc2011-11-14 14:51:28 -080015009 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015010 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000015011 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015012 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015013
Daniel Vetter92907cb2015-11-23 09:04:05 +010015014 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015015 if (IS_ERR(fb))
15016 drm_gem_object_unreference_unlocked(&obj->base);
15017
15018 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080015019}
15020
Daniel Vetter06957262015-08-10 13:34:08 +020015021#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020015022static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020015023{
15024}
15025#endif
15026
Jesse Barnes79e53942008-11-07 14:24:08 -080015027static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080015028 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020015029 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080015030 .atomic_check = intel_atomic_check,
15031 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020015032 .atomic_state_alloc = intel_atomic_state_alloc,
15033 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080015034};
15035
Jesse Barnese70236a2009-09-21 10:42:27 -070015036/* Set up chip specific display functions */
15037static void intel_init_display(struct drm_device *dev)
15038{
15039 struct drm_i915_private *dev_priv = dev->dev_private;
15040
Daniel Vetteree9300b2013-06-03 22:40:22 +020015041 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
15042 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030015043 else if (IS_CHERRYVIEW(dev))
15044 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020015045 else if (IS_VALLEYVIEW(dev))
15046 dev_priv->display.find_dpll = vlv_find_best_dpll;
15047 else if (IS_PINEVIEW(dev))
15048 dev_priv->display.find_dpll = pnv_find_best_dpll;
15049 else
15050 dev_priv->display.find_dpll = i9xx_find_best_dpll;
15051
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015052 if (INTEL_INFO(dev)->gen >= 9) {
15053 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015054 dev_priv->display.get_initial_plane_config =
15055 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015056 dev_priv->display.crtc_compute_clock =
15057 haswell_crtc_compute_clock;
15058 dev_priv->display.crtc_enable = haswell_crtc_enable;
15059 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015060 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015061 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015062 dev_priv->display.get_initial_plane_config =
15063 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020015064 dev_priv->display.crtc_compute_clock =
15065 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020015066 dev_priv->display.crtc_enable = haswell_crtc_enable;
15067 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030015068 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015069 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015070 dev_priv->display.get_initial_plane_config =
15071 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015072 dev_priv->display.crtc_compute_clock =
15073 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015074 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15075 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Wayne Boyer666a4532015-12-09 12:29:35 -080015076 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015077 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015078 dev_priv->display.get_initial_plane_config =
15079 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015080 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015081 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15082 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015083 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015084 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015085 dev_priv->display.get_initial_plane_config =
15086 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015087 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015088 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15089 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015090 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015091
Jesse Barnese70236a2009-09-21 10:42:27 -070015092 /* Returns the core display clock speed */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070015093 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015094 dev_priv->display.get_display_clock_speed =
15095 skylake_get_display_clock_speed;
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015096 else if (IS_BROXTON(dev))
15097 dev_priv->display.get_display_clock_speed =
15098 broxton_get_display_clock_speed;
Ville Syrjälä1652d192015-03-31 14:12:01 +030015099 else if (IS_BROADWELL(dev))
15100 dev_priv->display.get_display_clock_speed =
15101 broadwell_get_display_clock_speed;
15102 else if (IS_HASWELL(dev))
15103 dev_priv->display.get_display_clock_speed =
15104 haswell_get_display_clock_speed;
Wayne Boyer666a4532015-12-09 12:29:35 -080015105 else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015106 dev_priv->display.get_display_clock_speed =
15107 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015108 else if (IS_GEN5(dev))
15109 dev_priv->display.get_display_clock_speed =
15110 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030015111 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030015112 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070015113 dev_priv->display.get_display_clock_speed =
15114 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030015115 else if (IS_GM45(dev))
15116 dev_priv->display.get_display_clock_speed =
15117 gm45_get_display_clock_speed;
15118 else if (IS_CRESTLINE(dev))
15119 dev_priv->display.get_display_clock_speed =
15120 i965gm_get_display_clock_speed;
15121 else if (IS_PINEVIEW(dev))
15122 dev_priv->display.get_display_clock_speed =
15123 pnv_get_display_clock_speed;
15124 else if (IS_G33(dev) || IS_G4X(dev))
15125 dev_priv->display.get_display_clock_speed =
15126 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070015127 else if (IS_I915G(dev))
15128 dev_priv->display.get_display_clock_speed =
15129 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020015130 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070015131 dev_priv->display.get_display_clock_speed =
15132 i9xx_misc_get_display_clock_speed;
15133 else if (IS_I915GM(dev))
15134 dev_priv->display.get_display_clock_speed =
15135 i915gm_get_display_clock_speed;
15136 else if (IS_I865G(dev))
15137 dev_priv->display.get_display_clock_speed =
15138 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020015139 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070015140 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015141 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015142 else { /* 830 */
15143 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015144 dev_priv->display.get_display_clock_speed =
15145 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015146 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015147
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015148 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015149 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015150 } else if (IS_GEN6(dev)) {
15151 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015152 } else if (IS_IVYBRIDGE(dev)) {
15153 /* FIXME: detect B0+ stepping and use auto training */
15154 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030015155 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015156 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015157 if (IS_BROADWELL(dev)) {
15158 dev_priv->display.modeset_commit_cdclk =
15159 broadwell_modeset_commit_cdclk;
15160 dev_priv->display.modeset_calc_cdclk =
15161 broadwell_modeset_calc_cdclk;
15162 }
Wayne Boyer666a4532015-12-09 12:29:35 -080015163 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015164 dev_priv->display.modeset_commit_cdclk =
15165 valleyview_modeset_commit_cdclk;
15166 dev_priv->display.modeset_calc_cdclk =
15167 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053015168 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015169 dev_priv->display.modeset_commit_cdclk =
15170 broxton_modeset_commit_cdclk;
15171 dev_priv->display.modeset_calc_cdclk =
15172 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015173 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015174
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015175 switch (INTEL_INFO(dev)->gen) {
15176 case 2:
15177 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15178 break;
15179
15180 case 3:
15181 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15182 break;
15183
15184 case 4:
15185 case 5:
15186 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15187 break;
15188
15189 case 6:
15190 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15191 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015192 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070015193 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015194 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15195 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000015196 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000015197 /* Drop through - unsupported since execlist only. */
15198 default:
15199 /* Default just returns -ENODEV to indicate unsupported */
15200 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015201 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020015202
Ville Syrjäläe39b9992014-09-04 14:53:14 +030015203 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070015204}
15205
Jesse Barnesb690e962010-07-19 13:53:12 -070015206/*
15207 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15208 * resume, or other times. This quirk makes sure that's the case for
15209 * affected systems.
15210 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015211static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015212{
15213 struct drm_i915_private *dev_priv = dev->dev_private;
15214
15215 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015216 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015217}
15218
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015219static void quirk_pipeb_force(struct drm_device *dev)
15220{
15221 struct drm_i915_private *dev_priv = dev->dev_private;
15222
15223 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15224 DRM_INFO("applying pipe b force quirk\n");
15225}
15226
Keith Packard435793d2011-07-12 14:56:22 -070015227/*
15228 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15229 */
15230static void quirk_ssc_force_disable(struct drm_device *dev)
15231{
15232 struct drm_i915_private *dev_priv = dev->dev_private;
15233 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015234 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015235}
15236
Carsten Emde4dca20e2012-03-15 15:56:26 +010015237/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015238 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15239 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015240 */
15241static void quirk_invert_brightness(struct drm_device *dev)
15242{
15243 struct drm_i915_private *dev_priv = dev->dev_private;
15244 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015245 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015246}
15247
Scot Doyle9c72cc62014-07-03 23:27:50 +000015248/* Some VBT's incorrectly indicate no backlight is present */
15249static void quirk_backlight_present(struct drm_device *dev)
15250{
15251 struct drm_i915_private *dev_priv = dev->dev_private;
15252 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15253 DRM_INFO("applying backlight present quirk\n");
15254}
15255
Jesse Barnesb690e962010-07-19 13:53:12 -070015256struct intel_quirk {
15257 int device;
15258 int subsystem_vendor;
15259 int subsystem_device;
15260 void (*hook)(struct drm_device *dev);
15261};
15262
Egbert Eich5f85f172012-10-14 15:46:38 +020015263/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15264struct intel_dmi_quirk {
15265 void (*hook)(struct drm_device *dev);
15266 const struct dmi_system_id (*dmi_id_list)[];
15267};
15268
15269static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15270{
15271 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15272 return 1;
15273}
15274
15275static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15276 {
15277 .dmi_id_list = &(const struct dmi_system_id[]) {
15278 {
15279 .callback = intel_dmi_reverse_brightness,
15280 .ident = "NCR Corporation",
15281 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15282 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15283 },
15284 },
15285 { } /* terminating entry */
15286 },
15287 .hook = quirk_invert_brightness,
15288 },
15289};
15290
Ben Widawskyc43b5632012-04-16 14:07:40 -070015291static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015292 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15293 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15294
Jesse Barnesb690e962010-07-19 13:53:12 -070015295 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15296 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15297
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015298 /* 830 needs to leave pipe A & dpll A up */
15299 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15300
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015301 /* 830 needs to leave pipe B & dpll B up */
15302 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15303
Keith Packard435793d2011-07-12 14:56:22 -070015304 /* Lenovo U160 cannot use SSC on LVDS */
15305 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015306
15307 /* Sony Vaio Y cannot use SSC on LVDS */
15308 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015309
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015310 /* Acer Aspire 5734Z must invert backlight brightness */
15311 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15312
15313 /* Acer/eMachines G725 */
15314 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15315
15316 /* Acer/eMachines e725 */
15317 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15318
15319 /* Acer/Packard Bell NCL20 */
15320 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15321
15322 /* Acer Aspire 4736Z */
15323 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015324
15325 /* Acer Aspire 5336 */
15326 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015327
15328 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15329 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015330
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015331 /* Acer C720 Chromebook (Core i3 4005U) */
15332 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15333
jens steinb2a96012014-10-28 20:25:53 +010015334 /* Apple Macbook 2,1 (Core 2 T7400) */
15335 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15336
Jani Nikula1b9448b02015-11-05 11:49:59 +020015337 /* Apple Macbook 4,1 */
15338 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15339
Scot Doyled4967d82014-07-03 23:27:52 +000015340 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15341 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015342
15343 /* HP Chromebook 14 (Celeron 2955U) */
15344 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015345
15346 /* Dell Chromebook 11 */
15347 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015348
15349 /* Dell Chromebook 11 (2015 version) */
15350 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015351};
15352
15353static void intel_init_quirks(struct drm_device *dev)
15354{
15355 struct pci_dev *d = dev->pdev;
15356 int i;
15357
15358 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15359 struct intel_quirk *q = &intel_quirks[i];
15360
15361 if (d->device == q->device &&
15362 (d->subsystem_vendor == q->subsystem_vendor ||
15363 q->subsystem_vendor == PCI_ANY_ID) &&
15364 (d->subsystem_device == q->subsystem_device ||
15365 q->subsystem_device == PCI_ANY_ID))
15366 q->hook(dev);
15367 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015368 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15369 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15370 intel_dmi_quirks[i].hook(dev);
15371 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015372}
15373
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015374/* Disable the VGA plane that we never use */
15375static void i915_disable_vga(struct drm_device *dev)
15376{
15377 struct drm_i915_private *dev_priv = dev->dev_private;
15378 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015379 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015380
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015381 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015382 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015383 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015384 sr1 = inb(VGA_SR_DATA);
15385 outb(sr1 | 1<<5, VGA_SR_DATA);
15386 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15387 udelay(300);
15388
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015389 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015390 POSTING_READ(vga_reg);
15391}
15392
Daniel Vetterf8175862012-04-10 15:50:11 +020015393void intel_modeset_init_hw(struct drm_device *dev)
15394{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015395 struct drm_i915_private *dev_priv = dev->dev_private;
15396
Ville Syrjäläb6283052015-06-03 15:45:07 +030015397 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015398
15399 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15400
Daniel Vetterf8175862012-04-10 15:50:11 +020015401 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015402 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015403}
15404
Matt Roperd93c0372015-12-03 11:37:41 -080015405/*
15406 * Calculate what we think the watermarks should be for the state we've read
15407 * out of the hardware and then immediately program those watermarks so that
15408 * we ensure the hardware settings match our internal state.
15409 *
15410 * We can calculate what we think WM's should be by creating a duplicate of the
15411 * current state (which was constructed during hardware readout) and running it
15412 * through the atomic check code to calculate new watermark values in the
15413 * state object.
15414 */
15415static void sanitize_watermarks(struct drm_device *dev)
15416{
15417 struct drm_i915_private *dev_priv = to_i915(dev);
15418 struct drm_atomic_state *state;
15419 struct drm_crtc *crtc;
15420 struct drm_crtc_state *cstate;
15421 struct drm_modeset_acquire_ctx ctx;
15422 int ret;
15423 int i;
15424
15425 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015426 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015427 return;
15428
15429 /*
15430 * We need to hold connection_mutex before calling duplicate_state so
15431 * that the connector loop is protected.
15432 */
15433 drm_modeset_acquire_init(&ctx, 0);
15434retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015435 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015436 if (ret == -EDEADLK) {
15437 drm_modeset_backoff(&ctx);
15438 goto retry;
15439 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015440 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015441 }
15442
15443 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15444 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015445 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015446
Matt Ropered4a6a72016-02-23 17:20:13 -080015447 /*
15448 * Hardware readout is the only time we don't want to calculate
15449 * intermediate watermarks (since we don't trust the current
15450 * watermarks).
15451 */
15452 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15453
Matt Roperd93c0372015-12-03 11:37:41 -080015454 ret = intel_atomic_check(dev, state);
15455 if (ret) {
15456 /*
15457 * If we fail here, it means that the hardware appears to be
15458 * programmed in a way that shouldn't be possible, given our
15459 * understanding of watermark requirements. This might mean a
15460 * mistake in the hardware readout code or a mistake in the
15461 * watermark calculations for a given platform. Raise a WARN
15462 * so that this is noticeable.
15463 *
15464 * If this actually happens, we'll have to just leave the
15465 * BIOS-programmed watermarks untouched and hope for the best.
15466 */
15467 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015468 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015469 }
15470
15471 /* Write calculated watermark values back */
15472 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15473 for_each_crtc_in_state(state, crtc, cstate, i) {
15474 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15475
Matt Ropered4a6a72016-02-23 17:20:13 -080015476 cs->wm.need_postvbl_update = true;
15477 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015478 }
15479
15480 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015481fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015482 drm_modeset_drop_locks(&ctx);
15483 drm_modeset_acquire_fini(&ctx);
15484}
15485
Jesse Barnes79e53942008-11-07 14:24:08 -080015486void intel_modeset_init(struct drm_device *dev)
15487{
Jesse Barnes652c3932009-08-17 13:31:43 -070015488 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015489 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015490 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015491 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015492
15493 drm_mode_config_init(dev);
15494
15495 dev->mode_config.min_width = 0;
15496 dev->mode_config.min_height = 0;
15497
Dave Airlie019d96c2011-09-29 16:20:42 +010015498 dev->mode_config.preferred_depth = 24;
15499 dev->mode_config.prefer_shadow = 1;
15500
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015501 dev->mode_config.allow_fb_modifiers = true;
15502
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015503 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015504
Jesse Barnesb690e962010-07-19 13:53:12 -070015505 intel_init_quirks(dev);
15506
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015507 intel_init_pm(dev);
15508
Ben Widawskye3c74752013-04-05 13:12:39 -070015509 if (INTEL_INFO(dev)->num_pipes == 0)
15510 return;
15511
Lukas Wunner69f92f62015-07-15 13:57:35 +020015512 /*
15513 * There may be no VBT; and if the BIOS enabled SSC we can
15514 * just keep using it to avoid unnecessary flicker. Whereas if the
15515 * BIOS isn't using it, don't assume it will work even if the VBT
15516 * indicates as much.
15517 */
15518 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15519 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15520 DREF_SSC1_ENABLE);
15521
15522 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15523 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15524 bios_lvds_use_ssc ? "en" : "dis",
15525 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15526 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15527 }
15528 }
15529
Jesse Barnese70236a2009-09-21 10:42:27 -070015530 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015531 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015532
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015533 if (IS_GEN2(dev)) {
15534 dev->mode_config.max_width = 2048;
15535 dev->mode_config.max_height = 2048;
15536 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015537 dev->mode_config.max_width = 4096;
15538 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015539 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015540 dev->mode_config.max_width = 8192;
15541 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015542 }
Damien Lespiau068be562014-03-28 14:17:49 +000015543
Ville Syrjälädc41c152014-08-13 11:57:05 +030015544 if (IS_845G(dev) || IS_I865G(dev)) {
15545 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15546 dev->mode_config.cursor_height = 1023;
15547 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015548 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15549 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15550 } else {
15551 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15552 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15553 }
15554
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015555 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015556
Zhao Yakui28c97732009-10-09 11:39:41 +080015557 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015558 INTEL_INFO(dev)->num_pipes,
15559 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015560
Damien Lespiau055e3932014-08-18 13:49:10 +010015561 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015562 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015563 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015564 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015565 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015566 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015567 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015568 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015569 }
15570
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015571 intel_update_czclk(dev_priv);
15572 intel_update_cdclk(dev);
15573
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015574 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015575
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015576 /* Just disable it once at startup */
15577 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015578 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015579
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015580 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015581 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015582 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015583
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015584 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015585 struct intel_initial_plane_config plane_config = {};
15586
Jesse Barnes46f297f2014-03-07 08:57:48 -080015587 if (!crtc->active)
15588 continue;
15589
Jesse Barnes46f297f2014-03-07 08:57:48 -080015590 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015591 * Note that reserving the BIOS fb up front prevents us
15592 * from stuffing other stolen allocations like the ring
15593 * on top. This prevents some ugliness at boot time, and
15594 * can even allow for smooth boot transitions if the BIOS
15595 * fb is large enough for the active pipe configuration.
15596 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015597 dev_priv->display.get_initial_plane_config(crtc,
15598 &plane_config);
15599
15600 /*
15601 * If the fb is shared between multiple heads, we'll
15602 * just get the first one.
15603 */
15604 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015605 }
Matt Roperd93c0372015-12-03 11:37:41 -080015606
15607 /*
15608 * Make sure hardware watermarks really match the state we read out.
15609 * Note that we need to do this after reconstructing the BIOS fb's
15610 * since the watermark calculation done here will use pstate->fb.
15611 */
15612 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015613}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015614
Daniel Vetter7fad7982012-07-04 17:51:47 +020015615static void intel_enable_pipe_a(struct drm_device *dev)
15616{
15617 struct intel_connector *connector;
15618 struct drm_connector *crt = NULL;
15619 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015620 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015621
15622 /* We can't just switch on the pipe A, we need to set things up with a
15623 * proper mode and output configuration. As a gross hack, enable pipe A
15624 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015625 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015626 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15627 crt = &connector->base;
15628 break;
15629 }
15630 }
15631
15632 if (!crt)
15633 return;
15634
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015635 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015636 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015637}
15638
Daniel Vetterfa555832012-10-10 23:14:00 +020015639static bool
15640intel_check_plane_mapping(struct intel_crtc *crtc)
15641{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015642 struct drm_device *dev = crtc->base.dev;
15643 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015644 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015645
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015646 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015647 return true;
15648
Ville Syrjälä649636e2015-09-22 19:50:01 +030015649 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015650
15651 if ((val & DISPLAY_PLANE_ENABLE) &&
15652 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15653 return false;
15654
15655 return true;
15656}
15657
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015658static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15659{
15660 struct drm_device *dev = crtc->base.dev;
15661 struct intel_encoder *encoder;
15662
15663 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15664 return true;
15665
15666 return false;
15667}
15668
Ville Syrjälädd756192016-02-17 21:28:45 +020015669static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15670{
15671 struct drm_device *dev = encoder->base.dev;
15672 struct intel_connector *connector;
15673
15674 for_each_connector_on_encoder(dev, &encoder->base, connector)
15675 return true;
15676
15677 return false;
15678}
15679
Daniel Vetter24929352012-07-02 20:28:59 +020015680static void intel_sanitize_crtc(struct intel_crtc *crtc)
15681{
15682 struct drm_device *dev = crtc->base.dev;
15683 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015684 i915_reg_t reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015685
Daniel Vetter24929352012-07-02 20:28:59 +020015686 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter24929352012-07-02 20:28:59 +020015687 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15688
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015689 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015690 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015691 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015692 struct intel_plane *plane;
15693
Daniel Vetter96256042015-02-13 21:03:42 +010015694 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015695
15696 /* Disable everything but the primary plane */
15697 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15698 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15699 continue;
15700
15701 plane->disable_plane(&plane->base, &crtc->base);
15702 }
Daniel Vetter96256042015-02-13 21:03:42 +010015703 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015704
Daniel Vetter24929352012-07-02 20:28:59 +020015705 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015706 * disable the crtc (and hence change the state) if it is wrong. Note
15707 * that gen4+ has a fixed plane -> pipe mapping. */
15708 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015709 bool plane;
15710
Daniel Vetter24929352012-07-02 20:28:59 +020015711 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15712 crtc->base.base.id);
15713
15714 /* Pipe has the wrong plane attached and the plane is active.
15715 * Temporarily change the plane mapping and disable everything
15716 * ... */
15717 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015718 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015719 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015720 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015721 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015722 }
Daniel Vetter24929352012-07-02 20:28:59 +020015723
Daniel Vetter7fad7982012-07-04 17:51:47 +020015724 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15725 crtc->pipe == PIPE_A && !crtc->active) {
15726 /* BIOS forgot to enable pipe A, this mostly happens after
15727 * resume. Force-enable the pipe to fix this, the update_dpms
15728 * call below we restore the pipe to the right state, but leave
15729 * the required bits on. */
15730 intel_enable_pipe_a(dev);
15731 }
15732
Daniel Vetter24929352012-07-02 20:28:59 +020015733 /* Adjust the state of the output pipe according to whether we
15734 * have active connectors/encoders. */
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015735 if (!intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015736 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015737
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015738 if (crtc->active != crtc->base.state->active) {
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015739 struct intel_encoder *encoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015740
15741 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015742 * functions or because of calls to intel_crtc_disable_noatomic,
15743 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015744 * pipe A quirk. */
15745 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15746 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015747 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015748 crtc->active ? "enabled" : "disabled");
15749
Maarten Lankhorst4be40c92015-07-14 13:45:32 +020015750 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015751 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015752 crtc->base.enabled = crtc->active;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015753 crtc->base.state->connector_mask = 0;
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015754 crtc->base.state->encoder_mask = 0;
Daniel Vetter24929352012-07-02 20:28:59 +020015755
15756 /* Because we only establish the connector -> encoder ->
15757 * crtc links if something is active, this means the
15758 * crtc is now deactivated. Break the links. connector
15759 * -> encoder links are only establish when things are
15760 * actually up, hence no need to break them. */
15761 WARN_ON(crtc->active);
15762
Maarten Lankhorst2d406bb2015-08-05 12:37:09 +020015763 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetter24929352012-07-02 20:28:59 +020015764 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015765 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015766
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015767 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015768 /*
15769 * We start out with underrun reporting disabled to avoid races.
15770 * For correct bookkeeping mark this on active crtcs.
15771 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015772 * Also on gmch platforms we dont have any hardware bits to
15773 * disable the underrun reporting. Which means we need to start
15774 * out with underrun reporting disabled also on inactive pipes,
15775 * since otherwise we'll complain about the garbage we read when
15776 * e.g. coming up after runtime pm.
15777 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015778 * No protection against concurrent access is required - at
15779 * worst a fifo underrun happens which also sets this to false.
15780 */
15781 crtc->cpu_fifo_underrun_disabled = true;
15782 crtc->pch_fifo_underrun_disabled = true;
15783 }
Daniel Vetter24929352012-07-02 20:28:59 +020015784}
15785
15786static void intel_sanitize_encoder(struct intel_encoder *encoder)
15787{
15788 struct intel_connector *connector;
15789 struct drm_device *dev = encoder->base.dev;
15790
15791 /* We need to check both for a crtc link (meaning that the
15792 * encoder is active and trying to read from a pipe) and the
15793 * pipe itself being active. */
15794 bool has_active_crtc = encoder->base.crtc &&
15795 to_intel_crtc(encoder->base.crtc)->active;
15796
Ville Syrjälädd756192016-02-17 21:28:45 +020015797 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015798 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15799 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015800 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015801
15802 /* Connector is active, but has no active pipe. This is
15803 * fallout from our resume register restoring. Disable
15804 * the encoder manually again. */
15805 if (encoder->base.crtc) {
15806 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15807 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015808 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015809 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015810 if (encoder->post_disable)
15811 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015812 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015813 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015814
15815 /* Inconsistent output/port/pipe state happens presumably due to
15816 * a bug in one of the get_hw_state functions. Or someplace else
15817 * in our code, like the register restore mess on resume. Clamp
15818 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015819 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015820 if (connector->encoder != encoder)
15821 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015822 connector->base.dpms = DRM_MODE_DPMS_OFF;
15823 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015824 }
15825 }
15826 /* Enabled encoders without active connectors will be fixed in
15827 * the crtc fixup. */
15828}
15829
Imre Deak04098752014-02-18 00:02:16 +020015830void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015831{
15832 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015833 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015834
Imre Deak04098752014-02-18 00:02:16 +020015835 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15836 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15837 i915_disable_vga(dev);
15838 }
15839}
15840
15841void i915_redisable_vga(struct drm_device *dev)
15842{
15843 struct drm_i915_private *dev_priv = dev->dev_private;
15844
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015845 /* This function can be called both from intel_modeset_setup_hw_state or
15846 * at a very early point in our resume sequence, where the power well
15847 * structures are not yet restored. Since this function is at a very
15848 * paranoid "someone might have enabled VGA while we were not looking"
15849 * level, just check if the power well is enabled instead of trying to
15850 * follow the "don't touch the power well if we don't need it" policy
15851 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015852 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015853 return;
15854
Imre Deak04098752014-02-18 00:02:16 +020015855 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015856
15857 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015858}
15859
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015860static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015861{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015862 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015863
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015864 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015865}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015866
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015867/* FIXME read out full plane state for all planes */
15868static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015869{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015870 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015871 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015872 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015873
Matt Roper19b8d382015-09-24 15:53:17 -070015874 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015875 primary_get_hw_state(to_intel_plane(primary));
15876
15877 if (plane_state->visible)
15878 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015879}
15880
Daniel Vetter30e984d2013-06-05 13:34:17 +020015881static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015882{
15883 struct drm_i915_private *dev_priv = dev->dev_private;
15884 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015885 struct intel_crtc *crtc;
15886 struct intel_encoder *encoder;
15887 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015888 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015889
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015890 dev_priv->active_crtcs = 0;
15891
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015892 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015893 struct intel_crtc_state *crtc_state = crtc->config;
15894 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015895
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015896 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15897 memset(crtc_state, 0, sizeof(*crtc_state));
15898 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015899
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015900 crtc_state->base.active = crtc_state->base.enable =
15901 dev_priv->display.get_pipe_config(crtc, crtc_state);
15902
15903 crtc->base.enabled = crtc_state->base.enable;
15904 crtc->active = crtc_state->base.active;
15905
15906 if (crtc_state->base.active) {
15907 dev_priv->active_crtcs |= 1 << crtc->pipe;
15908
15909 if (IS_BROADWELL(dev_priv)) {
15910 pixclk = ilk_pipe_pixel_rate(crtc_state);
15911
15912 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15913 if (crtc_state->ips_enabled)
15914 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15915 } else if (IS_VALLEYVIEW(dev_priv) ||
15916 IS_CHERRYVIEW(dev_priv) ||
15917 IS_BROXTON(dev_priv))
15918 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15919 else
15920 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15921 }
15922
15923 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015924
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015925 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015926
15927 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15928 crtc->base.base.id,
15929 crtc->active ? "enabled" : "disabled");
15930 }
15931
Daniel Vetter53589012013-06-05 13:34:16 +020015932 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15933 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15934
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015935 pll->on = pll->get_hw_state(dev_priv, pll,
15936 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015937 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015938 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015939 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015940 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015941 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015942 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015943 }
Daniel Vetter53589012013-06-05 13:34:16 +020015944 }
Daniel Vetter53589012013-06-05 13:34:16 +020015945
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015946 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015947 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015948
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015949 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015950 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015951 }
15952
Damien Lespiaub2784e12014-08-05 11:29:37 +010015953 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015954 pipe = 0;
15955
15956 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015957 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15958 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015959 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015960 } else {
15961 encoder->base.crtc = NULL;
15962 }
15963
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015964 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015965 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015966 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015967 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015968 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015969 }
15970
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015971 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015972 if (connector->get_hw_state(connector)) {
15973 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015974
15975 encoder = connector->encoder;
15976 connector->base.encoder = &encoder->base;
15977
15978 if (encoder->base.crtc &&
15979 encoder->base.crtc->state->active) {
15980 /*
15981 * This has to be done during hardware readout
15982 * because anything calling .crtc_disable may
15983 * rely on the connector_mask being accurate.
15984 */
15985 encoder->base.crtc->state->connector_mask |=
15986 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015987 encoder->base.crtc->state->encoder_mask |=
15988 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015989 }
15990
Daniel Vetter24929352012-07-02 20:28:59 +020015991 } else {
15992 connector->base.dpms = DRM_MODE_DPMS_OFF;
15993 connector->base.encoder = NULL;
15994 }
15995 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15996 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015997 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015998 connector->base.encoder ? "enabled" : "disabled");
15999 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016000
16001 for_each_intel_crtc(dev, crtc) {
16002 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16003
16004 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16005 if (crtc->base.state->active) {
16006 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16007 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16008 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16009
16010 /*
16011 * The initial mode needs to be set in order to keep
16012 * the atomic core happy. It wants a valid mode if the
16013 * crtc's enabled, so we do the above call.
16014 *
16015 * At this point some state updated by the connectors
16016 * in their ->detect() callback has not run yet, so
16017 * no recalculation can be done yet.
16018 *
16019 * Even if we could do a recalculation and modeset
16020 * right now it would cause a double modeset if
16021 * fbdev or userspace chooses a different initial mode.
16022 *
16023 * If that happens, someone indicated they wanted a
16024 * mode change, which means it's safe to do a full
16025 * recalculation.
16026 */
16027 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030016028
16029 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16030 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016031 }
16032 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020016033}
16034
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016035/* Scan out the current hw modeset state,
16036 * and sanitizes it to the current state
16037 */
16038static void
16039intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020016040{
16041 struct drm_i915_private *dev_priv = dev->dev_private;
16042 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016043 struct intel_crtc *crtc;
16044 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020016045 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016046
16047 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016048
16049 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010016050 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016051 intel_sanitize_encoder(encoder);
16052 }
16053
Damien Lespiau055e3932014-08-18 13:49:10 +010016054 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020016055 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16056 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016057 intel_dump_pipe_config(crtc, crtc->config,
16058 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020016059 }
Daniel Vetter9a935852012-07-05 22:34:27 +020016060
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020016061 intel_modeset_update_connector_atomic_state(dev);
16062
Daniel Vetter35c95372013-07-17 06:55:04 +020016063 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16064 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16065
16066 if (!pll->on || pll->active)
16067 continue;
16068
16069 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16070
16071 pll->disable(dev_priv, pll);
16072 pll->on = false;
16073 }
16074
Wayne Boyer666a4532015-12-09 12:29:35 -080016075 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030016076 vlv_wm_get_hw_state(dev);
16077 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000016078 skl_wm_get_hw_state(dev);
16079 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030016080 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016081
16082 for_each_intel_crtc(dev, crtc) {
16083 unsigned long put_domains;
16084
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010016085 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016086 if (WARN_ON(put_domains))
16087 modeset_put_power_domains(dev_priv, put_domains);
16088 }
16089 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020016090
16091 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016092}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030016093
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016094void intel_display_resume(struct drm_device *dev)
16095{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016096 struct drm_i915_private *dev_priv = to_i915(dev);
16097 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16098 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016099 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016100 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020016101
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016102 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016103
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016104 /*
16105 * This is a cludge because with real atomic modeset mode_config.mutex
16106 * won't be taken. Unfortunately some probed state like
16107 * audio_codec_enable is still protected by mode_config.mutex, so lock
16108 * it here for now.
16109 */
16110 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016111 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016112
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016113retry:
16114 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016115
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016116 if (ret == 0 && !setup) {
16117 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016118
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016119 intel_modeset_setup_hw_state(dev);
16120 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010016121 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020016122
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016123 if (ret == 0 && state) {
16124 struct drm_crtc_state *crtc_state;
16125 struct drm_crtc *crtc;
16126 int i;
16127
16128 state->acquire_ctx = &ctx;
16129
16130 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16131 /*
16132 * Force recalculation even if we restore
16133 * current state. With fast modeset this may not result
16134 * in a modeset when the state is compatible.
16135 */
16136 crtc_state->mode_changed = true;
16137 }
16138
16139 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016140 }
16141
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016142 if (ret == -EDEADLK) {
16143 drm_modeset_backoff(&ctx);
16144 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016145 }
16146
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016147 drm_modeset_drop_locks(&ctx);
16148 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016149 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016150
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016151 if (ret) {
16152 DRM_ERROR("Restoring old state failed with %i\n", ret);
16153 drm_atomic_state_free(state);
16154 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010016155}
16156
16157void intel_modeset_gem_init(struct drm_device *dev)
16158{
Jesse Barnes484b41d2014-03-07 08:57:55 -080016159 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016160 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016161 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016162
Imre Deakae484342014-03-31 15:10:44 +030016163 intel_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +030016164
Chris Wilson1833b132012-05-09 11:56:28 +010016165 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016166
16167 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016168
16169 /*
16170 * Make sure any fbs we allocated at startup are properly
16171 * pinned & fenced. When we do the allocation it's too early
16172 * for this.
16173 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016174 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016175 obj = intel_fb_obj(c->primary->fb);
16176 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016177 continue;
16178
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016179 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016180 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16181 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016182 mutex_unlock(&dev->struct_mutex);
16183 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016184 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16185 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016186 drm_framebuffer_unreference(c->primary->fb);
16187 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016188 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080016189 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016190 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016191 }
16192 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016193
16194 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016195}
16196
Imre Deak4932e2c2014-02-11 17:12:48 +020016197void intel_connector_unregister(struct intel_connector *intel_connector)
16198{
16199 struct drm_connector *connector = &intel_connector->base;
16200
16201 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010016202 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016203}
16204
Jesse Barnes79e53942008-11-07 14:24:08 -080016205void intel_modeset_cleanup(struct drm_device *dev)
16206{
Jesse Barnes652c3932009-08-17 13:31:43 -070016207 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020016208 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070016209
Imre Deak2eb52522014-11-19 15:30:05 +020016210 intel_disable_gt_powersave(dev);
16211
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016212 intel_backlight_unregister(dev);
16213
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016214 /*
16215 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016216 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016217 * experience fancy races otherwise.
16218 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016219 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016220
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016221 /*
16222 * Due to the hpd irq storm handling the hotplug work can re-arm the
16223 * poll handlers. Hence disable polling after hpd handling is shut down.
16224 */
Keith Packardf87ea762010-10-03 19:36:26 -070016225 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016226
Jesse Barnes723bfd72010-10-07 16:01:13 -070016227 intel_unregister_dsm_handler();
16228
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016229 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016230
Chris Wilson1630fe72011-07-08 12:22:42 +010016231 /* flush any delayed tasks or pending work */
16232 flush_scheduled_work();
16233
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016234 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016235 for_each_intel_connector(dev, connector)
16236 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016237
Jesse Barnes79e53942008-11-07 14:24:08 -080016238 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016239
16240 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030016241
Imre Deakae484342014-03-31 15:10:44 +030016242 intel_cleanup_gt_powersave(dev);
Daniel Vetterf5949142016-01-13 11:55:28 +010016243
16244 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016245}
16246
Dave Airlie28d52042009-09-21 14:33:58 +100016247/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016248 * Return which encoder is currently attached for connector.
16249 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016250struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016251{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016252 return &intel_attached_encoder(connector)->base;
16253}
Jesse Barnes79e53942008-11-07 14:24:08 -080016254
Chris Wilsondf0e9242010-09-09 16:20:55 +010016255void intel_connector_attach_encoder(struct intel_connector *connector,
16256 struct intel_encoder *encoder)
16257{
16258 connector->encoder = encoder;
16259 drm_mode_connector_attach_encoder(&connector->base,
16260 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016261}
Dave Airlie28d52042009-09-21 14:33:58 +100016262
16263/*
16264 * set vga decode state - true == enable VGA decode
16265 */
16266int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16267{
16268 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016269 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016270 u16 gmch_ctrl;
16271
Chris Wilson75fa0412014-02-07 18:37:02 -020016272 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16273 DRM_ERROR("failed to read control word\n");
16274 return -EIO;
16275 }
16276
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016277 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16278 return 0;
16279
Dave Airlie28d52042009-09-21 14:33:58 +100016280 if (state)
16281 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16282 else
16283 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016284
16285 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16286 DRM_ERROR("failed to write control word\n");
16287 return -EIO;
16288 }
16289
Dave Airlie28d52042009-09-21 14:33:58 +100016290 return 0;
16291}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016292
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016293struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016294
16295 u32 power_well_driver;
16296
Chris Wilson63b66e52013-08-08 15:12:06 +020016297 int num_transcoders;
16298
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016299 struct intel_cursor_error_state {
16300 u32 control;
16301 u32 position;
16302 u32 base;
16303 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016304 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016305
16306 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016307 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016308 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030016309 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016310 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016311
16312 struct intel_plane_error_state {
16313 u32 control;
16314 u32 stride;
16315 u32 size;
16316 u32 pos;
16317 u32 addr;
16318 u32 surface;
16319 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016320 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016321
16322 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016323 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016324 enum transcoder cpu_transcoder;
16325
16326 u32 conf;
16327
16328 u32 htotal;
16329 u32 hblank;
16330 u32 hsync;
16331 u32 vtotal;
16332 u32 vblank;
16333 u32 vsync;
16334 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016335};
16336
16337struct intel_display_error_state *
16338intel_display_capture_error_state(struct drm_device *dev)
16339{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016340 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016341 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016342 int transcoders[] = {
16343 TRANSCODER_A,
16344 TRANSCODER_B,
16345 TRANSCODER_C,
16346 TRANSCODER_EDP,
16347 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016348 int i;
16349
Chris Wilson63b66e52013-08-08 15:12:06 +020016350 if (INTEL_INFO(dev)->num_pipes == 0)
16351 return NULL;
16352
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016353 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016354 if (error == NULL)
16355 return NULL;
16356
Imre Deak190be112013-11-25 17:15:31 +020016357 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016358 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16359
Damien Lespiau055e3932014-08-18 13:49:10 +010016360 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016361 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016362 __intel_display_power_is_enabled(dev_priv,
16363 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016364 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016365 continue;
16366
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016367 error->cursor[i].control = I915_READ(CURCNTR(i));
16368 error->cursor[i].position = I915_READ(CURPOS(i));
16369 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016370
16371 error->plane[i].control = I915_READ(DSPCNTR(i));
16372 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016373 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016374 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016375 error->plane[i].pos = I915_READ(DSPPOS(i));
16376 }
Paulo Zanonica291362013-03-06 20:03:14 -030016377 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16378 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016379 if (INTEL_INFO(dev)->gen >= 4) {
16380 error->plane[i].surface = I915_READ(DSPSURF(i));
16381 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16382 }
16383
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016384 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030016385
Sonika Jindal3abfce72014-07-21 15:23:43 +053016386 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030016387 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016388 }
16389
16390 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16391 if (HAS_DDI(dev_priv->dev))
16392 error->num_transcoders++; /* Account for eDP. */
16393
16394 for (i = 0; i < error->num_transcoders; i++) {
16395 enum transcoder cpu_transcoder = transcoders[i];
16396
Imre Deakddf9c532013-11-27 22:02:02 +020016397 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016398 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016399 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016400 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016401 continue;
16402
Chris Wilson63b66e52013-08-08 15:12:06 +020016403 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16404
16405 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16406 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16407 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16408 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16409 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16410 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16411 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016412 }
16413
16414 return error;
16415}
16416
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016417#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16418
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016419void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016420intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016421 struct drm_device *dev,
16422 struct intel_display_error_state *error)
16423{
Damien Lespiau055e3932014-08-18 13:49:10 +010016424 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016425 int i;
16426
Chris Wilson63b66e52013-08-08 15:12:06 +020016427 if (!error)
16428 return;
16429
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016430 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016431 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016432 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016433 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016434 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016435 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016436 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016437 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016438 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030016439 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016440
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016441 err_printf(m, "Plane [%d]:\n", i);
16442 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16443 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016444 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016445 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16446 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016447 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016448 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016449 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016450 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016451 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16452 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016453 }
16454
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016455 err_printf(m, "Cursor [%d]:\n", i);
16456 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16457 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16458 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016459 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016460
16461 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010016462 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016463 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016464 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016465 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016466 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16467 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16468 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16469 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16470 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16471 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16472 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16473 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016474}