blob: 9c20c2ee742f5b6e100ca11e88b51a50198f9fde [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 -08001153static const char *state_string(bool enabled)
1154{
1155 return enabled ? "on" : "off";
1156}
1157
1158/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001159void assert_pll(struct drm_i915_private *dev_priv,
1160 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001161{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001162 u32 val;
1163 bool cur_state;
1164
Ville Syrjälä649636e2015-09-22 19:50:01 +03001165 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001166 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001167 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001168 "PLL state assertion failure (expected %s, current %s)\n",
1169 state_string(state), state_string(cur_state));
1170}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001171
Jani Nikula23538ef2013-08-27 15:12:22 +03001172/* XXX: the dsi pll is shared between MIPI DSI ports */
1173static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1174{
1175 u32 val;
1176 bool cur_state;
1177
Ville Syrjäläa5805162015-05-26 20:42:30 +03001178 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001179 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001180 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001181
1182 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001183 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001184 "DSI PLL state assertion failure (expected %s, current %s)\n",
1185 state_string(state), state_string(cur_state));
1186}
1187#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1188#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1189
Daniel Vetter55607e82013-06-16 21:42:39 +02001190struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001191intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001192{
Daniel Vettere2b78262013-06-07 23:10:03 +02001193 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1194
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001195 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001196 return NULL;
1197
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001198 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001199}
1200
Jesse Barnesb24e7172011-01-04 15:09:30 -08001201/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001202void assert_shared_dpll(struct drm_i915_private *dev_priv,
1203 struct intel_shared_dpll *pll,
1204 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001205{
Jesse Barnes040484a2011-01-03 12:14:26 -08001206 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001207 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001208
Chris Wilson92b27b02012-05-20 18:10:50 +01001209 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001210 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001211 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001212
Daniel Vetter53589012013-06-05 13:34:16 +02001213 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001214 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001215 "%s assertion failure (expected %s, current %s)\n",
1216 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001217}
Jesse Barnes040484a2011-01-03 12:14:26 -08001218
1219static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1220 enum pipe pipe, bool state)
1221{
Jesse Barnes040484a2011-01-03 12:14:26 -08001222 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001223 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1224 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001225
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001226 if (HAS_DDI(dev_priv->dev)) {
1227 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001228 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001229 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001230 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001231 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001232 cur_state = !!(val & FDI_TX_ENABLE);
1233 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001234 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001235 "FDI TX state assertion failure (expected %s, current %s)\n",
1236 state_string(state), state_string(cur_state));
1237}
1238#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1239#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1240
1241static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1242 enum pipe pipe, bool state)
1243{
Jesse Barnes040484a2011-01-03 12:14:26 -08001244 u32 val;
1245 bool cur_state;
1246
Ville Syrjälä649636e2015-09-22 19:50:01 +03001247 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001248 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001249 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001250 "FDI RX state assertion failure (expected %s, current %s)\n",
1251 state_string(state), state_string(cur_state));
1252}
1253#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1254#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1255
1256static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1257 enum pipe pipe)
1258{
Jesse Barnes040484a2011-01-03 12:14:26 -08001259 u32 val;
1260
1261 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001262 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001263 return;
1264
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001265 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001266 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001267 return;
1268
Ville Syrjälä649636e2015-09-22 19:50:01 +03001269 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001270 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 -08001271}
1272
Daniel Vetter55607e82013-06-16 21:42:39 +02001273void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1274 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001275{
Jesse Barnes040484a2011-01-03 12:14:26 -08001276 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001277 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001278
Ville Syrjälä649636e2015-09-22 19:50:01 +03001279 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001280 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001281 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001282 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1283 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001284}
1285
Daniel Vetterb680c372014-09-19 18:27:27 +02001286void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1287 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001288{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001289 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001290 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001291 u32 val;
1292 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001293 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001294
Jani Nikulabedd4db2014-08-22 15:04:13 +03001295 if (WARN_ON(HAS_DDI(dev)))
1296 return;
1297
1298 if (HAS_PCH_SPLIT(dev)) {
1299 u32 port_sel;
1300
Jesse Barnesea0760c2011-01-04 15:09:32 -08001301 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001302 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1303
1304 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1305 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1306 panel_pipe = PIPE_B;
1307 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001308 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001309 /* presumably write lock depends on pipe, not port select */
1310 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1311 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001312 } else {
1313 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001314 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1315 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001316 }
1317
1318 val = I915_READ(pp_reg);
1319 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001320 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001321 locked = false;
1322
Rob Clarke2c719b2014-12-15 13:56:32 -05001323 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001324 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001325 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001326}
1327
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001328static void assert_cursor(struct drm_i915_private *dev_priv,
1329 enum pipe pipe, bool state)
1330{
1331 struct drm_device *dev = dev_priv->dev;
1332 bool cur_state;
1333
Paulo Zanonid9d82082014-02-27 16:30:56 -03001334 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001335 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001336 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001337 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001338
Rob Clarke2c719b2014-12-15 13:56:32 -05001339 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001340 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1341 pipe_name(pipe), state_string(state), state_string(cur_state));
1342}
1343#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1344#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1345
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001346void assert_pipe(struct drm_i915_private *dev_priv,
1347 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001348{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001349 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001350 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1351 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001352
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001353 /* if we need the pipe quirk it must be always on */
1354 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1355 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001356 state = true;
1357
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001358 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001359 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001360 cur_state = false;
1361 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001362 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001363 cur_state = !!(val & PIPECONF_ENABLE);
1364 }
1365
Rob Clarke2c719b2014-12-15 13:56:32 -05001366 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001367 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001368 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001369}
1370
Chris Wilson931872f2012-01-16 23:01:13 +00001371static void assert_plane(struct drm_i915_private *dev_priv,
1372 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001373{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001374 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001375 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001376
Ville Syrjälä649636e2015-09-22 19:50:01 +03001377 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001378 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001379 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001380 "plane %c assertion failure (expected %s, current %s)\n",
1381 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001382}
1383
Chris Wilson931872f2012-01-16 23:01:13 +00001384#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1385#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1386
Jesse Barnesb24e7172011-01-04 15:09:30 -08001387static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1388 enum pipe pipe)
1389{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001390 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001391 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001392
Ville Syrjälä653e1022013-06-04 13:49:05 +03001393 /* Primary planes are fixed to pipes on gen4+ */
1394 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001395 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001396 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001397 "plane %c assertion failure, should be disabled but not\n",
1398 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001399 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001400 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001401
Jesse Barnesb24e7172011-01-04 15:09:30 -08001402 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001403 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001404 u32 val = I915_READ(DSPCNTR(i));
1405 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001406 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001407 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001408 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1409 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001410 }
1411}
1412
Jesse Barnes19332d72013-03-28 09:55:38 -07001413static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1414 enum pipe pipe)
1415{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001416 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001417 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001418
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001419 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001420 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001421 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001422 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001423 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1424 sprite, pipe_name(pipe));
1425 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001426 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001427 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001428 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001429 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001430 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001431 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001432 }
1433 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001434 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001435 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001436 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001437 plane_name(pipe), pipe_name(pipe));
1438 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001439 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001440 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001441 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1442 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001443 }
1444}
1445
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001446static void assert_vblank_disabled(struct drm_crtc *crtc)
1447{
Rob Clarke2c719b2014-12-15 13:56:32 -05001448 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001449 drm_crtc_vblank_put(crtc);
1450}
1451
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001452static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001453{
1454 u32 val;
1455 bool enabled;
1456
Rob Clarke2c719b2014-12-15 13:56:32 -05001457 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001458
Jesse Barnes92f25842011-01-04 15:09:34 -08001459 val = I915_READ(PCH_DREF_CONTROL);
1460 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1461 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001462 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001463}
1464
Daniel Vetterab9412b2013-05-03 11:49:46 +02001465static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1466 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001467{
Jesse Barnes92f25842011-01-04 15:09:34 -08001468 u32 val;
1469 bool enabled;
1470
Ville Syrjälä649636e2015-09-22 19:50:01 +03001471 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001472 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001473 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001474 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1475 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001476}
1477
Keith Packard4e634382011-08-06 10:39:45 -07001478static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1479 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001480{
1481 if ((val & DP_PORT_EN) == 0)
1482 return false;
1483
1484 if (HAS_PCH_CPT(dev_priv->dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001485 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001486 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1487 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001488 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1489 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1490 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001491 } else {
1492 if ((val & DP_PIPE_MASK) != (pipe << 30))
1493 return false;
1494 }
1495 return true;
1496}
1497
Keith Packard1519b992011-08-06 10:35:34 -07001498static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1499 enum pipe pipe, u32 val)
1500{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001501 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001502 return false;
1503
1504 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001505 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001506 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001507 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1508 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1509 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001510 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001511 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001512 return false;
1513 }
1514 return true;
1515}
1516
1517static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1518 enum pipe pipe, u32 val)
1519{
1520 if ((val & LVDS_PORT_EN) == 0)
1521 return false;
1522
1523 if (HAS_PCH_CPT(dev_priv->dev)) {
1524 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1525 return false;
1526 } else {
1527 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1528 return false;
1529 }
1530 return true;
1531}
1532
1533static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1534 enum pipe pipe, u32 val)
1535{
1536 if ((val & ADPA_DAC_ENABLE) == 0)
1537 return false;
1538 if (HAS_PCH_CPT(dev_priv->dev)) {
1539 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1540 return false;
1541 } else {
1542 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1543 return false;
1544 }
1545 return true;
1546}
1547
Jesse Barnes291906f2011-02-02 12:28:03 -08001548static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001549 enum pipe pipe, i915_reg_t reg,
1550 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001551{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001552 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001553 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001554 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001555 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001556
Rob Clarke2c719b2014-12-15 13:56:32 -05001557 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001558 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001559 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001560}
1561
1562static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001563 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001564{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001565 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001566 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001567 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001568 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001569
Rob Clarke2c719b2014-12-15 13:56:32 -05001570 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001571 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001572 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001573}
1574
1575static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1576 enum pipe pipe)
1577{
Jesse Barnes291906f2011-02-02 12:28:03 -08001578 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001579
Keith Packardf0575e92011-07-25 22:12:43 -07001580 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1581 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1582 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001583
Ville Syrjälä649636e2015-09-22 19:50:01 +03001584 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001585 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001586 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001587 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001588
Ville Syrjälä649636e2015-09-22 19:50:01 +03001589 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001590 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001591 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001592 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001593
Paulo Zanonie2debe92013-02-18 19:00:27 -03001594 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1595 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1596 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001597}
1598
Ville Syrjäläd288f652014-10-28 13:20:22 +02001599static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001600 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001601{
Daniel Vetter426115c2013-07-11 22:13:42 +02001602 struct drm_device *dev = crtc->base.dev;
1603 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001604 i915_reg_t reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001605 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001606
Daniel Vetter426115c2013-07-11 22:13:42 +02001607 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001608
Daniel Vetter87442f72013-06-06 00:52:17 +02001609 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001610 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001611 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001612
Daniel Vetter426115c2013-07-11 22:13:42 +02001613 I915_WRITE(reg, dpll);
1614 POSTING_READ(reg);
1615 udelay(150);
1616
1617 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1618 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1619
Ville Syrjäläd288f652014-10-28 13:20:22 +02001620 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001621 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001622
1623 /* We do this three times for luck */
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 */
Daniel Vetter426115c2013-07-11 22:13:42 +02001630 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001631 POSTING_READ(reg);
1632 udelay(150); /* wait for warmup */
1633}
1634
Ville Syrjäläd288f652014-10-28 13:20:22 +02001635static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001636 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001637{
1638 struct drm_device *dev = crtc->base.dev;
1639 struct drm_i915_private *dev_priv = dev->dev_private;
1640 int pipe = crtc->pipe;
1641 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001642 u32 tmp;
1643
1644 assert_pipe_disabled(dev_priv, crtc->pipe);
1645
Ville Syrjäläa5805162015-05-26 20:42:30 +03001646 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001647
1648 /* Enable back the 10bit clock to display controller */
1649 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1650 tmp |= DPIO_DCLKP_EN;
1651 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1652
Ville Syrjälä54433e92015-05-26 20:42:31 +03001653 mutex_unlock(&dev_priv->sb_lock);
1654
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001655 /*
1656 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1657 */
1658 udelay(1);
1659
1660 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001661 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001662
1663 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001664 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001665 DRM_ERROR("PLL %d failed to lock\n", pipe);
1666
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001667 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001668 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001669 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001670}
1671
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001672static int intel_num_dvo_pipes(struct drm_device *dev)
1673{
1674 struct intel_crtc *crtc;
1675 int count = 0;
1676
1677 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001678 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001679 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001680
1681 return count;
1682}
1683
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001684static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001685{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001686 struct drm_device *dev = crtc->base.dev;
1687 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001688 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001689 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001690
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001691 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001692
1693 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001694 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001695
1696 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001697 if (IS_MOBILE(dev) && !IS_I830(dev))
1698 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001699
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001700 /* Enable DVO 2x clock on both PLLs if necessary */
1701 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1702 /*
1703 * It appears to be important that we don't enable this
1704 * for the current pipe before otherwise configuring the
1705 * PLL. No idea how this should be handled if multiple
1706 * DVO outputs are enabled simultaneosly.
1707 */
1708 dpll |= DPLL_DVO_2X_MODE;
1709 I915_WRITE(DPLL(!crtc->pipe),
1710 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1711 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001712
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001713 /*
1714 * Apparently we need to have VGA mode enabled prior to changing
1715 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1716 * dividers, even though the register value does change.
1717 */
1718 I915_WRITE(reg, 0);
1719
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001720 I915_WRITE(reg, dpll);
1721
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001722 /* Wait for the clocks to stabilize. */
1723 POSTING_READ(reg);
1724 udelay(150);
1725
1726 if (INTEL_INFO(dev)->gen >= 4) {
1727 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001728 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001729 } else {
1730 /* The pixel multiplier can only be updated once the
1731 * DPLL is enabled and the clocks are stable.
1732 *
1733 * So write it again.
1734 */
1735 I915_WRITE(reg, dpll);
1736 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001737
1738 /* We do this three times for luck */
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 */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001745 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001746 POSTING_READ(reg);
1747 udelay(150); /* wait for warmup */
1748}
1749
1750/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001751 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001752 * @dev_priv: i915 private structure
1753 * @pipe: pipe PLL to disable
1754 *
1755 * Disable the PLL for @pipe, making sure the pipe is off first.
1756 *
1757 * Note! This is for pre-ILK only.
1758 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001759static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001760{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001761 struct drm_device *dev = crtc->base.dev;
1762 struct drm_i915_private *dev_priv = dev->dev_private;
1763 enum pipe pipe = crtc->pipe;
1764
1765 /* Disable DVO 2x clock on both PLLs if necessary */
1766 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001767 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001768 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001769 I915_WRITE(DPLL(PIPE_B),
1770 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1771 I915_WRITE(DPLL(PIPE_A),
1772 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1773 }
1774
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001775 /* Don't disable pipe or pipe PLLs if needed */
1776 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1777 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001778 return;
1779
1780 /* Make sure the pipe isn't still relying on us */
1781 assert_pipe_disabled(dev_priv, pipe);
1782
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001783 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001784 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001785}
1786
Jesse Barnesf6071162013-10-01 10:41:38 -07001787static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1788{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001789 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001790
1791 /* Make sure the pipe isn't still relying on us */
1792 assert_pipe_disabled(dev_priv, pipe);
1793
Imre Deake5cbfbf2014-01-09 17:08:16 +02001794 /*
1795 * Leave integrated clock source and reference clock enabled for pipe B.
1796 * The latter is needed for VGA hotplug / manual detection.
1797 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001798 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001799 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001800 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001801 I915_WRITE(DPLL(pipe), val);
1802 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001803
1804}
1805
1806static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1807{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001808 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001809 u32 val;
1810
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001811 /* Make sure the pipe isn't still relying on us */
1812 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001813
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001814 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001815 val = DPLL_SSC_REF_CLK_CHV |
1816 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001817 if (pipe != PIPE_A)
1818 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1819 I915_WRITE(DPLL(pipe), val);
1820 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001821
Ville Syrjäläa5805162015-05-26 20:42:30 +03001822 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001823
1824 /* Disable 10bit clock to display controller */
1825 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1826 val &= ~DPIO_DCLKP_EN;
1827 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1828
Ville Syrjäläa5805162015-05-26 20:42:30 +03001829 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001830}
1831
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001832void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001833 struct intel_digital_port *dport,
1834 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001835{
1836 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001837 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001838
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001839 switch (dport->port) {
1840 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001841 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001842 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001843 break;
1844 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001845 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001846 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001847 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001848 break;
1849 case PORT_D:
1850 port_mask = DPLL_PORTD_READY_MASK;
1851 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001852 break;
1853 default:
1854 BUG();
1855 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001856
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001857 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1858 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1859 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001860}
1861
Daniel Vetterb14b1052014-04-24 23:55:13 +02001862static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1863{
1864 struct drm_device *dev = crtc->base.dev;
1865 struct drm_i915_private *dev_priv = dev->dev_private;
1866 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1867
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001868 if (WARN_ON(pll == NULL))
1869 return;
1870
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001871 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001872 if (pll->active == 0) {
1873 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1874 WARN_ON(pll->on);
1875 assert_shared_dpll_disabled(dev_priv, pll);
1876
1877 pll->mode_set(dev_priv, pll);
1878 }
1879}
1880
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001881/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001882 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001883 * @dev_priv: i915 private structure
1884 * @pipe: pipe PLL to enable
1885 *
1886 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1887 * drives the transcoder clock.
1888 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001889static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001890{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001891 struct drm_device *dev = crtc->base.dev;
1892 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001893 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001894
Daniel Vetter87a875b2013-06-05 13:34:19 +02001895 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001896 return;
1897
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001898 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001899 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001900
Damien Lespiau74dd6922014-07-29 18:06:17 +01001901 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001902 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001903 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001904
Daniel Vettercdbd2312013-06-05 13:34:03 +02001905 if (pll->active++) {
1906 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001907 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001908 return;
1909 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001910 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001911
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001912 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1913
Daniel Vetter46edb022013-06-05 13:34:12 +02001914 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001915 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001916 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001917}
1918
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001919static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001920{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001921 struct drm_device *dev = crtc->base.dev;
1922 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001923 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001924
Jesse Barnes92f25842011-01-04 15:09:34 -08001925 /* PCH only available on ILK+ */
Jesse Barnes80aa9312015-08-03 13:09:11 -07001926 if (INTEL_INFO(dev)->gen < 5)
1927 return;
1928
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001929 if (pll == NULL)
1930 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001931
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001932 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
Chris Wilson48da64a2012-05-13 20:16:12 +01001933 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001934
Daniel Vetter46edb022013-06-05 13:34:12 +02001935 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1936 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001937 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001938
Chris Wilson48da64a2012-05-13 20:16:12 +01001939 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001940 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001941 return;
1942 }
1943
Daniel Vettere9d69442013-06-05 13:34:15 +02001944 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001945 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001946 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001947 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001948
Daniel Vetter46edb022013-06-05 13:34:12 +02001949 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001950 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001951 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001952
1953 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001954}
1955
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001956static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1957 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001958{
Daniel Vetter23670b322012-11-01 09:15:30 +01001959 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001960 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001961 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001962 i915_reg_t reg;
1963 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001964
1965 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001966 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001967
1968 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001969 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001970 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001971
1972 /* FDI must be feeding us bits for PCH ports */
1973 assert_fdi_tx_enabled(dev_priv, pipe);
1974 assert_fdi_rx_enabled(dev_priv, pipe);
1975
Daniel Vetter23670b322012-11-01 09:15:30 +01001976 if (HAS_PCH_CPT(dev)) {
1977 /* Workaround: Set the timing override bit before enabling the
1978 * pch transcoder. */
1979 reg = TRANS_CHICKEN2(pipe);
1980 val = I915_READ(reg);
1981 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1982 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001983 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001984
Daniel Vetterab9412b2013-05-03 11:49:46 +02001985 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001986 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001987 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001988
1989 if (HAS_PCH_IBX(dev_priv->dev)) {
1990 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001991 * Make the BPC in transcoder be consistent with
1992 * that in pipeconf reg. For HDMI we must use 8bpc
1993 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001994 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001995 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001996 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1997 val |= PIPECONF_8BPC;
1998 else
1999 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002000 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002001
2002 val &= ~TRANS_INTERLACE_MASK;
2003 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002004 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002005 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002006 val |= TRANS_LEGACY_INTERLACED_ILK;
2007 else
2008 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002009 else
2010 val |= TRANS_PROGRESSIVE;
2011
Jesse Barnes040484a2011-01-03 12:14:26 -08002012 I915_WRITE(reg, val | TRANS_ENABLE);
2013 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002014 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002015}
2016
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002017static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002018 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002019{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002020 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002021
2022 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002023 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002024
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002025 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002026 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002027 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002028
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002029 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002030 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01002031 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002032 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002033
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002034 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002035 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002036
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002037 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2038 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002039 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002040 else
2041 val |= TRANS_PROGRESSIVE;
2042
Daniel Vetterab9412b2013-05-03 11:49:46 +02002043 I915_WRITE(LPT_TRANSCONF, val);
2044 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002045 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002046}
2047
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002048static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2049 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002050{
Daniel Vetter23670b322012-11-01 09:15:30 +01002051 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002052 i915_reg_t reg;
2053 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002054
2055 /* FDI relies on the transcoder */
2056 assert_fdi_tx_disabled(dev_priv, pipe);
2057 assert_fdi_rx_disabled(dev_priv, pipe);
2058
Jesse Barnes291906f2011-02-02 12:28:03 -08002059 /* Ports must be off as well */
2060 assert_pch_ports_disabled(dev_priv, pipe);
2061
Daniel Vetterab9412b2013-05-03 11:49:46 +02002062 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002063 val = I915_READ(reg);
2064 val &= ~TRANS_ENABLE;
2065 I915_WRITE(reg, val);
2066 /* wait for PCH transcoder off, transcoder state */
2067 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002068 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002069
Ville Syrjäläc4656132015-10-29 21:25:56 +02002070 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01002071 /* Workaround: Clear the timing override chicken bit again. */
2072 reg = TRANS_CHICKEN2(pipe);
2073 val = I915_READ(reg);
2074 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2075 I915_WRITE(reg, val);
2076 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002077}
2078
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002079static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002080{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002081 u32 val;
2082
Daniel Vetterab9412b2013-05-03 11:49:46 +02002083 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002084 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002085 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002086 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002087 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002088 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002089
2090 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002091 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01002092 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002093 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002094}
2095
2096/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002097 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002098 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002099 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002100 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002101 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002102 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002103static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002104{
Paulo Zanoni03722642014-01-17 13:51:09 -02002105 struct drm_device *dev = crtc->base.dev;
2106 struct drm_i915_private *dev_priv = dev->dev_private;
2107 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02002108 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01002109 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002110 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002111 u32 val;
2112
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002113 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
2114
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002115 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002116 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002117 assert_sprites_disabled(dev_priv, pipe);
2118
Paulo Zanoni681e5812012-12-06 11:12:38 -02002119 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002120 pch_transcoder = TRANSCODER_A;
2121 else
2122 pch_transcoder = pipe;
2123
Jesse Barnesb24e7172011-01-04 15:09:30 -08002124 /*
2125 * A pipe without a PLL won't actually be able to drive bits from
2126 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2127 * need the check.
2128 */
Imre Deak50360402015-01-16 00:55:16 -08002129 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Jani Nikulaa65347b2015-11-27 12:21:46 +02002130 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03002131 assert_dsi_pll_enabled(dev_priv);
2132 else
2133 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002134 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002135 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002136 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002137 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002138 assert_fdi_tx_pll_enabled(dev_priv,
2139 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002140 }
2141 /* FIXME: assert CPU port conditions for SNB+ */
2142 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002143
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002144 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002145 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002146 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002147 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2148 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002149 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002150 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002151
2152 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002153 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002154
2155 /*
2156 * Until the pipe starts DSL will read as 0, which would cause
2157 * an apparent vblank timestamp jump, which messes up also the
2158 * frame count when it's derived from the timestamps. So let's
2159 * wait for the pipe to start properly before we call
2160 * drm_crtc_vblank_on()
2161 */
2162 if (dev->max_vblank_count == 0 &&
2163 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2164 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002165}
2166
2167/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002168 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002169 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002170 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002171 * Disable the pipe of @crtc, making sure that various hardware
2172 * specific requirements are met, if applicable, e.g. plane
2173 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002174 *
2175 * Will wait until the pipe has shut down before returning.
2176 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002177static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002178{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002179 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002180 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002181 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002182 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002183 u32 val;
2184
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002185 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2186
Jesse Barnesb24e7172011-01-04 15:09:30 -08002187 /*
2188 * Make sure planes won't keep trying to pump pixels to us,
2189 * or we might hang the display.
2190 */
2191 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002192 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002193 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002194
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002195 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002196 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002197 if ((val & PIPECONF_ENABLE) == 0)
2198 return;
2199
Ville Syrjälä67adc642014-08-15 01:21:57 +03002200 /*
2201 * Double wide has implications for planes
2202 * so best keep it disabled when not needed.
2203 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002204 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002205 val &= ~PIPECONF_DOUBLE_WIDE;
2206
2207 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002208 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2209 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002210 val &= ~PIPECONF_ENABLE;
2211
2212 I915_WRITE(reg, val);
2213 if ((val & PIPECONF_ENABLE) == 0)
2214 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002215}
2216
Chris Wilson693db182013-03-05 14:52:39 +00002217static bool need_vtd_wa(struct drm_device *dev)
2218{
2219#ifdef CONFIG_INTEL_IOMMU
2220 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2221 return true;
2222#endif
2223 return false;
2224}
2225
Ville Syrjälä832be822016-01-12 21:08:33 +02002226static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2227{
2228 return IS_GEN2(dev_priv) ? 2048 : 4096;
2229}
2230
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002231static unsigned int intel_tile_width(const struct drm_i915_private *dev_priv,
2232 uint64_t fb_modifier, unsigned int cpp)
2233{
2234 switch (fb_modifier) {
2235 case DRM_FORMAT_MOD_NONE:
2236 return cpp;
2237 case I915_FORMAT_MOD_X_TILED:
2238 if (IS_GEN2(dev_priv))
2239 return 128;
2240 else
2241 return 512;
2242 case I915_FORMAT_MOD_Y_TILED:
2243 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2244 return 128;
2245 else
2246 return 512;
2247 case I915_FORMAT_MOD_Yf_TILED:
2248 switch (cpp) {
2249 case 1:
2250 return 64;
2251 case 2:
2252 case 4:
2253 return 128;
2254 case 8:
2255 case 16:
2256 return 256;
2257 default:
2258 MISSING_CASE(cpp);
2259 return cpp;
2260 }
2261 break;
2262 default:
2263 MISSING_CASE(fb_modifier);
2264 return cpp;
2265 }
2266}
2267
Ville Syrjälä832be822016-01-12 21:08:33 +02002268unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2269 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002270{
Ville Syrjälä832be822016-01-12 21:08:33 +02002271 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2272 return 1;
2273 else
2274 return intel_tile_size(dev_priv) /
2275 intel_tile_width(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002276}
2277
2278unsigned int
2279intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002280 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002281{
Ville Syrjälä832be822016-01-12 21:08:33 +02002282 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2283 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2284
2285 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002286}
2287
Daniel Vetter75c82a52015-10-14 16:51:04 +02002288static void
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002289intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2290 const struct drm_plane_state *plane_state)
2291{
Ville Syrjälä832be822016-01-12 21:08:33 +02002292 struct drm_i915_private *dev_priv = to_i915(fb->dev);
Daniel Vettera6d09182015-10-14 16:51:05 +02002293 struct intel_rotation_info *info = &view->params.rotation_info;
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002294 unsigned int tile_height, tile_pitch;
Ville Syrjälä832be822016-01-12 21:08:33 +02002295 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002296
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002297 *view = i915_ggtt_view_normal;
2298
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002299 if (!plane_state)
Daniel Vetter75c82a52015-10-14 16:51:04 +02002300 return;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002301
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002302 if (!intel_rotation_90_or_270(plane_state->rotation))
Daniel Vetter75c82a52015-10-14 16:51:04 +02002303 return;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002304
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002305 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002306
2307 info->height = fb->height;
2308 info->pixel_format = fb->pixel_format;
2309 info->pitch = fb->pitches[0];
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002310 info->uv_offset = fb->offsets[1];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002311 info->fb_modifier = fb->modifier[0];
2312
Ville Syrjälä832be822016-01-12 21:08:33 +02002313 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002314 tile_pitch = PAGE_SIZE / tile_height;
2315 info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2316 info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
2317 info->size = info->width_pages * info->height_pages * PAGE_SIZE;
2318
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002319 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002320 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
2321 tile_height = intel_tile_height(dev_priv, fb->modifier[1], cpp);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002322 tile_pitch = PAGE_SIZE / tile_height;
Ville Syrjälä832be822016-01-12 21:08:33 +02002323 info->width_pages_uv = DIV_ROUND_UP(fb->pitches[1], tile_pitch);
2324 info->height_pages_uv = DIV_ROUND_UP(fb->height / 2, tile_height);
2325 info->size_uv = info->width_pages_uv * info->height_pages_uv * PAGE_SIZE;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002326 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002327}
2328
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002329static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2330{
2331 if (INTEL_INFO(dev_priv)->gen >= 9)
2332 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002333 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002334 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002335 return 128 * 1024;
2336 else if (INTEL_INFO(dev_priv)->gen >= 4)
2337 return 4 * 1024;
2338 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002339 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002340}
2341
Chris Wilson127bd2a2010-07-23 23:32:05 +01002342int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002343intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2344 struct drm_framebuffer *fb,
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002345 const struct drm_plane_state *plane_state)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002346{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002347 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002348 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002349 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002350 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002351 u32 alignment;
2352 int ret;
2353
Matt Roperebcdd392014-07-09 16:22:11 -07002354 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2355
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002356 switch (fb->modifier[0]) {
2357 case DRM_FORMAT_MOD_NONE:
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002358 alignment = intel_linear_alignment(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002359 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002360 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002361 if (INTEL_INFO(dev)->gen >= 9)
2362 alignment = 256 * 1024;
2363 else {
2364 /* pin() will align the object as required by fence */
2365 alignment = 0;
2366 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002367 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002368 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002369 case I915_FORMAT_MOD_Yf_TILED:
2370 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2371 "Y tiling bo slipped through, driver bug!\n"))
2372 return -EINVAL;
2373 alignment = 1 * 1024 * 1024;
2374 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002375 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002376 MISSING_CASE(fb->modifier[0]);
2377 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002378 }
2379
Daniel Vetter75c82a52015-10-14 16:51:04 +02002380 intel_fill_fb_ggtt_view(&view, fb, plane_state);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002381
Chris Wilson693db182013-03-05 14:52:39 +00002382 /* Note that the w/a also requires 64 PTE of padding following the
2383 * bo. We currently fill all unused PTE with the shadow page and so
2384 * we should always have valid PTE following the scanout preventing
2385 * the VT-d warning.
2386 */
2387 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2388 alignment = 256 * 1024;
2389
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002390 /*
2391 * Global gtt pte registers are special registers which actually forward
2392 * writes to a chunk of system memory. Which means that there is no risk
2393 * that the register values disappear as soon as we call
2394 * intel_runtime_pm_put(), so it is correct to wrap only the
2395 * pin/unpin/fence and not more.
2396 */
2397 intel_runtime_pm_get(dev_priv);
2398
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002399 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2400 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002401 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002402 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002403
2404 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2405 * fence, whereas 965+ only requires a fence if using
2406 * framebuffer compression. For simplicity, we always install
2407 * a fence as the cost is not that onerous.
2408 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002409 if (view.type == I915_GGTT_VIEW_NORMAL) {
2410 ret = i915_gem_object_get_fence(obj);
2411 if (ret == -EDEADLK) {
2412 /*
2413 * -EDEADLK means there are no free fences
2414 * no pending flips.
2415 *
2416 * This is propagated to atomic, but it uses
2417 * -EDEADLK to force a locking recovery, so
2418 * change the returned error to -EBUSY.
2419 */
2420 ret = -EBUSY;
2421 goto err_unpin;
2422 } else if (ret)
2423 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002424
Vivek Kasireddy98072162015-10-29 18:54:38 -07002425 i915_gem_object_pin_fence(obj);
2426 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002427
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002428 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002429 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002430
2431err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002432 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002433err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002434 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002435 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002436}
2437
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002438static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2439 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002440{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002441 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002442 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002443
Matt Roperebcdd392014-07-09 16:22:11 -07002444 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2445
Daniel Vetter75c82a52015-10-14 16:51:04 +02002446 intel_fill_fb_ggtt_view(&view, fb, plane_state);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002447
Vivek Kasireddy98072162015-10-29 18:54:38 -07002448 if (view.type == I915_GGTT_VIEW_NORMAL)
2449 i915_gem_object_unpin_fence(obj);
2450
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002451 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002452}
2453
Daniel Vetterc2c75132012-07-05 12:17:30 +02002454/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2455 * is assumed to be a power-of-two. */
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002456unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2457 int *x, int *y,
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002458 uint64_t fb_modifier,
Chris Wilsonbc752862013-02-21 20:04:31 +00002459 unsigned int cpp,
2460 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002461{
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002462 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Chris Wilsonbc752862013-02-21 20:04:31 +00002463 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002464
Chris Wilsonbc752862013-02-21 20:04:31 +00002465 tile_rows = *y / 8;
2466 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002467
Chris Wilsonbc752862013-02-21 20:04:31 +00002468 tiles = *x / (512/cpp);
2469 *x %= 512/cpp;
2470
2471 return tile_rows * pitch * 8 + tiles * 4096;
2472 } else {
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002473 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
Chris Wilsonbc752862013-02-21 20:04:31 +00002474 unsigned int offset;
2475
2476 offset = *y * pitch + *x * cpp;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002477 *y = (offset & alignment) / pitch;
2478 *x = ((offset & alignment) - *y * pitch) / cpp;
2479 return offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002480 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002481}
2482
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002483static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002484{
2485 switch (format) {
2486 case DISPPLANE_8BPP:
2487 return DRM_FORMAT_C8;
2488 case DISPPLANE_BGRX555:
2489 return DRM_FORMAT_XRGB1555;
2490 case DISPPLANE_BGRX565:
2491 return DRM_FORMAT_RGB565;
2492 default:
2493 case DISPPLANE_BGRX888:
2494 return DRM_FORMAT_XRGB8888;
2495 case DISPPLANE_RGBX888:
2496 return DRM_FORMAT_XBGR8888;
2497 case DISPPLANE_BGRX101010:
2498 return DRM_FORMAT_XRGB2101010;
2499 case DISPPLANE_RGBX101010:
2500 return DRM_FORMAT_XBGR2101010;
2501 }
2502}
2503
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002504static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2505{
2506 switch (format) {
2507 case PLANE_CTL_FORMAT_RGB_565:
2508 return DRM_FORMAT_RGB565;
2509 default:
2510 case PLANE_CTL_FORMAT_XRGB_8888:
2511 if (rgb_order) {
2512 if (alpha)
2513 return DRM_FORMAT_ABGR8888;
2514 else
2515 return DRM_FORMAT_XBGR8888;
2516 } else {
2517 if (alpha)
2518 return DRM_FORMAT_ARGB8888;
2519 else
2520 return DRM_FORMAT_XRGB8888;
2521 }
2522 case PLANE_CTL_FORMAT_XRGB_2101010:
2523 if (rgb_order)
2524 return DRM_FORMAT_XBGR2101010;
2525 else
2526 return DRM_FORMAT_XRGB2101010;
2527 }
2528}
2529
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002530static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002531intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2532 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002533{
2534 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002535 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002536 struct drm_i915_gem_object *obj = NULL;
2537 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002538 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002539 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2540 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2541 PAGE_SIZE);
2542
2543 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002544
Chris Wilsonff2652e2014-03-10 08:07:02 +00002545 if (plane_config->size == 0)
2546 return false;
2547
Paulo Zanoni3badb492015-09-23 12:52:23 -03002548 /* If the FB is too big, just don't use it since fbdev is not very
2549 * important and we should probably use that space with FBC or other
2550 * features. */
2551 if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
2552 return false;
2553
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002554 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2555 base_aligned,
2556 base_aligned,
2557 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002558 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002559 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002560
Damien Lespiau49af4492015-01-20 12:51:44 +00002561 obj->tiling_mode = plane_config->tiling;
2562 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002563 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002564
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002565 mode_cmd.pixel_format = fb->pixel_format;
2566 mode_cmd.width = fb->width;
2567 mode_cmd.height = fb->height;
2568 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002569 mode_cmd.modifier[0] = fb->modifier[0];
2570 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002571
2572 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002573 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002574 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002575 DRM_DEBUG_KMS("intel fb init failed\n");
2576 goto out_unref_obj;
2577 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002578 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002579
Daniel Vetterf6936e22015-03-26 12:17:05 +01002580 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002581 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002582
2583out_unref_obj:
2584 drm_gem_object_unreference(&obj->base);
2585 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002586 return false;
2587}
2588
Matt Roperafd65eb2015-02-03 13:10:04 -08002589/* Update plane->state->fb to match plane->fb after driver-internal updates */
2590static void
2591update_state_fb(struct drm_plane *plane)
2592{
2593 if (plane->fb == plane->state->fb)
2594 return;
2595
2596 if (plane->state->fb)
2597 drm_framebuffer_unreference(plane->state->fb);
2598 plane->state->fb = plane->fb;
2599 if (plane->state->fb)
2600 drm_framebuffer_reference(plane->state->fb);
2601}
2602
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002603static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002604intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2605 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002606{
2607 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002608 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002609 struct drm_crtc *c;
2610 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002611 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002612 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002613 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002614 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2615 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002616 struct intel_plane_state *intel_state =
2617 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002618 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002619
Damien Lespiau2d140302015-02-05 17:22:18 +00002620 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002621 return;
2622
Daniel Vetterf6936e22015-03-26 12:17:05 +01002623 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002624 fb = &plane_config->fb->base;
2625 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002626 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002627
Damien Lespiau2d140302015-02-05 17:22:18 +00002628 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002629
2630 /*
2631 * Failed to alloc the obj, check to see if we should share
2632 * an fb with another CRTC instead
2633 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002634 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002635 i = to_intel_crtc(c);
2636
2637 if (c == &intel_crtc->base)
2638 continue;
2639
Matt Roper2ff8fde2014-07-08 07:50:07 -07002640 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002641 continue;
2642
Daniel Vetter88595ac2015-03-26 12:42:24 +01002643 fb = c->primary->fb;
2644 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002645 continue;
2646
Daniel Vetter88595ac2015-03-26 12:42:24 +01002647 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002648 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002649 drm_framebuffer_reference(fb);
2650 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002651 }
2652 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002653
Matt Roper200757f2015-12-03 11:37:36 -08002654 /*
2655 * We've failed to reconstruct the BIOS FB. Current display state
2656 * indicates that the primary plane is visible, but has a NULL FB,
2657 * which will lead to problems later if we don't fix it up. The
2658 * simplest solution is to just disable the primary plane now and
2659 * pretend the BIOS never had it enabled.
2660 */
2661 to_intel_plane_state(plane_state)->visible = false;
2662 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2663 intel_pre_disable_primary(&intel_crtc->base);
2664 intel_plane->disable_plane(primary, &intel_crtc->base);
2665
Daniel Vetter88595ac2015-03-26 12:42:24 +01002666 return;
2667
2668valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002669 plane_state->src_x = 0;
2670 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002671 plane_state->src_w = fb->width << 16;
2672 plane_state->src_h = fb->height << 16;
2673
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002674 plane_state->crtc_x = 0;
2675 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002676 plane_state->crtc_w = fb->width;
2677 plane_state->crtc_h = fb->height;
2678
Matt Roper0a8d8a82015-12-03 11:37:38 -08002679 intel_state->src.x1 = plane_state->src_x;
2680 intel_state->src.y1 = plane_state->src_y;
2681 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2682 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2683 intel_state->dst.x1 = plane_state->crtc_x;
2684 intel_state->dst.y1 = plane_state->crtc_y;
2685 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2686 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2687
Daniel Vetter88595ac2015-03-26 12:42:24 +01002688 obj = intel_fb_obj(fb);
2689 if (obj->tiling_mode != I915_TILING_NONE)
2690 dev_priv->preserve_bios_swizzle = true;
2691
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002692 drm_framebuffer_reference(fb);
2693 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002694 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002695 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002696 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002697}
2698
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002699static void i9xx_update_primary_plane(struct drm_plane *primary,
2700 const struct intel_crtc_state *crtc_state,
2701 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002702{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002703 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002704 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002705 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2706 struct drm_framebuffer *fb = plane_state->base.fb;
2707 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002708 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002709 unsigned long linear_offset;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002710 int x = plane_state->src.x1 >> 16;
2711 int y = plane_state->src.y1 >> 16;
Jesse Barnes81255562010-08-02 12:07:50 -07002712 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002713 i915_reg_t reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302714 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002715
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002716 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2717
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002718 dspcntr = DISPPLANE_GAMMA_ENABLE;
2719
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002720 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002721
2722 if (INTEL_INFO(dev)->gen < 4) {
2723 if (intel_crtc->pipe == PIPE_B)
2724 dspcntr |= DISPPLANE_SEL_PIPE_B;
2725
2726 /* pipesrc and dspsize control the size that is scaled from,
2727 * which should always be the user's requested size.
2728 */
2729 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002730 ((crtc_state->pipe_src_h - 1) << 16) |
2731 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002732 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002733 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2734 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002735 ((crtc_state->pipe_src_h - 1) << 16) |
2736 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002737 I915_WRITE(PRIMPOS(plane), 0);
2738 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002739 }
2740
Ville Syrjälä57779d02012-10-31 17:50:14 +02002741 switch (fb->pixel_format) {
2742 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002743 dspcntr |= DISPPLANE_8BPP;
2744 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002745 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002746 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002747 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002748 case DRM_FORMAT_RGB565:
2749 dspcntr |= DISPPLANE_BGRX565;
2750 break;
2751 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002752 dspcntr |= DISPPLANE_BGRX888;
2753 break;
2754 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002755 dspcntr |= DISPPLANE_RGBX888;
2756 break;
2757 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002758 dspcntr |= DISPPLANE_BGRX101010;
2759 break;
2760 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002761 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002762 break;
2763 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002764 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002765 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002766
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002767 if (INTEL_INFO(dev)->gen >= 4 &&
2768 obj->tiling_mode != I915_TILING_NONE)
2769 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002770
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002771 if (IS_G4X(dev))
2772 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2773
Ville Syrjäläb98971272014-08-27 16:51:22 +03002774 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002775
Daniel Vetterc2c75132012-07-05 12:17:30 +02002776 if (INTEL_INFO(dev)->gen >= 4) {
2777 intel_crtc->dspaddr_offset =
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002778 intel_gen4_compute_page_offset(dev_priv, &x, &y,
2779 fb->modifier[0],
Ville Syrjäläb98971272014-08-27 16:51:22 +03002780 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002781 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002782 linear_offset -= intel_crtc->dspaddr_offset;
2783 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002784 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002785 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002786
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002787 if (plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302788 dspcntr |= DISPPLANE_ROTATE_180;
2789
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002790 x += (crtc_state->pipe_src_w - 1);
2791 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302792
2793 /* Finding the last pixel of the last line of the display
2794 data and adding to linear_offset*/
2795 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002796 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
2797 (crtc_state->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302798 }
2799
Paulo Zanoni2db33662015-09-14 15:20:03 -03002800 intel_crtc->adjusted_x = x;
2801 intel_crtc->adjusted_y = y;
2802
Sonika Jindal48404c12014-08-22 14:06:04 +05302803 I915_WRITE(reg, dspcntr);
2804
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002805 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002806 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002807 I915_WRITE(DSPSURF(plane),
2808 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002809 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002810 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002811 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002812 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002813 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002814}
2815
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002816static void i9xx_disable_primary_plane(struct drm_plane *primary,
2817 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002818{
2819 struct drm_device *dev = crtc->dev;
2820 struct drm_i915_private *dev_priv = dev->dev_private;
2821 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002822 int plane = intel_crtc->plane;
2823
2824 I915_WRITE(DSPCNTR(plane), 0);
2825 if (INTEL_INFO(dev_priv)->gen >= 4)
2826 I915_WRITE(DSPSURF(plane), 0);
2827 else
2828 I915_WRITE(DSPADDR(plane), 0);
2829 POSTING_READ(DSPCNTR(plane));
2830}
2831
2832static void ironlake_update_primary_plane(struct drm_plane *primary,
2833 const struct intel_crtc_state *crtc_state,
2834 const struct intel_plane_state *plane_state)
2835{
2836 struct drm_device *dev = primary->dev;
2837 struct drm_i915_private *dev_priv = dev->dev_private;
2838 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2839 struct drm_framebuffer *fb = plane_state->base.fb;
2840 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002841 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002842 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002843 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002844 i915_reg_t reg = DSPCNTR(plane);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002845 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2846 int x = plane_state->src.x1 >> 16;
2847 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002848
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002849 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002850 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002851
2852 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2853 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2854
Ville Syrjälä57779d02012-10-31 17:50:14 +02002855 switch (fb->pixel_format) {
2856 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002857 dspcntr |= DISPPLANE_8BPP;
2858 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002859 case DRM_FORMAT_RGB565:
2860 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002861 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002862 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002863 dspcntr |= DISPPLANE_BGRX888;
2864 break;
2865 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002866 dspcntr |= DISPPLANE_RGBX888;
2867 break;
2868 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002869 dspcntr |= DISPPLANE_BGRX101010;
2870 break;
2871 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002872 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002873 break;
2874 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002875 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002876 }
2877
2878 if (obj->tiling_mode != I915_TILING_NONE)
2879 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002880
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002881 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002882 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002883
Ville Syrjäläb98971272014-08-27 16:51:22 +03002884 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002885 intel_crtc->dspaddr_offset =
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002886 intel_gen4_compute_page_offset(dev_priv, &x, &y,
2887 fb->modifier[0],
Ville Syrjäläb98971272014-08-27 16:51:22 +03002888 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002889 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002890 linear_offset -= intel_crtc->dspaddr_offset;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002891 if (plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302892 dspcntr |= DISPPLANE_ROTATE_180;
2893
2894 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002895 x += (crtc_state->pipe_src_w - 1);
2896 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302897
2898 /* Finding the last pixel of the last line of the display
2899 data and adding to linear_offset*/
2900 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002901 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
2902 (crtc_state->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302903 }
2904 }
2905
Paulo Zanoni2db33662015-09-14 15:20:03 -03002906 intel_crtc->adjusted_x = x;
2907 intel_crtc->adjusted_y = y;
2908
Sonika Jindal48404c12014-08-22 14:06:04 +05302909 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002910
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002911 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002912 I915_WRITE(DSPSURF(plane),
2913 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002914 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002915 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2916 } else {
2917 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2918 I915_WRITE(DSPLINOFF(plane), linear_offset);
2919 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002920 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002921}
2922
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002923u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2924 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002925{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002926 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2927 return 64;
2928 } else {
2929 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002930
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002931 return intel_tile_width(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002932 }
2933}
2934
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002935u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2936 struct drm_i915_gem_object *obj,
2937 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002938{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002939 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002940 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002941 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002942
Daniel Vetterce7f1722015-10-14 16:51:06 +02002943 intel_fill_fb_ggtt_view(&view, intel_plane->base.fb,
2944 intel_plane->base.state);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002945
Daniel Vetterce7f1722015-10-14 16:51:06 +02002946 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002947 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002948 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002949 return -1;
2950
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002951 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002952
2953 if (plane == 1) {
Daniel Vettera6d09182015-10-14 16:51:05 +02002954 offset += vma->ggtt_view.params.rotation_info.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002955 PAGE_SIZE;
2956 }
2957
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002958 WARN_ON(upper_32_bits(offset));
2959
2960 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002961}
2962
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002963static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2964{
2965 struct drm_device *dev = intel_crtc->base.dev;
2966 struct drm_i915_private *dev_priv = dev->dev_private;
2967
2968 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2969 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2970 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002971}
2972
Chandra Kondurua1b22782015-04-07 15:28:45 -07002973/*
2974 * This function detaches (aka. unbinds) unused scalers in hardware
2975 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002976static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002977{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002978 struct intel_crtc_scaler_state *scaler_state;
2979 int i;
2980
Chandra Kondurua1b22782015-04-07 15:28:45 -07002981 scaler_state = &intel_crtc->config->scaler_state;
2982
2983 /* loop through and disable scalers that aren't in use */
2984 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002985 if (!scaler_state->scalers[i].in_use)
2986 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002987 }
2988}
2989
Chandra Konduru6156a452015-04-27 13:48:39 -07002990u32 skl_plane_ctl_format(uint32_t pixel_format)
2991{
Chandra Konduru6156a452015-04-27 13:48:39 -07002992 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002993 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002994 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002995 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002996 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002997 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002998 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002999 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003000 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003001 /*
3002 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3003 * to be already pre-multiplied. We need to add a knob (or a different
3004 * DRM_FORMAT) for user-space to configure that.
3005 */
3006 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003007 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07003008 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003009 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003010 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07003011 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003012 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003013 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003014 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003015 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003016 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003017 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003018 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003019 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003020 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003021 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003022 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003023 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003024 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003025 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003026 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003027
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003028 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003029}
3030
3031u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3032{
Chandra Konduru6156a452015-04-27 13:48:39 -07003033 switch (fb_modifier) {
3034 case DRM_FORMAT_MOD_NONE:
3035 break;
3036 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003037 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003038 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003039 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003040 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003041 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003042 default:
3043 MISSING_CASE(fb_modifier);
3044 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003045
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003046 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003047}
3048
3049u32 skl_plane_ctl_rotation(unsigned int rotation)
3050{
Chandra Konduru6156a452015-04-27 13:48:39 -07003051 switch (rotation) {
3052 case BIT(DRM_ROTATE_0):
3053 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303054 /*
3055 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3056 * while i915 HW rotation is clockwise, thats why this swapping.
3057 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003058 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303059 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003060 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003061 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003062 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303063 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003064 default:
3065 MISSING_CASE(rotation);
3066 }
3067
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003068 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003069}
3070
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003071static void skylake_update_primary_plane(struct drm_plane *plane,
3072 const struct intel_crtc_state *crtc_state,
3073 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003074{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003075 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003076 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003077 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3078 struct drm_framebuffer *fb = plane_state->base.fb;
3079 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003080 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303081 u32 plane_ctl, stride_div, stride;
3082 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003083 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303084 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003085 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003086 int scaler_id = plane_state->scaler_id;
3087 int src_x = plane_state->src.x1 >> 16;
3088 int src_y = plane_state->src.y1 >> 16;
3089 int src_w = drm_rect_width(&plane_state->src) >> 16;
3090 int src_h = drm_rect_height(&plane_state->src) >> 16;
3091 int dst_x = plane_state->dst.x1;
3092 int dst_y = plane_state->dst.y1;
3093 int dst_w = drm_rect_width(&plane_state->dst);
3094 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003095
3096 plane_ctl = PLANE_CTL_ENABLE |
3097 PLANE_CTL_PIPE_GAMMA_ENABLE |
3098 PLANE_CTL_PIPE_CSC_ENABLE;
3099
Chandra Konduru6156a452015-04-27 13:48:39 -07003100 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3101 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003102 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003103 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003104
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003105 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003106 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003107 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303108
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003109 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003110
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303111 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003112 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3113
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303114 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003115 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303116 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003117 x_offset = stride * tile_height - src_y - src_h;
3118 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003119 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303120 } else {
3121 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003122 x_offset = src_x;
3123 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003124 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303125 }
3126 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003127
Paulo Zanoni2db33662015-09-14 15:20:03 -03003128 intel_crtc->adjusted_x = x_offset;
3129 intel_crtc->adjusted_y = y_offset;
3130
Damien Lespiau70d21f02013-07-03 21:06:04 +01003131 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303132 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3133 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3134 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003135
3136 if (scaler_id >= 0) {
3137 uint32_t ps_ctrl = 0;
3138
3139 WARN_ON(!dst_w || !dst_h);
3140 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3141 crtc_state->scaler_state.scalers[scaler_id].mode;
3142 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3143 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3144 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3145 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3146 I915_WRITE(PLANE_POS(pipe, 0), 0);
3147 } else {
3148 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3149 }
3150
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003151 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003152
3153 POSTING_READ(PLANE_SURF(pipe, 0));
3154}
3155
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003156static void skylake_disable_primary_plane(struct drm_plane *primary,
3157 struct drm_crtc *crtc)
3158{
3159 struct drm_device *dev = crtc->dev;
3160 struct drm_i915_private *dev_priv = dev->dev_private;
3161 int pipe = to_intel_crtc(crtc)->pipe;
3162
3163 if (dev_priv->fbc.deactivate)
3164 dev_priv->fbc.deactivate(dev_priv);
3165
3166 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3167 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3168 POSTING_READ(PLANE_SURF(pipe, 0));
3169}
3170
Jesse Barnes17638cd2011-06-24 12:19:23 -07003171/* Assume fb object is pinned & idle & fenced and just update base pointers */
3172static int
3173intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3174 int x, int y, enum mode_set_atomic state)
3175{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003176 /* Support for kgdboc is disabled, this needs a major rework. */
3177 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003178
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003179 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003180}
3181
Ville Syrjälä75147472014-11-24 18:28:11 +02003182static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003183{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003184 struct drm_crtc *crtc;
3185
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003186 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3188 enum plane plane = intel_crtc->plane;
3189
3190 intel_prepare_page_flip(dev, plane);
3191 intel_finish_page_flip_plane(dev, plane);
3192 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003193}
3194
3195static void intel_update_primary_planes(struct drm_device *dev)
3196{
Ville Syrjälä75147472014-11-24 18:28:11 +02003197 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003198
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003199 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003200 struct intel_plane *plane = to_intel_plane(crtc->primary);
3201 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003202
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003203 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003204 plane_state = to_intel_plane_state(plane->base.state);
3205
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003206 if (plane_state->visible)
3207 plane->update_plane(&plane->base,
3208 to_intel_crtc_state(crtc->state),
3209 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003210
3211 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003212 }
3213}
3214
Ville Syrjälä75147472014-11-24 18:28:11 +02003215void intel_prepare_reset(struct drm_device *dev)
3216{
3217 /* no reset support for gen2 */
3218 if (IS_GEN2(dev))
3219 return;
3220
3221 /* reset doesn't touch the display */
3222 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3223 return;
3224
3225 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003226 /*
3227 * Disabling the crtcs gracefully seems nicer. Also the
3228 * g33 docs say we should at least disable all the planes.
3229 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003230 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003231}
3232
3233void intel_finish_reset(struct drm_device *dev)
3234{
3235 struct drm_i915_private *dev_priv = to_i915(dev);
3236
3237 /*
3238 * Flips in the rings will be nuked by the reset,
3239 * so complete all pending flips so that user space
3240 * will get its events and not get stuck.
3241 */
3242 intel_complete_page_flips(dev);
3243
3244 /* no reset support for gen2 */
3245 if (IS_GEN2(dev))
3246 return;
3247
3248 /* reset doesn't touch the display */
3249 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3250 /*
3251 * Flips in the rings have been nuked by the reset,
3252 * so update the base address of all primary
3253 * planes to the the last fb to make sure we're
3254 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003255 *
3256 * FIXME: Atomic will make this obsolete since we won't schedule
3257 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003258 */
3259 intel_update_primary_planes(dev);
3260 return;
3261 }
3262
3263 /*
3264 * The display has been reset as well,
3265 * so need a full re-initialization.
3266 */
3267 intel_runtime_pm_disable_interrupts(dev_priv);
3268 intel_runtime_pm_enable_interrupts(dev_priv);
3269
3270 intel_modeset_init_hw(dev);
3271
3272 spin_lock_irq(&dev_priv->irq_lock);
3273 if (dev_priv->display.hpd_irq_setup)
3274 dev_priv->display.hpd_irq_setup(dev);
3275 spin_unlock_irq(&dev_priv->irq_lock);
3276
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003277 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003278
3279 intel_hpd_init(dev_priv);
3280
3281 drm_modeset_unlock_all(dev);
3282}
3283
Chris Wilson7d5e3792014-03-04 13:15:08 +00003284static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3285{
3286 struct drm_device *dev = crtc->dev;
3287 struct drm_i915_private *dev_priv = dev->dev_private;
3288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003289 bool pending;
3290
3291 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3292 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3293 return false;
3294
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003295 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003296 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003297 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003298
3299 return pending;
3300}
3301
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003302static void intel_update_pipe_config(struct intel_crtc *crtc,
3303 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003304{
3305 struct drm_device *dev = crtc->base.dev;
3306 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003307 struct intel_crtc_state *pipe_config =
3308 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003309
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003310 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3311 crtc->base.mode = crtc->base.state->mode;
3312
3313 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3314 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3315 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003316
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003317 if (HAS_DDI(dev))
3318 intel_set_pipe_csc(&crtc->base);
3319
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003320 /*
3321 * Update pipe size and adjust fitter if needed: the reason for this is
3322 * that in compute_mode_changes we check the native mode (not the pfit
3323 * mode) to see if we can flip rather than do a full mode set. In the
3324 * fastboot case, we'll flip, but if we don't update the pipesrc and
3325 * pfit state, we'll end up with a big fb scanned out into the wrong
3326 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003327 */
3328
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003329 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003330 ((pipe_config->pipe_src_w - 1) << 16) |
3331 (pipe_config->pipe_src_h - 1));
3332
3333 /* on skylake this is done by detaching scalers */
3334 if (INTEL_INFO(dev)->gen >= 9) {
3335 skl_detach_scalers(crtc);
3336
3337 if (pipe_config->pch_pfit.enabled)
3338 skylake_pfit_enable(crtc);
3339 } else if (HAS_PCH_SPLIT(dev)) {
3340 if (pipe_config->pch_pfit.enabled)
3341 ironlake_pfit_enable(crtc);
3342 else if (old_crtc_state->pch_pfit.enabled)
3343 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003344 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003345}
3346
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003347static void intel_fdi_normal_train(struct drm_crtc *crtc)
3348{
3349 struct drm_device *dev = crtc->dev;
3350 struct drm_i915_private *dev_priv = dev->dev_private;
3351 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3352 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003353 i915_reg_t reg;
3354 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003355
3356 /* enable normal train */
3357 reg = FDI_TX_CTL(pipe);
3358 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003359 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003360 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3361 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003362 } else {
3363 temp &= ~FDI_LINK_TRAIN_NONE;
3364 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003365 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003366 I915_WRITE(reg, temp);
3367
3368 reg = FDI_RX_CTL(pipe);
3369 temp = I915_READ(reg);
3370 if (HAS_PCH_CPT(dev)) {
3371 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3372 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3373 } else {
3374 temp &= ~FDI_LINK_TRAIN_NONE;
3375 temp |= FDI_LINK_TRAIN_NONE;
3376 }
3377 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3378
3379 /* wait one idle pattern time */
3380 POSTING_READ(reg);
3381 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003382
3383 /* IVB wants error correction enabled */
3384 if (IS_IVYBRIDGE(dev))
3385 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3386 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003387}
3388
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003389/* The FDI link training functions for ILK/Ibexpeak. */
3390static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3391{
3392 struct drm_device *dev = crtc->dev;
3393 struct drm_i915_private *dev_priv = dev->dev_private;
3394 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3395 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003396 i915_reg_t reg;
3397 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003398
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003399 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003400 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003401
Adam Jacksone1a44742010-06-25 15:32:14 -04003402 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3403 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003404 reg = FDI_RX_IMR(pipe);
3405 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003406 temp &= ~FDI_RX_SYMBOL_LOCK;
3407 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003408 I915_WRITE(reg, temp);
3409 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003410 udelay(150);
3411
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003412 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003413 reg = FDI_TX_CTL(pipe);
3414 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003415 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003416 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003417 temp &= ~FDI_LINK_TRAIN_NONE;
3418 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003419 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003420
Chris Wilson5eddb702010-09-11 13:48:45 +01003421 reg = FDI_RX_CTL(pipe);
3422 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003423 temp &= ~FDI_LINK_TRAIN_NONE;
3424 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3426
3427 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003428 udelay(150);
3429
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003430 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003431 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3432 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3433 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003434
Chris Wilson5eddb702010-09-11 13:48:45 +01003435 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003436 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003437 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003438 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3439
3440 if ((temp & FDI_RX_BIT_LOCK)) {
3441 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003442 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003443 break;
3444 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003445 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003446 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003447 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003448
3449 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003450 reg = FDI_TX_CTL(pipe);
3451 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003452 temp &= ~FDI_LINK_TRAIN_NONE;
3453 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003454 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003455
Chris Wilson5eddb702010-09-11 13:48:45 +01003456 reg = FDI_RX_CTL(pipe);
3457 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003458 temp &= ~FDI_LINK_TRAIN_NONE;
3459 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003460 I915_WRITE(reg, temp);
3461
3462 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003463 udelay(150);
3464
Chris Wilson5eddb702010-09-11 13:48:45 +01003465 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003466 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003467 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003468 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3469
3470 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003471 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003472 DRM_DEBUG_KMS("FDI train 2 done.\n");
3473 break;
3474 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003475 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003476 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003477 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003478
3479 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003480
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003481}
3482
Akshay Joshi0206e352011-08-16 15:34:10 -04003483static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003484 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3485 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3486 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3487 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3488};
3489
3490/* The FDI link training functions for SNB/Cougarpoint. */
3491static void gen6_fdi_link_train(struct drm_crtc *crtc)
3492{
3493 struct drm_device *dev = crtc->dev;
3494 struct drm_i915_private *dev_priv = dev->dev_private;
3495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3496 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003497 i915_reg_t reg;
3498 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499
Adam Jacksone1a44742010-06-25 15:32:14 -04003500 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3501 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003502 reg = FDI_RX_IMR(pipe);
3503 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003504 temp &= ~FDI_RX_SYMBOL_LOCK;
3505 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003506 I915_WRITE(reg, temp);
3507
3508 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003509 udelay(150);
3510
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003511 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003512 reg = FDI_TX_CTL(pipe);
3513 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003514 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003515 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003516 temp &= ~FDI_LINK_TRAIN_NONE;
3517 temp |= FDI_LINK_TRAIN_PATTERN_1;
3518 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3519 /* SNB-B */
3520 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003521 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003522
Daniel Vetterd74cf322012-10-26 10:58:13 +02003523 I915_WRITE(FDI_RX_MISC(pipe),
3524 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3525
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 if (HAS_PCH_CPT(dev)) {
3529 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3530 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3531 } else {
3532 temp &= ~FDI_LINK_TRAIN_NONE;
3533 temp |= FDI_LINK_TRAIN_PATTERN_1;
3534 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003535 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3536
3537 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003538 udelay(150);
3539
Akshay Joshi0206e352011-08-16 15:34:10 -04003540 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003541 reg = FDI_TX_CTL(pipe);
3542 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003543 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3544 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003545 I915_WRITE(reg, temp);
3546
3547 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003548 udelay(500);
3549
Sean Paulfa37d392012-03-02 12:53:39 -05003550 for (retry = 0; retry < 5; retry++) {
3551 reg = FDI_RX_IIR(pipe);
3552 temp = I915_READ(reg);
3553 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3554 if (temp & FDI_RX_BIT_LOCK) {
3555 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3556 DRM_DEBUG_KMS("FDI train 1 done.\n");
3557 break;
3558 }
3559 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003560 }
Sean Paulfa37d392012-03-02 12:53:39 -05003561 if (retry < 5)
3562 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003563 }
3564 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003565 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003566
3567 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003568 reg = FDI_TX_CTL(pipe);
3569 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003570 temp &= ~FDI_LINK_TRAIN_NONE;
3571 temp |= FDI_LINK_TRAIN_PATTERN_2;
3572 if (IS_GEN6(dev)) {
3573 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3574 /* SNB-B */
3575 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3576 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003577 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003578
Chris Wilson5eddb702010-09-11 13:48:45 +01003579 reg = FDI_RX_CTL(pipe);
3580 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003581 if (HAS_PCH_CPT(dev)) {
3582 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3583 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3584 } else {
3585 temp &= ~FDI_LINK_TRAIN_NONE;
3586 temp |= FDI_LINK_TRAIN_PATTERN_2;
3587 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003588 I915_WRITE(reg, temp);
3589
3590 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003591 udelay(150);
3592
Akshay Joshi0206e352011-08-16 15:34:10 -04003593 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003594 reg = FDI_TX_CTL(pipe);
3595 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003596 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3597 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003598 I915_WRITE(reg, temp);
3599
3600 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003601 udelay(500);
3602
Sean Paulfa37d392012-03-02 12:53:39 -05003603 for (retry = 0; retry < 5; retry++) {
3604 reg = FDI_RX_IIR(pipe);
3605 temp = I915_READ(reg);
3606 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3607 if (temp & FDI_RX_SYMBOL_LOCK) {
3608 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3609 DRM_DEBUG_KMS("FDI train 2 done.\n");
3610 break;
3611 }
3612 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003613 }
Sean Paulfa37d392012-03-02 12:53:39 -05003614 if (retry < 5)
3615 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003616 }
3617 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003618 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003619
3620 DRM_DEBUG_KMS("FDI train done.\n");
3621}
3622
Jesse Barnes357555c2011-04-28 15:09:55 -07003623/* Manual link training for Ivy Bridge A0 parts */
3624static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3625{
3626 struct drm_device *dev = crtc->dev;
3627 struct drm_i915_private *dev_priv = dev->dev_private;
3628 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3629 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003630 i915_reg_t reg;
3631 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003632
3633 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3634 for train result */
3635 reg = FDI_RX_IMR(pipe);
3636 temp = I915_READ(reg);
3637 temp &= ~FDI_RX_SYMBOL_LOCK;
3638 temp &= ~FDI_RX_BIT_LOCK;
3639 I915_WRITE(reg, temp);
3640
3641 POSTING_READ(reg);
3642 udelay(150);
3643
Daniel Vetter01a415f2012-10-27 15:58:40 +02003644 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3645 I915_READ(FDI_RX_IIR(pipe)));
3646
Jesse Barnes139ccd32013-08-19 11:04:55 -07003647 /* Try each vswing and preemphasis setting twice before moving on */
3648 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3649 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003650 reg = FDI_TX_CTL(pipe);
3651 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003652 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3653 temp &= ~FDI_TX_ENABLE;
3654 I915_WRITE(reg, temp);
3655
3656 reg = FDI_RX_CTL(pipe);
3657 temp = I915_READ(reg);
3658 temp &= ~FDI_LINK_TRAIN_AUTO;
3659 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3660 temp &= ~FDI_RX_ENABLE;
3661 I915_WRITE(reg, temp);
3662
3663 /* enable CPU FDI TX and PCH FDI RX */
3664 reg = FDI_TX_CTL(pipe);
3665 temp = I915_READ(reg);
3666 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003667 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003668 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003669 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003670 temp |= snb_b_fdi_train_param[j/2];
3671 temp |= FDI_COMPOSITE_SYNC;
3672 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3673
3674 I915_WRITE(FDI_RX_MISC(pipe),
3675 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3676
3677 reg = FDI_RX_CTL(pipe);
3678 temp = I915_READ(reg);
3679 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3680 temp |= FDI_COMPOSITE_SYNC;
3681 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3682
3683 POSTING_READ(reg);
3684 udelay(1); /* should be 0.5us */
3685
3686 for (i = 0; i < 4; i++) {
3687 reg = FDI_RX_IIR(pipe);
3688 temp = I915_READ(reg);
3689 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3690
3691 if (temp & FDI_RX_BIT_LOCK ||
3692 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3693 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3694 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3695 i);
3696 break;
3697 }
3698 udelay(1); /* should be 0.5us */
3699 }
3700 if (i == 4) {
3701 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3702 continue;
3703 }
3704
3705 /* Train 2 */
3706 reg = FDI_TX_CTL(pipe);
3707 temp = I915_READ(reg);
3708 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3709 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3710 I915_WRITE(reg, temp);
3711
3712 reg = FDI_RX_CTL(pipe);
3713 temp = I915_READ(reg);
3714 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3715 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003716 I915_WRITE(reg, temp);
3717
3718 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003719 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003720
Jesse Barnes139ccd32013-08-19 11:04:55 -07003721 for (i = 0; i < 4; i++) {
3722 reg = FDI_RX_IIR(pipe);
3723 temp = I915_READ(reg);
3724 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003725
Jesse Barnes139ccd32013-08-19 11:04:55 -07003726 if (temp & FDI_RX_SYMBOL_LOCK ||
3727 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3728 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3729 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3730 i);
3731 goto train_done;
3732 }
3733 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003734 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003735 if (i == 4)
3736 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003737 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003738
Jesse Barnes139ccd32013-08-19 11:04:55 -07003739train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003740 DRM_DEBUG_KMS("FDI train done.\n");
3741}
3742
Daniel Vetter88cefb62012-08-12 19:27:14 +02003743static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003744{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003745 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003746 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003747 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003748 i915_reg_t reg;
3749 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003750
Jesse Barnes0e23b992010-09-10 11:10:00 -07003751 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003752 reg = FDI_RX_CTL(pipe);
3753 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003754 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003755 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003756 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003757 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3758
3759 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003760 udelay(200);
3761
3762 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003763 temp = I915_READ(reg);
3764 I915_WRITE(reg, temp | FDI_PCDCLK);
3765
3766 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003767 udelay(200);
3768
Paulo Zanoni20749732012-11-23 15:30:38 -02003769 /* Enable CPU FDI TX PLL, always on for Ironlake */
3770 reg = FDI_TX_CTL(pipe);
3771 temp = I915_READ(reg);
3772 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3773 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003774
Paulo Zanoni20749732012-11-23 15:30:38 -02003775 POSTING_READ(reg);
3776 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003777 }
3778}
3779
Daniel Vetter88cefb62012-08-12 19:27:14 +02003780static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3781{
3782 struct drm_device *dev = intel_crtc->base.dev;
3783 struct drm_i915_private *dev_priv = dev->dev_private;
3784 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003785 i915_reg_t reg;
3786 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003787
3788 /* Switch from PCDclk to Rawclk */
3789 reg = FDI_RX_CTL(pipe);
3790 temp = I915_READ(reg);
3791 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3792
3793 /* Disable CPU FDI TX PLL */
3794 reg = FDI_TX_CTL(pipe);
3795 temp = I915_READ(reg);
3796 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3797
3798 POSTING_READ(reg);
3799 udelay(100);
3800
3801 reg = FDI_RX_CTL(pipe);
3802 temp = I915_READ(reg);
3803 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3804
3805 /* Wait for the clocks to turn off. */
3806 POSTING_READ(reg);
3807 udelay(100);
3808}
3809
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003810static void ironlake_fdi_disable(struct drm_crtc *crtc)
3811{
3812 struct drm_device *dev = crtc->dev;
3813 struct drm_i915_private *dev_priv = dev->dev_private;
3814 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3815 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003816 i915_reg_t reg;
3817 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003818
3819 /* disable CPU FDI tx and PCH FDI rx */
3820 reg = FDI_TX_CTL(pipe);
3821 temp = I915_READ(reg);
3822 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3823 POSTING_READ(reg);
3824
3825 reg = FDI_RX_CTL(pipe);
3826 temp = I915_READ(reg);
3827 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003828 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003829 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3830
3831 POSTING_READ(reg);
3832 udelay(100);
3833
3834 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003835 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003836 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003837
3838 /* still set train pattern 1 */
3839 reg = FDI_TX_CTL(pipe);
3840 temp = I915_READ(reg);
3841 temp &= ~FDI_LINK_TRAIN_NONE;
3842 temp |= FDI_LINK_TRAIN_PATTERN_1;
3843 I915_WRITE(reg, temp);
3844
3845 reg = FDI_RX_CTL(pipe);
3846 temp = I915_READ(reg);
3847 if (HAS_PCH_CPT(dev)) {
3848 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3849 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3850 } else {
3851 temp &= ~FDI_LINK_TRAIN_NONE;
3852 temp |= FDI_LINK_TRAIN_PATTERN_1;
3853 }
3854 /* BPC in FDI rx is consistent with that in PIPECONF */
3855 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003856 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003857 I915_WRITE(reg, temp);
3858
3859 POSTING_READ(reg);
3860 udelay(100);
3861}
3862
Chris Wilson5dce5b932014-01-20 10:17:36 +00003863bool intel_has_pending_fb_unpin(struct drm_device *dev)
3864{
3865 struct intel_crtc *crtc;
3866
3867 /* Note that we don't need to be called with mode_config.lock here
3868 * as our list of CRTC objects is static for the lifetime of the
3869 * device and so cannot disappear as we iterate. Similarly, we can
3870 * happily treat the predicates as racy, atomic checks as userspace
3871 * cannot claim and pin a new fb without at least acquring the
3872 * struct_mutex and so serialising with us.
3873 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003874 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003875 if (atomic_read(&crtc->unpin_work_count) == 0)
3876 continue;
3877
3878 if (crtc->unpin_work)
3879 intel_wait_for_vblank(dev, crtc->pipe);
3880
3881 return true;
3882 }
3883
3884 return false;
3885}
3886
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003887static void page_flip_completed(struct intel_crtc *intel_crtc)
3888{
3889 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3890 struct intel_unpin_work *work = intel_crtc->unpin_work;
3891
3892 /* ensure that the unpin work is consistent wrt ->pending. */
3893 smp_rmb();
3894 intel_crtc->unpin_work = NULL;
3895
3896 if (work->event)
3897 drm_send_vblank_event(intel_crtc->base.dev,
3898 intel_crtc->pipe,
3899 work->event);
3900
3901 drm_crtc_vblank_put(&intel_crtc->base);
3902
3903 wake_up_all(&dev_priv->pending_flip_queue);
3904 queue_work(dev_priv->wq, &work->work);
3905
3906 trace_i915_flip_complete(intel_crtc->plane,
3907 work->pending_flip_obj);
3908}
3909
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003910static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003911{
Chris Wilson0f911282012-04-17 10:05:38 +01003912 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003913 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003914 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003915
Daniel Vetter2c10d572012-12-20 21:24:07 +01003916 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003917
3918 ret = wait_event_interruptible_timeout(
3919 dev_priv->pending_flip_queue,
3920 !intel_crtc_has_pending_flip(crtc),
3921 60*HZ);
3922
3923 if (ret < 0)
3924 return ret;
3925
3926 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003927 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003928
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003929 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003930 if (intel_crtc->unpin_work) {
3931 WARN_ONCE(1, "Removing stuck page flip\n");
3932 page_flip_completed(intel_crtc);
3933 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003934 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003935 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003936
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003937 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003938}
3939
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003940static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3941{
3942 u32 temp;
3943
3944 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3945
3946 mutex_lock(&dev_priv->sb_lock);
3947
3948 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3949 temp |= SBI_SSCCTL_DISABLE;
3950 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3951
3952 mutex_unlock(&dev_priv->sb_lock);
3953}
3954
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003955/* Program iCLKIP clock to the desired frequency */
3956static void lpt_program_iclkip(struct drm_crtc *crtc)
3957{
3958 struct drm_device *dev = crtc->dev;
3959 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003960 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003961 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3962 u32 temp;
3963
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003964 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003965
3966 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003967 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003968 auxdiv = 1;
3969 divsel = 0x41;
3970 phaseinc = 0x20;
3971 } else {
3972 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003973 * but the adjusted_mode->crtc_clock in in KHz. To get the
3974 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003975 * convert the virtual clock precision to KHz here for higher
3976 * precision.
3977 */
3978 u32 iclk_virtual_root_freq = 172800 * 1000;
3979 u32 iclk_pi_range = 64;
3980 u32 desired_divisor, msb_divisor_value, pi_value;
3981
Ville Syrjäläa2572f52015-12-04 22:20:21 +02003982 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq, clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003983 msb_divisor_value = desired_divisor / iclk_pi_range;
3984 pi_value = desired_divisor % iclk_pi_range;
3985
3986 auxdiv = 0;
3987 divsel = msb_divisor_value - 2;
3988 phaseinc = pi_value;
3989 }
3990
3991 /* This should not happen with any sane values */
3992 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3993 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3994 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3995 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3996
3997 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 +03003998 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003999 auxdiv,
4000 divsel,
4001 phasedir,
4002 phaseinc);
4003
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004004 mutex_lock(&dev_priv->sb_lock);
4005
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004006 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004007 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004008 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4009 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4010 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4011 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4012 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4013 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004014 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004015
4016 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004017 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004018 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4019 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004020 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004021
4022 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004023 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004024 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004025 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004026
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004027 mutex_unlock(&dev_priv->sb_lock);
4028
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004029 /* Wait for initialization time */
4030 udelay(24);
4031
4032 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4033}
4034
Daniel Vetter275f01b22013-05-03 11:49:47 +02004035static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4036 enum pipe pch_transcoder)
4037{
4038 struct drm_device *dev = crtc->base.dev;
4039 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004040 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004041
4042 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4043 I915_READ(HTOTAL(cpu_transcoder)));
4044 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4045 I915_READ(HBLANK(cpu_transcoder)));
4046 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4047 I915_READ(HSYNC(cpu_transcoder)));
4048
4049 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4050 I915_READ(VTOTAL(cpu_transcoder)));
4051 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4052 I915_READ(VBLANK(cpu_transcoder)));
4053 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4054 I915_READ(VSYNC(cpu_transcoder)));
4055 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4056 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4057}
4058
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004059static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004060{
4061 struct drm_i915_private *dev_priv = dev->dev_private;
4062 uint32_t temp;
4063
4064 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004065 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004066 return;
4067
4068 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4069 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4070
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004071 temp &= ~FDI_BC_BIFURCATION_SELECT;
4072 if (enable)
4073 temp |= FDI_BC_BIFURCATION_SELECT;
4074
4075 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004076 I915_WRITE(SOUTH_CHICKEN1, temp);
4077 POSTING_READ(SOUTH_CHICKEN1);
4078}
4079
4080static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4081{
4082 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004083
4084 switch (intel_crtc->pipe) {
4085 case PIPE_A:
4086 break;
4087 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004088 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004089 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004090 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004091 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004092
4093 break;
4094 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004095 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004096
4097 break;
4098 default:
4099 BUG();
4100 }
4101}
4102
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004103/* Return which DP Port should be selected for Transcoder DP control */
4104static enum port
4105intel_trans_dp_port_sel(struct drm_crtc *crtc)
4106{
4107 struct drm_device *dev = crtc->dev;
4108 struct intel_encoder *encoder;
4109
4110 for_each_encoder_on_crtc(dev, crtc, encoder) {
4111 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4112 encoder->type == INTEL_OUTPUT_EDP)
4113 return enc_to_dig_port(&encoder->base)->port;
4114 }
4115
4116 return -1;
4117}
4118
Jesse Barnesf67a5592011-01-05 10:31:48 -08004119/*
4120 * Enable PCH resources required for PCH ports:
4121 * - PCH PLLs
4122 * - FDI training & RX/TX
4123 * - update transcoder timings
4124 * - DP transcoding bits
4125 * - transcoder
4126 */
4127static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004128{
4129 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004130 struct drm_i915_private *dev_priv = dev->dev_private;
4131 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4132 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004133 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004134
Daniel Vetterab9412b2013-05-03 11:49:46 +02004135 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004136
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004137 if (IS_IVYBRIDGE(dev))
4138 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4139
Daniel Vettercd986ab2012-10-26 10:58:12 +02004140 /* Write the TU size bits before fdi link training, so that error
4141 * detection works. */
4142 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4143 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4144
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004145 /*
4146 * Sometimes spurious CPU pipe underruns happen during FDI
4147 * training, at least with VGA+HDMI cloning. Suppress them.
4148 */
4149 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4150
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004151 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004152 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004153
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004154 /* We need to program the right clock selection before writing the pixel
4155 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004156 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004157 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004158
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004159 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004160 temp |= TRANS_DPLL_ENABLE(pipe);
4161 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004162 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004163 temp |= sel;
4164 else
4165 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004166 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004167 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004168
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004169 /* XXX: pch pll's can be enabled any time before we enable the PCH
4170 * transcoder, and we actually should do this to not upset any PCH
4171 * transcoder that already use the clock when we share it.
4172 *
4173 * Note that enable_shared_dpll tries to do the right thing, but
4174 * get_shared_dpll unconditionally resets the pll - we need that to have
4175 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004176 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004177
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004178 /* set transcoder timing, panel must allow it */
4179 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004180 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004181
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004182 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004183
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004184 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4185
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004186 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004187 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004188 const struct drm_display_mode *adjusted_mode =
4189 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004190 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004191 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004192 temp = I915_READ(reg);
4193 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004194 TRANS_DP_SYNC_MASK |
4195 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004196 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004197 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004198
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004199 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004200 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004201 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004202 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004203
4204 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004205 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004206 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004207 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004208 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004209 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004210 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004211 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004212 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004213 break;
4214 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004215 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004216 }
4217
Chris Wilson5eddb702010-09-11 13:48:45 +01004218 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004219 }
4220
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004221 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004222}
4223
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004224static void lpt_pch_enable(struct drm_crtc *crtc)
4225{
4226 struct drm_device *dev = crtc->dev;
4227 struct drm_i915_private *dev_priv = dev->dev_private;
4228 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004229 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004230
Daniel Vetterab9412b2013-05-03 11:49:46 +02004231 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004232
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004233 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004234
Paulo Zanoni0540e482012-10-31 18:12:40 -02004235 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004236 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004237
Paulo Zanoni937bb612012-10-31 18:12:47 -02004238 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004239}
4240
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004241struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4242 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004243{
Daniel Vettere2b78262013-06-07 23:10:03 +02004244 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004245 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004246 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004247 enum intel_dpll_id i;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01004248 int max = dev_priv->num_shared_dpll;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004249
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004250 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4251
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004252 if (HAS_PCH_IBX(dev_priv->dev)) {
4253 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004254 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004255 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004256
Daniel Vetter46edb022013-06-05 13:34:12 +02004257 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4258 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004259
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004260 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004261
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004262 goto found;
4263 }
4264
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304265 if (IS_BROXTON(dev_priv->dev)) {
4266 /* PLL is attached to port in bxt */
4267 struct intel_encoder *encoder;
4268 struct intel_digital_port *intel_dig_port;
4269
4270 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4271 if (WARN_ON(!encoder))
4272 return NULL;
4273
4274 intel_dig_port = enc_to_dig_port(&encoder->base);
4275 /* 1:1 mapping between ports and PLLs */
4276 i = (enum intel_dpll_id)intel_dig_port->port;
4277 pll = &dev_priv->shared_dplls[i];
4278 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4279 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004280 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304281
4282 goto found;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01004283 } else if (INTEL_INFO(dev_priv)->gen < 9 && HAS_DDI(dev_priv))
4284 /* Do not consider SPLL */
4285 max = 2;
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304286
Maarten Lankhorst00490c22015-11-16 14:42:12 +01004287 for (i = 0; i < max; i++) {
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004288 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004289
4290 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004291 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004292 continue;
4293
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004294 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004295 &shared_dpll[i].hw_state,
4296 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004297 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004298 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004299 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004300 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004301 goto found;
4302 }
4303 }
4304
4305 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004306 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4307 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004308 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004309 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4310 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004311 goto found;
4312 }
4313 }
4314
4315 return NULL;
4316
4317found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004318 if (shared_dpll[i].crtc_mask == 0)
4319 shared_dpll[i].hw_state =
4320 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004321
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004322 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004323 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4324 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004325
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004326 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004327
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004328 return pll;
4329}
4330
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004331static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004332{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004333 struct drm_i915_private *dev_priv = to_i915(state->dev);
4334 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004335 struct intel_shared_dpll *pll;
4336 enum intel_dpll_id i;
4337
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004338 if (!to_intel_atomic_state(state)->dpll_set)
4339 return;
4340
4341 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004342 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4343 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004344 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004345 }
4346}
4347
Daniel Vettera1520312013-05-03 11:49:50 +02004348static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004349{
4350 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004351 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004352 u32 temp;
4353
4354 temp = I915_READ(dslreg);
4355 udelay(500);
4356 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004357 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004358 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004359 }
4360}
4361
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004362static int
4363skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4364 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4365 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004366{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004367 struct intel_crtc_scaler_state *scaler_state =
4368 &crtc_state->scaler_state;
4369 struct intel_crtc *intel_crtc =
4370 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004371 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004372
4373 need_scaling = intel_rotation_90_or_270(rotation) ?
4374 (src_h != dst_w || src_w != dst_h):
4375 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004376
4377 /*
4378 * if plane is being disabled or scaler is no more required or force detach
4379 * - free scaler binded to this plane/crtc
4380 * - in order to do this, update crtc->scaler_usage
4381 *
4382 * Here scaler state in crtc_state is set free so that
4383 * scaler can be assigned to other user. Actual register
4384 * update to free the scaler is done in plane/panel-fit programming.
4385 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4386 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004387 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004388 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004389 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004390 scaler_state->scalers[*scaler_id].in_use = 0;
4391
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004392 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4393 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4394 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004395 scaler_state->scaler_users);
4396 *scaler_id = -1;
4397 }
4398 return 0;
4399 }
4400
4401 /* range checks */
4402 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4403 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4404
4405 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4406 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004407 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004408 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004409 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004410 return -EINVAL;
4411 }
4412
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004413 /* mark this plane as a scaler user in crtc_state */
4414 scaler_state->scaler_users |= (1 << scaler_user);
4415 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4416 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4417 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4418 scaler_state->scaler_users);
4419
4420 return 0;
4421}
4422
4423/**
4424 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4425 *
4426 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004427 *
4428 * Return
4429 * 0 - scaler_usage updated successfully
4430 * error - requested scaling cannot be supported or other error condition
4431 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004432int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004433{
4434 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004435 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004436
4437 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4438 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4439
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004440 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004441 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4442 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004443 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004444}
4445
4446/**
4447 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4448 *
4449 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004450 * @plane_state: atomic plane state to update
4451 *
4452 * Return
4453 * 0 - scaler_usage updated successfully
4454 * error - requested scaling cannot be supported or other error condition
4455 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004456static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4457 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004458{
4459
4460 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004461 struct intel_plane *intel_plane =
4462 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004463 struct drm_framebuffer *fb = plane_state->base.fb;
4464 int ret;
4465
4466 bool force_detach = !fb || !plane_state->visible;
4467
4468 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4469 intel_plane->base.base.id, intel_crtc->pipe,
4470 drm_plane_index(&intel_plane->base));
4471
4472 ret = skl_update_scaler(crtc_state, force_detach,
4473 drm_plane_index(&intel_plane->base),
4474 &plane_state->scaler_id,
4475 plane_state->base.rotation,
4476 drm_rect_width(&plane_state->src) >> 16,
4477 drm_rect_height(&plane_state->src) >> 16,
4478 drm_rect_width(&plane_state->dst),
4479 drm_rect_height(&plane_state->dst));
4480
4481 if (ret || plane_state->scaler_id < 0)
4482 return ret;
4483
Chandra Kondurua1b22782015-04-07 15:28:45 -07004484 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004485 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004486 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004487 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004488 return -EINVAL;
4489 }
4490
4491 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004492 switch (fb->pixel_format) {
4493 case DRM_FORMAT_RGB565:
4494 case DRM_FORMAT_XBGR8888:
4495 case DRM_FORMAT_XRGB8888:
4496 case DRM_FORMAT_ABGR8888:
4497 case DRM_FORMAT_ARGB8888:
4498 case DRM_FORMAT_XRGB2101010:
4499 case DRM_FORMAT_XBGR2101010:
4500 case DRM_FORMAT_YUYV:
4501 case DRM_FORMAT_YVYU:
4502 case DRM_FORMAT_UYVY:
4503 case DRM_FORMAT_VYUY:
4504 break;
4505 default:
4506 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4507 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4508 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004509 }
4510
Chandra Kondurua1b22782015-04-07 15:28:45 -07004511 return 0;
4512}
4513
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004514static void skylake_scaler_disable(struct intel_crtc *crtc)
4515{
4516 int i;
4517
4518 for (i = 0; i < crtc->num_scalers; i++)
4519 skl_detach_scaler(crtc, i);
4520}
4521
4522static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004523{
4524 struct drm_device *dev = crtc->base.dev;
4525 struct drm_i915_private *dev_priv = dev->dev_private;
4526 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004527 struct intel_crtc_scaler_state *scaler_state =
4528 &crtc->config->scaler_state;
4529
4530 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4531
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004532 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004533 int id;
4534
4535 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4536 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4537 return;
4538 }
4539
4540 id = scaler_state->scaler_id;
4541 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4542 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4543 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4544 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4545
4546 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004547 }
4548}
4549
Jesse Barnesb074cec2013-04-25 12:55:02 -07004550static void ironlake_pfit_enable(struct intel_crtc *crtc)
4551{
4552 struct drm_device *dev = crtc->base.dev;
4553 struct drm_i915_private *dev_priv = dev->dev_private;
4554 int pipe = crtc->pipe;
4555
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004556 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004557 /* Force use of hard-coded filter coefficients
4558 * as some pre-programmed values are broken,
4559 * e.g. x201.
4560 */
4561 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4562 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4563 PF_PIPE_SEL_IVB(pipe));
4564 else
4565 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004566 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4567 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004568 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004569}
4570
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004571void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004572{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004573 struct drm_device *dev = crtc->base.dev;
4574 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004575
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004576 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004577 return;
4578
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004579 /* We can only enable IPS after we enable a plane and wait for a vblank */
4580 intel_wait_for_vblank(dev, crtc->pipe);
4581
Paulo Zanonid77e4532013-09-24 13:52:55 -03004582 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004583 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004584 mutex_lock(&dev_priv->rps.hw_lock);
4585 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4586 mutex_unlock(&dev_priv->rps.hw_lock);
4587 /* Quoting Art Runyan: "its not safe to expect any particular
4588 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004589 * mailbox." Moreover, the mailbox may return a bogus state,
4590 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004591 */
4592 } else {
4593 I915_WRITE(IPS_CTL, IPS_ENABLE);
4594 /* The bit only becomes 1 in the next vblank, so this wait here
4595 * is essentially intel_wait_for_vblank. If we don't have this
4596 * and don't wait for vblanks until the end of crtc_enable, then
4597 * the HW state readout code will complain that the expected
4598 * IPS_CTL value is not the one we read. */
4599 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4600 DRM_ERROR("Timed out waiting for IPS enable\n");
4601 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004602}
4603
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004604void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004605{
4606 struct drm_device *dev = crtc->base.dev;
4607 struct drm_i915_private *dev_priv = dev->dev_private;
4608
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004609 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004610 return;
4611
4612 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004613 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004614 mutex_lock(&dev_priv->rps.hw_lock);
4615 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4616 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004617 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4618 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4619 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004620 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004621 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004622 POSTING_READ(IPS_CTL);
4623 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004624
4625 /* We need to wait for a vblank before we can disable the plane. */
4626 intel_wait_for_vblank(dev, crtc->pipe);
4627}
4628
4629/** Loads the palette/gamma unit for the CRTC with the prepared values */
4630static void intel_crtc_load_lut(struct drm_crtc *crtc)
4631{
4632 struct drm_device *dev = crtc->dev;
4633 struct drm_i915_private *dev_priv = dev->dev_private;
4634 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4635 enum pipe pipe = intel_crtc->pipe;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004636 int i;
4637 bool reenable_ips = false;
4638
4639 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004640 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004641 return;
4642
Imre Deak50360402015-01-16 00:55:16 -08004643 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Jani Nikulaa65347b2015-11-27 12:21:46 +02004644 if (intel_crtc->config->has_dsi_encoder)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004645 assert_dsi_pll_enabled(dev_priv);
4646 else
4647 assert_pll_enabled(dev_priv, pipe);
4648 }
4649
Paulo Zanonid77e4532013-09-24 13:52:55 -03004650 /* Workaround : Do not read or write the pipe palette/gamma data while
4651 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4652 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004653 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004654 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4655 GAMMA_MODE_MODE_SPLIT)) {
4656 hsw_disable_ips(intel_crtc);
4657 reenable_ips = true;
4658 }
4659
4660 for (i = 0; i < 256; i++) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004661 i915_reg_t palreg;
Ville Syrjäläf65a9c52015-09-18 20:03:28 +03004662
4663 if (HAS_GMCH_DISPLAY(dev))
4664 palreg = PALETTE(pipe, i);
4665 else
4666 palreg = LGC_PALETTE(pipe, i);
4667
4668 I915_WRITE(palreg,
Paulo Zanonid77e4532013-09-24 13:52:55 -03004669 (intel_crtc->lut_r[i] << 16) |
4670 (intel_crtc->lut_g[i] << 8) |
4671 intel_crtc->lut_b[i]);
4672 }
4673
4674 if (reenable_ips)
4675 hsw_enable_ips(intel_crtc);
4676}
4677
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004678static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004679{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004680 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004681 struct drm_device *dev = intel_crtc->base.dev;
4682 struct drm_i915_private *dev_priv = dev->dev_private;
4683
4684 mutex_lock(&dev->struct_mutex);
4685 dev_priv->mm.interruptible = false;
4686 (void) intel_overlay_switch_off(intel_crtc->overlay);
4687 dev_priv->mm.interruptible = true;
4688 mutex_unlock(&dev->struct_mutex);
4689 }
4690
4691 /* Let userspace switch the overlay on again. In most cases userspace
4692 * has to recompute where to put it anyway.
4693 */
4694}
4695
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004696/**
4697 * intel_post_enable_primary - Perform operations after enabling primary plane
4698 * @crtc: the CRTC whose primary plane was just enabled
4699 *
4700 * Performs potentially sleeping operations that must be done after the primary
4701 * plane is enabled, such as updating FBC and IPS. Note that this may be
4702 * called due to an explicit primary plane update, or due to an implicit
4703 * re-enable that is caused when a sprite plane is updated to no longer
4704 * completely hide the primary plane.
4705 */
4706static void
4707intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004708{
4709 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004710 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004711 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4712 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004713
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004714 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004715 * FIXME IPS should be fine as long as one plane is
4716 * enabled, but in practice it seems to have problems
4717 * when going from primary only to sprite only and vice
4718 * versa.
4719 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004720 hsw_enable_ips(intel_crtc);
4721
Daniel Vetterf99d7062014-06-19 16:01:59 +02004722 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004723 * Gen2 reports pipe underruns whenever all planes are disabled.
4724 * So don't enable underrun reporting before at least some planes
4725 * are enabled.
4726 * FIXME: Need to fix the logic to work when we turn off all planes
4727 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004728 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004729 if (IS_GEN2(dev))
4730 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4731
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004732 /* Underruns don't always raise interrupts, so check manually. */
4733 intel_check_cpu_fifo_underruns(dev_priv);
4734 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004735}
4736
4737/**
4738 * intel_pre_disable_primary - Perform operations before disabling primary plane
4739 * @crtc: the CRTC whose primary plane is to be disabled
4740 *
4741 * Performs potentially sleeping operations that must be done before the
4742 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4743 * be called due to an explicit primary plane update, or due to an implicit
4744 * disable that is caused when a sprite plane completely hides the primary
4745 * plane.
4746 */
4747static void
4748intel_pre_disable_primary(struct drm_crtc *crtc)
4749{
4750 struct drm_device *dev = crtc->dev;
4751 struct drm_i915_private *dev_priv = dev->dev_private;
4752 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4753 int pipe = intel_crtc->pipe;
4754
4755 /*
4756 * Gen2 reports pipe underruns whenever all planes are disabled.
4757 * So diasble underrun reporting before all the planes get disabled.
4758 * FIXME: Need to fix the logic to work when we turn off all planes
4759 * but leave the pipe running.
4760 */
4761 if (IS_GEN2(dev))
4762 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4763
4764 /*
4765 * Vblank time updates from the shadow to live plane control register
4766 * are blocked if the memory self-refresh mode is active at that
4767 * moment. So to make sure the plane gets truly disabled, disable
4768 * first the self-refresh mode. The self-refresh enable bit in turn
4769 * will be checked/applied by the HW only at the next frame start
4770 * event which is after the vblank start event, so we need to have a
4771 * wait-for-vblank between disabling the plane and the pipe.
4772 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004773 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004774 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004775 dev_priv->wm.vlv.cxsr = false;
4776 intel_wait_for_vblank(dev, pipe);
4777 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004778
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004779 /*
4780 * FIXME IPS should be fine as long as one plane is
4781 * enabled, but in practice it seems to have problems
4782 * when going from primary only to sprite only and vice
4783 * versa.
4784 */
4785 hsw_disable_ips(intel_crtc);
4786}
4787
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004788static void intel_post_plane_update(struct intel_crtc *crtc)
4789{
4790 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004791 struct intel_crtc_state *pipe_config =
4792 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004793 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004794
4795 if (atomic->wait_vblank)
4796 intel_wait_for_vblank(dev, crtc->pipe);
4797
4798 intel_frontbuffer_flip(dev, atomic->fb_bits);
4799
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004800 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004801
Maarten Lankhorstb9001112015-11-19 16:07:16 +01004802 if (pipe_config->wm_changed && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004803 intel_update_watermarks(&crtc->base);
4804
Paulo Zanonic80ac852015-07-02 19:25:13 -03004805 if (atomic->update_fbc)
Paulo Zanoni754d1132015-10-13 19:13:25 -03004806 intel_fbc_update(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004807
4808 if (atomic->post_enable_primary)
4809 intel_post_enable_primary(&crtc->base);
4810
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004811 memset(atomic, 0, sizeof(*atomic));
4812}
4813
4814static void intel_pre_plane_update(struct intel_crtc *crtc)
4815{
4816 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004817 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004818 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004819 struct intel_crtc_state *pipe_config =
4820 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004821
Paulo Zanonic80ac852015-07-02 19:25:13 -03004822 if (atomic->disable_fbc)
Paulo Zanonid029bca2015-10-15 10:44:46 -03004823 intel_fbc_deactivate(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004824
Rodrigo Vivi066cf552015-06-26 13:55:54 -07004825 if (crtc->atomic.disable_ips)
4826 hsw_disable_ips(crtc);
4827
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004828 if (atomic->pre_disable_primary)
4829 intel_pre_disable_primary(&crtc->base);
Ville Syrjälä852eb002015-06-24 22:00:07 +03004830
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004831 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004832 crtc->wm.cxsr_allowed = false;
4833 intel_set_memory_cxsr(dev_priv, false);
4834 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004835
Matt Roper396e33a2016-01-06 11:34:30 -08004836 /*
4837 * IVB workaround: must disable low power watermarks for at least
4838 * one frame before enabling scaling. LP watermarks can be re-enabled
4839 * when scaling is disabled.
4840 *
4841 * WaCxSRDisabledForSpriteScaling:ivb
4842 */
4843 if (pipe_config->disable_lp_wm) {
4844 ilk_disable_lp_wm(dev);
4845 intel_wait_for_vblank(dev, crtc->pipe);
4846 }
4847
4848 /*
4849 * If we're doing a modeset, we're done. No need to do any pre-vblank
4850 * watermark programming here.
4851 */
4852 if (needs_modeset(&pipe_config->base))
4853 return;
4854
4855 /*
4856 * For platforms that support atomic watermarks, program the
4857 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4858 * will be the intermediate values that are safe for both pre- and
4859 * post- vblank; when vblank happens, the 'active' values will be set
4860 * to the final 'target' values and we'll do this again to get the
4861 * optimal watermarks. For gen9+ platforms, the values we program here
4862 * will be the final target values which will get automatically latched
4863 * at vblank time; no further programming will be necessary.
4864 *
4865 * If a platform hasn't been transitioned to atomic watermarks yet,
4866 * we'll continue to update watermarks the old way, if flags tell
4867 * us to.
4868 */
4869 if (dev_priv->display.initial_watermarks != NULL)
4870 dev_priv->display.initial_watermarks(pipe_config);
4871 else if (pipe_config->wm_changed)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004872 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004873}
4874
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004875static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004876{
4877 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004878 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004879 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004880 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004881
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004882 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004883
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004884 drm_for_each_plane_mask(p, dev, plane_mask)
4885 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004886
Daniel Vetterf99d7062014-06-19 16:01:59 +02004887 /*
4888 * FIXME: Once we grow proper nuclear flip support out of this we need
4889 * to compute the mask of flip planes precisely. For the time being
4890 * consider this a flip to a NULL plane.
4891 */
4892 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004893}
4894
Jesse Barnesf67a5592011-01-05 10:31:48 -08004895static void ironlake_crtc_enable(struct drm_crtc *crtc)
4896{
4897 struct drm_device *dev = crtc->dev;
4898 struct drm_i915_private *dev_priv = dev->dev_private;
4899 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004900 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004901 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004902
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004903 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004904 return;
4905
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004906 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004907 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4908
4909 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004910 intel_prepare_shared_dpll(intel_crtc);
4911
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004912 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304913 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004914
4915 intel_set_pipe_timings(intel_crtc);
4916
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004917 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004918 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004919 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004920 }
4921
4922 ironlake_set_pipeconf(crtc);
4923
Jesse Barnesf67a5592011-01-05 10:31:48 -08004924 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004925
Daniel Vettera72e4c92014-09-30 10:56:47 +02004926 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004927
Daniel Vetterf6736a12013-06-05 13:34:30 +02004928 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004929 if (encoder->pre_enable)
4930 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004931
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004932 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004933 /* Note: FDI PLL enabling _must_ be done before we enable the
4934 * cpu pipes, hence this is separate from all the other fdi/pch
4935 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004936 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004937 } else {
4938 assert_fdi_tx_disabled(dev_priv, pipe);
4939 assert_fdi_rx_disabled(dev_priv, pipe);
4940 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004941
Jesse Barnesb074cec2013-04-25 12:55:02 -07004942 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004943
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004944 /*
4945 * On ILK+ LUT must be loaded before the pipe is running but with
4946 * clocks enabled
4947 */
4948 intel_crtc_load_lut(crtc);
4949
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004950 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004951 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004952
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004953 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004954 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004955
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004956 assert_vblank_disabled(crtc);
4957 drm_crtc_vblank_on(crtc);
4958
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004959 for_each_encoder_on_crtc(dev, crtc, encoder)
4960 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004961
4962 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004963 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004964
4965 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4966 if (intel_crtc->config->has_pch_encoder)
4967 intel_wait_for_vblank(dev, pipe);
4968 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanonid029bca2015-10-15 10:44:46 -03004969
4970 intel_fbc_enable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004971}
4972
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004973/* IPS only exists on ULT machines and is tied to pipe A. */
4974static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4975{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004976 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004977}
4978
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004979static void haswell_crtc_enable(struct drm_crtc *crtc)
4980{
4981 struct drm_device *dev = crtc->dev;
4982 struct drm_i915_private *dev_priv = dev->dev_private;
4983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4984 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004985 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4986 struct intel_crtc_state *pipe_config =
4987 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004988
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004989 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004990 return;
4991
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004992 if (intel_crtc->config->has_pch_encoder)
4993 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4994 false);
4995
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004996 if (intel_crtc_to_shared_dpll(intel_crtc))
4997 intel_enable_shared_dpll(intel_crtc);
4998
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004999 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305000 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005001
5002 intel_set_pipe_timings(intel_crtc);
5003
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005004 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5005 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5006 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005007 }
5008
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005009 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005010 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005011 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005012 }
5013
5014 haswell_set_pipeconf(crtc);
5015
5016 intel_set_pipe_csc(crtc);
5017
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005018 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005019
Daniel Vetter6b698512015-11-28 11:05:39 +01005020 if (intel_crtc->config->has_pch_encoder)
5021 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5022 else
5023 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5024
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305025 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005026 if (encoder->pre_enable)
5027 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305028 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005029
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005030 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03005031 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03005032
Jani Nikulaa65347b2015-11-27 12:21:46 +02005033 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305034 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005035
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005036 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005037 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005038 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005039 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005040
5041 /*
5042 * On ILK+ LUT must be loaded before the pipe is running but with
5043 * clocks enabled
5044 */
5045 intel_crtc_load_lut(crtc);
5046
Paulo Zanoni1f544382012-10-24 11:32:00 -02005047 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02005048 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305049 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005050
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005051 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005052 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005053
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005054 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005055 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005056
Jani Nikulaa65347b2015-11-27 12:21:46 +02005057 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005058 intel_ddi_set_vc_payload_alloc(crtc, true);
5059
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005060 assert_vblank_disabled(crtc);
5061 drm_crtc_vblank_on(crtc);
5062
Jani Nikula8807e552013-08-30 19:40:32 +03005063 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005064 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005065 intel_opregion_notify_encoder(encoder, true);
5066 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005067
Daniel Vetter6b698512015-11-28 11:05:39 +01005068 if (intel_crtc->config->has_pch_encoder) {
5069 intel_wait_for_vblank(dev, pipe);
5070 intel_wait_for_vblank(dev, pipe);
5071 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005072 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5073 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01005074 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005075
Paulo Zanonie4916942013-09-20 16:21:19 -03005076 /* If we change the relative order between pipe/planes enabling, we need
5077 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005078 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5079 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5080 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5081 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5082 }
Paulo Zanonid029bca2015-10-15 10:44:46 -03005083
5084 intel_fbc_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005085}
5086
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005087static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005088{
5089 struct drm_device *dev = crtc->base.dev;
5090 struct drm_i915_private *dev_priv = dev->dev_private;
5091 int pipe = crtc->pipe;
5092
5093 /* To avoid upsetting the power well on haswell only disable the pfit if
5094 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005095 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005096 I915_WRITE(PF_CTL(pipe), 0);
5097 I915_WRITE(PF_WIN_POS(pipe), 0);
5098 I915_WRITE(PF_WIN_SZ(pipe), 0);
5099 }
5100}
5101
Jesse Barnes6be4a602010-09-10 10:26:01 -07005102static void ironlake_crtc_disable(struct drm_crtc *crtc)
5103{
5104 struct drm_device *dev = crtc->dev;
5105 struct drm_i915_private *dev_priv = dev->dev_private;
5106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005107 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005108 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005109
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005110 if (intel_crtc->config->has_pch_encoder)
5111 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5112
Daniel Vetterea9d7582012-07-10 10:42:52 +02005113 for_each_encoder_on_crtc(dev, crtc, encoder)
5114 encoder->disable(encoder);
5115
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005116 drm_crtc_vblank_off(crtc);
5117 assert_vblank_disabled(crtc);
5118
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005119 /*
5120 * Sometimes spurious CPU pipe underruns happen when the
5121 * pipe is already disabled, but FDI RX/TX is still enabled.
5122 * Happens at least with VGA+HDMI cloning. Suppress them.
5123 */
5124 if (intel_crtc->config->has_pch_encoder)
5125 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5126
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005127 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005128
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005129 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005130
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005131 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005132 ironlake_fdi_disable(crtc);
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005133 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5134 }
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005135
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005136 for_each_encoder_on_crtc(dev, crtc, encoder)
5137 if (encoder->post_disable)
5138 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005139
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005140 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005141 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005142
Daniel Vetterd925c592013-06-05 13:34:04 +02005143 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005144 i915_reg_t reg;
5145 u32 temp;
5146
Daniel Vetterd925c592013-06-05 13:34:04 +02005147 /* disable TRANS_DP_CTL */
5148 reg = TRANS_DP_CTL(pipe);
5149 temp = I915_READ(reg);
5150 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5151 TRANS_DP_PORT_SEL_MASK);
5152 temp |= TRANS_DP_PORT_SEL_NONE;
5153 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005154
Daniel Vetterd925c592013-06-05 13:34:04 +02005155 /* disable DPLL_SEL */
5156 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005157 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005158 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005159 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005160
Daniel Vetterd925c592013-06-05 13:34:04 +02005161 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005162 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005163
5164 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanonid029bca2015-10-15 10:44:46 -03005165
5166 intel_fbc_disable_crtc(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005167}
5168
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005169static void haswell_crtc_disable(struct drm_crtc *crtc)
5170{
5171 struct drm_device *dev = crtc->dev;
5172 struct drm_i915_private *dev_priv = dev->dev_private;
5173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5174 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005175 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005176
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005177 if (intel_crtc->config->has_pch_encoder)
5178 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5179 false);
5180
Jani Nikula8807e552013-08-30 19:40:32 +03005181 for_each_encoder_on_crtc(dev, crtc, encoder) {
5182 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005183 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005184 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005185
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005186 drm_crtc_vblank_off(crtc);
5187 assert_vblank_disabled(crtc);
5188
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005189 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005190
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005191 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005192 intel_ddi_set_vc_payload_alloc(crtc, false);
5193
Jani Nikulaa65347b2015-11-27 12:21:46 +02005194 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305195 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005196
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005197 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005198 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005199 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005200 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005201
Jani Nikulaa65347b2015-11-27 12:21:46 +02005202 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305203 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005204
Imre Deak97b040a2014-06-25 22:01:50 +03005205 for_each_encoder_on_crtc(dev, crtc, encoder)
5206 if (encoder->post_disable)
5207 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005208
Ville Syrjälä92966a32015-12-08 16:05:48 +02005209 if (intel_crtc->config->has_pch_encoder) {
5210 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005211 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005212 intel_ddi_fdi_disable(crtc);
5213
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005214 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5215 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005216 }
Paulo Zanonid029bca2015-10-15 10:44:46 -03005217
5218 intel_fbc_disable_crtc(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005219}
5220
Jesse Barnes2dd24552013-04-25 12:55:01 -07005221static void i9xx_pfit_enable(struct intel_crtc *crtc)
5222{
5223 struct drm_device *dev = crtc->base.dev;
5224 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005225 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005226
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005227 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005228 return;
5229
Daniel Vetterc0b03412013-05-28 12:05:54 +02005230 /*
5231 * The panel fitter should only be adjusted whilst the pipe is disabled,
5232 * according to register description and PRM.
5233 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005234 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5235 assert_pipe_disabled(dev_priv, crtc->pipe);
5236
Jesse Barnesb074cec2013-04-25 12:55:02 -07005237 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5238 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005239
5240 /* Border color in case we don't scale up to the full screen. Black by
5241 * default, change to something else for debugging. */
5242 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005243}
5244
Dave Airlied05410f2014-06-05 13:22:59 +10005245static enum intel_display_power_domain port_to_power_domain(enum port port)
5246{
5247 switch (port) {
5248 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005249 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005250 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005251 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005252 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005253 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005254 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005255 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005256 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005257 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005258 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005259 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005260 return POWER_DOMAIN_PORT_OTHER;
5261 }
5262}
5263
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005264static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5265{
5266 switch (port) {
5267 case PORT_A:
5268 return POWER_DOMAIN_AUX_A;
5269 case PORT_B:
5270 return POWER_DOMAIN_AUX_B;
5271 case PORT_C:
5272 return POWER_DOMAIN_AUX_C;
5273 case PORT_D:
5274 return POWER_DOMAIN_AUX_D;
5275 case PORT_E:
5276 /* FIXME: Check VBT for actual wiring of PORT E */
5277 return POWER_DOMAIN_AUX_D;
5278 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005279 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005280 return POWER_DOMAIN_AUX_A;
5281 }
5282}
5283
Imre Deak319be8a2014-03-04 19:22:57 +02005284enum intel_display_power_domain
5285intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005286{
Imre Deak319be8a2014-03-04 19:22:57 +02005287 struct drm_device *dev = intel_encoder->base.dev;
5288 struct intel_digital_port *intel_dig_port;
5289
5290 switch (intel_encoder->type) {
5291 case INTEL_OUTPUT_UNKNOWN:
5292 /* Only DDI platforms should ever use this output type */
5293 WARN_ON_ONCE(!HAS_DDI(dev));
5294 case INTEL_OUTPUT_DISPLAYPORT:
5295 case INTEL_OUTPUT_HDMI:
5296 case INTEL_OUTPUT_EDP:
5297 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005298 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005299 case INTEL_OUTPUT_DP_MST:
5300 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5301 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005302 case INTEL_OUTPUT_ANALOG:
5303 return POWER_DOMAIN_PORT_CRT;
5304 case INTEL_OUTPUT_DSI:
5305 return POWER_DOMAIN_PORT_DSI;
5306 default:
5307 return POWER_DOMAIN_PORT_OTHER;
5308 }
5309}
5310
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005311enum intel_display_power_domain
5312intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5313{
5314 struct drm_device *dev = intel_encoder->base.dev;
5315 struct intel_digital_port *intel_dig_port;
5316
5317 switch (intel_encoder->type) {
5318 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005319 case INTEL_OUTPUT_HDMI:
5320 /*
5321 * Only DDI platforms should ever use these output types.
5322 * We can get here after the HDMI detect code has already set
5323 * the type of the shared encoder. Since we can't be sure
5324 * what's the status of the given connectors, play safe and
5325 * run the DP detection too.
5326 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005327 WARN_ON_ONCE(!HAS_DDI(dev));
5328 case INTEL_OUTPUT_DISPLAYPORT:
5329 case INTEL_OUTPUT_EDP:
5330 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5331 return port_to_aux_power_domain(intel_dig_port->port);
5332 case INTEL_OUTPUT_DP_MST:
5333 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5334 return port_to_aux_power_domain(intel_dig_port->port);
5335 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005336 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005337 return POWER_DOMAIN_AUX_A;
5338 }
5339}
5340
Imre Deak319be8a2014-03-04 19:22:57 +02005341static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5342{
5343 struct drm_device *dev = crtc->dev;
5344 struct intel_encoder *intel_encoder;
5345 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5346 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005347 unsigned long mask;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02005348 enum transcoder transcoder = intel_crtc->config->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005349
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005350 if (!crtc->state->active)
5351 return 0;
5352
Imre Deak77d22dc2014-03-05 16:20:52 +02005353 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5354 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005355 if (intel_crtc->config->pch_pfit.enabled ||
5356 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005357 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5358
Imre Deak319be8a2014-03-04 19:22:57 +02005359 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5360 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5361
Imre Deak77d22dc2014-03-05 16:20:52 +02005362 return mask;
5363}
5364
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005365static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
5366{
5367 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5368 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5369 enum intel_display_power_domain domain;
5370 unsigned long domains, new_domains, old_domains;
5371
5372 old_domains = intel_crtc->enabled_power_domains;
5373 intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
5374
5375 domains = new_domains & ~old_domains;
5376
5377 for_each_power_domain(domain, domains)
5378 intel_display_power_get(dev_priv, domain);
5379
5380 return old_domains & ~new_domains;
5381}
5382
5383static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5384 unsigned long domains)
5385{
5386 enum intel_display_power_domain domain;
5387
5388 for_each_power_domain(domain, domains)
5389 intel_display_power_put(dev_priv, domain);
5390}
5391
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005392static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005393{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005394 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005395 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005396 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005397 unsigned long put_domains[I915_MAX_PIPES] = {};
5398 struct drm_crtc_state *crtc_state;
5399 struct drm_crtc *crtc;
5400 int i;
Imre Deak77d22dc2014-03-05 16:20:52 +02005401
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005402 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5403 if (needs_modeset(crtc->state))
5404 put_domains[to_intel_crtc(crtc)->pipe] =
5405 modeset_get_crtc_power_domains(crtc);
Imre Deak77d22dc2014-03-05 16:20:52 +02005406 }
5407
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005408 if (dev_priv->display.modeset_commit_cdclk &&
5409 intel_state->dev_cdclk != dev_priv->cdclk_freq)
5410 dev_priv->display.modeset_commit_cdclk(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005411
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005412 for (i = 0; i < I915_MAX_PIPES; i++)
5413 if (put_domains[i])
5414 modeset_put_power_domains(dev_priv, put_domains[i]);
Imre Deak77d22dc2014-03-05 16:20:52 +02005415}
5416
Mika Kaholaadafdc62015-08-18 14:36:59 +03005417static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5418{
5419 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5420
5421 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5422 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5423 return max_cdclk_freq;
5424 else if (IS_CHERRYVIEW(dev_priv))
5425 return max_cdclk_freq*95/100;
5426 else if (INTEL_INFO(dev_priv)->gen < 4)
5427 return 2*max_cdclk_freq*90/100;
5428 else
5429 return max_cdclk_freq*90/100;
5430}
5431
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005432static void intel_update_max_cdclk(struct drm_device *dev)
5433{
5434 struct drm_i915_private *dev_priv = dev->dev_private;
5435
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005436 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005437 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5438
5439 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5440 dev_priv->max_cdclk_freq = 675000;
5441 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5442 dev_priv->max_cdclk_freq = 540000;
5443 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5444 dev_priv->max_cdclk_freq = 450000;
5445 else
5446 dev_priv->max_cdclk_freq = 337500;
5447 } else if (IS_BROADWELL(dev)) {
5448 /*
5449 * FIXME with extra cooling we can allow
5450 * 540 MHz for ULX and 675 Mhz for ULT.
5451 * How can we know if extra cooling is
5452 * available? PCI ID, VTB, something else?
5453 */
5454 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5455 dev_priv->max_cdclk_freq = 450000;
5456 else if (IS_BDW_ULX(dev))
5457 dev_priv->max_cdclk_freq = 450000;
5458 else if (IS_BDW_ULT(dev))
5459 dev_priv->max_cdclk_freq = 540000;
5460 else
5461 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005462 } else if (IS_CHERRYVIEW(dev)) {
5463 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005464 } else if (IS_VALLEYVIEW(dev)) {
5465 dev_priv->max_cdclk_freq = 400000;
5466 } else {
5467 /* otherwise assume cdclk is fixed */
5468 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5469 }
5470
Mika Kaholaadafdc62015-08-18 14:36:59 +03005471 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5472
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005473 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5474 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005475
5476 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5477 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005478}
5479
5480static void intel_update_cdclk(struct drm_device *dev)
5481{
5482 struct drm_i915_private *dev_priv = dev->dev_private;
5483
5484 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5485 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5486 dev_priv->cdclk_freq);
5487
5488 /*
5489 * Program the gmbus_freq based on the cdclk frequency.
5490 * BSpec erroneously claims we should aim for 4MHz, but
5491 * in fact 1MHz is the correct frequency.
5492 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005493 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005494 /*
5495 * Program the gmbus_freq based on the cdclk frequency.
5496 * BSpec erroneously claims we should aim for 4MHz, but
5497 * in fact 1MHz is the correct frequency.
5498 */
5499 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5500 }
5501
5502 if (dev_priv->max_cdclk_freq == 0)
5503 intel_update_max_cdclk(dev);
5504}
5505
Damien Lespiau70d0c572015-06-04 18:21:29 +01005506static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305507{
5508 struct drm_i915_private *dev_priv = dev->dev_private;
5509 uint32_t divider;
5510 uint32_t ratio;
5511 uint32_t current_freq;
5512 int ret;
5513
5514 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5515 switch (frequency) {
5516 case 144000:
5517 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5518 ratio = BXT_DE_PLL_RATIO(60);
5519 break;
5520 case 288000:
5521 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5522 ratio = BXT_DE_PLL_RATIO(60);
5523 break;
5524 case 384000:
5525 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5526 ratio = BXT_DE_PLL_RATIO(60);
5527 break;
5528 case 576000:
5529 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5530 ratio = BXT_DE_PLL_RATIO(60);
5531 break;
5532 case 624000:
5533 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5534 ratio = BXT_DE_PLL_RATIO(65);
5535 break;
5536 case 19200:
5537 /*
5538 * Bypass frequency with DE PLL disabled. Init ratio, divider
5539 * to suppress GCC warning.
5540 */
5541 ratio = 0;
5542 divider = 0;
5543 break;
5544 default:
5545 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5546
5547 return;
5548 }
5549
5550 mutex_lock(&dev_priv->rps.hw_lock);
5551 /* Inform power controller of upcoming frequency change */
5552 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5553 0x80000000);
5554 mutex_unlock(&dev_priv->rps.hw_lock);
5555
5556 if (ret) {
5557 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5558 ret, frequency);
5559 return;
5560 }
5561
5562 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5563 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5564 current_freq = current_freq * 500 + 1000;
5565
5566 /*
5567 * DE PLL has to be disabled when
5568 * - setting to 19.2MHz (bypass, PLL isn't used)
5569 * - before setting to 624MHz (PLL needs toggling)
5570 * - before setting to any frequency from 624MHz (PLL needs toggling)
5571 */
5572 if (frequency == 19200 || frequency == 624000 ||
5573 current_freq == 624000) {
5574 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5575 /* Timeout 200us */
5576 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5577 1))
5578 DRM_ERROR("timout waiting for DE PLL unlock\n");
5579 }
5580
5581 if (frequency != 19200) {
5582 uint32_t val;
5583
5584 val = I915_READ(BXT_DE_PLL_CTL);
5585 val &= ~BXT_DE_PLL_RATIO_MASK;
5586 val |= ratio;
5587 I915_WRITE(BXT_DE_PLL_CTL, val);
5588
5589 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5590 /* Timeout 200us */
5591 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5592 DRM_ERROR("timeout waiting for DE PLL lock\n");
5593
5594 val = I915_READ(CDCLK_CTL);
5595 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5596 val |= divider;
5597 /*
5598 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5599 * enable otherwise.
5600 */
5601 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5602 if (frequency >= 500000)
5603 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5604
5605 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5606 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5607 val |= (frequency - 1000) / 500;
5608 I915_WRITE(CDCLK_CTL, val);
5609 }
5610
5611 mutex_lock(&dev_priv->rps.hw_lock);
5612 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5613 DIV_ROUND_UP(frequency, 25000));
5614 mutex_unlock(&dev_priv->rps.hw_lock);
5615
5616 if (ret) {
5617 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5618 ret, frequency);
5619 return;
5620 }
5621
Damien Lespiaua47871b2015-06-04 18:21:34 +01005622 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305623}
5624
5625void broxton_init_cdclk(struct drm_device *dev)
5626{
5627 struct drm_i915_private *dev_priv = dev->dev_private;
5628 uint32_t val;
5629
5630 /*
5631 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5632 * or else the reset will hang because there is no PCH to respond.
5633 * Move the handshake programming to initialization sequence.
5634 * Previously was left up to BIOS.
5635 */
5636 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5637 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5638 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5639
5640 /* Enable PG1 for cdclk */
5641 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5642
5643 /* check if cd clock is enabled */
5644 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5645 DRM_DEBUG_KMS("Display already initialized\n");
5646 return;
5647 }
5648
5649 /*
5650 * FIXME:
5651 * - The initial CDCLK needs to be read from VBT.
5652 * Need to make this change after VBT has changes for BXT.
5653 * - check if setting the max (or any) cdclk freq is really necessary
5654 * here, it belongs to modeset time
5655 */
5656 broxton_set_cdclk(dev, 624000);
5657
5658 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005659 POSTING_READ(DBUF_CTL);
5660
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305661 udelay(10);
5662
5663 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5664 DRM_ERROR("DBuf power enable timeout!\n");
5665}
5666
5667void broxton_uninit_cdclk(struct drm_device *dev)
5668{
5669 struct drm_i915_private *dev_priv = dev->dev_private;
5670
5671 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005672 POSTING_READ(DBUF_CTL);
5673
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305674 udelay(10);
5675
5676 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5677 DRM_ERROR("DBuf power disable timeout!\n");
5678
5679 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5680 broxton_set_cdclk(dev, 19200);
5681
5682 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5683}
5684
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005685static const struct skl_cdclk_entry {
5686 unsigned int freq;
5687 unsigned int vco;
5688} skl_cdclk_frequencies[] = {
5689 { .freq = 308570, .vco = 8640 },
5690 { .freq = 337500, .vco = 8100 },
5691 { .freq = 432000, .vco = 8640 },
5692 { .freq = 450000, .vco = 8100 },
5693 { .freq = 540000, .vco = 8100 },
5694 { .freq = 617140, .vco = 8640 },
5695 { .freq = 675000, .vco = 8100 },
5696};
5697
5698static unsigned int skl_cdclk_decimal(unsigned int freq)
5699{
5700 return (freq - 1000) / 500;
5701}
5702
5703static unsigned int skl_cdclk_get_vco(unsigned int freq)
5704{
5705 unsigned int i;
5706
5707 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5708 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5709
5710 if (e->freq == freq)
5711 return e->vco;
5712 }
5713
5714 return 8100;
5715}
5716
5717static void
5718skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5719{
5720 unsigned int min_freq;
5721 u32 val;
5722
5723 /* select the minimum CDCLK before enabling DPLL 0 */
5724 val = I915_READ(CDCLK_CTL);
5725 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5726 val |= CDCLK_FREQ_337_308;
5727
5728 if (required_vco == 8640)
5729 min_freq = 308570;
5730 else
5731 min_freq = 337500;
5732
5733 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5734
5735 I915_WRITE(CDCLK_CTL, val);
5736 POSTING_READ(CDCLK_CTL);
5737
5738 /*
5739 * We always enable DPLL0 with the lowest link rate possible, but still
5740 * taking into account the VCO required to operate the eDP panel at the
5741 * desired frequency. The usual DP link rates operate with a VCO of
5742 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5743 * The modeset code is responsible for the selection of the exact link
5744 * rate later on, with the constraint of choosing a frequency that
5745 * works with required_vco.
5746 */
5747 val = I915_READ(DPLL_CTRL1);
5748
5749 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5750 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5751 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5752 if (required_vco == 8640)
5753 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5754 SKL_DPLL0);
5755 else
5756 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5757 SKL_DPLL0);
5758
5759 I915_WRITE(DPLL_CTRL1, val);
5760 POSTING_READ(DPLL_CTRL1);
5761
5762 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5763
5764 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5765 DRM_ERROR("DPLL0 not locked\n");
5766}
5767
5768static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5769{
5770 int ret;
5771 u32 val;
5772
5773 /* inform PCU we want to change CDCLK */
5774 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5775 mutex_lock(&dev_priv->rps.hw_lock);
5776 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5777 mutex_unlock(&dev_priv->rps.hw_lock);
5778
5779 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5780}
5781
5782static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5783{
5784 unsigned int i;
5785
5786 for (i = 0; i < 15; i++) {
5787 if (skl_cdclk_pcu_ready(dev_priv))
5788 return true;
5789 udelay(10);
5790 }
5791
5792 return false;
5793}
5794
5795static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5796{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005797 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005798 u32 freq_select, pcu_ack;
5799
5800 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5801
5802 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5803 DRM_ERROR("failed to inform PCU about cdclk change\n");
5804 return;
5805 }
5806
5807 /* set CDCLK_CTL */
5808 switch(freq) {
5809 case 450000:
5810 case 432000:
5811 freq_select = CDCLK_FREQ_450_432;
5812 pcu_ack = 1;
5813 break;
5814 case 540000:
5815 freq_select = CDCLK_FREQ_540;
5816 pcu_ack = 2;
5817 break;
5818 case 308570:
5819 case 337500:
5820 default:
5821 freq_select = CDCLK_FREQ_337_308;
5822 pcu_ack = 0;
5823 break;
5824 case 617140:
5825 case 675000:
5826 freq_select = CDCLK_FREQ_675_617;
5827 pcu_ack = 3;
5828 break;
5829 }
5830
5831 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5832 POSTING_READ(CDCLK_CTL);
5833
5834 /* inform PCU of the change */
5835 mutex_lock(&dev_priv->rps.hw_lock);
5836 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5837 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005838
5839 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005840}
5841
5842void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5843{
5844 /* disable DBUF power */
5845 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5846 POSTING_READ(DBUF_CTL);
5847
5848 udelay(10);
5849
5850 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5851 DRM_ERROR("DBuf power disable timeout\n");
5852
Imre Deakab96c1ee2015-11-04 19:24:18 +02005853 /* disable DPLL0 */
5854 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5855 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5856 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005857}
5858
5859void skl_init_cdclk(struct drm_i915_private *dev_priv)
5860{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005861 unsigned int required_vco;
5862
Gary Wang39d9b852015-08-28 16:40:34 +08005863 /* DPLL0 not enabled (happens on early BIOS versions) */
5864 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5865 /* enable DPLL0 */
5866 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5867 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005868 }
5869
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005870 /* set CDCLK to the frequency the BIOS chose */
5871 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5872
5873 /* enable DBUF power */
5874 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5875 POSTING_READ(DBUF_CTL);
5876
5877 udelay(10);
5878
5879 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5880 DRM_ERROR("DBuf power enable timeout\n");
5881}
5882
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305883int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5884{
5885 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5886 uint32_t cdctl = I915_READ(CDCLK_CTL);
5887 int freq = dev_priv->skl_boot_cdclk;
5888
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305889 /*
5890 * check if the pre-os intialized the display
5891 * There is SWF18 scratchpad register defined which is set by the
5892 * pre-os which can be used by the OS drivers to check the status
5893 */
5894 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5895 goto sanitize;
5896
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305897 /* Is PLL enabled and locked ? */
5898 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5899 goto sanitize;
5900
5901 /* DPLL okay; verify the cdclock
5902 *
5903 * Noticed in some instances that the freq selection is correct but
5904 * decimal part is programmed wrong from BIOS where pre-os does not
5905 * enable display. Verify the same as well.
5906 */
5907 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5908 /* All well; nothing to sanitize */
5909 return false;
5910sanitize:
5911 /*
5912 * As of now initialize with max cdclk till
5913 * we get dynamic cdclk support
5914 * */
5915 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5916 skl_init_cdclk(dev_priv);
5917
5918 /* we did have to sanitize */
5919 return true;
5920}
5921
Jesse Barnes30a970c2013-11-04 13:48:12 -08005922/* Adjust CDclk dividers to allow high res or save power if possible */
5923static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5924{
5925 struct drm_i915_private *dev_priv = dev->dev_private;
5926 u32 val, cmd;
5927
Vandana Kannan164dfd22014-11-24 13:37:41 +05305928 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5929 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005930
Ville Syrjälädfcab172014-06-13 13:37:47 +03005931 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005932 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005933 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005934 cmd = 1;
5935 else
5936 cmd = 0;
5937
5938 mutex_lock(&dev_priv->rps.hw_lock);
5939 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5940 val &= ~DSPFREQGUAR_MASK;
5941 val |= (cmd << DSPFREQGUAR_SHIFT);
5942 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5943 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5944 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5945 50)) {
5946 DRM_ERROR("timed out waiting for CDclk change\n");
5947 }
5948 mutex_unlock(&dev_priv->rps.hw_lock);
5949
Ville Syrjälä54433e92015-05-26 20:42:31 +03005950 mutex_lock(&dev_priv->sb_lock);
5951
Ville Syrjälädfcab172014-06-13 13:37:47 +03005952 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005953 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005954
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005955 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005956
Jesse Barnes30a970c2013-11-04 13:48:12 -08005957 /* adjust cdclk divider */
5958 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005959 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005960 val |= divider;
5961 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005962
5963 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005964 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005965 50))
5966 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005967 }
5968
Jesse Barnes30a970c2013-11-04 13:48:12 -08005969 /* adjust self-refresh exit latency value */
5970 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5971 val &= ~0x7f;
5972
5973 /*
5974 * For high bandwidth configs, we set a higher latency in the bunit
5975 * so that the core display fetch happens in time to avoid underruns.
5976 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005977 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005978 val |= 4500 / 250; /* 4.5 usec */
5979 else
5980 val |= 3000 / 250; /* 3.0 usec */
5981 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005982
Ville Syrjäläa5805162015-05-26 20:42:30 +03005983 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005984
Ville Syrjäläb6283052015-06-03 15:45:07 +03005985 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005986}
5987
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005988static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5989{
5990 struct drm_i915_private *dev_priv = dev->dev_private;
5991 u32 val, cmd;
5992
Vandana Kannan164dfd22014-11-24 13:37:41 +05305993 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5994 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005995
5996 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005997 case 333333:
5998 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005999 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006000 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006001 break;
6002 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01006003 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006004 return;
6005 }
6006
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02006007 /*
6008 * Specs are full of misinformation, but testing on actual
6009 * hardware has shown that we just need to write the desired
6010 * CCK divider into the Punit register.
6011 */
6012 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6013
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006014 mutex_lock(&dev_priv->rps.hw_lock);
6015 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6016 val &= ~DSPFREQGUAR_MASK_CHV;
6017 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6018 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6019 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6020 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6021 50)) {
6022 DRM_ERROR("timed out waiting for CDclk change\n");
6023 }
6024 mutex_unlock(&dev_priv->rps.hw_lock);
6025
Ville Syrjäläb6283052015-06-03 15:45:07 +03006026 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006027}
6028
Jesse Barnes30a970c2013-11-04 13:48:12 -08006029static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6030 int max_pixclk)
6031{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006032 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006033 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006034
Jesse Barnes30a970c2013-11-04 13:48:12 -08006035 /*
6036 * Really only a few cases to deal with, as only 4 CDclks are supported:
6037 * 200MHz
6038 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006039 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006040 * 400MHz (VLV only)
6041 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6042 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006043 *
6044 * We seem to get an unstable or solid color picture at 200MHz.
6045 * Not sure what's wrong. For now use 200MHz only when all pipes
6046 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08006047 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006048 if (!IS_CHERRYVIEW(dev_priv) &&
6049 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006050 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006051 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006052 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006053 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006054 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006055 else
6056 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006057}
6058
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306059static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
6060 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006061{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306062 /*
6063 * FIXME:
6064 * - remove the guardband, it's not needed on BXT
6065 * - set 19.2MHz bypass frequency if there are no active pipes
6066 */
6067 if (max_pixclk > 576000*9/10)
6068 return 624000;
6069 else if (max_pixclk > 384000*9/10)
6070 return 576000;
6071 else if (max_pixclk > 288000*9/10)
6072 return 384000;
6073 else if (max_pixclk > 144000*9/10)
6074 return 288000;
6075 else
6076 return 144000;
6077}
6078
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006079/* Compute the max pixel clock for new configuration. Uses atomic state if
6080 * that's non-NULL, look at current state otherwise. */
6081static int intel_mode_max_pixclk(struct drm_device *dev,
6082 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006083{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006084 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
6085 struct drm_i915_private *dev_priv = dev->dev_private;
6086 struct drm_crtc *crtc;
6087 struct drm_crtc_state *crtc_state;
6088 unsigned max_pixclk = 0, i;
6089 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006090
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006091 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6092 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006093
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006094 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6095 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006096
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006097 if (crtc_state->enable)
6098 pixclk = crtc_state->adjusted_mode.crtc_clock;
6099
6100 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006101 }
6102
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006103 if (!intel_state->active_crtcs)
6104 return 0;
6105
6106 for_each_pipe(dev_priv, pipe)
6107 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6108
Jesse Barnes30a970c2013-11-04 13:48:12 -08006109 return max_pixclk;
6110}
6111
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006112static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006113{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006114 struct drm_device *dev = state->dev;
6115 struct drm_i915_private *dev_priv = dev->dev_private;
6116 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006117 struct intel_atomic_state *intel_state =
6118 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006119
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006120 if (max_pixclk < 0)
6121 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006122
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006123 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006124 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306125
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006126 if (!intel_state->active_crtcs)
6127 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6128
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006129 return 0;
6130}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006131
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006132static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
6133{
6134 struct drm_device *dev = state->dev;
6135 struct drm_i915_private *dev_priv = dev->dev_private;
6136 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006137 struct intel_atomic_state *intel_state =
6138 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006139
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006140 if (max_pixclk < 0)
6141 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006142
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006143 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006144 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006145
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006146 if (!intel_state->active_crtcs)
6147 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
6148
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006149 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006150}
6151
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006152static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6153{
6154 unsigned int credits, default_credits;
6155
6156 if (IS_CHERRYVIEW(dev_priv))
6157 default_credits = PFI_CREDIT(12);
6158 else
6159 default_credits = PFI_CREDIT(8);
6160
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006161 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006162 /* CHV suggested value is 31 or 63 */
6163 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006164 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006165 else
6166 credits = PFI_CREDIT(15);
6167 } else {
6168 credits = default_credits;
6169 }
6170
6171 /*
6172 * WA - write default credits before re-programming
6173 * FIXME: should we also set the resend bit here?
6174 */
6175 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6176 default_credits);
6177
6178 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6179 credits | PFI_CREDIT_RESEND);
6180
6181 /*
6182 * FIXME is this guaranteed to clear
6183 * immediately or should we poll for it?
6184 */
6185 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6186}
6187
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006188static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006189{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006190 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006191 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006192 struct intel_atomic_state *old_intel_state =
6193 to_intel_atomic_state(old_state);
6194 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006195
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006196 /*
6197 * FIXME: We can end up here with all power domains off, yet
6198 * with a CDCLK frequency other than the minimum. To account
6199 * for this take the PIPE-A power domain, which covers the HW
6200 * blocks needed for the following programming. This can be
6201 * removed once it's guaranteed that we get here either with
6202 * the minimum CDCLK set, or the required power domains
6203 * enabled.
6204 */
6205 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006206
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006207 if (IS_CHERRYVIEW(dev))
6208 cherryview_set_cdclk(dev, req_cdclk);
6209 else
6210 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006211
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006212 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006213
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006214 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006215}
6216
Jesse Barnes89b667f2013-04-18 14:51:36 -07006217static void valleyview_crtc_enable(struct drm_crtc *crtc)
6218{
6219 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006220 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006221 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6222 struct intel_encoder *encoder;
6223 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006224
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006225 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006226 return;
6227
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006228 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306229 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006230
6231 intel_set_pipe_timings(intel_crtc);
6232
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006233 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6234 struct drm_i915_private *dev_priv = dev->dev_private;
6235
6236 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6237 I915_WRITE(CHV_CANVAS(pipe), 0);
6238 }
6239
Daniel Vetter5b18e572014-04-24 23:55:06 +02006240 i9xx_set_pipeconf(intel_crtc);
6241
Jesse Barnes89b667f2013-04-18 14:51:36 -07006242 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006243
Daniel Vettera72e4c92014-09-30 10:56:47 +02006244 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006245
Jesse Barnes89b667f2013-04-18 14:51:36 -07006246 for_each_encoder_on_crtc(dev, crtc, encoder)
6247 if (encoder->pre_pll_enable)
6248 encoder->pre_pll_enable(encoder);
6249
Jani Nikulaa65347b2015-11-27 12:21:46 +02006250 if (!intel_crtc->config->has_dsi_encoder) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006251 if (IS_CHERRYVIEW(dev)) {
6252 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006253 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006254 } else {
6255 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006256 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006257 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006258 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006259
6260 for_each_encoder_on_crtc(dev, crtc, encoder)
6261 if (encoder->pre_enable)
6262 encoder->pre_enable(encoder);
6263
Jesse Barnes2dd24552013-04-25 12:55:01 -07006264 i9xx_pfit_enable(intel_crtc);
6265
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006266 intel_crtc_load_lut(crtc);
6267
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006268 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006269
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006270 assert_vblank_disabled(crtc);
6271 drm_crtc_vblank_on(crtc);
6272
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006273 for_each_encoder_on_crtc(dev, crtc, encoder)
6274 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006275}
6276
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006277static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6278{
6279 struct drm_device *dev = crtc->base.dev;
6280 struct drm_i915_private *dev_priv = dev->dev_private;
6281
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006282 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6283 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006284}
6285
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006286static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006287{
6288 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006289 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006290 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006291 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006292 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006293
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006294 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006295 return;
6296
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006297 i9xx_set_pll_dividers(intel_crtc);
6298
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006299 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306300 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006301
6302 intel_set_pipe_timings(intel_crtc);
6303
Daniel Vetter5b18e572014-04-24 23:55:06 +02006304 i9xx_set_pipeconf(intel_crtc);
6305
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006306 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006307
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006308 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006309 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006310
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006311 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006312 if (encoder->pre_enable)
6313 encoder->pre_enable(encoder);
6314
Daniel Vetterf6736a12013-06-05 13:34:30 +02006315 i9xx_enable_pll(intel_crtc);
6316
Jesse Barnes2dd24552013-04-25 12:55:01 -07006317 i9xx_pfit_enable(intel_crtc);
6318
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006319 intel_crtc_load_lut(crtc);
6320
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006321 intel_update_watermarks(crtc);
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);
Paulo Zanonid029bca2015-10-15 10:44:46 -03006329
6330 intel_fbc_enable(intel_crtc);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006331}
6332
Daniel Vetter87476d62013-04-11 16:29:06 +02006333static void i9xx_pfit_disable(struct intel_crtc *crtc)
6334{
6335 struct drm_device *dev = crtc->base.dev;
6336 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006337
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006338 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006339 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006340
6341 assert_pipe_disabled(dev_priv, crtc->pipe);
6342
Daniel Vetter328d8e82013-05-08 10:36:31 +02006343 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6344 I915_READ(PFIT_CONTROL));
6345 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006346}
6347
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006348static void i9xx_crtc_disable(struct drm_crtc *crtc)
6349{
6350 struct drm_device *dev = crtc->dev;
6351 struct drm_i915_private *dev_priv = dev->dev_private;
6352 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006353 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006354 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006355
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006356 /*
6357 * On gen2 planes are double buffered but the pipe isn't, so we must
6358 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006359 * We also need to wait on all gmch platforms because of the
6360 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006361 */
Imre Deak564ed192014-06-13 14:54:21 +03006362 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006363
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006364 for_each_encoder_on_crtc(dev, crtc, encoder)
6365 encoder->disable(encoder);
6366
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006367 drm_crtc_vblank_off(crtc);
6368 assert_vblank_disabled(crtc);
6369
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006370 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006371
Daniel Vetter87476d62013-04-11 16:29:06 +02006372 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006373
Jesse Barnes89b667f2013-04-18 14:51:36 -07006374 for_each_encoder_on_crtc(dev, crtc, encoder)
6375 if (encoder->post_disable)
6376 encoder->post_disable(encoder);
6377
Jani Nikulaa65347b2015-11-27 12:21:46 +02006378 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006379 if (IS_CHERRYVIEW(dev))
6380 chv_disable_pll(dev_priv, pipe);
6381 else if (IS_VALLEYVIEW(dev))
6382 vlv_disable_pll(dev_priv, pipe);
6383 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006384 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006385 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006386
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006387 for_each_encoder_on_crtc(dev, crtc, encoder)
6388 if (encoder->post_pll_disable)
6389 encoder->post_pll_disable(encoder);
6390
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006391 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006392 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Paulo Zanonid029bca2015-10-15 10:44:46 -03006393
6394 intel_fbc_disable_crtc(intel_crtc);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006395}
6396
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006397static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006398{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006399 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006400 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006401 enum intel_display_power_domain domain;
6402 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006403
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006404 if (!intel_crtc->active)
6405 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006406
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006407 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006408 WARN_ON(intel_crtc->unpin_work);
6409
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006410 intel_pre_disable_primary(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006411
6412 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6413 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006414 }
6415
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006416 dev_priv->display.crtc_disable(crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006417 intel_crtc->active = false;
6418 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006419 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006420
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006421 domains = intel_crtc->enabled_power_domains;
6422 for_each_power_domain(domain, domains)
6423 intel_display_power_put(dev_priv, domain);
6424 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006425
6426 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6427 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006428}
6429
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006430/*
6431 * turn all crtc's off, but do not adjust state
6432 * This has to be paired with a call to intel_modeset_setup_hw_state.
6433 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006434int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006435{
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006436 struct drm_mode_config *config = &dev->mode_config;
6437 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
6438 struct drm_atomic_state *state;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006439 struct drm_crtc *crtc;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006440 unsigned crtc_mask = 0;
6441 int ret = 0;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006442
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006443 if (WARN_ON(!ctx))
6444 return 0;
6445
6446 lockdep_assert_held(&ctx->ww_ctx);
6447 state = drm_atomic_state_alloc(dev);
6448 if (WARN_ON(!state))
6449 return -ENOMEM;
6450
6451 state->acquire_ctx = ctx;
6452 state->allow_modeset = true;
6453
6454 for_each_crtc(dev, crtc) {
6455 struct drm_crtc_state *crtc_state =
6456 drm_atomic_get_crtc_state(state, crtc);
6457
6458 ret = PTR_ERR_OR_ZERO(crtc_state);
6459 if (ret)
6460 goto free;
6461
6462 if (!crtc_state->active)
6463 continue;
6464
6465 crtc_state->active = false;
6466 crtc_mask |= 1 << drm_crtc_index(crtc);
6467 }
6468
6469 if (crtc_mask) {
Maarten Lankhorst74c090b2015-07-13 16:30:30 +02006470 ret = drm_atomic_commit(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006471
6472 if (!ret) {
6473 for_each_crtc(dev, crtc)
6474 if (crtc_mask & (1 << drm_crtc_index(crtc)))
6475 crtc->state->active = true;
6476
6477 return ret;
6478 }
6479 }
6480
6481free:
6482 if (ret)
6483 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
6484 drm_atomic_state_free(state);
6485 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006486}
6487
Chris Wilsonea5b2132010-08-04 13:50:23 +01006488void intel_encoder_destroy(struct drm_encoder *encoder)
6489{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006490 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006491
Chris Wilsonea5b2132010-08-04 13:50:23 +01006492 drm_encoder_cleanup(encoder);
6493 kfree(intel_encoder);
6494}
6495
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006496/* Cross check the actual hw state with our own modeset state tracking (and it's
6497 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006498static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006499{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006500 struct drm_crtc *crtc = connector->base.state->crtc;
6501
6502 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6503 connector->base.base.id,
6504 connector->base.name);
6505
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006506 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006507 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006508 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006509
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006510 I915_STATE_WARN(!crtc,
6511 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006512
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006513 if (!crtc)
6514 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006515
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006516 I915_STATE_WARN(!crtc->state->active,
6517 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006518
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006519 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006520 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006521
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006522 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006523 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006524
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006525 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006526 "attached encoder crtc differs from connector crtc\n");
6527 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006528 I915_STATE_WARN(crtc && crtc->state->active,
6529 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006530 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6531 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006532 }
6533}
6534
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006535int intel_connector_init(struct intel_connector *connector)
6536{
6537 struct drm_connector_state *connector_state;
6538
6539 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6540 if (!connector_state)
6541 return -ENOMEM;
6542
6543 connector->base.state = connector_state;
6544 return 0;
6545}
6546
6547struct intel_connector *intel_connector_alloc(void)
6548{
6549 struct intel_connector *connector;
6550
6551 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6552 if (!connector)
6553 return NULL;
6554
6555 if (intel_connector_init(connector) < 0) {
6556 kfree(connector);
6557 return NULL;
6558 }
6559
6560 return connector;
6561}
6562
Daniel Vetterf0947c32012-07-02 13:10:34 +02006563/* Simple connector->get_hw_state implementation for encoders that support only
6564 * one connector and no cloning and hence the encoder state determines the state
6565 * of the connector. */
6566bool intel_connector_get_hw_state(struct intel_connector *connector)
6567{
Daniel Vetter24929352012-07-02 20:28:59 +02006568 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006569 struct intel_encoder *encoder = connector->encoder;
6570
6571 return encoder->get_hw_state(encoder, &pipe);
6572}
6573
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006574static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006575{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006576 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6577 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006578
6579 return 0;
6580}
6581
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006582static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006583 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006584{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006585 struct drm_atomic_state *state = pipe_config->base.state;
6586 struct intel_crtc *other_crtc;
6587 struct intel_crtc_state *other_crtc_state;
6588
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006589 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6590 pipe_name(pipe), pipe_config->fdi_lanes);
6591 if (pipe_config->fdi_lanes > 4) {
6592 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6593 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006594 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006595 }
6596
Paulo Zanonibafb6552013-11-02 21:07:44 -07006597 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006598 if (pipe_config->fdi_lanes > 2) {
6599 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6600 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006601 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006602 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006603 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006604 }
6605 }
6606
6607 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006608 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006609
6610 /* Ivybridge 3 pipe is really complicated */
6611 switch (pipe) {
6612 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006613 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006614 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006615 if (pipe_config->fdi_lanes <= 2)
6616 return 0;
6617
6618 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6619 other_crtc_state =
6620 intel_atomic_get_crtc_state(state, other_crtc);
6621 if (IS_ERR(other_crtc_state))
6622 return PTR_ERR(other_crtc_state);
6623
6624 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006625 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6626 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006627 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006628 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006629 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006630 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006631 if (pipe_config->fdi_lanes > 2) {
6632 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6633 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006634 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006635 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006636
6637 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6638 other_crtc_state =
6639 intel_atomic_get_crtc_state(state, other_crtc);
6640 if (IS_ERR(other_crtc_state))
6641 return PTR_ERR(other_crtc_state);
6642
6643 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006644 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006645 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006646 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006647 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006648 default:
6649 BUG();
6650 }
6651}
6652
Daniel Vettere29c22c2013-02-21 00:00:16 +01006653#define RETRY 1
6654static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006655 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006656{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006657 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006658 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006659 int lane, link_bw, fdi_dotclock, ret;
6660 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006661
Daniel Vettere29c22c2013-02-21 00:00:16 +01006662retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006663 /* FDI is a binary signal running at ~2.7GHz, encoding
6664 * each output octet as 10 bits. The actual frequency
6665 * is stored as a divider into a 100MHz clock, and the
6666 * mode pixel clock is stored in units of 1KHz.
6667 * Hence the bw of each lane in terms of the mode signal
6668 * is:
6669 */
6670 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6671
Damien Lespiau241bfc32013-09-25 16:45:37 +01006672 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006673
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006674 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006675 pipe_config->pipe_bpp);
6676
6677 pipe_config->fdi_lanes = lane;
6678
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006679 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006680 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006681
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006682 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6683 intel_crtc->pipe, pipe_config);
6684 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006685 pipe_config->pipe_bpp -= 2*3;
6686 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6687 pipe_config->pipe_bpp);
6688 needs_recompute = true;
6689 pipe_config->bw_constrained = true;
6690
6691 goto retry;
6692 }
6693
6694 if (needs_recompute)
6695 return RETRY;
6696
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006697 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006698}
6699
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006700static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6701 struct intel_crtc_state *pipe_config)
6702{
6703 if (pipe_config->pipe_bpp > 24)
6704 return false;
6705
6706 /* HSW can handle pixel rate up to cdclk? */
6707 if (IS_HASWELL(dev_priv->dev))
6708 return true;
6709
6710 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006711 * We compare against max which means we must take
6712 * the increased cdclk requirement into account when
6713 * calculating the new cdclk.
6714 *
6715 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006716 */
6717 return ilk_pipe_pixel_rate(pipe_config) <=
6718 dev_priv->max_cdclk_freq * 95 / 100;
6719}
6720
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006721static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006722 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006723{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006724 struct drm_device *dev = crtc->base.dev;
6725 struct drm_i915_private *dev_priv = dev->dev_private;
6726
Jani Nikulad330a952014-01-21 11:24:25 +02006727 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006728 hsw_crtc_supports_ips(crtc) &&
6729 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006730}
6731
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006732static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6733{
6734 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6735
6736 /* GDG double wide on either pipe, otherwise pipe A only */
6737 return INTEL_INFO(dev_priv)->gen < 4 &&
6738 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6739}
6740
Daniel Vettera43f6e02013-06-07 23:10:32 +02006741static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006742 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006743{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006744 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006745 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006746 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006747
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006748 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006749 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006750 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006751
6752 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006753 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006754 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006755 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006756 if (intel_crtc_supports_double_wide(crtc) &&
6757 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006758 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006759 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006760 }
6761
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006762 if (adjusted_mode->crtc_clock > clock_limit) {
6763 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6764 adjusted_mode->crtc_clock, clock_limit,
6765 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006766 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006767 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006768 }
Chris Wilson89749352010-09-12 18:25:19 +01006769
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006770 /*
6771 * Pipe horizontal size must be even in:
6772 * - DVO ganged mode
6773 * - LVDS dual channel mode
6774 * - Double wide pipe
6775 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006776 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006777 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6778 pipe_config->pipe_src_w &= ~1;
6779
Damien Lespiau8693a822013-05-03 18:48:11 +01006780 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6781 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006782 */
6783 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006784 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006785 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006786
Damien Lespiauf5adf942013-06-24 18:29:34 +01006787 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006788 hsw_compute_ips_config(crtc, pipe_config);
6789
Daniel Vetter877d48d2013-04-19 11:24:43 +02006790 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006791 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006792
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006793 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006794}
6795
Ville Syrjälä1652d192015-03-31 14:12:01 +03006796static int skylake_get_display_clock_speed(struct drm_device *dev)
6797{
6798 struct drm_i915_private *dev_priv = to_i915(dev);
6799 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6800 uint32_t cdctl = I915_READ(CDCLK_CTL);
6801 uint32_t linkrate;
6802
Damien Lespiau414355a2015-06-04 18:21:31 +01006803 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006804 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006805
6806 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6807 return 540000;
6808
6809 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006810 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006811
Damien Lespiau71cd8422015-04-30 16:39:17 +01006812 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6813 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006814 /* vco 8640 */
6815 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6816 case CDCLK_FREQ_450_432:
6817 return 432000;
6818 case CDCLK_FREQ_337_308:
6819 return 308570;
6820 case CDCLK_FREQ_675_617:
6821 return 617140;
6822 default:
6823 WARN(1, "Unknown cd freq selection\n");
6824 }
6825 } else {
6826 /* vco 8100 */
6827 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6828 case CDCLK_FREQ_450_432:
6829 return 450000;
6830 case CDCLK_FREQ_337_308:
6831 return 337500;
6832 case CDCLK_FREQ_675_617:
6833 return 675000;
6834 default:
6835 WARN(1, "Unknown cd freq selection\n");
6836 }
6837 }
6838
6839 /* error case, do as if DPLL0 isn't enabled */
6840 return 24000;
6841}
6842
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006843static int broxton_get_display_clock_speed(struct drm_device *dev)
6844{
6845 struct drm_i915_private *dev_priv = to_i915(dev);
6846 uint32_t cdctl = I915_READ(CDCLK_CTL);
6847 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6848 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6849 int cdclk;
6850
6851 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6852 return 19200;
6853
6854 cdclk = 19200 * pll_ratio / 2;
6855
6856 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6857 case BXT_CDCLK_CD2X_DIV_SEL_1:
6858 return cdclk; /* 576MHz or 624MHz */
6859 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6860 return cdclk * 2 / 3; /* 384MHz */
6861 case BXT_CDCLK_CD2X_DIV_SEL_2:
6862 return cdclk / 2; /* 288MHz */
6863 case BXT_CDCLK_CD2X_DIV_SEL_4:
6864 return cdclk / 4; /* 144MHz */
6865 }
6866
6867 /* error case, do as if DE PLL isn't enabled */
6868 return 19200;
6869}
6870
Ville Syrjälä1652d192015-03-31 14:12:01 +03006871static int broadwell_get_display_clock_speed(struct drm_device *dev)
6872{
6873 struct drm_i915_private *dev_priv = dev->dev_private;
6874 uint32_t lcpll = I915_READ(LCPLL_CTL);
6875 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6876
6877 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6878 return 800000;
6879 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6880 return 450000;
6881 else if (freq == LCPLL_CLK_FREQ_450)
6882 return 450000;
6883 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6884 return 540000;
6885 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6886 return 337500;
6887 else
6888 return 675000;
6889}
6890
6891static int haswell_get_display_clock_speed(struct drm_device *dev)
6892{
6893 struct drm_i915_private *dev_priv = dev->dev_private;
6894 uint32_t lcpll = I915_READ(LCPLL_CTL);
6895 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6896
6897 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6898 return 800000;
6899 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6900 return 450000;
6901 else if (freq == LCPLL_CLK_FREQ_450)
6902 return 450000;
6903 else if (IS_HSW_ULT(dev))
6904 return 337500;
6905 else
6906 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006907}
6908
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006909static int valleyview_get_display_clock_speed(struct drm_device *dev)
6910{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006911 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6912 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006913}
6914
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006915static int ilk_get_display_clock_speed(struct drm_device *dev)
6916{
6917 return 450000;
6918}
6919
Jesse Barnese70236a2009-09-21 10:42:27 -07006920static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006921{
Jesse Barnese70236a2009-09-21 10:42:27 -07006922 return 400000;
6923}
Jesse Barnes79e53942008-11-07 14:24:08 -08006924
Jesse Barnese70236a2009-09-21 10:42:27 -07006925static int i915_get_display_clock_speed(struct drm_device *dev)
6926{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006927 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006928}
Jesse Barnes79e53942008-11-07 14:24:08 -08006929
Jesse Barnese70236a2009-09-21 10:42:27 -07006930static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6931{
6932 return 200000;
6933}
Jesse Barnes79e53942008-11-07 14:24:08 -08006934
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006935static int pnv_get_display_clock_speed(struct drm_device *dev)
6936{
6937 u16 gcfgc = 0;
6938
6939 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6940
6941 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6942 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006943 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006944 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006945 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006946 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006947 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006948 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6949 return 200000;
6950 default:
6951 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6952 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006953 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006954 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006955 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006956 }
6957}
6958
Jesse Barnese70236a2009-09-21 10:42:27 -07006959static int i915gm_get_display_clock_speed(struct drm_device *dev)
6960{
6961 u16 gcfgc = 0;
6962
6963 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6964
6965 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006966 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006967 else {
6968 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6969 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006970 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006971 default:
6972 case GC_DISPLAY_CLOCK_190_200_MHZ:
6973 return 190000;
6974 }
6975 }
6976}
Jesse Barnes79e53942008-11-07 14:24:08 -08006977
Jesse Barnese70236a2009-09-21 10:42:27 -07006978static int i865_get_display_clock_speed(struct drm_device *dev)
6979{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006980 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006981}
6982
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006983static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006984{
6985 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006986
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006987 /*
6988 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6989 * encoding is different :(
6990 * FIXME is this the right way to detect 852GM/852GMV?
6991 */
6992 if (dev->pdev->revision == 0x1)
6993 return 133333;
6994
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006995 pci_bus_read_config_word(dev->pdev->bus,
6996 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6997
Jesse Barnese70236a2009-09-21 10:42:27 -07006998 /* Assume that the hardware is in the high speed state. This
6999 * should be the default.
7000 */
7001 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7002 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007003 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07007004 case GC_CLOCK_100_200:
7005 return 200000;
7006 case GC_CLOCK_166_250:
7007 return 250000;
7008 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007009 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007010 case GC_CLOCK_133_266:
7011 case GC_CLOCK_133_266_2:
7012 case GC_CLOCK_166_266:
7013 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007014 }
7015
7016 /* Shouldn't happen */
7017 return 0;
7018}
7019
7020static int i830_get_display_clock_speed(struct drm_device *dev)
7021{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007022 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08007023}
7024
Ville Syrjälä34edce22015-05-22 11:22:33 +03007025static unsigned int intel_hpll_vco(struct drm_device *dev)
7026{
7027 struct drm_i915_private *dev_priv = dev->dev_private;
7028 static const unsigned int blb_vco[8] = {
7029 [0] = 3200000,
7030 [1] = 4000000,
7031 [2] = 5333333,
7032 [3] = 4800000,
7033 [4] = 6400000,
7034 };
7035 static const unsigned int pnv_vco[8] = {
7036 [0] = 3200000,
7037 [1] = 4000000,
7038 [2] = 5333333,
7039 [3] = 4800000,
7040 [4] = 2666667,
7041 };
7042 static const unsigned int cl_vco[8] = {
7043 [0] = 3200000,
7044 [1] = 4000000,
7045 [2] = 5333333,
7046 [3] = 6400000,
7047 [4] = 3333333,
7048 [5] = 3566667,
7049 [6] = 4266667,
7050 };
7051 static const unsigned int elk_vco[8] = {
7052 [0] = 3200000,
7053 [1] = 4000000,
7054 [2] = 5333333,
7055 [3] = 4800000,
7056 };
7057 static const unsigned int ctg_vco[8] = {
7058 [0] = 3200000,
7059 [1] = 4000000,
7060 [2] = 5333333,
7061 [3] = 6400000,
7062 [4] = 2666667,
7063 [5] = 4266667,
7064 };
7065 const unsigned int *vco_table;
7066 unsigned int vco;
7067 uint8_t tmp = 0;
7068
7069 /* FIXME other chipsets? */
7070 if (IS_GM45(dev))
7071 vco_table = ctg_vco;
7072 else if (IS_G4X(dev))
7073 vco_table = elk_vco;
7074 else if (IS_CRESTLINE(dev))
7075 vco_table = cl_vco;
7076 else if (IS_PINEVIEW(dev))
7077 vco_table = pnv_vco;
7078 else if (IS_G33(dev))
7079 vco_table = blb_vco;
7080 else
7081 return 0;
7082
7083 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7084
7085 vco = vco_table[tmp & 0x7];
7086 if (vco == 0)
7087 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7088 else
7089 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7090
7091 return vco;
7092}
7093
7094static int gm45_get_display_clock_speed(struct drm_device *dev)
7095{
7096 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7097 uint16_t tmp = 0;
7098
7099 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7100
7101 cdclk_sel = (tmp >> 12) & 0x1;
7102
7103 switch (vco) {
7104 case 2666667:
7105 case 4000000:
7106 case 5333333:
7107 return cdclk_sel ? 333333 : 222222;
7108 case 3200000:
7109 return cdclk_sel ? 320000 : 228571;
7110 default:
7111 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7112 return 222222;
7113 }
7114}
7115
7116static int i965gm_get_display_clock_speed(struct drm_device *dev)
7117{
7118 static const uint8_t div_3200[] = { 16, 10, 8 };
7119 static const uint8_t div_4000[] = { 20, 12, 10 };
7120 static const uint8_t div_5333[] = { 24, 16, 14 };
7121 const uint8_t *div_table;
7122 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7123 uint16_t tmp = 0;
7124
7125 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7126
7127 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7128
7129 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7130 goto fail;
7131
7132 switch (vco) {
7133 case 3200000:
7134 div_table = div_3200;
7135 break;
7136 case 4000000:
7137 div_table = div_4000;
7138 break;
7139 case 5333333:
7140 div_table = div_5333;
7141 break;
7142 default:
7143 goto fail;
7144 }
7145
7146 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7147
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007148fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007149 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7150 return 200000;
7151}
7152
7153static int g33_get_display_clock_speed(struct drm_device *dev)
7154{
7155 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7156 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7157 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7158 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7159 const uint8_t *div_table;
7160 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7161 uint16_t tmp = 0;
7162
7163 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7164
7165 cdclk_sel = (tmp >> 4) & 0x7;
7166
7167 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7168 goto fail;
7169
7170 switch (vco) {
7171 case 3200000:
7172 div_table = div_3200;
7173 break;
7174 case 4000000:
7175 div_table = div_4000;
7176 break;
7177 case 4800000:
7178 div_table = div_4800;
7179 break;
7180 case 5333333:
7181 div_table = div_5333;
7182 break;
7183 default:
7184 goto fail;
7185 }
7186
7187 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7188
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007189fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007190 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7191 return 190476;
7192}
7193
Zhenyu Wang2c072452009-06-05 15:38:42 +08007194static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007195intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007196{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007197 while (*num > DATA_LINK_M_N_MASK ||
7198 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007199 *num >>= 1;
7200 *den >>= 1;
7201 }
7202}
7203
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007204static void compute_m_n(unsigned int m, unsigned int n,
7205 uint32_t *ret_m, uint32_t *ret_n)
7206{
7207 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7208 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7209 intel_reduce_m_n_ratio(ret_m, ret_n);
7210}
7211
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007212void
7213intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7214 int pixel_clock, int link_clock,
7215 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007216{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007217 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007218
7219 compute_m_n(bits_per_pixel * pixel_clock,
7220 link_clock * nlanes * 8,
7221 &m_n->gmch_m, &m_n->gmch_n);
7222
7223 compute_m_n(pixel_clock, link_clock,
7224 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007225}
7226
Chris Wilsona7615032011-01-12 17:04:08 +00007227static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7228{
Jani Nikulad330a952014-01-21 11:24:25 +02007229 if (i915.panel_use_ssc >= 0)
7230 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007231 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007232 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007233}
7234
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007235static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7236 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007237{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007238 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007239 struct drm_i915_private *dev_priv = dev->dev_private;
7240 int refclk;
7241
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007242 WARN_ON(!crtc_state->base.state);
7243
Wayne Boyer666a4532015-12-09 12:29:35 -08007244 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007245 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007246 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007247 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007248 refclk = dev_priv->vbt.lvds_ssc_freq;
7249 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007250 } else if (!IS_GEN2(dev)) {
7251 refclk = 96000;
7252 } else {
7253 refclk = 48000;
7254 }
7255
7256 return refclk;
7257}
7258
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007259static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007260{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007261 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007262}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007263
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007264static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7265{
7266 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007267}
7268
Daniel Vetterf47709a2013-03-28 10:42:02 +01007269static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007270 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007271 intel_clock_t *reduced_clock)
7272{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007273 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007274 u32 fp, fp2 = 0;
7275
7276 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007277 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007278 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007279 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007280 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007281 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007282 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007283 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007284 }
7285
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007286 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007287
Daniel Vetterf47709a2013-03-28 10:42:02 +01007288 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007289 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007290 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007291 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007292 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007293 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007294 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007295 }
7296}
7297
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007298static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7299 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007300{
7301 u32 reg_val;
7302
7303 /*
7304 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7305 * and set it to a reasonable value instead.
7306 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007307 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007308 reg_val &= 0xffffff00;
7309 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007310 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007311
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007312 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007313 reg_val &= 0x8cffffff;
7314 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007315 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007316
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;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007319 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007320
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007321 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007322 reg_val &= 0x00ffffff;
7323 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007324 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007325}
7326
Daniel Vetterb5518422013-05-03 11:49:48 +02007327static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7328 struct intel_link_m_n *m_n)
7329{
7330 struct drm_device *dev = crtc->base.dev;
7331 struct drm_i915_private *dev_priv = dev->dev_private;
7332 int pipe = crtc->pipe;
7333
Daniel Vettere3b95f12013-05-03 11:49:49 +02007334 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7335 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7336 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7337 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007338}
7339
7340static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007341 struct intel_link_m_n *m_n,
7342 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007343{
7344 struct drm_device *dev = crtc->base.dev;
7345 struct drm_i915_private *dev_priv = dev->dev_private;
7346 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007347 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007348
7349 if (INTEL_INFO(dev)->gen >= 5) {
7350 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7351 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7352 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7353 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007354 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7355 * for gen < 8) and if DRRS is supported (to make sure the
7356 * registers are not unnecessarily accessed).
7357 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307358 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007359 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007360 I915_WRITE(PIPE_DATA_M2(transcoder),
7361 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7362 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7363 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7364 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7365 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007366 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007367 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7368 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7369 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7370 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007371 }
7372}
7373
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307374void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007375{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307376 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7377
7378 if (m_n == M1_N1) {
7379 dp_m_n = &crtc->config->dp_m_n;
7380 dp_m2_n2 = &crtc->config->dp_m2_n2;
7381 } else if (m_n == M2_N2) {
7382
7383 /*
7384 * M2_N2 registers are not supported. Hence m2_n2 divider value
7385 * needs to be programmed into M1_N1.
7386 */
7387 dp_m_n = &crtc->config->dp_m2_n2;
7388 } else {
7389 DRM_ERROR("Unsupported divider value\n");
7390 return;
7391 }
7392
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007393 if (crtc->config->has_pch_encoder)
7394 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007395 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307396 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007397}
7398
Daniel Vetter251ac862015-06-18 10:30:24 +02007399static void vlv_compute_dpll(struct intel_crtc *crtc,
7400 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007401{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007402 u32 dpll, dpll_md;
7403
7404 /*
7405 * Enable DPIO clock input. We should never disable the reference
7406 * clock for pipe B, since VGA hotplug / manual detection depends
7407 * on it.
7408 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007409 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7410 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007411 /* We should never disable this, set it here for state tracking */
7412 if (crtc->pipe == PIPE_B)
7413 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7414 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007415 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007416
Ville Syrjäläd288f652014-10-28 13:20:22 +02007417 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007418 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007419 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007420}
7421
Ville Syrjäläd288f652014-10-28 13:20:22 +02007422static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007423 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007424{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007425 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007426 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007427 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007428 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007429 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007430 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007431
Ville Syrjäläa5805162015-05-26 20:42:30 +03007432 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007433
Ville Syrjäläd288f652014-10-28 13:20:22 +02007434 bestn = pipe_config->dpll.n;
7435 bestm1 = pipe_config->dpll.m1;
7436 bestm2 = pipe_config->dpll.m2;
7437 bestp1 = pipe_config->dpll.p1;
7438 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007439
Jesse Barnes89b667f2013-04-18 14:51:36 -07007440 /* See eDP HDMI DPIO driver vbios notes doc */
7441
7442 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007443 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007444 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007445
7446 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007447 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007448
7449 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007450 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007451 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007452 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007453
7454 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007455 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007456
7457 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007458 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7459 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7460 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007461 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007462
7463 /*
7464 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7465 * but we don't support that).
7466 * Note: don't use the DAC post divider as it seems unstable.
7467 */
7468 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007469 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007470
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007471 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007472 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007473
Jesse Barnes89b667f2013-04-18 14:51:36 -07007474 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007475 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007476 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7477 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007478 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007479 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007480 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007481 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007482 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007483
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007484 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007485 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007486 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007487 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007488 0x0df40000);
7489 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007490 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007491 0x0df70000);
7492 } else { /* HDMI or VGA */
7493 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007494 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007495 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007496 0x0df70000);
7497 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007498 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007499 0x0df40000);
7500 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007501
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007502 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007503 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007504 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7505 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007506 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007507 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007508
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007509 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007510 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007511}
7512
Daniel Vetter251ac862015-06-18 10:30:24 +02007513static void chv_compute_dpll(struct intel_crtc *crtc,
7514 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007515{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007516 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7517 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007518 DPLL_VCO_ENABLE;
7519 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007520 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007521
Ville Syrjäläd288f652014-10-28 13:20:22 +02007522 pipe_config->dpll_hw_state.dpll_md =
7523 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007524}
7525
Ville Syrjäläd288f652014-10-28 13:20:22 +02007526static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007527 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007528{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007529 struct drm_device *dev = crtc->base.dev;
7530 struct drm_i915_private *dev_priv = dev->dev_private;
7531 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007532 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007533 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307534 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007535 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307536 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307537 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007538
Ville Syrjäläd288f652014-10-28 13:20:22 +02007539 bestn = pipe_config->dpll.n;
7540 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7541 bestm1 = pipe_config->dpll.m1;
7542 bestm2 = pipe_config->dpll.m2 >> 22;
7543 bestp1 = pipe_config->dpll.p1;
7544 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307545 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307546 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307547 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007548
7549 /*
7550 * Enable Refclk and SSC
7551 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007552 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007553 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007554
Ville Syrjäläa5805162015-05-26 20:42:30 +03007555 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007556
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007557 /* p1 and p2 divider */
7558 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7559 5 << DPIO_CHV_S1_DIV_SHIFT |
7560 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7561 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7562 1 << DPIO_CHV_K_DIV_SHIFT);
7563
7564 /* Feedback post-divider - m2 */
7565 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7566
7567 /* Feedback refclk divider - n and m1 */
7568 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7569 DPIO_CHV_M1_DIV_BY_2 |
7570 1 << DPIO_CHV_N_DIV_SHIFT);
7571
7572 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007573 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007574
7575 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307576 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7577 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7578 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7579 if (bestm2_frac)
7580 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7581 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007582
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307583 /* Program digital lock detect threshold */
7584 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7585 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7586 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7587 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7588 if (!bestm2_frac)
7589 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7590 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7591
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007592 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307593 if (vco == 5400000) {
7594 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7595 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7596 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7597 tribuf_calcntr = 0x9;
7598 } else if (vco <= 6200000) {
7599 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7600 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7601 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7602 tribuf_calcntr = 0x9;
7603 } else if (vco <= 6480000) {
7604 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7605 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7606 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7607 tribuf_calcntr = 0x8;
7608 } else {
7609 /* Not supported. Apply the same limits as in the max case */
7610 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7611 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7612 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7613 tribuf_calcntr = 0;
7614 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007615 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7616
Ville Syrjälä968040b2015-03-11 22:52:08 +02007617 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307618 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7619 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7620 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7621
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007622 /* AFC Recal */
7623 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7624 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7625 DPIO_AFC_RECAL);
7626
Ville Syrjäläa5805162015-05-26 20:42:30 +03007627 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007628}
7629
Ville Syrjäläd288f652014-10-28 13:20:22 +02007630/**
7631 * vlv_force_pll_on - forcibly enable just the PLL
7632 * @dev_priv: i915 private structure
7633 * @pipe: pipe PLL to enable
7634 * @dpll: PLL configuration
7635 *
7636 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7637 * in cases where we need the PLL enabled even when @pipe is not going to
7638 * be enabled.
7639 */
7640void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7641 const struct dpll *dpll)
7642{
7643 struct intel_crtc *crtc =
7644 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007645 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007646 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007647 .pixel_multiplier = 1,
7648 .dpll = *dpll,
7649 };
7650
7651 if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007652 chv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007653 chv_prepare_pll(crtc, &pipe_config);
7654 chv_enable_pll(crtc, &pipe_config);
7655 } else {
Daniel Vetter251ac862015-06-18 10:30:24 +02007656 vlv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007657 vlv_prepare_pll(crtc, &pipe_config);
7658 vlv_enable_pll(crtc, &pipe_config);
7659 }
7660}
7661
7662/**
7663 * vlv_force_pll_off - forcibly disable just the PLL
7664 * @dev_priv: i915 private structure
7665 * @pipe: pipe PLL to disable
7666 *
7667 * Disable the PLL for @pipe. To be used in cases where we need
7668 * the PLL enabled even when @pipe is not going to be enabled.
7669 */
7670void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7671{
7672 if (IS_CHERRYVIEW(dev))
7673 chv_disable_pll(to_i915(dev), pipe);
7674 else
7675 vlv_disable_pll(to_i915(dev), pipe);
7676}
7677
Daniel Vetter251ac862015-06-18 10:30:24 +02007678static void i9xx_compute_dpll(struct intel_crtc *crtc,
7679 struct intel_crtc_state *crtc_state,
7680 intel_clock_t *reduced_clock,
7681 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007682{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007683 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007684 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007685 u32 dpll;
7686 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007687 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007688
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007689 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307690
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007691 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7692 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007693
7694 dpll = DPLL_VGA_MODE_DIS;
7695
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007696 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007697 dpll |= DPLLB_MODE_LVDS;
7698 else
7699 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007700
Daniel Vetteref1b4602013-06-01 17:17:04 +02007701 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007702 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007703 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007704 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007705
7706 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007707 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007708
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007709 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007710 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007711
7712 /* compute bitmask from p1 value */
7713 if (IS_PINEVIEW(dev))
7714 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7715 else {
7716 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7717 if (IS_G4X(dev) && reduced_clock)
7718 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7719 }
7720 switch (clock->p2) {
7721 case 5:
7722 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7723 break;
7724 case 7:
7725 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7726 break;
7727 case 10:
7728 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7729 break;
7730 case 14:
7731 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7732 break;
7733 }
7734 if (INTEL_INFO(dev)->gen >= 4)
7735 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7736
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007737 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007738 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007739 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007740 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7741 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7742 else
7743 dpll |= PLL_REF_INPUT_DREFCLK;
7744
7745 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007746 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007747
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007748 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007749 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007750 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007751 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007752 }
7753}
7754
Daniel Vetter251ac862015-06-18 10:30:24 +02007755static void i8xx_compute_dpll(struct intel_crtc *crtc,
7756 struct intel_crtc_state *crtc_state,
7757 intel_clock_t *reduced_clock,
7758 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007759{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007760 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007761 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007762 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007763 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007764
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007765 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307766
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007767 dpll = DPLL_VGA_MODE_DIS;
7768
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007769 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007770 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7771 } else {
7772 if (clock->p1 == 2)
7773 dpll |= PLL_P1_DIVIDE_BY_TWO;
7774 else
7775 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7776 if (clock->p2 == 4)
7777 dpll |= PLL_P2_DIVIDE_BY_4;
7778 }
7779
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007780 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007781 dpll |= DPLL_DVO_2X_MODE;
7782
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007783 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007784 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7785 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7786 else
7787 dpll |= PLL_REF_INPUT_DREFCLK;
7788
7789 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007790 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007791}
7792
Daniel Vetter8a654f32013-06-01 17:16:22 +02007793static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007794{
7795 struct drm_device *dev = intel_crtc->base.dev;
7796 struct drm_i915_private *dev_priv = dev->dev_private;
7797 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007798 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007799 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007800 uint32_t crtc_vtotal, crtc_vblank_end;
7801 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007802
7803 /* We need to be careful not to changed the adjusted mode, for otherwise
7804 * the hw state checker will get angry at the mismatch. */
7805 crtc_vtotal = adjusted_mode->crtc_vtotal;
7806 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007807
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007808 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007809 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007810 crtc_vtotal -= 1;
7811 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007812
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007813 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007814 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7815 else
7816 vsyncshift = adjusted_mode->crtc_hsync_start -
7817 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007818 if (vsyncshift < 0)
7819 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007820 }
7821
7822 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007823 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007824
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007825 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007826 (adjusted_mode->crtc_hdisplay - 1) |
7827 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007828 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007829 (adjusted_mode->crtc_hblank_start - 1) |
7830 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007831 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007832 (adjusted_mode->crtc_hsync_start - 1) |
7833 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7834
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007835 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007836 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007837 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007838 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007839 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007840 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007841 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007842 (adjusted_mode->crtc_vsync_start - 1) |
7843 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7844
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007845 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7846 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7847 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7848 * bits. */
7849 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7850 (pipe == PIPE_B || pipe == PIPE_C))
7851 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7852
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007853 /* pipesrc controls the size that is scaled from, which should
7854 * always be the user's requested size.
7855 */
7856 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007857 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7858 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007859}
7860
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007861static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007862 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007863{
7864 struct drm_device *dev = crtc->base.dev;
7865 struct drm_i915_private *dev_priv = dev->dev_private;
7866 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7867 uint32_t tmp;
7868
7869 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007870 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7871 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007872 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007873 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7874 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007875 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007876 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7877 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007878
7879 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007880 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7881 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007882 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007883 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7884 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007885 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007886 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7887 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007888
7889 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007890 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7891 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7892 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007893 }
7894
7895 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007896 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7897 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7898
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007899 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7900 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007901}
7902
Daniel Vetterf6a83282014-02-11 15:28:57 -08007903void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007904 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007905{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007906 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7907 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7908 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7909 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007910
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007911 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7912 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7913 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7914 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007915
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007916 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007917 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007918
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007919 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7920 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007921
7922 mode->hsync = drm_mode_hsync(mode);
7923 mode->vrefresh = drm_mode_vrefresh(mode);
7924 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007925}
7926
Daniel Vetter84b046f2013-02-19 18:48:54 +01007927static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7928{
7929 struct drm_device *dev = intel_crtc->base.dev;
7930 struct drm_i915_private *dev_priv = dev->dev_private;
7931 uint32_t pipeconf;
7932
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007933 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007934
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007935 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7936 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7937 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007938
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007939 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007940 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007941
Daniel Vetterff9ce462013-04-24 14:57:17 +02007942 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007943 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007944 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007945 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007946 pipeconf |= PIPECONF_DITHER_EN |
7947 PIPECONF_DITHER_TYPE_SP;
7948
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007949 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007950 case 18:
7951 pipeconf |= PIPECONF_6BPC;
7952 break;
7953 case 24:
7954 pipeconf |= PIPECONF_8BPC;
7955 break;
7956 case 30:
7957 pipeconf |= PIPECONF_10BPC;
7958 break;
7959 default:
7960 /* Case prevented by intel_choose_pipe_bpp_dither. */
7961 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007962 }
7963 }
7964
7965 if (HAS_PIPE_CXSR(dev)) {
7966 if (intel_crtc->lowfreq_avail) {
7967 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7968 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7969 } else {
7970 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007971 }
7972 }
7973
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007974 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007975 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007976 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007977 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7978 else
7979 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7980 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007981 pipeconf |= PIPECONF_PROGRESSIVE;
7982
Wayne Boyer666a4532015-12-09 12:29:35 -08007983 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7984 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007985 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007986
Daniel Vetter84b046f2013-02-19 18:48:54 +01007987 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7988 POSTING_READ(PIPECONF(intel_crtc->pipe));
7989}
7990
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007991static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7992 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007993{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007994 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007995 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007996 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007997 intel_clock_t clock;
7998 bool ok;
Ma Lingd4906092009-03-18 20:13:27 +08007999 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008000 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008001 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008002 struct drm_connector_state *connector_state;
8003 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008004
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008005 memset(&crtc_state->dpll_hw_state, 0,
8006 sizeof(crtc_state->dpll_hw_state));
8007
Jani Nikulaa65347b2015-11-27 12:21:46 +02008008 if (crtc_state->has_dsi_encoder)
Daniel Vetter5b18e572014-04-24 23:55:06 +02008009 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008010
Jani Nikulaa65347b2015-11-27 12:21:46 +02008011 for_each_connector_in_state(state, connector, connector_state, i) {
8012 if (connector_state->crtc == &crtc->base)
8013 num_connectors++;
8014 }
8015
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008016 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008017 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03008018
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008019 /*
8020 * Returns a set of divisors for the desired target clock with
8021 * the given refclk, or FALSE. The returned values represent
8022 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
8023 * 2) / p1 / p2.
8024 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008025 limit = intel_limit(crtc_state, refclk);
8026 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008027 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008028 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03008029 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008030 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8031 return -EINVAL;
8032 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008033
Jani Nikulaf2335332013-09-13 11:03:09 +03008034 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008035 crtc_state->dpll.n = clock.n;
8036 crtc_state->dpll.m1 = clock.m1;
8037 crtc_state->dpll.m2 = clock.m2;
8038 crtc_state->dpll.p1 = clock.p1;
8039 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008040 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008041
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008042 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008043 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02008044 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008045 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02008046 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008047 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02008048 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008049 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008050 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02008051 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008052 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008053
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008054 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008055}
8056
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008057static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008058 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008059{
8060 struct drm_device *dev = crtc->base.dev;
8061 struct drm_i915_private *dev_priv = dev->dev_private;
8062 uint32_t tmp;
8063
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008064 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8065 return;
8066
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008067 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008068 if (!(tmp & PFIT_ENABLE))
8069 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008070
Daniel Vetter06922822013-07-11 13:35:40 +02008071 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008072 if (INTEL_INFO(dev)->gen < 4) {
8073 if (crtc->pipe != PIPE_B)
8074 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008075 } else {
8076 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8077 return;
8078 }
8079
Daniel Vetter06922822013-07-11 13:35:40 +02008080 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008081 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
8082 if (INTEL_INFO(dev)->gen < 5)
8083 pipe_config->gmch_pfit.lvds_border_bits =
8084 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
8085}
8086
Jesse Barnesacbec812013-09-20 11:29:32 -07008087static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008088 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008089{
8090 struct drm_device *dev = crtc->base.dev;
8091 struct drm_i915_private *dev_priv = dev->dev_private;
8092 int pipe = pipe_config->cpu_transcoder;
8093 intel_clock_t clock;
8094 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008095 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008096
Shobhit Kumarf573de52014-07-30 20:32:37 +05308097 /* In case of MIPI DPLL will not even be used */
8098 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8099 return;
8100
Ville Syrjäläa5805162015-05-26 20:42:30 +03008101 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008102 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008103 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008104
8105 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8106 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8107 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8108 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8109 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8110
Imre Deakdccbea32015-06-22 23:35:51 +03008111 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008112}
8113
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008114static void
8115i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8116 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008117{
8118 struct drm_device *dev = crtc->base.dev;
8119 struct drm_i915_private *dev_priv = dev->dev_private;
8120 u32 val, base, offset;
8121 int pipe = crtc->pipe, plane = crtc->plane;
8122 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008123 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008124 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008125 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008126
Damien Lespiau42a7b082015-02-05 19:35:13 +00008127 val = I915_READ(DSPCNTR(plane));
8128 if (!(val & DISPLAY_PLANE_ENABLE))
8129 return;
8130
Damien Lespiaud9806c92015-01-21 14:07:19 +00008131 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008132 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008133 DRM_DEBUG_KMS("failed to alloc fb\n");
8134 return;
8135 }
8136
Damien Lespiau1b842c82015-01-21 13:50:54 +00008137 fb = &intel_fb->base;
8138
Daniel Vetter18c52472015-02-10 17:16:09 +00008139 if (INTEL_INFO(dev)->gen >= 4) {
8140 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008141 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008142 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8143 }
8144 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008145
8146 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008147 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008148 fb->pixel_format = fourcc;
8149 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008150
8151 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008152 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008153 offset = I915_READ(DSPTILEOFF(plane));
8154 else
8155 offset = I915_READ(DSPLINOFF(plane));
8156 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8157 } else {
8158 base = I915_READ(DSPADDR(plane));
8159 }
8160 plane_config->base = base;
8161
8162 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008163 fb->width = ((val >> 16) & 0xfff) + 1;
8164 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008165
8166 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008167 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008168
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008169 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008170 fb->pixel_format,
8171 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008172
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008173 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008174
Damien Lespiau2844a922015-01-20 12:51:48 +00008175 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8176 pipe_name(pipe), plane, fb->width, fb->height,
8177 fb->bits_per_pixel, base, fb->pitches[0],
8178 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008179
Damien Lespiau2d140302015-02-05 17:22:18 +00008180 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008181}
8182
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008183static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008184 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008185{
8186 struct drm_device *dev = crtc->base.dev;
8187 struct drm_i915_private *dev_priv = dev->dev_private;
8188 int pipe = pipe_config->cpu_transcoder;
8189 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8190 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008191 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008192 int refclk = 100000;
8193
Ville Syrjäläa5805162015-05-26 20:42:30 +03008194 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008195 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8196 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8197 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8198 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008199 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008200 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008201
8202 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008203 clock.m2 = (pll_dw0 & 0xff) << 22;
8204 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8205 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008206 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8207 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8208 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8209
Imre Deakdccbea32015-06-22 23:35:51 +03008210 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008211}
8212
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008213static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008214 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008215{
8216 struct drm_device *dev = crtc->base.dev;
8217 struct drm_i915_private *dev_priv = dev->dev_private;
8218 uint32_t tmp;
8219
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008220 if (!intel_display_power_is_enabled(dev_priv,
8221 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008222 return false;
8223
Daniel Vettere143a212013-07-04 12:01:15 +02008224 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008225 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008226
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008227 tmp = I915_READ(PIPECONF(crtc->pipe));
8228 if (!(tmp & PIPECONF_ENABLE))
8229 return false;
8230
Wayne Boyer666a4532015-12-09 12:29:35 -08008231 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008232 switch (tmp & PIPECONF_BPC_MASK) {
8233 case PIPECONF_6BPC:
8234 pipe_config->pipe_bpp = 18;
8235 break;
8236 case PIPECONF_8BPC:
8237 pipe_config->pipe_bpp = 24;
8238 break;
8239 case PIPECONF_10BPC:
8240 pipe_config->pipe_bpp = 30;
8241 break;
8242 default:
8243 break;
8244 }
8245 }
8246
Wayne Boyer666a4532015-12-09 12:29:35 -08008247 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8248 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008249 pipe_config->limited_color_range = true;
8250
Ville Syrjälä282740f2013-09-04 18:30:03 +03008251 if (INTEL_INFO(dev)->gen < 4)
8252 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8253
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008254 intel_get_pipe_timings(crtc, pipe_config);
8255
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008256 i9xx_get_pfit_config(crtc, pipe_config);
8257
Daniel Vetter6c49f242013-06-06 12:45:25 +02008258 if (INTEL_INFO(dev)->gen >= 4) {
8259 tmp = I915_READ(DPLL_MD(crtc->pipe));
8260 pipe_config->pixel_multiplier =
8261 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8262 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008263 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008264 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8265 tmp = I915_READ(DPLL(crtc->pipe));
8266 pipe_config->pixel_multiplier =
8267 ((tmp & SDVO_MULTIPLIER_MASK)
8268 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8269 } else {
8270 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8271 * port and will be fixed up in the encoder->get_config
8272 * function. */
8273 pipe_config->pixel_multiplier = 1;
8274 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008275 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008276 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008277 /*
8278 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8279 * on 830. Filter it out here so that we don't
8280 * report errors due to that.
8281 */
8282 if (IS_I830(dev))
8283 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8284
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008285 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8286 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008287 } else {
8288 /* Mask out read-only status bits. */
8289 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8290 DPLL_PORTC_READY_MASK |
8291 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008292 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008293
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008294 if (IS_CHERRYVIEW(dev))
8295 chv_crtc_clock_get(crtc, pipe_config);
8296 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008297 vlv_crtc_clock_get(crtc, pipe_config);
8298 else
8299 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008300
Ville Syrjälä0f646142015-08-26 19:39:18 +03008301 /*
8302 * Normally the dotclock is filled in by the encoder .get_config()
8303 * but in case the pipe is enabled w/o any ports we need a sane
8304 * default.
8305 */
8306 pipe_config->base.adjusted_mode.crtc_clock =
8307 pipe_config->port_clock / pipe_config->pixel_multiplier;
8308
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008309 return true;
8310}
8311
Paulo Zanonidde86e22012-12-01 12:04:25 -02008312static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008313{
8314 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008315 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008316 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008317 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008318 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008319 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008320 bool has_ck505 = false;
8321 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008322
8323 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008324 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008325 switch (encoder->type) {
8326 case INTEL_OUTPUT_LVDS:
8327 has_panel = true;
8328 has_lvds = true;
8329 break;
8330 case INTEL_OUTPUT_EDP:
8331 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008332 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008333 has_cpu_edp = true;
8334 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008335 default:
8336 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008337 }
8338 }
8339
Keith Packard99eb6a02011-09-26 14:29:12 -07008340 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008341 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008342 can_ssc = has_ck505;
8343 } else {
8344 has_ck505 = false;
8345 can_ssc = true;
8346 }
8347
Imre Deak2de69052013-05-08 13:14:04 +03008348 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8349 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008350
8351 /* Ironlake: try to setup display ref clock before DPLL
8352 * enabling. This is only under driver's control after
8353 * PCH B stepping, previous chipset stepping should be
8354 * ignoring this setting.
8355 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008356 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008357
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008358 /* As we must carefully and slowly disable/enable each source in turn,
8359 * compute the final state we want first and check if we need to
8360 * make any changes at all.
8361 */
8362 final = val;
8363 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008364 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008365 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008366 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008367 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8368
8369 final &= ~DREF_SSC_SOURCE_MASK;
8370 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8371 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008372
Keith Packard199e5d72011-09-22 12:01:57 -07008373 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008374 final |= DREF_SSC_SOURCE_ENABLE;
8375
8376 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8377 final |= DREF_SSC1_ENABLE;
8378
8379 if (has_cpu_edp) {
8380 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8381 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8382 else
8383 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8384 } else
8385 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8386 } else {
8387 final |= DREF_SSC_SOURCE_DISABLE;
8388 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8389 }
8390
8391 if (final == val)
8392 return;
8393
8394 /* Always enable nonspread source */
8395 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8396
8397 if (has_ck505)
8398 val |= DREF_NONSPREAD_CK505_ENABLE;
8399 else
8400 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8401
8402 if (has_panel) {
8403 val &= ~DREF_SSC_SOURCE_MASK;
8404 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008405
Keith Packard199e5d72011-09-22 12:01:57 -07008406 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008407 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008408 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008409 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008410 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008411 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008412
8413 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008414 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008415 POSTING_READ(PCH_DREF_CONTROL);
8416 udelay(200);
8417
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008418 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008419
8420 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008421 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008422 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008423 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008424 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008425 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008426 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008427 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008428 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008429
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008430 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008431 POSTING_READ(PCH_DREF_CONTROL);
8432 udelay(200);
8433 } else {
8434 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8435
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008436 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008437
8438 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008439 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008440
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
8445 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008446 val &= ~DREF_SSC_SOURCE_MASK;
8447 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008448
8449 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008450 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008451
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008452 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008453 POSTING_READ(PCH_DREF_CONTROL);
8454 udelay(200);
8455 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008456
8457 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008458}
8459
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008460static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008461{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008462 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008463
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008464 tmp = I915_READ(SOUTH_CHICKEN2);
8465 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8466 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008467
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008468 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8469 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8470 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008471
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008472 tmp = I915_READ(SOUTH_CHICKEN2);
8473 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8474 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008475
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008476 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8477 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8478 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008479}
8480
8481/* WaMPhyProgramming:hsw */
8482static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8483{
8484 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008485
8486 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8487 tmp &= ~(0xFF << 24);
8488 tmp |= (0x12 << 24);
8489 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8490
Paulo Zanonidde86e22012-12-01 12:04:25 -02008491 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8492 tmp |= (1 << 11);
8493 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8494
8495 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8496 tmp |= (1 << 11);
8497 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8498
Paulo Zanonidde86e22012-12-01 12:04:25 -02008499 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8500 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8501 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8502
8503 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8504 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8505 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8506
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008507 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8508 tmp &= ~(7 << 13);
8509 tmp |= (5 << 13);
8510 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008511
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008512 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8513 tmp &= ~(7 << 13);
8514 tmp |= (5 << 13);
8515 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008516
8517 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8518 tmp &= ~0xFF;
8519 tmp |= 0x1C;
8520 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8521
8522 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8523 tmp &= ~0xFF;
8524 tmp |= 0x1C;
8525 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8526
8527 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8528 tmp &= ~(0xFF << 16);
8529 tmp |= (0x1C << 16);
8530 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8531
8532 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8533 tmp &= ~(0xFF << 16);
8534 tmp |= (0x1C << 16);
8535 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8536
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008537 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8538 tmp |= (1 << 27);
8539 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008540
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008541 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8542 tmp |= (1 << 27);
8543 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008544
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008545 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8546 tmp &= ~(0xF << 28);
8547 tmp |= (4 << 28);
8548 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008549
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008550 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8551 tmp &= ~(0xF << 28);
8552 tmp |= (4 << 28);
8553 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008554}
8555
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008556/* Implements 3 different sequences from BSpec chapter "Display iCLK
8557 * Programming" based on the parameters passed:
8558 * - Sequence to enable CLKOUT_DP
8559 * - Sequence to enable CLKOUT_DP without spread
8560 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8561 */
8562static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8563 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008564{
8565 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008566 uint32_t reg, tmp;
8567
8568 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8569 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008570 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008571 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008572
Ville Syrjäläa5805162015-05-26 20:42:30 +03008573 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008574
8575 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8576 tmp &= ~SBI_SSCCTL_DISABLE;
8577 tmp |= SBI_SSCCTL_PATHALT;
8578 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8579
8580 udelay(24);
8581
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008582 if (with_spread) {
8583 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8584 tmp &= ~SBI_SSCCTL_PATHALT;
8585 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008586
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008587 if (with_fdi) {
8588 lpt_reset_fdi_mphy(dev_priv);
8589 lpt_program_fdi_mphy(dev_priv);
8590 }
8591 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008592
Ville Syrjäläc2699522015-08-27 23:55:59 +03008593 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008594 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8595 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8596 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008597
Ville Syrjäläa5805162015-05-26 20:42:30 +03008598 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008599}
8600
Paulo Zanoni47701c32013-07-23 11:19:25 -03008601/* Sequence to disable CLKOUT_DP */
8602static void lpt_disable_clkout_dp(struct drm_device *dev)
8603{
8604 struct drm_i915_private *dev_priv = dev->dev_private;
8605 uint32_t reg, tmp;
8606
Ville Syrjäläa5805162015-05-26 20:42:30 +03008607 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008608
Ville Syrjäläc2699522015-08-27 23:55:59 +03008609 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008610 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8611 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8612 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8613
8614 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8615 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8616 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8617 tmp |= SBI_SSCCTL_PATHALT;
8618 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8619 udelay(32);
8620 }
8621 tmp |= SBI_SSCCTL_DISABLE;
8622 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8623 }
8624
Ville Syrjäläa5805162015-05-26 20:42:30 +03008625 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008626}
8627
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008628#define BEND_IDX(steps) ((50 + (steps)) / 5)
8629
8630static const uint16_t sscdivintphase[] = {
8631 [BEND_IDX( 50)] = 0x3B23,
8632 [BEND_IDX( 45)] = 0x3B23,
8633 [BEND_IDX( 40)] = 0x3C23,
8634 [BEND_IDX( 35)] = 0x3C23,
8635 [BEND_IDX( 30)] = 0x3D23,
8636 [BEND_IDX( 25)] = 0x3D23,
8637 [BEND_IDX( 20)] = 0x3E23,
8638 [BEND_IDX( 15)] = 0x3E23,
8639 [BEND_IDX( 10)] = 0x3F23,
8640 [BEND_IDX( 5)] = 0x3F23,
8641 [BEND_IDX( 0)] = 0x0025,
8642 [BEND_IDX( -5)] = 0x0025,
8643 [BEND_IDX(-10)] = 0x0125,
8644 [BEND_IDX(-15)] = 0x0125,
8645 [BEND_IDX(-20)] = 0x0225,
8646 [BEND_IDX(-25)] = 0x0225,
8647 [BEND_IDX(-30)] = 0x0325,
8648 [BEND_IDX(-35)] = 0x0325,
8649 [BEND_IDX(-40)] = 0x0425,
8650 [BEND_IDX(-45)] = 0x0425,
8651 [BEND_IDX(-50)] = 0x0525,
8652};
8653
8654/*
8655 * Bend CLKOUT_DP
8656 * steps -50 to 50 inclusive, in steps of 5
8657 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8658 * change in clock period = -(steps / 10) * 5.787 ps
8659 */
8660static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8661{
8662 uint32_t tmp;
8663 int idx = BEND_IDX(steps);
8664
8665 if (WARN_ON(steps % 5 != 0))
8666 return;
8667
8668 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8669 return;
8670
8671 mutex_lock(&dev_priv->sb_lock);
8672
8673 if (steps % 10 != 0)
8674 tmp = 0xAAAAAAAB;
8675 else
8676 tmp = 0x00000000;
8677 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8678
8679 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8680 tmp &= 0xffff0000;
8681 tmp |= sscdivintphase[idx];
8682 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8683
8684 mutex_unlock(&dev_priv->sb_lock);
8685}
8686
8687#undef BEND_IDX
8688
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008689static void lpt_init_pch_refclk(struct drm_device *dev)
8690{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008691 struct intel_encoder *encoder;
8692 bool has_vga = false;
8693
Damien Lespiaub2784e12014-08-05 11:29:37 +01008694 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008695 switch (encoder->type) {
8696 case INTEL_OUTPUT_ANALOG:
8697 has_vga = true;
8698 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008699 default:
8700 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008701 }
8702 }
8703
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008704 if (has_vga) {
8705 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008706 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008707 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008708 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008709 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008710}
8711
Paulo Zanonidde86e22012-12-01 12:04:25 -02008712/*
8713 * Initialize reference clocks when the driver loads
8714 */
8715void intel_init_pch_refclk(struct drm_device *dev)
8716{
8717 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8718 ironlake_init_pch_refclk(dev);
8719 else if (HAS_PCH_LPT(dev))
8720 lpt_init_pch_refclk(dev);
8721}
8722
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008723static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008724{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008725 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008726 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008727 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008728 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008729 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008730 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008731 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008732 bool is_lvds = false;
8733
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008734 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008735 if (connector_state->crtc != crtc_state->base.crtc)
8736 continue;
8737
8738 encoder = to_intel_encoder(connector_state->best_encoder);
8739
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008740 switch (encoder->type) {
8741 case INTEL_OUTPUT_LVDS:
8742 is_lvds = true;
8743 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008744 default:
8745 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008746 }
8747 num_connectors++;
8748 }
8749
8750 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008751 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008752 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008753 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008754 }
8755
8756 return 120000;
8757}
8758
Daniel Vetter6ff93602013-04-19 11:24:36 +02008759static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008760{
8761 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8762 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8763 int pipe = intel_crtc->pipe;
8764 uint32_t val;
8765
Daniel Vetter78114072013-06-13 00:54:57 +02008766 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008767
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008768 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008769 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008770 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008771 break;
8772 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008773 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008774 break;
8775 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008776 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008777 break;
8778 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008779 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008780 break;
8781 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008782 /* Case prevented by intel_choose_pipe_bpp_dither. */
8783 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008784 }
8785
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008786 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008787 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8788
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008789 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008790 val |= PIPECONF_INTERLACED_ILK;
8791 else
8792 val |= PIPECONF_PROGRESSIVE;
8793
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008794 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008795 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008796
Paulo Zanonic8203562012-09-12 10:06:29 -03008797 I915_WRITE(PIPECONF(pipe), val);
8798 POSTING_READ(PIPECONF(pipe));
8799}
8800
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008801/*
8802 * Set up the pipe CSC unit.
8803 *
8804 * Currently only full range RGB to limited range RGB conversion
8805 * is supported, but eventually this should handle various
8806 * RGB<->YCbCr scenarios as well.
8807 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008808static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008809{
8810 struct drm_device *dev = crtc->dev;
8811 struct drm_i915_private *dev_priv = dev->dev_private;
8812 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8813 int pipe = intel_crtc->pipe;
8814 uint16_t coeff = 0x7800; /* 1.0 */
8815
8816 /*
8817 * TODO: Check what kind of values actually come out of the pipe
8818 * with these coeff/postoff values and adjust to get the best
8819 * accuracy. Perhaps we even need to take the bpc value into
8820 * consideration.
8821 */
8822
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008823 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008824 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8825
8826 /*
8827 * GY/GU and RY/RU should be the other way around according
8828 * to BSpec, but reality doesn't agree. Just set them up in
8829 * a way that results in the correct picture.
8830 */
8831 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8832 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8833
8834 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8835 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8836
8837 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8838 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8839
8840 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8841 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8842 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8843
8844 if (INTEL_INFO(dev)->gen > 6) {
8845 uint16_t postoff = 0;
8846
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008847 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008848 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008849
8850 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8851 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8852 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8853
8854 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8855 } else {
8856 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8857
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008858 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008859 mode |= CSC_BLACK_SCREEN_OFFSET;
8860
8861 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8862 }
8863}
8864
Daniel Vetter6ff93602013-04-19 11:24:36 +02008865static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008866{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008867 struct drm_device *dev = crtc->dev;
8868 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008870 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008871 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008872 uint32_t val;
8873
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008874 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008875
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008876 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008877 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8878
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008879 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008880 val |= PIPECONF_INTERLACED_ILK;
8881 else
8882 val |= PIPECONF_PROGRESSIVE;
8883
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008884 I915_WRITE(PIPECONF(cpu_transcoder), val);
8885 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008886
8887 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8888 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008889
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308890 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008891 val = 0;
8892
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008893 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008894 case 18:
8895 val |= PIPEMISC_DITHER_6_BPC;
8896 break;
8897 case 24:
8898 val |= PIPEMISC_DITHER_8_BPC;
8899 break;
8900 case 30:
8901 val |= PIPEMISC_DITHER_10_BPC;
8902 break;
8903 case 36:
8904 val |= PIPEMISC_DITHER_12_BPC;
8905 break;
8906 default:
8907 /* Case prevented by pipe_config_set_bpp. */
8908 BUG();
8909 }
8910
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008911 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008912 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8913
8914 I915_WRITE(PIPEMISC(pipe), val);
8915 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008916}
8917
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008918static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008919 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008920 intel_clock_t *clock,
8921 bool *has_reduced_clock,
8922 intel_clock_t *reduced_clock)
8923{
8924 struct drm_device *dev = crtc->dev;
8925 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008926 int refclk;
8927 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008928 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008929
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008930 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008931
8932 /*
8933 * Returns a set of divisors for the desired target clock with the given
8934 * refclk, or FALSE. The returned values represent the clock equation:
8935 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8936 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008937 limit = intel_limit(crtc_state, refclk);
8938 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008939 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008940 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008941 if (!ret)
8942 return false;
8943
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008944 return true;
8945}
8946
Paulo Zanonid4b19312012-11-29 11:29:32 -02008947int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8948{
8949 /*
8950 * Account for spread spectrum to avoid
8951 * oversubscribing the link. Max center spread
8952 * is 2.5%; use 5% for safety's sake.
8953 */
8954 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008955 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008956}
8957
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008958static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008959{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008960 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008961}
8962
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008963static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008964 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008965 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008966 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008967{
8968 struct drm_crtc *crtc = &intel_crtc->base;
8969 struct drm_device *dev = crtc->dev;
8970 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008971 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008972 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008973 struct drm_connector_state *connector_state;
8974 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008975 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008976 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008977 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008978
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008979 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008980 if (connector_state->crtc != crtc_state->base.crtc)
8981 continue;
8982
8983 encoder = to_intel_encoder(connector_state->best_encoder);
8984
8985 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008986 case INTEL_OUTPUT_LVDS:
8987 is_lvds = true;
8988 break;
8989 case INTEL_OUTPUT_SDVO:
8990 case INTEL_OUTPUT_HDMI:
8991 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008992 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008993 default:
8994 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008995 }
8996
8997 num_connectors++;
8998 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008999
Chris Wilsonc1858122010-12-03 21:35:48 +00009000 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07009001 factor = 21;
9002 if (is_lvds) {
9003 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009004 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02009005 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07009006 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009007 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07009008 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00009009
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009010 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02009011 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00009012
Daniel Vetter9a7c7892013-04-04 22:20:34 +02009013 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
9014 *fp2 |= FP_CB_TUNE;
9015
Chris Wilson5eddb702010-09-11 13:48:45 +01009016 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08009017
Eric Anholta07d6782011-03-30 13:01:08 -07009018 if (is_lvds)
9019 dpll |= DPLLB_MODE_LVDS;
9020 else
9021 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009022
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009023 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02009024 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009025
9026 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02009027 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009028 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02009029 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08009030
Eric Anholta07d6782011-03-30 13:01:08 -07009031 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009032 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009033 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009034 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009035
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009036 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07009037 case 5:
9038 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9039 break;
9040 case 7:
9041 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9042 break;
9043 case 10:
9044 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9045 break;
9046 case 14:
9047 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9048 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009049 }
9050
Daniel Vetterb4c09f32013-04-30 14:01:42 +02009051 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05009052 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08009053 else
9054 dpll |= PLL_REF_INPUT_DREFCLK;
9055
Daniel Vetter959e16d2013-06-05 13:34:21 +02009056 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009057}
9058
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009059static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9060 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08009061{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009062 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009063 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009064 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03009065 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01009066 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009067 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009068
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03009069 memset(&crtc_state->dpll_hw_state, 0,
9070 sizeof(crtc_state->dpll_hw_state));
9071
Ville Syrjälä7905df22015-11-25 16:35:30 +02009072 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08009073
Paulo Zanoni5dc52982012-10-05 12:05:56 -03009074 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
9075 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
9076
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009077 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03009078 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009079 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009080 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9081 return -EINVAL;
9082 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01009083 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009084 if (!crtc_state->clock_set) {
9085 crtc_state->dpll.n = clock.n;
9086 crtc_state->dpll.m1 = clock.m1;
9087 crtc_state->dpll.m2 = clock.m2;
9088 crtc_state->dpll.p1 = clock.p1;
9089 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009090 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009091
Paulo Zanoni5dc52982012-10-05 12:05:56 -03009092 /* 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 +02009093 if (crtc_state->has_pch_encoder) {
9094 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009095 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009096 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009097
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009098 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009099 &fp, &reduced_clock,
9100 has_reduced_clock ? &fp2 : NULL);
9101
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009102 crtc_state->dpll_hw_state.dpll = dpll;
9103 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009104 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009105 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009106 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009107 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009108
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009109 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009110 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03009111 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009112 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07009113 return -EINVAL;
9114 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009115 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009116
Rodrigo Viviab585de2015-03-24 12:40:09 -07009117 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009118 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02009119 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009120 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009121
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009122 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009123}
9124
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009125static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9126 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009127{
9128 struct drm_device *dev = crtc->base.dev;
9129 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009130 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009131
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009132 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9133 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9134 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9135 & ~TU_SIZE_MASK;
9136 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9137 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9138 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9139}
9140
9141static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9142 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009143 struct intel_link_m_n *m_n,
9144 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009145{
9146 struct drm_device *dev = crtc->base.dev;
9147 struct drm_i915_private *dev_priv = dev->dev_private;
9148 enum pipe pipe = crtc->pipe;
9149
9150 if (INTEL_INFO(dev)->gen >= 5) {
9151 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9152 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9153 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9154 & ~TU_SIZE_MASK;
9155 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9156 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9157 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009158 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9159 * gen < 8) and if DRRS is supported (to make sure the
9160 * registers are not unnecessarily read).
9161 */
9162 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009163 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009164 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9165 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9166 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9167 & ~TU_SIZE_MASK;
9168 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9169 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9170 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9171 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009172 } else {
9173 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9174 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9175 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9176 & ~TU_SIZE_MASK;
9177 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9178 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9179 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9180 }
9181}
9182
9183void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009184 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009185{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009186 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009187 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9188 else
9189 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009190 &pipe_config->dp_m_n,
9191 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009192}
9193
Daniel Vetter72419202013-04-04 13:28:53 +02009194static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009195 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009196{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009197 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009198 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009199}
9200
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009201static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009202 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009203{
9204 struct drm_device *dev = crtc->base.dev;
9205 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009206 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9207 uint32_t ps_ctrl = 0;
9208 int id = -1;
9209 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009210
Chandra Kondurua1b22782015-04-07 15:28:45 -07009211 /* find scaler attached to this pipe */
9212 for (i = 0; i < crtc->num_scalers; i++) {
9213 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9214 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9215 id = i;
9216 pipe_config->pch_pfit.enabled = true;
9217 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9218 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9219 break;
9220 }
9221 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009222
Chandra Kondurua1b22782015-04-07 15:28:45 -07009223 scaler_state->scaler_id = id;
9224 if (id >= 0) {
9225 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9226 } else {
9227 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009228 }
9229}
9230
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009231static void
9232skylake_get_initial_plane_config(struct intel_crtc *crtc,
9233 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009234{
9235 struct drm_device *dev = crtc->base.dev;
9236 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009237 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009238 int pipe = crtc->pipe;
9239 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009240 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009241 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009242 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009243
Damien Lespiaud9806c92015-01-21 14:07:19 +00009244 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009245 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009246 DRM_DEBUG_KMS("failed to alloc fb\n");
9247 return;
9248 }
9249
Damien Lespiau1b842c82015-01-21 13:50:54 +00009250 fb = &intel_fb->base;
9251
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009252 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009253 if (!(val & PLANE_CTL_ENABLE))
9254 goto error;
9255
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009256 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9257 fourcc = skl_format_to_fourcc(pixel_format,
9258 val & PLANE_CTL_ORDER_RGBX,
9259 val & PLANE_CTL_ALPHA_MASK);
9260 fb->pixel_format = fourcc;
9261 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9262
Damien Lespiau40f46282015-02-27 11:15:21 +00009263 tiling = val & PLANE_CTL_TILED_MASK;
9264 switch (tiling) {
9265 case PLANE_CTL_TILED_LINEAR:
9266 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9267 break;
9268 case PLANE_CTL_TILED_X:
9269 plane_config->tiling = I915_TILING_X;
9270 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9271 break;
9272 case PLANE_CTL_TILED_Y:
9273 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9274 break;
9275 case PLANE_CTL_TILED_YF:
9276 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9277 break;
9278 default:
9279 MISSING_CASE(tiling);
9280 goto error;
9281 }
9282
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009283 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9284 plane_config->base = base;
9285
9286 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9287
9288 val = I915_READ(PLANE_SIZE(pipe, 0));
9289 fb->height = ((val >> 16) & 0xfff) + 1;
9290 fb->width = ((val >> 0) & 0x1fff) + 1;
9291
9292 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009293 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009294 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009295 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9296
9297 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009298 fb->pixel_format,
9299 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009300
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009301 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009302
9303 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9304 pipe_name(pipe), fb->width, fb->height,
9305 fb->bits_per_pixel, base, fb->pitches[0],
9306 plane_config->size);
9307
Damien Lespiau2d140302015-02-05 17:22:18 +00009308 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009309 return;
9310
9311error:
9312 kfree(fb);
9313}
9314
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009315static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009316 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009317{
9318 struct drm_device *dev = crtc->base.dev;
9319 struct drm_i915_private *dev_priv = dev->dev_private;
9320 uint32_t tmp;
9321
9322 tmp = I915_READ(PF_CTL(crtc->pipe));
9323
9324 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009325 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009326 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9327 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009328
9329 /* We currently do not free assignements of panel fitters on
9330 * ivb/hsw (since we don't use the higher upscaling modes which
9331 * differentiates them) so just WARN about this case for now. */
9332 if (IS_GEN7(dev)) {
9333 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9334 PF_PIPE_SEL_IVB(crtc->pipe));
9335 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009336 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009337}
9338
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009339static void
9340ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9341 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009342{
9343 struct drm_device *dev = crtc->base.dev;
9344 struct drm_i915_private *dev_priv = dev->dev_private;
9345 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009346 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009347 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009348 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009349 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009350 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009351
Damien Lespiau42a7b082015-02-05 19:35:13 +00009352 val = I915_READ(DSPCNTR(pipe));
9353 if (!(val & DISPLAY_PLANE_ENABLE))
9354 return;
9355
Damien Lespiaud9806c92015-01-21 14:07:19 +00009356 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009357 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009358 DRM_DEBUG_KMS("failed to alloc fb\n");
9359 return;
9360 }
9361
Damien Lespiau1b842c82015-01-21 13:50:54 +00009362 fb = &intel_fb->base;
9363
Daniel Vetter18c52472015-02-10 17:16:09 +00009364 if (INTEL_INFO(dev)->gen >= 4) {
9365 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009366 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009367 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9368 }
9369 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009370
9371 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009372 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009373 fb->pixel_format = fourcc;
9374 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009375
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009376 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009377 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009378 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009379 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009380 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009381 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009382 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009383 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009384 }
9385 plane_config->base = base;
9386
9387 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009388 fb->width = ((val >> 16) & 0xfff) + 1;
9389 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009390
9391 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009392 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009393
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009394 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009395 fb->pixel_format,
9396 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009397
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009398 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009399
Damien Lespiau2844a922015-01-20 12:51:48 +00009400 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9401 pipe_name(pipe), fb->width, fb->height,
9402 fb->bits_per_pixel, base, fb->pitches[0],
9403 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009404
Damien Lespiau2d140302015-02-05 17:22:18 +00009405 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009406}
9407
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009408static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009409 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009410{
9411 struct drm_device *dev = crtc->base.dev;
9412 struct drm_i915_private *dev_priv = dev->dev_private;
9413 uint32_t tmp;
9414
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009415 if (!intel_display_power_is_enabled(dev_priv,
9416 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009417 return false;
9418
Daniel Vettere143a212013-07-04 12:01:15 +02009419 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009420 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009421
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009422 tmp = I915_READ(PIPECONF(crtc->pipe));
9423 if (!(tmp & PIPECONF_ENABLE))
9424 return false;
9425
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009426 switch (tmp & PIPECONF_BPC_MASK) {
9427 case PIPECONF_6BPC:
9428 pipe_config->pipe_bpp = 18;
9429 break;
9430 case PIPECONF_8BPC:
9431 pipe_config->pipe_bpp = 24;
9432 break;
9433 case PIPECONF_10BPC:
9434 pipe_config->pipe_bpp = 30;
9435 break;
9436 case PIPECONF_12BPC:
9437 pipe_config->pipe_bpp = 36;
9438 break;
9439 default:
9440 break;
9441 }
9442
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009443 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9444 pipe_config->limited_color_range = true;
9445
Daniel Vetterab9412b2013-05-03 11:49:46 +02009446 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009447 struct intel_shared_dpll *pll;
9448
Daniel Vetter88adfff2013-03-28 10:42:01 +01009449 pipe_config->has_pch_encoder = true;
9450
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009451 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9452 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9453 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009454
9455 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009456
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009457 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009458 pipe_config->shared_dpll =
9459 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009460 } else {
9461 tmp = I915_READ(PCH_DPLL_SEL);
9462 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9463 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9464 else
9465 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9466 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009467
9468 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9469
9470 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9471 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009472
9473 tmp = pipe_config->dpll_hw_state.dpll;
9474 pipe_config->pixel_multiplier =
9475 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9476 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009477
9478 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009479 } else {
9480 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009481 }
9482
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009483 intel_get_pipe_timings(crtc, pipe_config);
9484
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009485 ironlake_get_pfit_config(crtc, pipe_config);
9486
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009487 return true;
9488}
9489
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009490static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9491{
9492 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009493 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009494
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009495 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009496 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009497 pipe_name(crtc->pipe));
9498
Rob Clarke2c719b2014-12-15 13:56:32 -05009499 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9500 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009501 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9502 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009503 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9504 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009505 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009506 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009507 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009508 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009509 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009510 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009511 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009512 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009513 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009514
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009515 /*
9516 * In theory we can still leave IRQs enabled, as long as only the HPD
9517 * interrupts remain enabled. We used to check for that, but since it's
9518 * gen-specific and since we only disable LCPLL after we fully disable
9519 * the interrupts, the check below should be enough.
9520 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009521 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009522}
9523
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009524static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9525{
9526 struct drm_device *dev = dev_priv->dev;
9527
9528 if (IS_HASWELL(dev))
9529 return I915_READ(D_COMP_HSW);
9530 else
9531 return I915_READ(D_COMP_BDW);
9532}
9533
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009534static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9535{
9536 struct drm_device *dev = dev_priv->dev;
9537
9538 if (IS_HASWELL(dev)) {
9539 mutex_lock(&dev_priv->rps.hw_lock);
9540 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9541 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009542 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009543 mutex_unlock(&dev_priv->rps.hw_lock);
9544 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009545 I915_WRITE(D_COMP_BDW, val);
9546 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009547 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009548}
9549
9550/*
9551 * This function implements pieces of two sequences from BSpec:
9552 * - Sequence for display software to disable LCPLL
9553 * - Sequence for display software to allow package C8+
9554 * The steps implemented here are just the steps that actually touch the LCPLL
9555 * register. Callers should take care of disabling all the display engine
9556 * functions, doing the mode unset, fixing interrupts, etc.
9557 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009558static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9559 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009560{
9561 uint32_t val;
9562
9563 assert_can_disable_lcpll(dev_priv);
9564
9565 val = I915_READ(LCPLL_CTL);
9566
9567 if (switch_to_fclk) {
9568 val |= LCPLL_CD_SOURCE_FCLK;
9569 I915_WRITE(LCPLL_CTL, val);
9570
9571 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9572 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9573 DRM_ERROR("Switching to FCLK failed\n");
9574
9575 val = I915_READ(LCPLL_CTL);
9576 }
9577
9578 val |= LCPLL_PLL_DISABLE;
9579 I915_WRITE(LCPLL_CTL, val);
9580 POSTING_READ(LCPLL_CTL);
9581
9582 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9583 DRM_ERROR("LCPLL still locked\n");
9584
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009585 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009586 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009587 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009588 ndelay(100);
9589
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009590 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9591 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009592 DRM_ERROR("D_COMP RCOMP still in progress\n");
9593
9594 if (allow_power_down) {
9595 val = I915_READ(LCPLL_CTL);
9596 val |= LCPLL_POWER_DOWN_ALLOW;
9597 I915_WRITE(LCPLL_CTL, val);
9598 POSTING_READ(LCPLL_CTL);
9599 }
9600}
9601
9602/*
9603 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9604 * source.
9605 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009606static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009607{
9608 uint32_t val;
9609
9610 val = I915_READ(LCPLL_CTL);
9611
9612 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9613 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9614 return;
9615
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009616 /*
9617 * Make sure we're not on PC8 state before disabling PC8, otherwise
9618 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009619 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009620 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009621
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009622 if (val & LCPLL_POWER_DOWN_ALLOW) {
9623 val &= ~LCPLL_POWER_DOWN_ALLOW;
9624 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009625 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009626 }
9627
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009628 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009629 val |= D_COMP_COMP_FORCE;
9630 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009631 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009632
9633 val = I915_READ(LCPLL_CTL);
9634 val &= ~LCPLL_PLL_DISABLE;
9635 I915_WRITE(LCPLL_CTL, val);
9636
9637 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9638 DRM_ERROR("LCPLL not locked yet\n");
9639
9640 if (val & LCPLL_CD_SOURCE_FCLK) {
9641 val = I915_READ(LCPLL_CTL);
9642 val &= ~LCPLL_CD_SOURCE_FCLK;
9643 I915_WRITE(LCPLL_CTL, val);
9644
9645 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9646 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9647 DRM_ERROR("Switching back to LCPLL failed\n");
9648 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009649
Mika Kuoppala59bad942015-01-16 11:34:40 +02009650 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009651 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009652}
9653
Paulo Zanoni765dab672014-03-07 20:08:18 -03009654/*
9655 * Package states C8 and deeper are really deep PC states that can only be
9656 * reached when all the devices on the system allow it, so even if the graphics
9657 * device allows PC8+, it doesn't mean the system will actually get to these
9658 * states. Our driver only allows PC8+ when going into runtime PM.
9659 *
9660 * The requirements for PC8+ are that all the outputs are disabled, the power
9661 * well is disabled and most interrupts are disabled, and these are also
9662 * requirements for runtime PM. When these conditions are met, we manually do
9663 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9664 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9665 * hang the machine.
9666 *
9667 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9668 * the state of some registers, so when we come back from PC8+ we need to
9669 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9670 * need to take care of the registers kept by RC6. Notice that this happens even
9671 * if we don't put the device in PCI D3 state (which is what currently happens
9672 * because of the runtime PM support).
9673 *
9674 * For more, read "Display Sequences for Package C8" on the hardware
9675 * documentation.
9676 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009677void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009678{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009679 struct drm_device *dev = dev_priv->dev;
9680 uint32_t val;
9681
Paulo Zanonic67a4702013-08-19 13:18:09 -03009682 DRM_DEBUG_KMS("Enabling package C8+\n");
9683
Ville Syrjäläc2699522015-08-27 23:55:59 +03009684 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009685 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9686 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9687 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9688 }
9689
9690 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009691 hsw_disable_lcpll(dev_priv, true, true);
9692}
9693
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009694void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009695{
9696 struct drm_device *dev = dev_priv->dev;
9697 uint32_t val;
9698
Paulo Zanonic67a4702013-08-19 13:18:09 -03009699 DRM_DEBUG_KMS("Disabling package C8+\n");
9700
9701 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009702 lpt_init_pch_refclk(dev);
9703
Ville Syrjäläc2699522015-08-27 23:55:59 +03009704 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009705 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9706 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9707 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9708 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009709}
9710
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009711static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309712{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009713 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009714 struct intel_atomic_state *old_intel_state =
9715 to_intel_atomic_state(old_state);
9716 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309717
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009718 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309719}
9720
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009721/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009722static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009723{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009724 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9725 struct drm_i915_private *dev_priv = state->dev->dev_private;
9726 struct drm_crtc *crtc;
9727 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009728 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009729 unsigned max_pixel_rate = 0, i;
9730 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009731
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009732 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9733 sizeof(intel_state->min_pixclk));
9734
9735 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009736 int pixel_rate;
9737
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009738 crtc_state = to_intel_crtc_state(cstate);
9739 if (!crtc_state->base.enable) {
9740 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009741 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009742 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009743
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009744 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009745
9746 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009747 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009748 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9749
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009750 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009751 }
9752
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009753 if (!intel_state->active_crtcs)
9754 return 0;
9755
9756 for_each_pipe(dev_priv, pipe)
9757 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9758
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009759 return max_pixel_rate;
9760}
9761
9762static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9763{
9764 struct drm_i915_private *dev_priv = dev->dev_private;
9765 uint32_t val, data;
9766 int ret;
9767
9768 if (WARN((I915_READ(LCPLL_CTL) &
9769 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9770 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9771 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9772 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9773 "trying to change cdclk frequency with cdclk not enabled\n"))
9774 return;
9775
9776 mutex_lock(&dev_priv->rps.hw_lock);
9777 ret = sandybridge_pcode_write(dev_priv,
9778 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9779 mutex_unlock(&dev_priv->rps.hw_lock);
9780 if (ret) {
9781 DRM_ERROR("failed to inform pcode about cdclk change\n");
9782 return;
9783 }
9784
9785 val = I915_READ(LCPLL_CTL);
9786 val |= LCPLL_CD_SOURCE_FCLK;
9787 I915_WRITE(LCPLL_CTL, val);
9788
9789 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9790 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9791 DRM_ERROR("Switching to FCLK failed\n");
9792
9793 val = I915_READ(LCPLL_CTL);
9794 val &= ~LCPLL_CLK_FREQ_MASK;
9795
9796 switch (cdclk) {
9797 case 450000:
9798 val |= LCPLL_CLK_FREQ_450;
9799 data = 0;
9800 break;
9801 case 540000:
9802 val |= LCPLL_CLK_FREQ_54O_BDW;
9803 data = 1;
9804 break;
9805 case 337500:
9806 val |= LCPLL_CLK_FREQ_337_5_BDW;
9807 data = 2;
9808 break;
9809 case 675000:
9810 val |= LCPLL_CLK_FREQ_675_BDW;
9811 data = 3;
9812 break;
9813 default:
9814 WARN(1, "invalid cdclk frequency\n");
9815 return;
9816 }
9817
9818 I915_WRITE(LCPLL_CTL, val);
9819
9820 val = I915_READ(LCPLL_CTL);
9821 val &= ~LCPLL_CD_SOURCE_FCLK;
9822 I915_WRITE(LCPLL_CTL, val);
9823
9824 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9825 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9826 DRM_ERROR("Switching back to LCPLL failed\n");
9827
9828 mutex_lock(&dev_priv->rps.hw_lock);
9829 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9830 mutex_unlock(&dev_priv->rps.hw_lock);
9831
9832 intel_update_cdclk(dev);
9833
9834 WARN(cdclk != dev_priv->cdclk_freq,
9835 "cdclk requested %d kHz but got %d kHz\n",
9836 cdclk, dev_priv->cdclk_freq);
9837}
9838
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009839static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009840{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009841 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009842 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009843 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009844 int cdclk;
9845
9846 /*
9847 * FIXME should also account for plane ratio
9848 * once 64bpp pixel formats are supported.
9849 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009850 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009851 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009852 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009853 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009854 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009855 cdclk = 450000;
9856 else
9857 cdclk = 337500;
9858
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009859 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009860 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9861 cdclk, dev_priv->max_cdclk_freq);
9862 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009863 }
9864
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009865 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9866 if (!intel_state->active_crtcs)
9867 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009868
9869 return 0;
9870}
9871
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009872static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009873{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009874 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009875 struct intel_atomic_state *old_intel_state =
9876 to_intel_atomic_state(old_state);
9877 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009878
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009879 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009880}
9881
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009882static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9883 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009884{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009885 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009886 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009887
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009888 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009889
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009890 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009891}
9892
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309893static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9894 enum port port,
9895 struct intel_crtc_state *pipe_config)
9896{
9897 switch (port) {
9898 case PORT_A:
9899 pipe_config->ddi_pll_sel = SKL_DPLL0;
9900 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9901 break;
9902 case PORT_B:
9903 pipe_config->ddi_pll_sel = SKL_DPLL1;
9904 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9905 break;
9906 case PORT_C:
9907 pipe_config->ddi_pll_sel = SKL_DPLL2;
9908 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9909 break;
9910 default:
9911 DRM_ERROR("Incorrect port type\n");
9912 }
9913}
9914
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009915static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9916 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009917 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009918{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009919 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009920
9921 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9922 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9923
9924 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009925 case SKL_DPLL0:
9926 /*
9927 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9928 * of the shared DPLL framework and thus needs to be read out
9929 * separately
9930 */
9931 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9932 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9933 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009934 case SKL_DPLL1:
9935 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9936 break;
9937 case SKL_DPLL2:
9938 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9939 break;
9940 case SKL_DPLL3:
9941 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9942 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009943 }
9944}
9945
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009946static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9947 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009948 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009949{
9950 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9951
9952 switch (pipe_config->ddi_pll_sel) {
9953 case PORT_CLK_SEL_WRPLL1:
9954 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9955 break;
9956 case PORT_CLK_SEL_WRPLL2:
9957 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9958 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009959 case PORT_CLK_SEL_SPLL:
9960 pipe_config->shared_dpll = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009961 break;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009962 }
9963}
9964
Daniel Vetter26804af2014-06-25 22:01:55 +03009965static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009966 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009967{
9968 struct drm_device *dev = crtc->base.dev;
9969 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009970 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009971 enum port port;
9972 uint32_t tmp;
9973
9974 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9975
9976 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9977
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009978 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009979 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309980 else if (IS_BROXTON(dev))
9981 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009982 else
9983 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009984
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009985 if (pipe_config->shared_dpll >= 0) {
9986 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9987
9988 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9989 &pipe_config->dpll_hw_state));
9990 }
9991
Daniel Vetter26804af2014-06-25 22:01:55 +03009992 /*
9993 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9994 * DDI E. So just check whether this pipe is wired to DDI E and whether
9995 * the PCH transcoder is on.
9996 */
Damien Lespiauca370452013-12-03 13:56:24 +00009997 if (INTEL_INFO(dev)->gen < 9 &&
9998 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009999 pipe_config->has_pch_encoder = true;
10000
10001 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10002 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10003 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10004
10005 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10006 }
10007}
10008
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010009static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010010 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010011{
10012 struct drm_device *dev = crtc->base.dev;
10013 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010014 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010015 uint32_t tmp;
10016
Daniel Vetterf458ebb2014-09-30 10:56:39 +020010017 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +020010018 POWER_DOMAIN_PIPE(crtc->pipe)))
10019 return false;
10020
Daniel Vettere143a212013-07-04 12:01:15 +020010021 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010022 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
10023
Daniel Vettereccb1402013-05-22 00:50:22 +020010024 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10025 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10026 enum pipe trans_edp_pipe;
10027 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10028 default:
10029 WARN(1, "unknown pipe linked to edp transcoder\n");
10030 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10031 case TRANS_DDI_EDP_INPUT_A_ON:
10032 trans_edp_pipe = PIPE_A;
10033 break;
10034 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10035 trans_edp_pipe = PIPE_B;
10036 break;
10037 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10038 trans_edp_pipe = PIPE_C;
10039 break;
10040 }
10041
10042 if (trans_edp_pipe == crtc->pipe)
10043 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10044 }
10045
Daniel Vetterf458ebb2014-09-30 10:56:39 +020010046 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +020010047 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -030010048 return false;
10049
Daniel Vettereccb1402013-05-22 00:50:22 +020010050 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010051 if (!(tmp & PIPECONF_ENABLE))
10052 return false;
10053
Daniel Vetter26804af2014-06-25 22:01:55 +030010054 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010055
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010056 intel_get_pipe_timings(crtc, pipe_config);
10057
Chandra Kondurua1b22782015-04-07 15:28:45 -070010058 if (INTEL_INFO(dev)->gen >= 9) {
10059 skl_init_scalers(dev, crtc, pipe_config);
10060 }
10061
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010062 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010063
10064 if (INTEL_INFO(dev)->gen >= 9) {
10065 pipe_config->scaler_state.scaler_id = -1;
10066 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10067 }
10068
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010069 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010070 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010071 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010072 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010073 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010074 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010075
Jesse Barnese59150d2014-01-07 13:30:45 -080010076 if (IS_HASWELL(dev))
10077 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10078 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010079
Clint Taylorebb69c92014-09-30 10:30:22 -070010080 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
10081 pipe_config->pixel_multiplier =
10082 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10083 } else {
10084 pipe_config->pixel_multiplier = 1;
10085 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010086
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010087 return true;
10088}
10089
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010090static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10091 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010092{
10093 struct drm_device *dev = crtc->dev;
10094 struct drm_i915_private *dev_priv = dev->dev_private;
10095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010096 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010097
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010098 if (plane_state && plane_state->visible) {
10099 unsigned int width = plane_state->base.crtc_w;
10100 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010101 unsigned int stride = roundup_pow_of_two(width) * 4;
10102
10103 switch (stride) {
10104 default:
10105 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10106 width, stride);
10107 stride = 256;
10108 /* fallthrough */
10109 case 256:
10110 case 512:
10111 case 1024:
10112 case 2048:
10113 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010114 }
10115
Ville Syrjälädc41c152014-08-13 11:57:05 +030010116 cntl |= CURSOR_ENABLE |
10117 CURSOR_GAMMA_ENABLE |
10118 CURSOR_FORMAT_ARGB |
10119 CURSOR_STRIDE(stride);
10120
10121 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010122 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010123
Ville Syrjälädc41c152014-08-13 11:57:05 +030010124 if (intel_crtc->cursor_cntl != 0 &&
10125 (intel_crtc->cursor_base != base ||
10126 intel_crtc->cursor_size != size ||
10127 intel_crtc->cursor_cntl != cntl)) {
10128 /* On these chipsets we can only modify the base/size/stride
10129 * whilst the cursor is disabled.
10130 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010131 I915_WRITE(CURCNTR(PIPE_A), 0);
10132 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010133 intel_crtc->cursor_cntl = 0;
10134 }
10135
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010136 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010137 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010138 intel_crtc->cursor_base = base;
10139 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010140
10141 if (intel_crtc->cursor_size != size) {
10142 I915_WRITE(CURSIZE, size);
10143 intel_crtc->cursor_size = size;
10144 }
10145
Chris Wilson4b0e3332014-05-30 16:35:26 +030010146 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010147 I915_WRITE(CURCNTR(PIPE_A), cntl);
10148 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010149 intel_crtc->cursor_cntl = cntl;
10150 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010151}
10152
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010153static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10154 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010155{
10156 struct drm_device *dev = crtc->dev;
10157 struct drm_i915_private *dev_priv = dev->dev_private;
10158 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10159 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010160 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010161
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010162 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010163 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010164 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010165 case 64:
10166 cntl |= CURSOR_MODE_64_ARGB_AX;
10167 break;
10168 case 128:
10169 cntl |= CURSOR_MODE_128_ARGB_AX;
10170 break;
10171 case 256:
10172 cntl |= CURSOR_MODE_256_ARGB_AX;
10173 break;
10174 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010175 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010176 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010177 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010178 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010179
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010180 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010181 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010182
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010183 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10184 cntl |= CURSOR_ROTATE_180;
10185 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010186
Chris Wilson4b0e3332014-05-30 16:35:26 +030010187 if (intel_crtc->cursor_cntl != cntl) {
10188 I915_WRITE(CURCNTR(pipe), cntl);
10189 POSTING_READ(CURCNTR(pipe));
10190 intel_crtc->cursor_cntl = cntl;
10191 }
10192
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010193 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010194 I915_WRITE(CURBASE(pipe), base);
10195 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010196
10197 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010198}
10199
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010200/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010201static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010202 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010203{
10204 struct drm_device *dev = crtc->dev;
10205 struct drm_i915_private *dev_priv = dev->dev_private;
10206 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10207 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010208 u32 base = intel_crtc->cursor_addr;
10209 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010210
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010211 if (plane_state) {
10212 int x = plane_state->base.crtc_x;
10213 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010214
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010215 if (x < 0) {
10216 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10217 x = -x;
10218 }
10219 pos |= x << CURSOR_X_SHIFT;
10220
10221 if (y < 0) {
10222 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10223 y = -y;
10224 }
10225 pos |= y << CURSOR_Y_SHIFT;
10226
10227 /* ILK+ do this automagically */
10228 if (HAS_GMCH_DISPLAY(dev) &&
10229 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10230 base += (plane_state->base.crtc_h *
10231 plane_state->base.crtc_w - 1) * 4;
10232 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010233 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010234
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010235 I915_WRITE(CURPOS(pipe), pos);
10236
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010237 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010238 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010239 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010240 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010241}
10242
Ville Syrjälädc41c152014-08-13 11:57:05 +030010243static bool cursor_size_ok(struct drm_device *dev,
10244 uint32_t width, uint32_t height)
10245{
10246 if (width == 0 || height == 0)
10247 return false;
10248
10249 /*
10250 * 845g/865g are special in that they are only limited by
10251 * the width of their cursors, the height is arbitrary up to
10252 * the precision of the register. Everything else requires
10253 * square cursors, limited to a few power-of-two sizes.
10254 */
10255 if (IS_845G(dev) || IS_I865G(dev)) {
10256 if ((width & 63) != 0)
10257 return false;
10258
10259 if (width > (IS_845G(dev) ? 64 : 512))
10260 return false;
10261
10262 if (height > 1023)
10263 return false;
10264 } else {
10265 switch (width | height) {
10266 case 256:
10267 case 128:
10268 if (IS_GEN2(dev))
10269 return false;
10270 case 64:
10271 break;
10272 default:
10273 return false;
10274 }
10275 }
10276
10277 return true;
10278}
10279
Jesse Barnes79e53942008-11-07 14:24:08 -080010280static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010281 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010282{
James Simmons72034252010-08-03 01:33:19 +010010283 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010285
James Simmons72034252010-08-03 01:33:19 +010010286 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010287 intel_crtc->lut_r[i] = red[i] >> 8;
10288 intel_crtc->lut_g[i] = green[i] >> 8;
10289 intel_crtc->lut_b[i] = blue[i] >> 8;
10290 }
10291
10292 intel_crtc_load_lut(crtc);
10293}
10294
Jesse Barnes79e53942008-11-07 14:24:08 -080010295/* VESA 640x480x72Hz mode to set on the pipe */
10296static struct drm_display_mode load_detect_mode = {
10297 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10298 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10299};
10300
Daniel Vettera8bb6812014-02-10 18:00:39 +010010301struct drm_framebuffer *
10302__intel_framebuffer_create(struct drm_device *dev,
10303 struct drm_mode_fb_cmd2 *mode_cmd,
10304 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010305{
10306 struct intel_framebuffer *intel_fb;
10307 int ret;
10308
10309 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010310 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010311 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010312
10313 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010314 if (ret)
10315 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010316
10317 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010318
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010319err:
10320 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010321 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010322}
10323
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010324static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010325intel_framebuffer_create(struct drm_device *dev,
10326 struct drm_mode_fb_cmd2 *mode_cmd,
10327 struct drm_i915_gem_object *obj)
10328{
10329 struct drm_framebuffer *fb;
10330 int ret;
10331
10332 ret = i915_mutex_lock_interruptible(dev);
10333 if (ret)
10334 return ERR_PTR(ret);
10335 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10336 mutex_unlock(&dev->struct_mutex);
10337
10338 return fb;
10339}
10340
Chris Wilsond2dff872011-04-19 08:36:26 +010010341static u32
10342intel_framebuffer_pitch_for_width(int width, int bpp)
10343{
10344 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10345 return ALIGN(pitch, 64);
10346}
10347
10348static u32
10349intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10350{
10351 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010352 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010353}
10354
10355static struct drm_framebuffer *
10356intel_framebuffer_create_for_mode(struct drm_device *dev,
10357 struct drm_display_mode *mode,
10358 int depth, int bpp)
10359{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010360 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010361 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010362 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010363
10364 obj = i915_gem_alloc_object(dev,
10365 intel_framebuffer_size_for_mode(mode, bpp));
10366 if (obj == NULL)
10367 return ERR_PTR(-ENOMEM);
10368
10369 mode_cmd.width = mode->hdisplay;
10370 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010371 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10372 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010373 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010374
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010375 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10376 if (IS_ERR(fb))
10377 drm_gem_object_unreference_unlocked(&obj->base);
10378
10379 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010380}
10381
10382static struct drm_framebuffer *
10383mode_fits_in_fbdev(struct drm_device *dev,
10384 struct drm_display_mode *mode)
10385{
Daniel Vetter06957262015-08-10 13:34:08 +020010386#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010387 struct drm_i915_private *dev_priv = dev->dev_private;
10388 struct drm_i915_gem_object *obj;
10389 struct drm_framebuffer *fb;
10390
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010391 if (!dev_priv->fbdev)
10392 return NULL;
10393
10394 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010395 return NULL;
10396
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010397 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010398 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010399
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010400 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010401 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10402 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010403 return NULL;
10404
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010405 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010406 return NULL;
10407
10408 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010409#else
10410 return NULL;
10411#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010412}
10413
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010414static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10415 struct drm_crtc *crtc,
10416 struct drm_display_mode *mode,
10417 struct drm_framebuffer *fb,
10418 int x, int y)
10419{
10420 struct drm_plane_state *plane_state;
10421 int hdisplay, vdisplay;
10422 int ret;
10423
10424 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10425 if (IS_ERR(plane_state))
10426 return PTR_ERR(plane_state);
10427
10428 if (mode)
10429 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10430 else
10431 hdisplay = vdisplay = 0;
10432
10433 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10434 if (ret)
10435 return ret;
10436 drm_atomic_set_fb_for_plane(plane_state, fb);
10437 plane_state->crtc_x = 0;
10438 plane_state->crtc_y = 0;
10439 plane_state->crtc_w = hdisplay;
10440 plane_state->crtc_h = vdisplay;
10441 plane_state->src_x = x << 16;
10442 plane_state->src_y = y << 16;
10443 plane_state->src_w = hdisplay << 16;
10444 plane_state->src_h = vdisplay << 16;
10445
10446 return 0;
10447}
10448
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010449bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010450 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010451 struct intel_load_detect_pipe *old,
10452 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010453{
10454 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010455 struct intel_encoder *intel_encoder =
10456 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010457 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010458 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010459 struct drm_crtc *crtc = NULL;
10460 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010461 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010462 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010463 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010464 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010465 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010466 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010467
Chris Wilsond2dff872011-04-19 08:36:26 +010010468 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010469 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010470 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010471
Rob Clark51fd3712013-11-19 12:10:12 -050010472retry:
10473 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10474 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010475 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010476
Jesse Barnes79e53942008-11-07 14:24:08 -080010477 /*
10478 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010479 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010480 * - if the connector already has an assigned crtc, use it (but make
10481 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010482 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010483 * - try to find the first unused crtc that can drive this connector,
10484 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010485 */
10486
10487 /* See if we already have a CRTC for this connector */
10488 if (encoder->crtc) {
10489 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010490
Rob Clark51fd3712013-11-19 12:10:12 -050010491 ret = drm_modeset_lock(&crtc->mutex, ctx);
10492 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010493 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010494 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10495 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010496 goto fail;
Daniel Vetter7b240562012-12-12 00:35:33 +010010497
Daniel Vetter24218aa2012-08-12 19:27:11 +020010498 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010499 old->load_detect_temp = false;
10500
10501 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010502 if (connector->dpms != DRM_MODE_DPMS_ON)
10503 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010504
Chris Wilson71731882011-04-19 23:10:58 +010010505 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010506 }
10507
10508 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010509 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010510 i++;
10511 if (!(encoder->possible_crtcs & (1 << i)))
10512 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010513 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010514 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +030010515
10516 crtc = possible_crtc;
10517 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010518 }
10519
10520 /*
10521 * If we didn't find an unused CRTC, don't use any.
10522 */
10523 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010524 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010525 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010526 }
10527
Rob Clark51fd3712013-11-19 12:10:12 -050010528 ret = drm_modeset_lock(&crtc->mutex, ctx);
10529 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010530 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010531 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10532 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010533 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010534
10535 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +020010536 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010537 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010538 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010539
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010540 state = drm_atomic_state_alloc(dev);
10541 if (!state)
10542 return false;
10543
10544 state->acquire_ctx = ctx;
10545
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010546 connector_state = drm_atomic_get_connector_state(state, connector);
10547 if (IS_ERR(connector_state)) {
10548 ret = PTR_ERR(connector_state);
10549 goto fail;
10550 }
10551
10552 connector_state->crtc = crtc;
10553 connector_state->best_encoder = &intel_encoder->base;
10554
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010555 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10556 if (IS_ERR(crtc_state)) {
10557 ret = PTR_ERR(crtc_state);
10558 goto fail;
10559 }
10560
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010561 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010562
Chris Wilson64927112011-04-20 07:25:26 +010010563 if (!mode)
10564 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010565
Chris Wilsond2dff872011-04-19 08:36:26 +010010566 /* We need a framebuffer large enough to accommodate all accesses
10567 * that the plane may generate whilst we perform load detection.
10568 * We can not rely on the fbcon either being present (we get called
10569 * during its initialisation to detect all boot displays, or it may
10570 * not even exist) or that it is large enough to satisfy the
10571 * requested mode.
10572 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010573 fb = mode_fits_in_fbdev(dev, mode);
10574 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010575 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010576 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10577 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010578 } else
10579 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010580 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010581 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010582 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010583 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010584
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010585 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10586 if (ret)
10587 goto fail;
10588
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010589 drm_mode_copy(&crtc_state->base.mode, mode);
10590
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020010591 if (drm_atomic_commit(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010592 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010593 if (old->release_fb)
10594 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010595 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010596 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010597 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010598
Jesse Barnes79e53942008-11-07 14:24:08 -080010599 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010600 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010601 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010602
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010603fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010604 drm_atomic_state_free(state);
10605 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010606
Rob Clark51fd3712013-11-19 12:10:12 -050010607 if (ret == -EDEADLK) {
10608 drm_modeset_backoff(ctx);
10609 goto retry;
10610 }
10611
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010612 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010613}
10614
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010615void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010616 struct intel_load_detect_pipe *old,
10617 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010618{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010619 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010620 struct intel_encoder *intel_encoder =
10621 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010622 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010623 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010624 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010625 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010626 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010627 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010628 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010629
Chris Wilsond2dff872011-04-19 08:36:26 +010010630 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010631 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010632 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010633
Chris Wilson8261b192011-04-19 23:18:09 +010010634 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010635 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010636 if (!state)
10637 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010638
10639 state->acquire_ctx = ctx;
10640
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010641 connector_state = drm_atomic_get_connector_state(state, connector);
10642 if (IS_ERR(connector_state))
10643 goto fail;
10644
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010645 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10646 if (IS_ERR(crtc_state))
10647 goto fail;
10648
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010649 connector_state->best_encoder = NULL;
10650 connector_state->crtc = NULL;
10651
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010652 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010653
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010654 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10655 0, 0);
10656 if (ret)
10657 goto fail;
10658
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020010659 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010660 if (ret)
10661 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010662
Daniel Vetter36206362012-12-10 20:42:17 +010010663 if (old->release_fb) {
10664 drm_framebuffer_unregister_private(old->release_fb);
10665 drm_framebuffer_unreference(old->release_fb);
10666 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010667
Chris Wilson0622a532011-04-21 09:32:11 +010010668 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010669 }
10670
Eric Anholtc751ce42010-03-25 11:48:48 -070010671 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010672 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10673 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010674
10675 return;
10676fail:
10677 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10678 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010679}
10680
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010681static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010682 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010683{
10684 struct drm_i915_private *dev_priv = dev->dev_private;
10685 u32 dpll = pipe_config->dpll_hw_state.dpll;
10686
10687 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010688 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010689 else if (HAS_PCH_SPLIT(dev))
10690 return 120000;
10691 else if (!IS_GEN2(dev))
10692 return 96000;
10693 else
10694 return 48000;
10695}
10696
Jesse Barnes79e53942008-11-07 14:24:08 -080010697/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010698static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010699 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010700{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010701 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010702 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010703 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010704 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010705 u32 fp;
10706 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010707 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010708 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010709
10710 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010711 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010712 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010713 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010714
10715 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010716 if (IS_PINEVIEW(dev)) {
10717 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10718 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010719 } else {
10720 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10721 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10722 }
10723
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010724 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010725 if (IS_PINEVIEW(dev))
10726 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10727 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010728 else
10729 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010730 DPLL_FPA01_P1_POST_DIV_SHIFT);
10731
10732 switch (dpll & DPLL_MODE_MASK) {
10733 case DPLLB_MODE_DAC_SERIAL:
10734 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10735 5 : 10;
10736 break;
10737 case DPLLB_MODE_LVDS:
10738 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10739 7 : 14;
10740 break;
10741 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010742 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010743 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010744 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010745 }
10746
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010747 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010748 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010749 else
Imre Deakdccbea32015-06-22 23:35:51 +030010750 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010751 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010752 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010753 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010754
10755 if (is_lvds) {
10756 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10757 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010758
10759 if (lvds & LVDS_CLKB_POWER_UP)
10760 clock.p2 = 7;
10761 else
10762 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010763 } else {
10764 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10765 clock.p1 = 2;
10766 else {
10767 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10768 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10769 }
10770 if (dpll & PLL_P2_DIVIDE_BY_4)
10771 clock.p2 = 4;
10772 else
10773 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010774 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010775
Imre Deakdccbea32015-06-22 23:35:51 +030010776 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010777 }
10778
Ville Syrjälä18442d02013-09-13 16:00:08 +030010779 /*
10780 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010781 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010782 * encoder's get_config() function.
10783 */
Imre Deakdccbea32015-06-22 23:35:51 +030010784 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010785}
10786
Ville Syrjälä6878da02013-09-13 15:59:11 +030010787int intel_dotclock_calculate(int link_freq,
10788 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010789{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010790 /*
10791 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010792 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010793 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010794 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010795 *
10796 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010797 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010798 */
10799
Ville Syrjälä6878da02013-09-13 15:59:11 +030010800 if (!m_n->link_n)
10801 return 0;
10802
10803 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10804}
10805
Ville Syrjälä18442d02013-09-13 16:00:08 +030010806static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010807 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010808{
10809 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010810
10811 /* read out port_clock from the DPLL */
10812 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010813
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010814 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010815 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010816 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010817 * agree once we know their relationship in the encoder's
10818 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010819 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010820 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010821 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10822 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010823}
10824
10825/** Returns the currently programmed mode of the given pipe. */
10826struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10827 struct drm_crtc *crtc)
10828{
Jesse Barnes548f2452011-02-17 10:40:53 -080010829 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010831 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010832 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010833 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010834 int htot = I915_READ(HTOTAL(cpu_transcoder));
10835 int hsync = I915_READ(HSYNC(cpu_transcoder));
10836 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10837 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010838 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010839
10840 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10841 if (!mode)
10842 return NULL;
10843
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010844 /*
10845 * Construct a pipe_config sufficient for getting the clock info
10846 * back out of crtc_clock_get.
10847 *
10848 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10849 * to use a real value here instead.
10850 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010851 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010852 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010853 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10854 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10855 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010856 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10857
Ville Syrjälä773ae032013-09-23 17:48:20 +030010858 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010859 mode->hdisplay = (htot & 0xffff) + 1;
10860 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10861 mode->hsync_start = (hsync & 0xffff) + 1;
10862 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10863 mode->vdisplay = (vtot & 0xffff) + 1;
10864 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10865 mode->vsync_start = (vsync & 0xffff) + 1;
10866 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10867
10868 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010869
10870 return mode;
10871}
10872
Chris Wilsonf047e392012-07-21 12:31:41 +010010873void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010874{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010875 struct drm_i915_private *dev_priv = dev->dev_private;
10876
Chris Wilsonf62a0072014-02-21 17:55:39 +000010877 if (dev_priv->mm.busy)
10878 return;
10879
Paulo Zanoni43694d62014-03-07 20:08:08 -030010880 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010881 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010882 if (INTEL_INFO(dev)->gen >= 6)
10883 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010884 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010885}
10886
10887void intel_mark_idle(struct drm_device *dev)
10888{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010889 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010890
Chris Wilsonf62a0072014-02-21 17:55:39 +000010891 if (!dev_priv->mm.busy)
10892 return;
10893
10894 dev_priv->mm.busy = false;
10895
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010896 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010897 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010898
Paulo Zanoni43694d62014-03-07 20:08:08 -030010899 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010900}
10901
Jesse Barnes79e53942008-11-07 14:24:08 -080010902static void intel_crtc_destroy(struct drm_crtc *crtc)
10903{
10904 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010905 struct drm_device *dev = crtc->dev;
10906 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010907
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010908 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010909 work = intel_crtc->unpin_work;
10910 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010911 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010912
10913 if (work) {
10914 cancel_work_sync(&work->work);
10915 kfree(work);
10916 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010917
10918 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010919
Jesse Barnes79e53942008-11-07 14:24:08 -080010920 kfree(intel_crtc);
10921}
10922
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010923static void intel_unpin_work_fn(struct work_struct *__work)
10924{
10925 struct intel_unpin_work *work =
10926 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010927 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10928 struct drm_device *dev = crtc->base.dev;
10929 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010930
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010931 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010932 intel_unpin_fb_obj(work->old_fb, primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010933 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010934
John Harrisonf06cc1b2014-11-24 18:49:37 +000010935 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010936 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010937 mutex_unlock(&dev->struct_mutex);
10938
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010939 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010940 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010941
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010942 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10943 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010944
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010945 kfree(work);
10946}
10947
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010948static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010949 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010950{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10952 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010953 unsigned long flags;
10954
10955 /* Ignore early vblank irqs */
10956 if (intel_crtc == NULL)
10957 return;
10958
Daniel Vetterf3260382014-09-15 14:55:23 +020010959 /*
10960 * This is called both by irq handlers and the reset code (to complete
10961 * lost pageflips) so needs the full irqsave spinlocks.
10962 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010963 spin_lock_irqsave(&dev->event_lock, flags);
10964 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010965
10966 /* Ensure we don't miss a work->pending update ... */
10967 smp_rmb();
10968
10969 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010970 spin_unlock_irqrestore(&dev->event_lock, flags);
10971 return;
10972 }
10973
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010974 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010975
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010976 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010977}
10978
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010979void intel_finish_page_flip(struct drm_device *dev, int pipe)
10980{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010981 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010982 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10983
Mario Kleiner49b14a52010-12-09 07:00:07 +010010984 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010985}
10986
10987void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10988{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010989 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010990 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10991
Mario Kleiner49b14a52010-12-09 07:00:07 +010010992 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010993}
10994
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010995/* Is 'a' after or equal to 'b'? */
10996static bool g4x_flip_count_after_eq(u32 a, u32 b)
10997{
10998 return !((a - b) & 0x80000000);
10999}
11000
11001static bool page_flip_finished(struct intel_crtc *crtc)
11002{
11003 struct drm_device *dev = crtc->base.dev;
11004 struct drm_i915_private *dev_priv = dev->dev_private;
11005
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030011006 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
11007 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
11008 return true;
11009
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011010 /*
11011 * The relevant registers doen't exist on pre-ctg.
11012 * As the flip done interrupt doesn't trigger for mmio
11013 * flips on gmch platforms, a flip count check isn't
11014 * really needed there. But since ctg has the registers,
11015 * include it in the check anyway.
11016 */
11017 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11018 return true;
11019
11020 /*
11021 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11022 * used the same base address. In that case the mmio flip might
11023 * have completed, but the CS hasn't even executed the flip yet.
11024 *
11025 * A flip count check isn't enough as the CS might have updated
11026 * the base address just after start of vblank, but before we
11027 * managed to process the interrupt. This means we'd complete the
11028 * CS flip too soon.
11029 *
11030 * Combining both checks should get us a good enough result. It may
11031 * still happen that the CS flip has been executed, but has not
11032 * yet actually completed. But in case the base address is the same
11033 * anyway, we don't really care.
11034 */
11035 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11036 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030011037 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011038 crtc->unpin_work->flip_count);
11039}
11040
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011041void intel_prepare_page_flip(struct drm_device *dev, int plane)
11042{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011043 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011044 struct intel_crtc *intel_crtc =
11045 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
11046 unsigned long flags;
11047
Daniel Vetterf3260382014-09-15 14:55:23 +020011048
11049 /*
11050 * This is called both by irq handlers and the reset code (to complete
11051 * lost pageflips) so needs the full irqsave spinlocks.
11052 *
11053 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000011054 * generate a page-flip completion irq, i.e. every modeset
11055 * is also accompanied by a spurious intel_prepare_page_flip().
11056 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011057 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011058 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000011059 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011060 spin_unlock_irqrestore(&dev->event_lock, flags);
11061}
11062
Chris Wilson60426392015-10-10 10:44:32 +010011063static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011064{
11065 /* Ensure that the work item is consistent when activating it ... */
11066 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010011067 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011068 /* and that it is marked active as soon as the irq could fire. */
11069 smp_wmb();
11070}
11071
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011072static int intel_gen2_queue_flip(struct drm_device *dev,
11073 struct drm_crtc *crtc,
11074 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011075 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011076 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011077 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011078{
John Harrison6258fbe2015-05-29 17:43:48 +010011079 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011080 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011081 u32 flip_mask;
11082 int ret;
11083
John Harrison5fb9de12015-05-29 17:44:07 +010011084 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011085 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011086 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011087
11088 /* Can't queue multiple flips, so wait for the previous
11089 * one to finish before executing the next.
11090 */
11091 if (intel_crtc->plane)
11092 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11093 else
11094 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011095 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11096 intel_ring_emit(ring, MI_NOOP);
11097 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11098 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11099 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011100 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011101 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011102
Chris Wilson60426392015-10-10 10:44:32 +010011103 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011104 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011105}
11106
11107static int intel_gen3_queue_flip(struct drm_device *dev,
11108 struct drm_crtc *crtc,
11109 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011110 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011111 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011112 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011113{
John Harrison6258fbe2015-05-29 17:43:48 +010011114 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011115 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011116 u32 flip_mask;
11117 int ret;
11118
John Harrison5fb9de12015-05-29 17:44:07 +010011119 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011120 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011121 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011122
11123 if (intel_crtc->plane)
11124 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11125 else
11126 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011127 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11128 intel_ring_emit(ring, MI_NOOP);
11129 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11130 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11131 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011132 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011133 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011134
Chris Wilson60426392015-10-10 10:44:32 +010011135 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011136 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011137}
11138
11139static int intel_gen4_queue_flip(struct drm_device *dev,
11140 struct drm_crtc *crtc,
11141 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011142 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011143 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011144 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011145{
John Harrison6258fbe2015-05-29 17:43:48 +010011146 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011147 struct drm_i915_private *dev_priv = dev->dev_private;
11148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11149 uint32_t pf, pipesrc;
11150 int ret;
11151
John Harrison5fb9de12015-05-29 17:44:07 +010011152 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011153 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011154 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011155
11156 /* i965+ uses the linear or tiled offsets from the
11157 * Display Registers (which do not change across a page-flip)
11158 * so we need only reprogram the base address.
11159 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020011160 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11161 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11162 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011163 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011164 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011165
11166 /* XXX Enabling the panel-fitter across page-flip is so far
11167 * untested on non-native modes, so ignore it for now.
11168 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11169 */
11170 pf = 0;
11171 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011172 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011173
Chris Wilson60426392015-10-10 10:44:32 +010011174 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011175 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011176}
11177
11178static int intel_gen6_queue_flip(struct drm_device *dev,
11179 struct drm_crtc *crtc,
11180 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011181 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011182 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011183 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011184{
John Harrison6258fbe2015-05-29 17:43:48 +010011185 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011186 struct drm_i915_private *dev_priv = dev->dev_private;
11187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11188 uint32_t pf, pipesrc;
11189 int ret;
11190
John Harrison5fb9de12015-05-29 17:44:07 +010011191 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011192 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011193 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011194
Daniel Vetter6d90c952012-04-26 23:28:05 +020011195 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11196 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11197 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011198 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011199
Chris Wilson99d9acd2012-04-17 20:37:00 +010011200 /* Contrary to the suggestions in the documentation,
11201 * "Enable Panel Fitter" does not seem to be required when page
11202 * flipping with a non-native mode, and worse causes a normal
11203 * modeset to fail.
11204 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11205 */
11206 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011207 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011208 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011209
Chris Wilson60426392015-10-10 10:44:32 +010011210 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011211 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011212}
11213
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011214static int intel_gen7_queue_flip(struct drm_device *dev,
11215 struct drm_crtc *crtc,
11216 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011217 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011218 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011219 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011220{
John Harrison6258fbe2015-05-29 17:43:48 +010011221 struct intel_engine_cs *ring = req->ring;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011222 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011223 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011224 int len, ret;
11225
Robin Schroereba905b2014-05-18 02:24:50 +020011226 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011227 case PLANE_A:
11228 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11229 break;
11230 case PLANE_B:
11231 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11232 break;
11233 case PLANE_C:
11234 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11235 break;
11236 default:
11237 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011238 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011239 }
11240
Chris Wilsonffe74d72013-08-26 20:58:12 +010011241 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011242 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011243 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011244 /*
11245 * On Gen 8, SRM is now taking an extra dword to accommodate
11246 * 48bits addresses, and we need a NOOP for the batch size to
11247 * stay even.
11248 */
11249 if (IS_GEN8(dev))
11250 len += 2;
11251 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011252
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011253 /*
11254 * BSpec MI_DISPLAY_FLIP for IVB:
11255 * "The full packet must be contained within the same cache line."
11256 *
11257 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11258 * cacheline, if we ever start emitting more commands before
11259 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11260 * then do the cacheline alignment, and finally emit the
11261 * MI_DISPLAY_FLIP.
11262 */
John Harrisonbba09b12015-05-29 17:44:06 +010011263 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011264 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011265 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011266
John Harrison5fb9de12015-05-29 17:44:07 +010011267 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011268 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011269 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011270
Chris Wilsonffe74d72013-08-26 20:58:12 +010011271 /* Unmask the flip-done completion message. Note that the bspec says that
11272 * we should do this for both the BCS and RCS, and that we must not unmask
11273 * more than one flip event at any time (or ensure that one flip message
11274 * can be sent by waiting for flip-done prior to queueing new flips).
11275 * Experimentation says that BCS works despite DERRMR masking all
11276 * flip-done completion events and that unmasking all planes at once
11277 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11278 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11279 */
11280 if (ring->id == RCS) {
11281 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
Ville Syrjäläf92a9162015-11-04 23:20:07 +020011282 intel_ring_emit_reg(ring, DERRMR);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011283 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11284 DERRMR_PIPEB_PRI_FLIP_DONE |
11285 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011286 if (IS_GEN8(dev))
Arun Siluveryf1afe242015-08-04 16:22:20 +010011287 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011288 MI_SRM_LRM_GLOBAL_GTT);
11289 else
Arun Siluveryf1afe242015-08-04 16:22:20 +010011290 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011291 MI_SRM_LRM_GLOBAL_GTT);
Ville Syrjäläf92a9162015-11-04 23:20:07 +020011292 intel_ring_emit_reg(ring, DERRMR);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011293 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011294 if (IS_GEN8(dev)) {
11295 intel_ring_emit(ring, 0);
11296 intel_ring_emit(ring, MI_NOOP);
11297 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011298 }
11299
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011300 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011301 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011302 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011303 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011304
Chris Wilson60426392015-10-10 10:44:32 +010011305 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011306 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011307}
11308
Sourab Gupta84c33a62014-06-02 16:47:17 +053011309static bool use_mmio_flip(struct intel_engine_cs *ring,
11310 struct drm_i915_gem_object *obj)
11311{
11312 /*
11313 * This is not being used for older platforms, because
11314 * non-availability of flip done interrupt forces us to use
11315 * CS flips. Older platforms derive flip done using some clever
11316 * tricks involving the flip_pending status bits and vblank irqs.
11317 * So using MMIO flips there would disrupt this mechanism.
11318 */
11319
Chris Wilson8e09bf82014-07-08 10:40:30 +010011320 if (ring == NULL)
11321 return true;
11322
Sourab Gupta84c33a62014-06-02 16:47:17 +053011323 if (INTEL_INFO(ring->dev)->gen < 5)
11324 return false;
11325
11326 if (i915.use_mmio_flip < 0)
11327 return false;
11328 else if (i915.use_mmio_flip > 0)
11329 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011330 else if (i915.enable_execlists)
11331 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011332 else if (obj->base.dma_buf &&
11333 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11334 false))
11335 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011336 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011337 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011338}
11339
Chris Wilson60426392015-10-10 10:44:32 +010011340static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011341 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011342 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011343{
11344 struct drm_device *dev = intel_crtc->base.dev;
11345 struct drm_i915_private *dev_priv = dev->dev_private;
11346 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011347 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011348 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011349
11350 ctl = I915_READ(PLANE_CTL(pipe, 0));
11351 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011352 switch (fb->modifier[0]) {
11353 case DRM_FORMAT_MOD_NONE:
11354 break;
11355 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011356 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011357 break;
11358 case I915_FORMAT_MOD_Y_TILED:
11359 ctl |= PLANE_CTL_TILED_Y;
11360 break;
11361 case I915_FORMAT_MOD_Yf_TILED:
11362 ctl |= PLANE_CTL_TILED_YF;
11363 break;
11364 default:
11365 MISSING_CASE(fb->modifier[0]);
11366 }
Damien Lespiauff944562014-11-20 14:58:16 +000011367
11368 /*
11369 * The stride is either expressed as a multiple of 64 bytes chunks for
11370 * linear buffers or in number of tiles for tiled buffers.
11371 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011372 if (intel_rotation_90_or_270(rotation)) {
11373 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011374 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011375 stride = DIV_ROUND_UP(fb->height, tile_height);
11376 } else {
11377 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011378 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11379 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011380 }
Damien Lespiauff944562014-11-20 14:58:16 +000011381
11382 /*
11383 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11384 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11385 */
11386 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11387 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11388
Chris Wilson60426392015-10-10 10:44:32 +010011389 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011390 POSTING_READ(PLANE_SURF(pipe, 0));
11391}
11392
Chris Wilson60426392015-10-10 10:44:32 +010011393static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11394 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011395{
11396 struct drm_device *dev = intel_crtc->base.dev;
11397 struct drm_i915_private *dev_priv = dev->dev_private;
11398 struct intel_framebuffer *intel_fb =
11399 to_intel_framebuffer(intel_crtc->base.primary->fb);
11400 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011401 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011402 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011403
Sourab Gupta84c33a62014-06-02 16:47:17 +053011404 dspcntr = I915_READ(reg);
11405
Damien Lespiauc5d97472014-10-25 00:11:11 +010011406 if (obj->tiling_mode != I915_TILING_NONE)
11407 dspcntr |= DISPPLANE_TILED;
11408 else
11409 dspcntr &= ~DISPPLANE_TILED;
11410
Sourab Gupta84c33a62014-06-02 16:47:17 +053011411 I915_WRITE(reg, dspcntr);
11412
Chris Wilson60426392015-10-10 10:44:32 +010011413 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011414 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011415}
11416
11417/*
11418 * XXX: This is the temporary way to update the plane registers until we get
11419 * around to using the usual plane update functions for MMIO flips
11420 */
Chris Wilson60426392015-10-10 10:44:32 +010011421static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011422{
Chris Wilson60426392015-10-10 10:44:32 +010011423 struct intel_crtc *crtc = mmio_flip->crtc;
11424 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011425
Chris Wilson60426392015-10-10 10:44:32 +010011426 spin_lock_irq(&crtc->base.dev->event_lock);
11427 work = crtc->unpin_work;
11428 spin_unlock_irq(&crtc->base.dev->event_lock);
11429 if (work == NULL)
11430 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011431
Chris Wilson60426392015-10-10 10:44:32 +010011432 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011433
Chris Wilson60426392015-10-10 10:44:32 +010011434 intel_pipe_update_start(crtc);
11435
11436 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011437 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011438 else
11439 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011440 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011441
Chris Wilson60426392015-10-10 10:44:32 +010011442 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011443}
11444
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011445static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011446{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011447 struct intel_mmio_flip *mmio_flip =
11448 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011449 struct intel_framebuffer *intel_fb =
11450 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11451 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011452
Chris Wilson60426392015-10-10 10:44:32 +010011453 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011454 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011455 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011456 false, NULL,
11457 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011458 i915_gem_request_unreference__unlocked(mmio_flip->req);
11459 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011460
Alex Goinsfd8e0582015-11-25 18:43:38 -080011461 /* For framebuffer backed by dmabuf, wait for fence */
11462 if (obj->base.dma_buf)
11463 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11464 false, false,
11465 MAX_SCHEDULE_TIMEOUT) < 0);
11466
Chris Wilson60426392015-10-10 10:44:32 +010011467 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011468 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011469}
11470
11471static int intel_queue_mmio_flip(struct drm_device *dev,
11472 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011473 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011474{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011475 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011476
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011477 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11478 if (mmio_flip == NULL)
11479 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011480
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011481 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011482 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011483 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011484 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011485
11486 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11487 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011488
Sourab Gupta84c33a62014-06-02 16:47:17 +053011489 return 0;
11490}
11491
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011492static int intel_default_queue_flip(struct drm_device *dev,
11493 struct drm_crtc *crtc,
11494 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011495 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011496 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011497 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011498{
11499 return -ENODEV;
11500}
11501
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011502static bool __intel_pageflip_stall_check(struct drm_device *dev,
11503 struct drm_crtc *crtc)
11504{
11505 struct drm_i915_private *dev_priv = dev->dev_private;
11506 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11507 struct intel_unpin_work *work = intel_crtc->unpin_work;
11508 u32 addr;
11509
11510 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11511 return true;
11512
Chris Wilson908565c2015-08-12 13:08:22 +010011513 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11514 return false;
11515
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011516 if (!work->enable_stall_check)
11517 return false;
11518
11519 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011520 if (work->flip_queued_req &&
11521 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011522 return false;
11523
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011524 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011525 }
11526
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011527 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011528 return false;
11529
11530 /* Potential stall - if we see that the flip has happened,
11531 * assume a missed interrupt. */
11532 if (INTEL_INFO(dev)->gen >= 4)
11533 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11534 else
11535 addr = I915_READ(DSPADDR(intel_crtc->plane));
11536
11537 /* There is a potential issue here with a false positive after a flip
11538 * to the same address. We could address this by checking for a
11539 * non-incrementing frame counter.
11540 */
11541 return addr == work->gtt_offset;
11542}
11543
11544void intel_check_page_flip(struct drm_device *dev, int pipe)
11545{
11546 struct drm_i915_private *dev_priv = dev->dev_private;
11547 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11548 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011549 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011550
Dave Gordon6c51d462015-03-06 15:34:26 +000011551 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011552
11553 if (crtc == NULL)
11554 return;
11555
Daniel Vetterf3260382014-09-15 14:55:23 +020011556 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011557 work = intel_crtc->unpin_work;
11558 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011559 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011560 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011561 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011562 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011563 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011564 if (work != NULL &&
11565 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11566 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011567 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011568}
11569
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011570static int intel_crtc_page_flip(struct drm_crtc *crtc,
11571 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011572 struct drm_pending_vblank_event *event,
11573 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011574{
11575 struct drm_device *dev = crtc->dev;
11576 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011577 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011578 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011579 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011580 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011581 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011582 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011583 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011584 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011585 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011586 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011587
Matt Roper2ff8fde2014-07-08 07:50:07 -070011588 /*
11589 * drm_mode_page_flip_ioctl() should already catch this, but double
11590 * check to be safe. In the future we may enable pageflipping from
11591 * a disabled primary plane.
11592 */
11593 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11594 return -EBUSY;
11595
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011596 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011597 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011598 return -EINVAL;
11599
11600 /*
11601 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11602 * Note that pitch changes could also affect these register.
11603 */
11604 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011605 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11606 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011607 return -EINVAL;
11608
Chris Wilsonf900db42014-02-20 09:26:13 +000011609 if (i915_terminally_wedged(&dev_priv->gpu_error))
11610 goto out_hang;
11611
Daniel Vetterb14c5672013-09-19 12:18:32 +020011612 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011613 if (work == NULL)
11614 return -ENOMEM;
11615
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011616 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011617 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011618 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011619 INIT_WORK(&work->work, intel_unpin_work_fn);
11620
Daniel Vetter87b6b102014-05-15 15:33:46 +020011621 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011622 if (ret)
11623 goto free_work;
11624
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011625 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011626 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011627 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011628 /* Before declaring the flip queue wedged, check if
11629 * the hardware completed the operation behind our backs.
11630 */
11631 if (__intel_pageflip_stall_check(dev, crtc)) {
11632 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11633 page_flip_completed(intel_crtc);
11634 } else {
11635 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011636 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011637
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011638 drm_crtc_vblank_put(crtc);
11639 kfree(work);
11640 return -EBUSY;
11641 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011642 }
11643 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011644 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011645
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011646 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11647 flush_workqueue(dev_priv->wq);
11648
Jesse Barnes75dfca82010-02-10 15:09:44 -080011649 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011650 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011651 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011652
Matt Roperf4510a22014-04-01 15:22:40 -070011653 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011654 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011655
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011656 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011657
Chris Wilson89ed88b2015-02-16 14:31:49 +000011658 ret = i915_mutex_lock_interruptible(dev);
11659 if (ret)
11660 goto cleanup;
11661
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011662 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011663 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011664
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011665 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030011666 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011667
Wayne Boyer666a4532015-12-09 12:29:35 -080011668 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011669 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011670 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011671 /* vlv: DISPLAY_FLIP fails to change tiling */
11672 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011673 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011674 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011675 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011676 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011677 if (ring == NULL || ring->id != RCS)
11678 ring = &dev_priv->ring[BCS];
11679 } else {
11680 ring = &dev_priv->ring[RCS];
11681 }
11682
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011683 mmio_flip = use_mmio_flip(ring, obj);
11684
11685 /* When using CS flips, we want to emit semaphores between rings.
11686 * However, when using mmio flips we will create a task to do the
11687 * synchronisation, so all we want here is to pin the framebuffer
11688 * into the display plane and skip any waits.
11689 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011690 if (!mmio_flip) {
11691 ret = i915_gem_object_sync(obj, ring, &request);
11692 if (ret)
11693 goto cleanup_pending;
11694 }
11695
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011696 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011697 crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011698 if (ret)
11699 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011700
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011701 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11702 obj, 0);
11703 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011704
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011705 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011706 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011707 if (ret)
11708 goto cleanup_unpin;
11709
John Harrisonf06cc1b2014-11-24 18:49:37 +000011710 i915_gem_request_assign(&work->flip_queued_req,
11711 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011712 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011713 if (!request) {
11714 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11715 if (ret)
11716 goto cleanup_unpin;
11717 }
11718
11719 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011720 page_flip_flags);
11721 if (ret)
11722 goto cleanup_unpin;
11723
John Harrison6258fbe2015-05-29 17:43:48 +010011724 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011725 }
11726
John Harrison91af1272015-06-18 13:14:56 +010011727 if (request)
John Harrison75289872015-05-29 17:43:49 +010011728 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011729
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011730 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011731 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011732
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011733 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011734 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011735 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011736
Paulo Zanonid029bca2015-10-15 10:44:46 -030011737 intel_fbc_deactivate(intel_crtc);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011738 intel_frontbuffer_flip_prepare(dev,
11739 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011740
Jesse Barnese5510fa2010-07-01 16:48:37 -070011741 trace_i915_flip_request(intel_crtc->plane, obj);
11742
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011743 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011744
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011745cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011746 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011747cleanup_pending:
John Harrison91af1272015-06-18 13:14:56 +010011748 if (request)
11749 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011750 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011751 mutex_unlock(&dev->struct_mutex);
11752cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011753 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011754 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011755
Chris Wilson89ed88b2015-02-16 14:31:49 +000011756 drm_gem_object_unreference_unlocked(&obj->base);
11757 drm_framebuffer_unreference(work->old_fb);
11758
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011759 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011760 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011761 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011762
Daniel Vetter87b6b102014-05-15 15:33:46 +020011763 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011764free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011765 kfree(work);
11766
Chris Wilsonf900db42014-02-20 09:26:13 +000011767 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011768 struct drm_atomic_state *state;
11769 struct drm_plane_state *plane_state;
11770
Chris Wilsonf900db42014-02-20 09:26:13 +000011771out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011772 state = drm_atomic_state_alloc(dev);
11773 if (!state)
11774 return -ENOMEM;
11775 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11776
11777retry:
11778 plane_state = drm_atomic_get_plane_state(state, primary);
11779 ret = PTR_ERR_OR_ZERO(plane_state);
11780 if (!ret) {
11781 drm_atomic_set_fb_for_plane(plane_state, fb);
11782
11783 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11784 if (!ret)
11785 ret = drm_atomic_commit(state);
11786 }
11787
11788 if (ret == -EDEADLK) {
11789 drm_modeset_backoff(state->acquire_ctx);
11790 drm_atomic_state_clear(state);
11791 goto retry;
11792 }
11793
11794 if (ret)
11795 drm_atomic_state_free(state);
11796
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011797 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011798 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011799 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011800 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011801 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011802 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011803 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011804}
11805
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011806
11807/**
11808 * intel_wm_need_update - Check whether watermarks need updating
11809 * @plane: drm plane
11810 * @state: new plane state
11811 *
11812 * Check current plane state versus the new one to determine whether
11813 * watermarks need to be recalculated.
11814 *
11815 * Returns true or false.
11816 */
11817static bool intel_wm_need_update(struct drm_plane *plane,
11818 struct drm_plane_state *state)
11819{
Matt Roperd21fbe82015-09-24 15:53:12 -070011820 struct intel_plane_state *new = to_intel_plane_state(state);
11821 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11822
11823 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011824 if (new->visible != cur->visible)
11825 return true;
11826
11827 if (!cur->base.fb || !new->base.fb)
11828 return false;
11829
11830 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11831 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011832 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11833 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11834 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11835 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011836 return true;
11837
11838 return false;
11839}
11840
Matt Roperd21fbe82015-09-24 15:53:12 -070011841static bool needs_scaling(struct intel_plane_state *state)
11842{
11843 int src_w = drm_rect_width(&state->src) >> 16;
11844 int src_h = drm_rect_height(&state->src) >> 16;
11845 int dst_w = drm_rect_width(&state->dst);
11846 int dst_h = drm_rect_height(&state->dst);
11847
11848 return (src_w != dst_w || src_h != dst_h);
11849}
11850
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011851int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11852 struct drm_plane_state *plane_state)
11853{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011854 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011855 struct drm_crtc *crtc = crtc_state->crtc;
11856 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11857 struct drm_plane *plane = plane_state->plane;
11858 struct drm_device *dev = crtc->dev;
11859 struct drm_i915_private *dev_priv = dev->dev_private;
11860 struct intel_plane_state *old_plane_state =
11861 to_intel_plane_state(plane->state);
11862 int idx = intel_crtc->base.base.id, ret;
11863 int i = drm_plane_index(plane);
11864 bool mode_changed = needs_modeset(crtc_state);
11865 bool was_crtc_enabled = crtc->state->active;
11866 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011867 bool turn_off, turn_on, visible, was_visible;
11868 struct drm_framebuffer *fb = plane_state->fb;
11869
11870 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11871 plane->type != DRM_PLANE_TYPE_CURSOR) {
11872 ret = skl_update_scaler_plane(
11873 to_intel_crtc_state(crtc_state),
11874 to_intel_plane_state(plane_state));
11875 if (ret)
11876 return ret;
11877 }
11878
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011879 was_visible = old_plane_state->visible;
11880 visible = to_intel_plane_state(plane_state)->visible;
11881
11882 if (!was_crtc_enabled && WARN_ON(was_visible))
11883 was_visible = false;
11884
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011885 /*
11886 * Visibility is calculated as if the crtc was on, but
11887 * after scaler setup everything depends on it being off
11888 * when the crtc isn't active.
11889 */
11890 if (!is_crtc_enabled)
11891 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011892
11893 if (!was_visible && !visible)
11894 return 0;
11895
11896 turn_off = was_visible && (!visible || mode_changed);
11897 turn_on = visible && (!was_visible || mode_changed);
11898
11899 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11900 plane->base.id, fb ? fb->base.id : -1);
11901
11902 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11903 plane->base.id, was_visible, visible,
11904 turn_off, turn_on, mode_changed);
11905
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011906 if (turn_on || turn_off) {
11907 pipe_config->wm_changed = true;
11908
Ville Syrjälä852eb002015-06-24 22:00:07 +030011909 /* must disable cxsr around plane enable/disable */
11910 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11911 if (is_crtc_enabled)
11912 intel_crtc->atomic.wait_vblank = true;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011913 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011914 }
11915 } else if (intel_wm_need_update(plane, plane_state)) {
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011916 pipe_config->wm_changed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011917 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011918
Matt Roper396e33a2016-01-06 11:34:30 -080011919 /* Pre-gen9 platforms need two-step watermark updates */
11920 if (pipe_config->wm_changed && INTEL_INFO(dev)->gen < 9 &&
11921 dev_priv->display.optimize_watermarks)
11922 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11923
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011924 if (visible || was_visible)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011925 intel_crtc->atomic.fb_bits |=
11926 to_intel_plane(plane)->frontbuffer_bit;
11927
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011928 switch (plane->type) {
11929 case DRM_PLANE_TYPE_PRIMARY:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011930 intel_crtc->atomic.pre_disable_primary = turn_off;
11931 intel_crtc->atomic.post_enable_primary = turn_on;
11932
Rodrigo Vivi066cf552015-06-26 13:55:54 -070011933 if (turn_off) {
11934 /*
11935 * FIXME: Actually if we will still have any other
11936 * plane enabled on the pipe we could let IPS enabled
11937 * still, but for now lets consider that when we make
11938 * primary invisible by setting DSPCNTR to 0 on
11939 * update_primary_plane function IPS needs to be
11940 * disable.
11941 */
11942 intel_crtc->atomic.disable_ips = true;
11943
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011944 intel_crtc->atomic.disable_fbc = true;
Rodrigo Vivi066cf552015-06-26 13:55:54 -070011945 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011946
11947 /*
11948 * FBC does not work on some platforms for rotated
11949 * planes, so disable it when rotation is not 0 and
11950 * update it when rotation is set back to 0.
11951 *
11952 * FIXME: This is redundant with the fbc update done in
11953 * the primary plane enable function except that that
11954 * one is done too late. We eventually need to unify
11955 * this.
11956 */
11957
11958 if (visible &&
11959 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11960 dev_priv->fbc.crtc == intel_crtc &&
11961 plane_state->rotation != BIT(DRM_ROTATE_0))
11962 intel_crtc->atomic.disable_fbc = true;
11963
11964 /*
11965 * BDW signals flip done immediately if the plane
11966 * is disabled, even if the plane enable is already
11967 * armed to occur at the next vblank :(
11968 */
11969 if (turn_on && IS_BROADWELL(dev))
11970 intel_crtc->atomic.wait_vblank = true;
11971
11972 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11973 break;
11974 case DRM_PLANE_TYPE_CURSOR:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011975 break;
11976 case DRM_PLANE_TYPE_OVERLAY:
Matt Roperd21fbe82015-09-24 15:53:12 -070011977 /*
11978 * WaCxSRDisabledForSpriteScaling:ivb
11979 *
11980 * cstate->update_wm was already set above, so this flag will
11981 * take effect when we commit and program watermarks.
11982 */
11983 if (IS_IVYBRIDGE(dev) &&
11984 needs_scaling(to_intel_plane_state(plane_state)) &&
11985 !needs_scaling(old_plane_state)) {
11986 to_intel_crtc_state(crtc_state)->disable_lp_wm = true;
11987 } else if (turn_off && !mode_changed) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011988 intel_crtc->atomic.wait_vblank = true;
11989 intel_crtc->atomic.update_sprite_watermarks |=
11990 1 << i;
11991 }
Matt Roperd21fbe82015-09-24 15:53:12 -070011992
11993 break;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011994 }
11995 return 0;
11996}
11997
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011998static bool encoders_cloneable(const struct intel_encoder *a,
11999 const struct intel_encoder *b)
12000{
12001 /* masks could be asymmetric, so check both ways */
12002 return a == b || (a->cloneable & (1 << b->type) &&
12003 b->cloneable & (1 << a->type));
12004}
12005
12006static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12007 struct intel_crtc *crtc,
12008 struct intel_encoder *encoder)
12009{
12010 struct intel_encoder *source_encoder;
12011 struct drm_connector *connector;
12012 struct drm_connector_state *connector_state;
12013 int i;
12014
12015 for_each_connector_in_state(state, connector, connector_state, i) {
12016 if (connector_state->crtc != &crtc->base)
12017 continue;
12018
12019 source_encoder =
12020 to_intel_encoder(connector_state->best_encoder);
12021 if (!encoders_cloneable(encoder, source_encoder))
12022 return false;
12023 }
12024
12025 return true;
12026}
12027
12028static bool check_encoder_cloning(struct drm_atomic_state *state,
12029 struct intel_crtc *crtc)
12030{
12031 struct intel_encoder *encoder;
12032 struct drm_connector *connector;
12033 struct drm_connector_state *connector_state;
12034 int i;
12035
12036 for_each_connector_in_state(state, connector, connector_state, i) {
12037 if (connector_state->crtc != &crtc->base)
12038 continue;
12039
12040 encoder = to_intel_encoder(connector_state->best_encoder);
12041 if (!check_single_encoder_cloning(state, crtc, encoder))
12042 return false;
12043 }
12044
12045 return true;
12046}
12047
12048static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12049 struct drm_crtc_state *crtc_state)
12050{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012051 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012052 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012054 struct intel_crtc_state *pipe_config =
12055 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012056 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012057 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012058 bool mode_changed = needs_modeset(crtc_state);
12059
12060 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12061 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12062 return -EINVAL;
12063 }
12064
Ville Syrjälä852eb002015-06-24 22:00:07 +030012065 if (mode_changed && !crtc_state->active)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012066 pipe_config->wm_changed = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012067
Maarten Lankhorstad421372015-06-15 12:33:42 +020012068 if (mode_changed && crtc_state->enable &&
12069 dev_priv->display.crtc_compute_clock &&
12070 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
12071 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12072 pipe_config);
12073 if (ret)
12074 return ret;
12075 }
12076
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012077 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012078 if (dev_priv->display.compute_pipe_wm) {
12079 ret = dev_priv->display.compute_pipe_wm(intel_crtc, state);
Matt Roper396e33a2016-01-06 11:34:30 -080012080 if (ret) {
12081 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012082 return ret;
Matt Roper396e33a2016-01-06 11:34:30 -080012083 }
12084 }
12085
12086 if (dev_priv->display.compute_intermediate_wm &&
12087 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12088 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12089 return 0;
12090
12091 /*
12092 * Calculate 'intermediate' watermarks that satisfy both the
12093 * old state and the new state. We can program these
12094 * immediately.
12095 */
12096 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12097 intel_crtc,
12098 pipe_config);
12099 if (ret) {
12100 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12101 return ret;
12102 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070012103 }
12104
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012105 if (INTEL_INFO(dev)->gen >= 9) {
12106 if (mode_changed)
12107 ret = skl_update_scaler_crtc(pipe_config);
12108
12109 if (!ret)
12110 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12111 pipe_config);
12112 }
12113
12114 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012115}
12116
Jani Nikula65b38e02015-04-13 11:26:56 +030012117static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012118 .mode_set_base_atomic = intel_pipe_set_base_atomic,
12119 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080012120 .atomic_begin = intel_begin_crtc_commit,
12121 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012122 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012123};
12124
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012125static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12126{
12127 struct intel_connector *connector;
12128
12129 for_each_intel_connector(dev, connector) {
12130 if (connector->base.encoder) {
12131 connector->base.state->best_encoder =
12132 connector->base.encoder;
12133 connector->base.state->crtc =
12134 connector->base.encoder->crtc;
12135 } else {
12136 connector->base.state->best_encoder = NULL;
12137 connector->base.state->crtc = NULL;
12138 }
12139 }
12140}
12141
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012142static void
Robin Schroereba905b2014-05-18 02:24:50 +020012143connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012144 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012145{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012146 int bpp = pipe_config->pipe_bpp;
12147
12148 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12149 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012150 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012151
12152 /* Don't use an invalid EDID bpc value */
12153 if (connector->base.display_info.bpc &&
12154 connector->base.display_info.bpc * 3 < bpp) {
12155 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12156 bpp, connector->base.display_info.bpc*3);
12157 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12158 }
12159
12160 /* Clamp bpp to 8 on screens without EDID 1.4 */
12161 if (connector->base.display_info.bpc == 0 && bpp > 24) {
12162 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
12163 bpp);
12164 pipe_config->pipe_bpp = 24;
12165 }
12166}
12167
12168static int
12169compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012170 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012171{
12172 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012173 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012174 struct drm_connector *connector;
12175 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012176 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012177
Wayne Boyer666a4532015-12-09 12:29:35 -080012178 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012179 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012180 else if (INTEL_INFO(dev)->gen >= 5)
12181 bpp = 12*3;
12182 else
12183 bpp = 8*3;
12184
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012185
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012186 pipe_config->pipe_bpp = bpp;
12187
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012188 state = pipe_config->base.state;
12189
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012190 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012191 for_each_connector_in_state(state, connector, connector_state, i) {
12192 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012193 continue;
12194
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012195 connected_sink_compute_bpp(to_intel_connector(connector),
12196 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012197 }
12198
12199 return bpp;
12200}
12201
Daniel Vetter644db712013-09-19 14:53:58 +020012202static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12203{
12204 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12205 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012206 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012207 mode->crtc_hdisplay, mode->crtc_hsync_start,
12208 mode->crtc_hsync_end, mode->crtc_htotal,
12209 mode->crtc_vdisplay, mode->crtc_vsync_start,
12210 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12211}
12212
Daniel Vetterc0b03412013-05-28 12:05:54 +020012213static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012214 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012215 const char *context)
12216{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012217 struct drm_device *dev = crtc->base.dev;
12218 struct drm_plane *plane;
12219 struct intel_plane *intel_plane;
12220 struct intel_plane_state *state;
12221 struct drm_framebuffer *fb;
12222
12223 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12224 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012225
12226 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
12227 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12228 pipe_config->pipe_bpp, pipe_config->dither);
12229 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12230 pipe_config->has_pch_encoder,
12231 pipe_config->fdi_lanes,
12232 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12233 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12234 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012235 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 +030012236 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012237 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012238 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12239 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12240 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012241
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012242 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 -070012243 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012244 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012245 pipe_config->dp_m2_n2.gmch_m,
12246 pipe_config->dp_m2_n2.gmch_n,
12247 pipe_config->dp_m2_n2.link_m,
12248 pipe_config->dp_m2_n2.link_n,
12249 pipe_config->dp_m2_n2.tu);
12250
Daniel Vetter55072d12014-11-20 16:10:28 +010012251 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12252 pipe_config->has_audio,
12253 pipe_config->has_infoframe);
12254
Daniel Vetterc0b03412013-05-28 12:05:54 +020012255 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012256 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012257 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012258 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12259 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012260 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012261 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12262 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012263 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12264 crtc->num_scalers,
12265 pipe_config->scaler_state.scaler_users,
12266 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012267 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12268 pipe_config->gmch_pfit.control,
12269 pipe_config->gmch_pfit.pgm_ratios,
12270 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012271 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012272 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012273 pipe_config->pch_pfit.size,
12274 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012275 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012276 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012277
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012278 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012279 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012280 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012281 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012282 pipe_config->ddi_pll_sel,
12283 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012284 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012285 pipe_config->dpll_hw_state.pll0,
12286 pipe_config->dpll_hw_state.pll1,
12287 pipe_config->dpll_hw_state.pll2,
12288 pipe_config->dpll_hw_state.pll3,
12289 pipe_config->dpll_hw_state.pll6,
12290 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012291 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012292 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012293 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012294 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012295 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12296 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12297 pipe_config->ddi_pll_sel,
12298 pipe_config->dpll_hw_state.ctrl1,
12299 pipe_config->dpll_hw_state.cfgcr1,
12300 pipe_config->dpll_hw_state.cfgcr2);
12301 } else if (HAS_DDI(dev)) {
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012302 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012303 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012304 pipe_config->dpll_hw_state.wrpll,
12305 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012306 } else {
12307 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12308 "fp0: 0x%x, fp1: 0x%x\n",
12309 pipe_config->dpll_hw_state.dpll,
12310 pipe_config->dpll_hw_state.dpll_md,
12311 pipe_config->dpll_hw_state.fp0,
12312 pipe_config->dpll_hw_state.fp1);
12313 }
12314
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012315 DRM_DEBUG_KMS("planes on this crtc\n");
12316 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12317 intel_plane = to_intel_plane(plane);
12318 if (intel_plane->pipe != crtc->pipe)
12319 continue;
12320
12321 state = to_intel_plane_state(plane->state);
12322 fb = state->base.fb;
12323 if (!fb) {
12324 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12325 "disabled, scaler_id = %d\n",
12326 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12327 plane->base.id, intel_plane->pipe,
12328 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12329 drm_plane_index(plane), state->scaler_id);
12330 continue;
12331 }
12332
12333 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12334 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12335 plane->base.id, intel_plane->pipe,
12336 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12337 drm_plane_index(plane));
12338 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12339 fb->base.id, fb->width, fb->height, fb->pixel_format);
12340 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12341 state->scaler_id,
12342 state->src.x1 >> 16, state->src.y1 >> 16,
12343 drm_rect_width(&state->src) >> 16,
12344 drm_rect_height(&state->src) >> 16,
12345 state->dst.x1, state->dst.y1,
12346 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12347 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012348}
12349
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012350static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012351{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012352 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012353 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012354 unsigned int used_ports = 0;
12355
12356 /*
12357 * Walk the connector list instead of the encoder
12358 * list to detect the problem on ddi platforms
12359 * where there's just one encoder per digital port.
12360 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012361 drm_for_each_connector(connector, dev) {
12362 struct drm_connector_state *connector_state;
12363 struct intel_encoder *encoder;
12364
12365 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12366 if (!connector_state)
12367 connector_state = connector->state;
12368
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012369 if (!connector_state->best_encoder)
12370 continue;
12371
12372 encoder = to_intel_encoder(connector_state->best_encoder);
12373
12374 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012375
12376 switch (encoder->type) {
12377 unsigned int port_mask;
12378 case INTEL_OUTPUT_UNKNOWN:
12379 if (WARN_ON(!HAS_DDI(dev)))
12380 break;
12381 case INTEL_OUTPUT_DISPLAYPORT:
12382 case INTEL_OUTPUT_HDMI:
12383 case INTEL_OUTPUT_EDP:
12384 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12385
12386 /* the same port mustn't appear more than once */
12387 if (used_ports & port_mask)
12388 return false;
12389
12390 used_ports |= port_mask;
12391 default:
12392 break;
12393 }
12394 }
12395
12396 return true;
12397}
12398
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012399static void
12400clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12401{
12402 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012403 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012404 struct intel_dpll_hw_state dpll_hw_state;
12405 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012406 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012407 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012408
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012409 /* FIXME: before the switch to atomic started, a new pipe_config was
12410 * kzalloc'd. Code that depends on any field being zero should be
12411 * fixed, so that the crtc_state can be safely duplicated. For now,
12412 * only fields that are know to not cause problems are preserved. */
12413
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012414 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012415 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012416 shared_dpll = crtc_state->shared_dpll;
12417 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012418 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012419 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012420
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012421 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012422
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012423 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012424 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012425 crtc_state->shared_dpll = shared_dpll;
12426 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012427 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012428 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012429}
12430
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012431static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012432intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012433 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012434{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012435 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012436 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012437 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012438 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012439 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012440 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012441 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012442
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012443 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012444
Daniel Vettere143a212013-07-04 12:01:15 +020012445 pipe_config->cpu_transcoder =
12446 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012447
Imre Deak2960bc92013-07-30 13:36:32 +030012448 /*
12449 * Sanitize sync polarity flags based on requested ones. If neither
12450 * positive or negative polarity is requested, treat this as meaning
12451 * negative polarity.
12452 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012453 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012454 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012455 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012456
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012457 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012458 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012459 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012460
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012461 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12462 pipe_config);
12463 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012464 goto fail;
12465
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012466 /*
12467 * Determine the real pipe dimensions. Note that stereo modes can
12468 * increase the actual pipe size due to the frame doubling and
12469 * insertion of additional space for blanks between the frame. This
12470 * is stored in the crtc timings. We use the requested mode to do this
12471 * computation to clearly distinguish it from the adjusted mode, which
12472 * can be changed by the connectors in the below retry loop.
12473 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012474 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012475 &pipe_config->pipe_src_w,
12476 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012477
Daniel Vettere29c22c2013-02-21 00:00:16 +010012478encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012479 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012480 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012481 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012482
Daniel Vetter135c81b2013-07-21 21:37:09 +020012483 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012484 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12485 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012486
Daniel Vetter7758a112012-07-08 19:40:39 +020012487 /* Pass our mode to the connectors and the CRTC to give them a chance to
12488 * adjust it according to limitations or connector properties, and also
12489 * a chance to reject the mode entirely.
12490 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012491 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012492 if (connector_state->crtc != crtc)
12493 continue;
12494
12495 encoder = to_intel_encoder(connector_state->best_encoder);
12496
Daniel Vetterefea6e82013-07-21 21:36:59 +020012497 if (!(encoder->compute_config(encoder, pipe_config))) {
12498 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012499 goto fail;
12500 }
12501 }
12502
Daniel Vetterff9a6752013-06-01 17:16:21 +020012503 /* Set default port clock if not overwritten by the encoder. Needs to be
12504 * done afterwards in case the encoder adjusts the mode. */
12505 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012506 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012507 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012508
Daniel Vettera43f6e02013-06-07 23:10:32 +020012509 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012510 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012511 DRM_DEBUG_KMS("CRTC fixup failed\n");
12512 goto fail;
12513 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012514
12515 if (ret == RETRY) {
12516 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12517 ret = -EINVAL;
12518 goto fail;
12519 }
12520
12521 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12522 retry = false;
12523 goto encoder_retry;
12524 }
12525
Daniel Vettere8fa4272015-08-12 11:43:34 +020012526 /* Dithering seems to not pass-through bits correctly when it should, so
12527 * only enable it on 6bpc panels. */
12528 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012529 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012530 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012531
Daniel Vetter7758a112012-07-08 19:40:39 +020012532fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012533 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012534}
12535
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012536static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012537intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012538{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012539 struct drm_crtc *crtc;
12540 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012541 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012542
Ville Syrjälä76688512014-01-10 11:28:06 +020012543 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012544 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012545 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012546
12547 /* Update hwmode for vblank functions */
12548 if (crtc->state->active)
12549 crtc->hwmode = crtc->state->adjusted_mode;
12550 else
12551 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012552
12553 /*
12554 * Update legacy state to satisfy fbc code. This can
12555 * be removed when fbc uses the atomic state.
12556 */
12557 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12558 struct drm_plane_state *plane_state = crtc->primary->state;
12559
12560 crtc->primary->fb = plane_state->fb;
12561 crtc->x = plane_state->src_x >> 16;
12562 crtc->y = plane_state->src_y >> 16;
12563 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012564 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012565}
12566
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012567static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012568{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012569 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012570
12571 if (clock1 == clock2)
12572 return true;
12573
12574 if (!clock1 || !clock2)
12575 return false;
12576
12577 diff = abs(clock1 - clock2);
12578
12579 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12580 return true;
12581
12582 return false;
12583}
12584
Daniel Vetter25c5b262012-07-08 22:08:04 +020012585#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12586 list_for_each_entry((intel_crtc), \
12587 &(dev)->mode_config.crtc_list, \
12588 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012589 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012590
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012591static bool
12592intel_compare_m_n(unsigned int m, unsigned int n,
12593 unsigned int m2, unsigned int n2,
12594 bool exact)
12595{
12596 if (m == m2 && n == n2)
12597 return true;
12598
12599 if (exact || !m || !n || !m2 || !n2)
12600 return false;
12601
12602 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12603
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012604 if (n > n2) {
12605 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012606 m2 <<= 1;
12607 n2 <<= 1;
12608 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012609 } else if (n < n2) {
12610 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012611 m <<= 1;
12612 n <<= 1;
12613 }
12614 }
12615
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012616 if (n != n2)
12617 return false;
12618
12619 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012620}
12621
12622static bool
12623intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12624 struct intel_link_m_n *m2_n2,
12625 bool adjust)
12626{
12627 if (m_n->tu == m2_n2->tu &&
12628 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12629 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12630 intel_compare_m_n(m_n->link_m, m_n->link_n,
12631 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12632 if (adjust)
12633 *m2_n2 = *m_n;
12634
12635 return true;
12636 }
12637
12638 return false;
12639}
12640
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012641static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012642intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012643 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012644 struct intel_crtc_state *pipe_config,
12645 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012646{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012647 bool ret = true;
12648
12649#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12650 do { \
12651 if (!adjust) \
12652 DRM_ERROR(fmt, ##__VA_ARGS__); \
12653 else \
12654 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12655 } while (0)
12656
Daniel Vetter66e985c2013-06-05 13:34:20 +020012657#define PIPE_CONF_CHECK_X(name) \
12658 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012659 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012660 "(expected 0x%08x, found 0x%08x)\n", \
12661 current_config->name, \
12662 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012663 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012664 }
12665
Daniel Vetter08a24032013-04-19 11:25:34 +020012666#define PIPE_CONF_CHECK_I(name) \
12667 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012668 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012669 "(expected %i, found %i)\n", \
12670 current_config->name, \
12671 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012672 ret = false; \
12673 }
12674
12675#define PIPE_CONF_CHECK_M_N(name) \
12676 if (!intel_compare_link_m_n(&current_config->name, \
12677 &pipe_config->name,\
12678 adjust)) { \
12679 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12680 "(expected tu %i gmch %i/%i link %i/%i, " \
12681 "found tu %i, gmch %i/%i link %i/%i)\n", \
12682 current_config->name.tu, \
12683 current_config->name.gmch_m, \
12684 current_config->name.gmch_n, \
12685 current_config->name.link_m, \
12686 current_config->name.link_n, \
12687 pipe_config->name.tu, \
12688 pipe_config->name.gmch_m, \
12689 pipe_config->name.gmch_n, \
12690 pipe_config->name.link_m, \
12691 pipe_config->name.link_n); \
12692 ret = false; \
12693 }
12694
12695#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12696 if (!intel_compare_link_m_n(&current_config->name, \
12697 &pipe_config->name, adjust) && \
12698 !intel_compare_link_m_n(&current_config->alt_name, \
12699 &pipe_config->name, adjust)) { \
12700 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12701 "(expected tu %i gmch %i/%i link %i/%i, " \
12702 "or tu %i gmch %i/%i link %i/%i, " \
12703 "found tu %i, gmch %i/%i link %i/%i)\n", \
12704 current_config->name.tu, \
12705 current_config->name.gmch_m, \
12706 current_config->name.gmch_n, \
12707 current_config->name.link_m, \
12708 current_config->name.link_n, \
12709 current_config->alt_name.tu, \
12710 current_config->alt_name.gmch_m, \
12711 current_config->alt_name.gmch_n, \
12712 current_config->alt_name.link_m, \
12713 current_config->alt_name.link_n, \
12714 pipe_config->name.tu, \
12715 pipe_config->name.gmch_m, \
12716 pipe_config->name.gmch_n, \
12717 pipe_config->name.link_m, \
12718 pipe_config->name.link_n); \
12719 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012720 }
12721
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012722/* This is required for BDW+ where there is only one set of registers for
12723 * switching between high and low RR.
12724 * This macro can be used whenever a comparison has to be made between one
12725 * hw state and multiple sw state variables.
12726 */
12727#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12728 if ((current_config->name != pipe_config->name) && \
12729 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012730 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012731 "(expected %i or %i, found %i)\n", \
12732 current_config->name, \
12733 current_config->alt_name, \
12734 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012735 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012736 }
12737
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012738#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12739 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012740 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012741 "(expected %i, found %i)\n", \
12742 current_config->name & (mask), \
12743 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012744 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012745 }
12746
Ville Syrjälä5e550652013-09-06 23:29:07 +030012747#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12748 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012749 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012750 "(expected %i, found %i)\n", \
12751 current_config->name, \
12752 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012753 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012754 }
12755
Daniel Vetterbb760062013-06-06 14:55:52 +020012756#define PIPE_CONF_QUIRK(quirk) \
12757 ((current_config->quirks | pipe_config->quirks) & (quirk))
12758
Daniel Vettereccb1402013-05-22 00:50:22 +020012759 PIPE_CONF_CHECK_I(cpu_transcoder);
12760
Daniel Vetter08a24032013-04-19 11:25:34 +020012761 PIPE_CONF_CHECK_I(has_pch_encoder);
12762 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012763 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012764
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012765 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012766 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012767
12768 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012769 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012770
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012771 if (current_config->has_drrs)
12772 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12773 } else
12774 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012775
Jani Nikulaa65347b2015-11-27 12:21:46 +020012776 PIPE_CONF_CHECK_I(has_dsi_encoder);
12777
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012778 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12779 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12780 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12781 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12782 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12783 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012784
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012785 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12786 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12787 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12788 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12789 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12790 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012791
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012792 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012793 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012794 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012795 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012796 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012797 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012798
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012799 PIPE_CONF_CHECK_I(has_audio);
12800
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012801 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012802 DRM_MODE_FLAG_INTERLACE);
12803
Daniel Vetterbb760062013-06-06 14:55:52 +020012804 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012805 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012806 DRM_MODE_FLAG_PHSYNC);
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_NHSYNC);
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_PVSYNC);
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_NVSYNC);
12813 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012814
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012815 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012816 /* pfit ratios are autocomputed by the hw on gen4+ */
12817 if (INTEL_INFO(dev)->gen < 4)
12818 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012819 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012820
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012821 if (!adjust) {
12822 PIPE_CONF_CHECK_I(pipe_src_w);
12823 PIPE_CONF_CHECK_I(pipe_src_h);
12824
12825 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12826 if (current_config->pch_pfit.enabled) {
12827 PIPE_CONF_CHECK_X(pch_pfit.pos);
12828 PIPE_CONF_CHECK_X(pch_pfit.size);
12829 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012830
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012831 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12832 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012833
Jesse Barnese59150d2014-01-07 13:30:45 -080012834 /* BDW+ don't expose a synchronous way to read the state */
12835 if (IS_HASWELL(dev))
12836 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012837
Ville Syrjälä282740f2013-09-04 18:30:03 +030012838 PIPE_CONF_CHECK_I(double_wide);
12839
Daniel Vetter26804af2014-06-25 22:01:55 +030012840 PIPE_CONF_CHECK_X(ddi_pll_sel);
12841
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012842 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012843 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012844 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012845 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12846 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012847 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012848 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012849 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12850 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12851 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012852
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012853 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12854 PIPE_CONF_CHECK_I(pipe_bpp);
12855
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012856 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012857 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012858
Daniel Vetter66e985c2013-06-05 13:34:20 +020012859#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012860#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012861#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012862#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012863#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012864#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012865#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012866
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012867 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012868}
12869
Damien Lespiau08db6652014-11-04 17:06:52 +000012870static void check_wm_state(struct drm_device *dev)
12871{
12872 struct drm_i915_private *dev_priv = dev->dev_private;
12873 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12874 struct intel_crtc *intel_crtc;
12875 int plane;
12876
12877 if (INTEL_INFO(dev)->gen < 9)
12878 return;
12879
12880 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12881 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12882
12883 for_each_intel_crtc(dev, intel_crtc) {
12884 struct skl_ddb_entry *hw_entry, *sw_entry;
12885 const enum pipe pipe = intel_crtc->pipe;
12886
12887 if (!intel_crtc->active)
12888 continue;
12889
12890 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012891 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012892 hw_entry = &hw_ddb.plane[pipe][plane];
12893 sw_entry = &sw_ddb->plane[pipe][plane];
12894
12895 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12896 continue;
12897
12898 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12899 "(expected (%u,%u), found (%u,%u))\n",
12900 pipe_name(pipe), plane + 1,
12901 sw_entry->start, sw_entry->end,
12902 hw_entry->start, hw_entry->end);
12903 }
12904
12905 /* cursor */
Matt Roper4969d332015-09-24 15:53:10 -070012906 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12907 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
Damien Lespiau08db6652014-11-04 17:06:52 +000012908
12909 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12910 continue;
12911
12912 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12913 "(expected (%u,%u), found (%u,%u))\n",
12914 pipe_name(pipe),
12915 sw_entry->start, sw_entry->end,
12916 hw_entry->start, hw_entry->end);
12917 }
12918}
12919
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012920static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012921check_connector_state(struct drm_device *dev,
12922 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012923{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012924 struct drm_connector_state *old_conn_state;
12925 struct drm_connector *connector;
12926 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012927
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012928 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12929 struct drm_encoder *encoder = connector->encoder;
12930 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012931
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012932 /* This also checks the encoder/connector hw state with the
12933 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012934 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012935
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012936 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012937 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012938 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012939}
12940
12941static void
12942check_encoder_state(struct drm_device *dev)
12943{
12944 struct intel_encoder *encoder;
12945 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012946
Damien Lespiaub2784e12014-08-05 11:29:37 +010012947 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012948 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012949 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012950
12951 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12952 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012953 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012954
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012955 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012956 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012957 continue;
12958 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012959
12960 I915_STATE_WARN(connector->base.state->crtc !=
12961 encoder->base.crtc,
12962 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012963 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012964
Rob Clarke2c719b2014-12-15 13:56:32 -050012965 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012966 "encoder's enabled state mismatch "
12967 "(expected %i, found %i)\n",
12968 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012969
12970 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012971 bool active;
12972
12973 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012974 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012975 "encoder detached but still enabled on pipe %c.\n",
12976 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012977 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012978 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012979}
12980
12981static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012982check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012983{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012984 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012985 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012986 struct drm_crtc_state *old_crtc_state;
12987 struct drm_crtc *crtc;
12988 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012989
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012990 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12991 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12992 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020012993 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012994
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012995 if (!needs_modeset(crtc->state) &&
12996 !to_intel_crtc_state(crtc->state)->update_pipe)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012997 continue;
12998
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012999 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
13000 pipe_config = to_intel_crtc_state(old_crtc_state);
13001 memset(pipe_config, 0, sizeof(*pipe_config));
13002 pipe_config->base.crtc = crtc;
13003 pipe_config->base.state = old_state;
13004
13005 DRM_DEBUG_KMS("[CRTC:%d]\n",
13006 crtc->base.id);
13007
13008 active = dev_priv->display.get_pipe_config(intel_crtc,
13009 pipe_config);
13010
13011 /* hw state is inconsistent with the pipe quirk */
13012 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13013 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13014 active = crtc->state->active;
13015
13016 I915_STATE_WARN(crtc->state->active != active,
13017 "crtc active state doesn't match with hw state "
13018 "(expected %i, found %i)\n", crtc->state->active, active);
13019
13020 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
13021 "transitional active state does not match atomic hw state "
13022 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
13023
13024 for_each_encoder_on_crtc(dev, crtc, encoder) {
13025 enum pipe pipe;
13026
13027 active = encoder->get_hw_state(encoder, &pipe);
13028 I915_STATE_WARN(active != crtc->state->active,
13029 "[ENCODER:%i] active %i with crtc active %i\n",
13030 encoder->base.base.id, active, crtc->state->active);
13031
13032 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13033 "Encoder connected to wrong pipe %c\n",
13034 pipe_name(pipe));
13035
13036 if (active)
13037 encoder->get_config(encoder, pipe_config);
13038 }
13039
13040 if (!crtc->state->active)
13041 continue;
13042
13043 sw_config = to_intel_crtc_state(crtc->state);
13044 if (!intel_pipe_config_compare(dev, sw_config,
13045 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050013046 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013047 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020013048 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013049 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020013050 "[sw state]");
13051 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013052 }
13053}
13054
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013055static void
13056check_shared_dpll_state(struct drm_device *dev)
13057{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013058 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013059 struct intel_crtc *crtc;
13060 struct intel_dpll_hw_state dpll_hw_state;
13061 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013062
13063 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13064 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13065 int enabled_crtcs = 0, active_crtcs = 0;
13066 bool active;
13067
13068 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13069
13070 DRM_DEBUG_KMS("%s\n", pll->name);
13071
13072 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
13073
Rob Clarke2c719b2014-12-15 13:56:32 -050013074 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020013075 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013076 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050013077 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020013078 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050013079 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020013080 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050013081 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020013082 "pll on state mismatch (expected %i, found %i)\n",
13083 pll->on, active);
13084
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013085 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080013086 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020013087 enabled_crtcs++;
13088 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13089 active_crtcs++;
13090 }
Rob Clarke2c719b2014-12-15 13:56:32 -050013091 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020013092 "pll active crtcs mismatch (expected %i, found %i)\n",
13093 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050013094 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020013095 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013096 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013097
Rob Clarke2c719b2014-12-15 13:56:32 -050013098 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020013099 sizeof(dpll_hw_state)),
13100 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020013101 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013102}
13103
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013104static void
13105intel_modeset_check_state(struct drm_device *dev,
13106 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013107{
Damien Lespiau08db6652014-11-04 17:06:52 +000013108 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013109 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013110 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013111 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013112 check_shared_dpll_state(dev);
13113}
13114
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020013115void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030013116 int dotclock)
13117{
13118 /*
13119 * FDI already provided one idea for the dotclock.
13120 * Yell if the encoder disagrees.
13121 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013122 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030013123 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013124 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030013125}
13126
Ville Syrjälä80715b22014-05-15 20:23:23 +030013127static void update_scanline_offset(struct intel_crtc *crtc)
13128{
13129 struct drm_device *dev = crtc->base.dev;
13130
13131 /*
13132 * The scanline counter increments at the leading edge of hsync.
13133 *
13134 * On most platforms it starts counting from vtotal-1 on the
13135 * first active line. That means the scanline counter value is
13136 * always one less than what we would expect. Ie. just after
13137 * start of vblank, which also occurs at start of hsync (on the
13138 * last active line), the scanline counter will read vblank_start-1.
13139 *
13140 * On gen2 the scanline counter starts counting from 1 instead
13141 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13142 * to keep the value positive), instead of adding one.
13143 *
13144 * On HSW+ the behaviour of the scanline counter depends on the output
13145 * type. For DP ports it behaves like most other platforms, but on HDMI
13146 * there's an extra 1 line difference. So we need to add two instead of
13147 * one to the value.
13148 */
13149 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013150 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013151 int vtotal;
13152
Ville Syrjälä124abe02015-09-08 13:40:45 +030013153 vtotal = adjusted_mode->crtc_vtotal;
13154 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013155 vtotal /= 2;
13156
13157 crtc->scanline_offset = vtotal - 1;
13158 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013159 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013160 crtc->scanline_offset = 2;
13161 } else
13162 crtc->scanline_offset = 1;
13163}
13164
Maarten Lankhorstad421372015-06-15 12:33:42 +020013165static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013166{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013167 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013168 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013169 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013170 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013171 struct intel_crtc_state *intel_crtc_state;
13172 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 Lankhorstad421372015-06-15 12:33:42 +020013180 int dpll;
13181
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013182 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030013183 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013184 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013185
Maarten Lankhorstad421372015-06-15 12:33:42 +020013186 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013187 continue;
13188
Maarten Lankhorstad421372015-06-15 12:33:42 +020013189 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
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
13194 shared_dpll[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 Lankhorst27c329e2015-06-15 12:33:56 +020013330 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013331 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013332
Maarten Lankhorstad421372015-06-15 12:33:42 +020013333 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013334
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013335 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013336 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013337
Maarten Lankhorstad421372015-06-15 12:33:42 +020013338 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013339}
13340
Matt Roperaa363132015-09-24 15:53:18 -070013341/*
13342 * Handle calculation of various watermark data at the end of the atomic check
13343 * phase. The code here should be run after the per-crtc and per-plane 'check'
13344 * handlers to ensure that all derived state has been updated.
13345 */
13346static void calc_watermark_data(struct drm_atomic_state *state)
13347{
13348 struct drm_device *dev = state->dev;
13349 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13350 struct drm_crtc *crtc;
13351 struct drm_crtc_state *cstate;
13352 struct drm_plane *plane;
13353 struct drm_plane_state *pstate;
13354
13355 /*
13356 * Calculate watermark configuration details now that derived
13357 * plane/crtc state is all properly updated.
13358 */
13359 drm_for_each_crtc(crtc, dev) {
13360 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13361 crtc->state;
13362
13363 if (cstate->active)
13364 intel_state->wm_config.num_pipes_active++;
13365 }
13366 drm_for_each_legacy_plane(plane, dev) {
13367 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13368 plane->state;
13369
13370 if (!to_intel_plane_state(pstate)->visible)
13371 continue;
13372
13373 intel_state->wm_config.sprites_enabled = true;
13374 if (pstate->crtc_w != pstate->src_w >> 16 ||
13375 pstate->crtc_h != pstate->src_h >> 16)
13376 intel_state->wm_config.sprites_scaled = true;
13377 }
13378}
13379
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013380/**
13381 * intel_atomic_check - validate state object
13382 * @dev: drm device
13383 * @state: state to validate
13384 */
13385static int intel_atomic_check(struct drm_device *dev,
13386 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013387{
Matt Roperaa363132015-09-24 15:53:18 -070013388 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013389 struct drm_crtc *crtc;
13390 struct drm_crtc_state *crtc_state;
13391 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013392 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013393
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013394 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013395 if (ret)
13396 return ret;
13397
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013398 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013399 struct intel_crtc_state *pipe_config =
13400 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013401
Maarten Lankhorstba8af3e2015-11-16 12:49:14 +010013402 memset(&to_intel_crtc(crtc)->atomic, 0,
13403 sizeof(struct intel_crtc_atomic_commit));
13404
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013405 /* Catch I915_MODE_FLAG_INHERITED */
13406 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13407 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013408
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013409 if (!crtc_state->enable) {
13410 if (needs_modeset(crtc_state))
13411 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013412 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013413 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013414
Daniel Vetter26495482015-07-15 14:15:52 +020013415 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013416 continue;
13417
Daniel Vetter26495482015-07-15 14:15:52 +020013418 /* FIXME: For only active_changed we shouldn't need to do any
13419 * state recomputation at all. */
13420
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013421 ret = drm_atomic_add_affected_connectors(state, crtc);
13422 if (ret)
13423 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013424
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013425 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013426 if (ret)
13427 return ret;
13428
Jani Nikula73831232015-11-19 10:26:30 +020013429 if (i915.fastboot &&
13430 intel_pipe_config_compare(state->dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013431 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013432 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013433 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013434 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013435 }
13436
13437 if (needs_modeset(crtc_state)) {
13438 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013439
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013440 ret = drm_atomic_add_affected_planes(state, crtc);
13441 if (ret)
13442 return ret;
13443 }
13444
Daniel Vetter26495482015-07-15 14:15:52 +020013445 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13446 needs_modeset(crtc_state) ?
13447 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013448 }
13449
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013450 if (any_ms) {
13451 ret = intel_modeset_checks(state);
13452
13453 if (ret)
13454 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013455 } else
Matt Roperaa363132015-09-24 15:53:18 -070013456 intel_state->cdclk = to_i915(state->dev)->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013457
Matt Roperaa363132015-09-24 15:53:18 -070013458 ret = drm_atomic_helper_check_planes(state->dev, state);
13459 if (ret)
13460 return ret;
13461
13462 calc_watermark_data(state);
13463
13464 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013465}
13466
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013467static int intel_atomic_prepare_commit(struct drm_device *dev,
13468 struct drm_atomic_state *state,
13469 bool async)
13470{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013471 struct drm_i915_private *dev_priv = dev->dev_private;
13472 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013473 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013474 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013475 struct drm_crtc *crtc;
13476 int i, ret;
13477
13478 if (async) {
13479 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13480 return -EINVAL;
13481 }
13482
13483 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13484 ret = intel_crtc_wait_for_pending_flips(crtc);
13485 if (ret)
13486 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013487
13488 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13489 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013490 }
13491
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013492 ret = mutex_lock_interruptible(&dev->struct_mutex);
13493 if (ret)
13494 return ret;
13495
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013496 ret = drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013497 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13498 u32 reset_counter;
13499
13500 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13501 mutex_unlock(&dev->struct_mutex);
13502
13503 for_each_plane_in_state(state, plane, plane_state, i) {
13504 struct intel_plane_state *intel_plane_state =
13505 to_intel_plane_state(plane_state);
13506
13507 if (!intel_plane_state->wait_req)
13508 continue;
13509
13510 ret = __i915_wait_request(intel_plane_state->wait_req,
13511 reset_counter, true,
13512 NULL, NULL);
13513
13514 /* Swallow -EIO errors to allow updates during hw lockup. */
13515 if (ret == -EIO)
13516 ret = 0;
13517
13518 if (ret)
13519 break;
13520 }
13521
13522 if (!ret)
13523 return 0;
13524
13525 mutex_lock(&dev->struct_mutex);
13526 drm_atomic_helper_cleanup_planes(dev, state);
13527 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013528
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013529 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013530 return ret;
13531}
13532
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013533/**
13534 * intel_atomic_commit - commit validated state object
13535 * @dev: DRM device
13536 * @state: the top-level driver state object
13537 * @async: asynchronous commit
13538 *
13539 * This function commits a top-level state object that has been validated
13540 * with drm_atomic_helper_check().
13541 *
13542 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13543 * we can only handle plane-related operations and do not yet support
13544 * asynchronous commit.
13545 *
13546 * RETURNS
13547 * Zero for success or -errno.
13548 */
13549static int intel_atomic_commit(struct drm_device *dev,
13550 struct drm_atomic_state *state,
13551 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013552{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013553 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013554 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013555 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013556 struct drm_crtc *crtc;
Matt Roper396e33a2016-01-06 11:34:30 -080013557 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013558 int ret = 0, i;
13559 bool hw_check = intel_state->modeset;
Daniel Vettera6778b32012-07-02 09:56:42 +020013560
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013561 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013562 if (ret) {
13563 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013564 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013565 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013566
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013567 drm_atomic_helper_swap_state(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013568 dev_priv->wm.config = to_intel_atomic_state(state)->wm_config;
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013569
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013570 if (intel_state->modeset) {
13571 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13572 sizeof(intel_state->min_pixclk));
13573 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013574 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013575 }
13576
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013577 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013578 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13579
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013580 if (!needs_modeset(crtc->state))
13581 continue;
13582
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013583 intel_pre_plane_update(intel_crtc);
Daniel Vetter460da9162013-03-27 00:44:51 +010013584
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013585 if (crtc_state->active) {
13586 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13587 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013588 intel_crtc->active = false;
13589 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013590
13591 /*
13592 * Underruns don't always raise
13593 * interrupts, so check manually.
13594 */
13595 intel_check_cpu_fifo_underruns(dev_priv);
13596 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013597
13598 if (!crtc->state->active)
13599 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013600 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013601 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013602
Daniel Vetterea9d7582012-07-10 10:42:52 +020013603 /* Only after disabling all output pipelines that will be changed can we
13604 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013605 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013606
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013607 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013608 intel_shared_dpll_commit(state);
13609
13610 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013611 modeset_update_crtc_power_domains(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013612 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013613
Daniel Vettera6778b32012-07-02 09:56:42 +020013614 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013615 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013616 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13617 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013618 bool update_pipe = !modeset &&
13619 to_intel_crtc_state(crtc->state)->update_pipe;
13620 unsigned long put_domains = 0;
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013621
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013622 if (modeset)
13623 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
13624
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013625 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013626 update_scanline_offset(to_intel_crtc(crtc));
13627 dev_priv->display.crtc_enable(crtc);
13628 }
13629
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013630 if (update_pipe) {
13631 put_domains = modeset_get_crtc_power_domains(crtc);
13632
13633 /* make sure intel_modeset_check_state runs */
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013634 hw_check = true;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013635 }
13636
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013637 if (!modeset)
13638 intel_pre_plane_update(intel_crtc);
13639
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013640 if (crtc->state->active &&
13641 (crtc->state->planes_changed || update_pipe))
Maarten Lankhorst62852622015-09-23 16:29:38 +020013642 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013643
13644 if (put_domains)
13645 modeset_put_power_domains(dev_priv, put_domains);
13646
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013647 intel_post_plane_update(intel_crtc);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013648
13649 if (modeset)
13650 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013651 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013652
Daniel Vettera6778b32012-07-02 09:56:42 +020013653 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013654
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013655 drm_atomic_helper_wait_for_vblanks(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013656
Matt Roper396e33a2016-01-06 11:34:30 -080013657 /*
13658 * Now that the vblank has passed, we can go ahead and program the
13659 * optimal watermarks on platforms that need two-step watermark
13660 * programming.
13661 *
13662 * TODO: Move this (and other cleanup) to an async worker eventually.
13663 */
13664 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13665 intel_cstate = to_intel_crtc_state(crtc->state);
13666
13667 if (dev_priv->display.optimize_watermarks)
13668 dev_priv->display.optimize_watermarks(intel_cstate);
13669 }
13670
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013671 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013672 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013673 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013674
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013675 if (hw_check)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013676 intel_modeset_check_state(dev, state);
13677
13678 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013679
Mika Kuoppala75714942015-12-16 09:26:48 +020013680 /* As one of the primary mmio accessors, KMS has a high likelihood
13681 * of triggering bugs in unclaimed access. After we finish
13682 * modesetting, see if an error has been flagged, and if so
13683 * enable debugging for the next modeset - and hope we catch
13684 * the culprit.
13685 *
13686 * XXX note that we assume display power is on at this point.
13687 * This might hold true now but we need to add pm helper to check
13688 * unclaimed only when the hardware is on, as atomic commits
13689 * can happen also when the device is completely off.
13690 */
13691 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13692
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013693 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013694}
13695
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013696void intel_crtc_restore_mode(struct drm_crtc *crtc)
13697{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013698 struct drm_device *dev = crtc->dev;
13699 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013700 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013701 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013702
13703 state = drm_atomic_state_alloc(dev);
13704 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013705 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013706 crtc->base.id);
13707 return;
13708 }
13709
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013710 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013711
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013712retry:
13713 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13714 ret = PTR_ERR_OR_ZERO(crtc_state);
13715 if (!ret) {
13716 if (!crtc_state->active)
13717 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013718
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013719 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013720 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013721 }
13722
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013723 if (ret == -EDEADLK) {
13724 drm_atomic_state_clear(state);
13725 drm_modeset_backoff(state->acquire_ctx);
13726 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013727 }
13728
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013729 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013730out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013731 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013732}
13733
Daniel Vetter25c5b262012-07-08 22:08:04 +020013734#undef for_each_intel_crtc_masked
13735
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013736static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013737 .gamma_set = intel_crtc_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013738 .set_config = drm_atomic_helper_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013739 .destroy = intel_crtc_destroy,
13740 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013741 .atomic_duplicate_state = intel_crtc_duplicate_state,
13742 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013743};
13744
Daniel Vetter53589012013-06-05 13:34:16 +020013745static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13746 struct intel_shared_dpll *pll,
13747 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013748{
Daniel Vetter53589012013-06-05 13:34:16 +020013749 uint32_t val;
13750
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013751 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013752 return false;
13753
Daniel Vetter53589012013-06-05 13:34:16 +020013754 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013755 hw_state->dpll = val;
13756 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13757 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013758
13759 return val & DPLL_VCO_ENABLE;
13760}
13761
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013762static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13763 struct intel_shared_dpll *pll)
13764{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013765 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13766 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013767}
13768
Daniel Vettere7b903d2013-06-05 13:34:14 +020013769static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13770 struct intel_shared_dpll *pll)
13771{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013772 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013773 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013774
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013775 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013776
13777 /* Wait for the clocks to stabilize. */
13778 POSTING_READ(PCH_DPLL(pll->id));
13779 udelay(150);
13780
13781 /* The pixel multiplier can only be updated once the
13782 * DPLL is enabled and the clocks are stable.
13783 *
13784 * So write it again.
13785 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013786 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013787 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013788 udelay(200);
13789}
13790
13791static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13792 struct intel_shared_dpll *pll)
13793{
13794 struct drm_device *dev = dev_priv->dev;
13795 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013796
13797 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013798 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013799 if (intel_crtc_to_shared_dpll(crtc) == pll)
13800 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13801 }
13802
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013803 I915_WRITE(PCH_DPLL(pll->id), 0);
13804 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013805 udelay(200);
13806}
13807
Daniel Vetter46edb022013-06-05 13:34:12 +020013808static char *ibx_pch_dpll_names[] = {
13809 "PCH DPLL A",
13810 "PCH DPLL B",
13811};
13812
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013813static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013814{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013815 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013816 int i;
13817
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013818 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013819
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013820 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013821 dev_priv->shared_dplls[i].id = i;
13822 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013823 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013824 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13825 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013826 dev_priv->shared_dplls[i].get_hw_state =
13827 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013828 }
13829}
13830
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013831static void intel_shared_dpll_init(struct drm_device *dev)
13832{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013833 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013834
Daniel Vetter9cd86932014-06-25 22:01:57 +030013835 if (HAS_DDI(dev))
13836 intel_ddi_pll_init(dev);
13837 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013838 ibx_pch_dpll_init(dev);
13839 else
13840 dev_priv->num_shared_dpll = 0;
13841
13842 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013843}
13844
Matt Roper6beb8c232014-12-01 15:40:14 -080013845/**
13846 * intel_prepare_plane_fb - Prepare fb for usage on plane
13847 * @plane: drm plane to prepare for
13848 * @fb: framebuffer to prepare for presentation
13849 *
13850 * Prepares a framebuffer for usage on a display plane. Generally this
13851 * involves pinning the underlying object and updating the frontbuffer tracking
13852 * bits. Some older platforms need special physical address handling for
13853 * cursor planes.
13854 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013855 * Must be called with struct_mutex held.
13856 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013857 * Returns 0 on success, negative error code on failure.
13858 */
13859int
13860intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013861 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013862{
13863 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013864 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013865 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013866 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013867 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013868 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013869
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013870 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013871 return 0;
13872
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013873 if (old_obj) {
13874 struct drm_crtc_state *crtc_state =
13875 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13876
13877 /* Big Hammer, we also need to ensure that any pending
13878 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13879 * current scanout is retired before unpinning the old
13880 * framebuffer. Note that we rely on userspace rendering
13881 * into the buffer attached to the pipe they are waiting
13882 * on. If not, userspace generates a GPU hang with IPEHR
13883 * point to the MI_WAIT_FOR_EVENT.
13884 *
13885 * This should only fail upon a hung GPU, in which case we
13886 * can safely continue.
13887 */
13888 if (needs_modeset(crtc_state))
13889 ret = i915_gem_object_wait_rendering(old_obj, true);
13890
13891 /* Swallow -EIO errors to allow updates during hw lockup. */
13892 if (ret && ret != -EIO)
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013893 return ret;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013894 }
13895
Alex Goins3c28ff22015-11-25 18:43:39 -080013896 /* For framebuffer backed by dmabuf, wait for fence */
13897 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013898 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013899
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013900 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13901 false, true,
13902 MAX_SCHEDULE_TIMEOUT);
13903 if (lret == -ERESTARTSYS)
13904 return lret;
13905
13906 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013907 }
13908
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013909 if (!obj) {
13910 ret = 0;
13911 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013912 INTEL_INFO(dev)->cursor_needs_physical) {
13913 int align = IS_I830(dev) ? 16 * 1024 : 256;
13914 ret = i915_gem_object_attach_phys(obj, align);
13915 if (ret)
13916 DRM_DEBUG_KMS("failed to attach phys object\n");
13917 } else {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013918 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state);
Matt Roper6beb8c232014-12-01 15:40:14 -080013919 }
13920
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013921 if (ret == 0) {
13922 if (obj) {
13923 struct intel_plane_state *plane_state =
13924 to_intel_plane_state(new_state);
13925
13926 i915_gem_request_assign(&plane_state->wait_req,
13927 obj->last_write_req);
13928 }
13929
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013930 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013931 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013932
Matt Roper6beb8c232014-12-01 15:40:14 -080013933 return ret;
13934}
13935
Matt Roper38f3ce32014-12-02 07:45:25 -080013936/**
13937 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13938 * @plane: drm plane to clean up for
13939 * @fb: old framebuffer that was on plane
13940 *
13941 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013942 *
13943 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013944 */
13945void
13946intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013947 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013948{
13949 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013950 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013951 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013952 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13953 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013954
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013955 old_intel_state = to_intel_plane_state(old_state);
13956
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013957 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013958 return;
13959
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013960 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13961 !INTEL_INFO(dev)->cursor_needs_physical))
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013962 intel_unpin_fb_obj(old_state->fb, old_state);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013963
13964 /* prepare_fb aborted? */
13965 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13966 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13967 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013968
13969 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
13970
Matt Roper465c1202014-05-29 08:06:54 -070013971}
13972
Chandra Konduru6156a452015-04-27 13:48:39 -070013973int
13974skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13975{
13976 int max_scale;
13977 struct drm_device *dev;
13978 struct drm_i915_private *dev_priv;
13979 int crtc_clock, cdclk;
13980
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013981 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013982 return DRM_PLANE_HELPER_NO_SCALING;
13983
13984 dev = intel_crtc->base.dev;
13985 dev_priv = dev->dev_private;
13986 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013987 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013988
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013989 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013990 return DRM_PLANE_HELPER_NO_SCALING;
13991
13992 /*
13993 * skl max scale is lower of:
13994 * close to 3 but not 3, -1 is for that purpose
13995 * or
13996 * cdclk/crtc_clock
13997 */
13998 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13999
14000 return max_scale;
14001}
14002
Matt Roper465c1202014-05-29 08:06:54 -070014003static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014004intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014005 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014006 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014007{
Matt Roper2b875c22014-12-01 15:40:13 -080014008 struct drm_crtc *crtc = state->base.crtc;
14009 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014010 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014011 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14012 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014013
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014014 /* use scaler when colorkey is not required */
14015 if (INTEL_INFO(plane->dev)->gen >= 9 &&
Maarten Lankhorst818ed962015-06-15 12:33:54 +020014016 state->ckey.flags == I915_SET_COLORKEY_NONE) {
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014017 min_scale = 1;
14018 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053014019 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014020 }
Sonika Jindald8106362015-04-10 14:37:28 +053014021
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014022 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14023 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014024 min_scale, max_scale,
14025 can_position, true,
14026 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014027}
14028
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020014029static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14030 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080014031{
14032 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080014033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020014034 struct intel_crtc_state *old_intel_state =
14035 to_intel_crtc_state(old_crtc_state);
14036 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030014037
Matt Roperc34c9ee2014-12-23 10:41:50 -080014038 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020014039 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020014040
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020014041 if (modeset)
14042 return;
14043
14044 if (to_intel_crtc_state(crtc->state)->update_pipe)
14045 intel_update_pipe_config(intel_crtc, old_intel_state);
14046 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020014047 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080014048}
14049
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020014050static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14051 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080014052{
Matt Roper32b7eee2014-12-24 07:59:06 -080014053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080014054
Maarten Lankhorst62852622015-09-23 16:29:38 +020014055 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014056}
14057
Matt Ropercf4c7c12014-12-04 10:27:42 -080014058/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014059 * intel_plane_destroy - destroy a plane
14060 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014061 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014062 * Common destruction function for all types of planes (primary, cursor,
14063 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014064 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014065void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014066{
14067 struct intel_plane *intel_plane = to_intel_plane(plane);
14068 drm_plane_cleanup(plane);
14069 kfree(intel_plane);
14070}
14071
Matt Roper65a3fea2015-01-21 16:35:42 -080014072const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014073 .update_plane = drm_atomic_helper_update_plane,
14074 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014075 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014076 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014077 .atomic_get_property = intel_plane_atomic_get_property,
14078 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014079 .atomic_duplicate_state = intel_plane_duplicate_state,
14080 .atomic_destroy_state = intel_plane_destroy_state,
14081
Matt Roper465c1202014-05-29 08:06:54 -070014082};
14083
14084static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14085 int pipe)
14086{
14087 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080014088 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070014089 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014090 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070014091
14092 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
14093 if (primary == NULL)
14094 return NULL;
14095
Matt Roper8e7d6882015-01-21 16:35:41 -080014096 state = intel_create_plane_state(&primary->base);
14097 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014098 kfree(primary);
14099 return NULL;
14100 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014101 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014102
Matt Roper465c1202014-05-29 08:06:54 -070014103 primary->can_scale = false;
14104 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014105 if (INTEL_INFO(dev)->gen >= 9) {
14106 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014107 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014108 }
Matt Roper465c1202014-05-29 08:06:54 -070014109 primary->pipe = pipe;
14110 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014111 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014112 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014113 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14114 primary->plane = !pipe;
14115
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014116 if (INTEL_INFO(dev)->gen >= 9) {
14117 intel_primary_formats = skl_primary_formats;
14118 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014119
14120 primary->update_plane = skylake_update_primary_plane;
14121 primary->disable_plane = skylake_disable_primary_plane;
14122 } else if (HAS_PCH_SPLIT(dev)) {
14123 intel_primary_formats = i965_primary_formats;
14124 num_formats = ARRAY_SIZE(i965_primary_formats);
14125
14126 primary->update_plane = ironlake_update_primary_plane;
14127 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014128 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014129 intel_primary_formats = i965_primary_formats;
14130 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014131
14132 primary->update_plane = i9xx_update_primary_plane;
14133 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014134 } else {
14135 intel_primary_formats = i8xx_primary_formats;
14136 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014137
14138 primary->update_plane = i9xx_update_primary_plane;
14139 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014140 }
14141
14142 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014143 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070014144 intel_primary_formats, num_formats,
14145 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053014146
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014147 if (INTEL_INFO(dev)->gen >= 4)
14148 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014149
Matt Roperea2c67b2014-12-23 10:41:52 -080014150 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14151
Matt Roper465c1202014-05-29 08:06:54 -070014152 return &primary->base;
14153}
14154
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014155void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14156{
14157 if (!dev->mode_config.rotation_property) {
14158 unsigned long flags = BIT(DRM_ROTATE_0) |
14159 BIT(DRM_ROTATE_180);
14160
14161 if (INTEL_INFO(dev)->gen >= 9)
14162 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14163
14164 dev->mode_config.rotation_property =
14165 drm_mode_create_rotation_property(dev, flags);
14166 }
14167 if (dev->mode_config.rotation_property)
14168 drm_object_attach_property(&plane->base.base,
14169 dev->mode_config.rotation_property,
14170 plane->base.state->rotation);
14171}
14172
Matt Roper3d7d6512014-06-10 08:28:13 -070014173static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014174intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014175 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014176 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014177{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014178 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014179 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014180 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014181 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014182 unsigned stride;
14183 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014184
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014185 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14186 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014187 DRM_PLANE_HELPER_NO_SCALING,
14188 DRM_PLANE_HELPER_NO_SCALING,
14189 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014190 if (ret)
14191 return ret;
14192
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014193 /* if we want to turn off the cursor ignore width and height */
14194 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014195 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014196
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014197 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014198 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014199 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14200 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014201 return -EINVAL;
14202 }
14203
Matt Roperea2c67b2014-12-23 10:41:52 -080014204 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14205 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014206 DRM_DEBUG_KMS("buffer is too small\n");
14207 return -ENOMEM;
14208 }
14209
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014210 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014211 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014212 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014213 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014214
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014215 /*
14216 * There's something wrong with the cursor on CHV pipe C.
14217 * If it straddles the left edge of the screen then
14218 * moving it away from the edge or disabling it often
14219 * results in a pipe underrun, and often that can lead to
14220 * dead pipe (constant underrun reported, and it scans
14221 * out just a solid color). To recover from that, the
14222 * display power well must be turned off and on again.
14223 * Refuse the put the cursor into that compromised position.
14224 */
14225 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14226 state->visible && state->base.crtc_x < 0) {
14227 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14228 return -EINVAL;
14229 }
14230
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014231 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014232}
14233
Matt Roperf4a2cf22014-12-01 15:40:12 -080014234static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014235intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014236 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014237{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014238 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14239
14240 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014241 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014242}
14243
14244static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014245intel_update_cursor_plane(struct drm_plane *plane,
14246 const struct intel_crtc_state *crtc_state,
14247 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014248{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014249 struct drm_crtc *crtc = crtc_state->base.crtc;
14250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014251 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014252 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014253 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014254
Matt Roperf4a2cf22014-12-01 15:40:12 -080014255 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014256 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014257 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014258 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014259 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014260 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014261
Gustavo Padovana912f122014-12-01 15:40:10 -080014262 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014263 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014264}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014265
Matt Roper3d7d6512014-06-10 08:28:13 -070014266static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14267 int pipe)
14268{
14269 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014270 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014271
14272 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14273 if (cursor == NULL)
14274 return NULL;
14275
Matt Roper8e7d6882015-01-21 16:35:41 -080014276 state = intel_create_plane_state(&cursor->base);
14277 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014278 kfree(cursor);
14279 return NULL;
14280 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014281 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014282
Matt Roper3d7d6512014-06-10 08:28:13 -070014283 cursor->can_scale = false;
14284 cursor->max_downscale = 1;
14285 cursor->pipe = pipe;
14286 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014287 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014288 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014289 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014290 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014291
14292 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014293 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014294 intel_cursor_formats,
14295 ARRAY_SIZE(intel_cursor_formats),
14296 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014297
14298 if (INTEL_INFO(dev)->gen >= 4) {
14299 if (!dev->mode_config.rotation_property)
14300 dev->mode_config.rotation_property =
14301 drm_mode_create_rotation_property(dev,
14302 BIT(DRM_ROTATE_0) |
14303 BIT(DRM_ROTATE_180));
14304 if (dev->mode_config.rotation_property)
14305 drm_object_attach_property(&cursor->base.base,
14306 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014307 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014308 }
14309
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014310 if (INTEL_INFO(dev)->gen >=9)
14311 state->scaler_id = -1;
14312
Matt Roperea2c67b2014-12-23 10:41:52 -080014313 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14314
Matt Roper3d7d6512014-06-10 08:28:13 -070014315 return &cursor->base;
14316}
14317
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014318static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14319 struct intel_crtc_state *crtc_state)
14320{
14321 int i;
14322 struct intel_scaler *intel_scaler;
14323 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14324
14325 for (i = 0; i < intel_crtc->num_scalers; i++) {
14326 intel_scaler = &scaler_state->scalers[i];
14327 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014328 intel_scaler->mode = PS_SCALER_MODE_DYN;
14329 }
14330
14331 scaler_state->scaler_id = -1;
14332}
14333
Hannes Ederb358d0a2008-12-18 21:18:47 +010014334static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014335{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014336 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014337 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014338 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014339 struct drm_plane *primary = NULL;
14340 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014341 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014342
Daniel Vetter955382f2013-09-19 14:05:45 +020014343 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014344 if (intel_crtc == NULL)
14345 return;
14346
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014347 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14348 if (!crtc_state)
14349 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014350 intel_crtc->config = crtc_state;
14351 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014352 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014353
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014354 /* initialize shared scalers */
14355 if (INTEL_INFO(dev)->gen >= 9) {
14356 if (pipe == PIPE_C)
14357 intel_crtc->num_scalers = 1;
14358 else
14359 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14360
14361 skl_init_scalers(dev, intel_crtc, crtc_state);
14362 }
14363
Matt Roper465c1202014-05-29 08:06:54 -070014364 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014365 if (!primary)
14366 goto fail;
14367
14368 cursor = intel_cursor_plane_create(dev, pipe);
14369 if (!cursor)
14370 goto fail;
14371
Matt Roper465c1202014-05-29 08:06:54 -070014372 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014373 cursor, &intel_crtc_funcs);
14374 if (ret)
14375 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014376
14377 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014378 for (i = 0; i < 256; i++) {
14379 intel_crtc->lut_r[i] = i;
14380 intel_crtc->lut_g[i] = i;
14381 intel_crtc->lut_b[i] = i;
14382 }
14383
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014384 /*
14385 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014386 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014387 */
Jesse Barnes80824002009-09-10 15:28:06 -070014388 intel_crtc->pipe = pipe;
14389 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014390 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014391 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014392 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014393 }
14394
Chris Wilson4b0e3332014-05-30 16:35:26 +030014395 intel_crtc->cursor_base = ~0;
14396 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014397 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014398
Ville Syrjälä852eb002015-06-24 22:00:07 +030014399 intel_crtc->wm.cxsr_allowed = true;
14400
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014401 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14402 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14403 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14404 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14405
Jesse Barnes79e53942008-11-07 14:24:08 -080014406 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014407
14408 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014409 return;
14410
14411fail:
14412 if (primary)
14413 drm_plane_cleanup(primary);
14414 if (cursor)
14415 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014416 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014417 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014418}
14419
Jesse Barnes752aa882013-10-31 18:55:49 +020014420enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14421{
14422 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014423 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014424
Rob Clark51fd3712013-11-19 12:10:12 -050014425 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014426
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014427 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014428 return INVALID_PIPE;
14429
14430 return to_intel_crtc(encoder->crtc)->pipe;
14431}
14432
Carl Worth08d7b3d2009-04-29 14:43:54 -070014433int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014434 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014435{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014436 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014437 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014438 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014439
Rob Clark7707e652014-07-17 23:30:04 -040014440 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014441
Rob Clark7707e652014-07-17 23:30:04 -040014442 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014443 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014444 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014445 }
14446
Rob Clark7707e652014-07-17 23:30:04 -040014447 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014448 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014449
Daniel Vetterc05422d2009-08-11 16:05:30 +020014450 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014451}
14452
Daniel Vetter66a92782012-07-12 20:08:18 +020014453static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014454{
Daniel Vetter66a92782012-07-12 20:08:18 +020014455 struct drm_device *dev = encoder->base.dev;
14456 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014457 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014458 int entry = 0;
14459
Damien Lespiaub2784e12014-08-05 11:29:37 +010014460 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014461 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014462 index_mask |= (1 << entry);
14463
Jesse Barnes79e53942008-11-07 14:24:08 -080014464 entry++;
14465 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014466
Jesse Barnes79e53942008-11-07 14:24:08 -080014467 return index_mask;
14468}
14469
Chris Wilson4d302442010-12-14 19:21:29 +000014470static bool has_edp_a(struct drm_device *dev)
14471{
14472 struct drm_i915_private *dev_priv = dev->dev_private;
14473
14474 if (!IS_MOBILE(dev))
14475 return false;
14476
14477 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14478 return false;
14479
Damien Lespiaue3589902014-02-07 19:12:50 +000014480 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014481 return false;
14482
14483 return true;
14484}
14485
Jesse Barnes84b4e042014-06-25 08:24:29 -070014486static bool intel_crt_present(struct drm_device *dev)
14487{
14488 struct drm_i915_private *dev_priv = dev->dev_private;
14489
Damien Lespiau884497e2013-12-03 13:56:23 +000014490 if (INTEL_INFO(dev)->gen >= 9)
14491 return false;
14492
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014493 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014494 return false;
14495
14496 if (IS_CHERRYVIEW(dev))
14497 return false;
14498
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014499 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14500 return false;
14501
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014502 /* DDI E can't be used if DDI A requires 4 lanes */
14503 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14504 return false;
14505
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014506 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014507 return false;
14508
14509 return true;
14510}
14511
Jesse Barnes79e53942008-11-07 14:24:08 -080014512static void intel_setup_outputs(struct drm_device *dev)
14513{
Eric Anholt725e30a2009-01-22 13:01:02 -080014514 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014515 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014516 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014517
Daniel Vetterc9093352013-06-06 22:22:47 +020014518 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014519
Jesse Barnes84b4e042014-06-25 08:24:29 -070014520 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014521 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014522
Vandana Kannanc776eb22014-08-19 12:05:01 +053014523 if (IS_BROXTON(dev)) {
14524 /*
14525 * FIXME: Broxton doesn't support port detection via the
14526 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14527 * detect the ports.
14528 */
14529 intel_ddi_init(dev, PORT_A);
14530 intel_ddi_init(dev, PORT_B);
14531 intel_ddi_init(dev, PORT_C);
14532 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014533 int found;
14534
Jesse Barnesde31fac2015-03-06 15:53:32 -080014535 /*
14536 * Haswell uses DDI functions to detect digital outputs.
14537 * On SKL pre-D0 the strap isn't connected, so we assume
14538 * it's there.
14539 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014540 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014541 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014542 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014543 intel_ddi_init(dev, PORT_A);
14544
14545 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14546 * register */
14547 found = I915_READ(SFUSE_STRAP);
14548
14549 if (found & SFUSE_STRAP_DDIB_DETECTED)
14550 intel_ddi_init(dev, PORT_B);
14551 if (found & SFUSE_STRAP_DDIC_DETECTED)
14552 intel_ddi_init(dev, PORT_C);
14553 if (found & SFUSE_STRAP_DDID_DETECTED)
14554 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014555 /*
14556 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14557 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014558 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014559 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14560 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14561 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14562 intel_ddi_init(dev, PORT_E);
14563
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014564 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014565 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014566 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014567
14568 if (has_edp_a(dev))
14569 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014570
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014571 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014572 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014573 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014574 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014575 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014576 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014577 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014578 }
14579
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014580 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014581 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014582
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014583 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014584 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014585
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014586 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014587 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014588
Daniel Vetter270b3042012-10-27 15:52:05 +020014589 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014590 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014591 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014592 /*
14593 * The DP_DETECTED bit is the latched state of the DDC
14594 * SDA pin at boot. However since eDP doesn't require DDC
14595 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14596 * eDP ports may have been muxed to an alternate function.
14597 * Thus we can't rely on the DP_DETECTED bit alone to detect
14598 * eDP ports. Consult the VBT as well as DP_DETECTED to
14599 * detect eDP ports.
14600 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014601 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014602 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014603 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14604 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014605 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014606 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014607
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014608 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014609 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014610 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14611 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014612 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014613 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014614
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014615 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014616 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014617 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14618 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14619 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14620 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014621 }
14622
Jani Nikula3cfca972013-08-27 15:12:26 +030014623 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014624 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014625 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014626
Paulo Zanonie2debe92013-02-18 19:00:27 -030014627 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014628 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014629 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014630 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014631 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014632 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014633 }
Ma Ling27185ae2009-08-24 13:50:23 +080014634
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014635 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014636 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014637 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014638
14639 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014640
Paulo Zanonie2debe92013-02-18 19:00:27 -030014641 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014642 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014643 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014644 }
Ma Ling27185ae2009-08-24 13:50:23 +080014645
Paulo Zanonie2debe92013-02-18 19:00:27 -030014646 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014647
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014648 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014649 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014650 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014651 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014652 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014653 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014654 }
Ma Ling27185ae2009-08-24 13:50:23 +080014655
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014656 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014657 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014658 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014659 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014660 intel_dvo_init(dev);
14661
Zhenyu Wang103a1962009-11-27 11:44:36 +080014662 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014663 intel_tv_init(dev);
14664
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014665 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014666
Damien Lespiaub2784e12014-08-05 11:29:37 +010014667 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014668 encoder->base.possible_crtcs = encoder->crtc_mask;
14669 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014670 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014671 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014672
Paulo Zanonidde86e22012-12-01 12:04:25 -020014673 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014674
14675 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014676}
14677
14678static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14679{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014680 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014681 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014682
Daniel Vetteref2d6332014-02-10 18:00:38 +010014683 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014684 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014685 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014686 drm_gem_object_unreference(&intel_fb->obj->base);
14687 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014688 kfree(intel_fb);
14689}
14690
14691static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014692 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014693 unsigned int *handle)
14694{
14695 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014696 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014697
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014698 if (obj->userptr.mm) {
14699 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14700 return -EINVAL;
14701 }
14702
Chris Wilson05394f32010-11-08 19:18:58 +000014703 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014704}
14705
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014706static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14707 struct drm_file *file,
14708 unsigned flags, unsigned color,
14709 struct drm_clip_rect *clips,
14710 unsigned num_clips)
14711{
14712 struct drm_device *dev = fb->dev;
14713 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14714 struct drm_i915_gem_object *obj = intel_fb->obj;
14715
14716 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014717 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014718 mutex_unlock(&dev->struct_mutex);
14719
14720 return 0;
14721}
14722
Jesse Barnes79e53942008-11-07 14:24:08 -080014723static const struct drm_framebuffer_funcs intel_fb_funcs = {
14724 .destroy = intel_user_framebuffer_destroy,
14725 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014726 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014727};
14728
Damien Lespiaub3218032015-02-27 11:15:18 +000014729static
14730u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14731 uint32_t pixel_format)
14732{
14733 u32 gen = INTEL_INFO(dev)->gen;
14734
14735 if (gen >= 9) {
14736 /* "The stride in bytes must not exceed the of the size of 8K
14737 * pixels and 32K bytes."
14738 */
14739 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014740 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014741 return 32*1024;
14742 } else if (gen >= 4) {
14743 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14744 return 16*1024;
14745 else
14746 return 32*1024;
14747 } else if (gen >= 3) {
14748 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14749 return 8*1024;
14750 else
14751 return 16*1024;
14752 } else {
14753 /* XXX DSPC is limited to 4k tiled */
14754 return 8*1024;
14755 }
14756}
14757
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014758static int intel_framebuffer_init(struct drm_device *dev,
14759 struct intel_framebuffer *intel_fb,
14760 struct drm_mode_fb_cmd2 *mode_cmd,
14761 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014762{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014763 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014764 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014765 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014766 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014767
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014768 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14769
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014770 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14771 /* Enforce that fb modifier and tiling mode match, but only for
14772 * X-tiled. This is needed for FBC. */
14773 if (!!(obj->tiling_mode == I915_TILING_X) !=
14774 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14775 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14776 return -EINVAL;
14777 }
14778 } else {
14779 if (obj->tiling_mode == I915_TILING_X)
14780 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14781 else if (obj->tiling_mode == I915_TILING_Y) {
14782 DRM_DEBUG("No Y tiling for legacy addfb\n");
14783 return -EINVAL;
14784 }
14785 }
14786
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014787 /* Passed in modifier sanity checking. */
14788 switch (mode_cmd->modifier[0]) {
14789 case I915_FORMAT_MOD_Y_TILED:
14790 case I915_FORMAT_MOD_Yf_TILED:
14791 if (INTEL_INFO(dev)->gen < 9) {
14792 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14793 mode_cmd->modifier[0]);
14794 return -EINVAL;
14795 }
14796 case DRM_FORMAT_MOD_NONE:
14797 case I915_FORMAT_MOD_X_TILED:
14798 break;
14799 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014800 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14801 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014802 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014803 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014804
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014805 stride_alignment = intel_fb_stride_alignment(dev_priv,
14806 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014807 mode_cmd->pixel_format);
14808 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14809 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14810 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014811 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014812 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014813
Damien Lespiaub3218032015-02-27 11:15:18 +000014814 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14815 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014816 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014817 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14818 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014819 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014820 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014821 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014822 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014823
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014824 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014825 mode_cmd->pitches[0] != obj->stride) {
14826 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14827 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014828 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014829 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014830
Ville Syrjälä57779d02012-10-31 17:50:14 +020014831 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014832 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014833 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014834 case DRM_FORMAT_RGB565:
14835 case DRM_FORMAT_XRGB8888:
14836 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014837 break;
14838 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014839 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014840 DRM_DEBUG("unsupported pixel format: %s\n",
14841 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014842 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014843 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014844 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014845 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014846 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14847 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014848 DRM_DEBUG("unsupported pixel format: %s\n",
14849 drm_get_format_name(mode_cmd->pixel_format));
14850 return -EINVAL;
14851 }
14852 break;
14853 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014854 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014855 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014856 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014857 DRM_DEBUG("unsupported pixel format: %s\n",
14858 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014859 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014860 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014861 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014862 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014863 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014864 DRM_DEBUG("unsupported pixel format: %s\n",
14865 drm_get_format_name(mode_cmd->pixel_format));
14866 return -EINVAL;
14867 }
14868 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014869 case DRM_FORMAT_YUYV:
14870 case DRM_FORMAT_UYVY:
14871 case DRM_FORMAT_YVYU:
14872 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014873 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014874 DRM_DEBUG("unsupported pixel format: %s\n",
14875 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014876 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014877 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014878 break;
14879 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014880 DRM_DEBUG("unsupported pixel format: %s\n",
14881 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014882 return -EINVAL;
14883 }
14884
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014885 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14886 if (mode_cmd->offsets[0] != 0)
14887 return -EINVAL;
14888
Damien Lespiauec2c9812015-01-20 12:51:45 +000014889 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014890 mode_cmd->pixel_format,
14891 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014892 /* FIXME drm helper for size checks (especially planar formats)? */
14893 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14894 return -EINVAL;
14895
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014896 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14897 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014898 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014899
Jesse Barnes79e53942008-11-07 14:24:08 -080014900 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14901 if (ret) {
14902 DRM_ERROR("framebuffer init failed %d\n", ret);
14903 return ret;
14904 }
14905
Jesse Barnes79e53942008-11-07 14:24:08 -080014906 return 0;
14907}
14908
Jesse Barnes79e53942008-11-07 14:24:08 -080014909static struct drm_framebuffer *
14910intel_user_framebuffer_create(struct drm_device *dev,
14911 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020014912 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014913{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014914 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014915 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014916 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014917
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014918 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014919 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014920 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014921 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014922
Daniel Vetter92907cb2015-11-23 09:04:05 +010014923 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014924 if (IS_ERR(fb))
14925 drm_gem_object_unreference_unlocked(&obj->base);
14926
14927 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014928}
14929
Daniel Vetter06957262015-08-10 13:34:08 +020014930#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014931static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014932{
14933}
14934#endif
14935
Jesse Barnes79e53942008-11-07 14:24:08 -080014936static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014937 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014938 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014939 .atomic_check = intel_atomic_check,
14940 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014941 .atomic_state_alloc = intel_atomic_state_alloc,
14942 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014943};
14944
Jesse Barnese70236a2009-09-21 10:42:27 -070014945/* Set up chip specific display functions */
14946static void intel_init_display(struct drm_device *dev)
14947{
14948 struct drm_i915_private *dev_priv = dev->dev_private;
14949
Daniel Vetteree9300b2013-06-03 22:40:22 +020014950 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14951 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014952 else if (IS_CHERRYVIEW(dev))
14953 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014954 else if (IS_VALLEYVIEW(dev))
14955 dev_priv->display.find_dpll = vlv_find_best_dpll;
14956 else if (IS_PINEVIEW(dev))
14957 dev_priv->display.find_dpll = pnv_find_best_dpll;
14958 else
14959 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14960
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014961 if (INTEL_INFO(dev)->gen >= 9) {
14962 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014963 dev_priv->display.get_initial_plane_config =
14964 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014965 dev_priv->display.crtc_compute_clock =
14966 haswell_crtc_compute_clock;
14967 dev_priv->display.crtc_enable = haswell_crtc_enable;
14968 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014969 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014970 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014971 dev_priv->display.get_initial_plane_config =
14972 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014973 dev_priv->display.crtc_compute_clock =
14974 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014975 dev_priv->display.crtc_enable = haswell_crtc_enable;
14976 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014977 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014978 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014979 dev_priv->display.get_initial_plane_config =
14980 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014981 dev_priv->display.crtc_compute_clock =
14982 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014983 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14984 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Wayne Boyer666a4532015-12-09 12:29:35 -080014985 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014986 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014987 dev_priv->display.get_initial_plane_config =
14988 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014989 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014990 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14991 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014992 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014993 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014994 dev_priv->display.get_initial_plane_config =
14995 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014996 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014997 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14998 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014999 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015000
Jesse Barnese70236a2009-09-21 10:42:27 -070015001 /* Returns the core display clock speed */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070015002 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015003 dev_priv->display.get_display_clock_speed =
15004 skylake_get_display_clock_speed;
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015005 else if (IS_BROXTON(dev))
15006 dev_priv->display.get_display_clock_speed =
15007 broxton_get_display_clock_speed;
Ville Syrjälä1652d192015-03-31 14:12:01 +030015008 else if (IS_BROADWELL(dev))
15009 dev_priv->display.get_display_clock_speed =
15010 broadwell_get_display_clock_speed;
15011 else if (IS_HASWELL(dev))
15012 dev_priv->display.get_display_clock_speed =
15013 haswell_get_display_clock_speed;
Wayne Boyer666a4532015-12-09 12:29:35 -080015014 else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015015 dev_priv->display.get_display_clock_speed =
15016 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015017 else if (IS_GEN5(dev))
15018 dev_priv->display.get_display_clock_speed =
15019 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030015020 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030015021 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070015022 dev_priv->display.get_display_clock_speed =
15023 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030015024 else if (IS_GM45(dev))
15025 dev_priv->display.get_display_clock_speed =
15026 gm45_get_display_clock_speed;
15027 else if (IS_CRESTLINE(dev))
15028 dev_priv->display.get_display_clock_speed =
15029 i965gm_get_display_clock_speed;
15030 else if (IS_PINEVIEW(dev))
15031 dev_priv->display.get_display_clock_speed =
15032 pnv_get_display_clock_speed;
15033 else if (IS_G33(dev) || IS_G4X(dev))
15034 dev_priv->display.get_display_clock_speed =
15035 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070015036 else if (IS_I915G(dev))
15037 dev_priv->display.get_display_clock_speed =
15038 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020015039 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070015040 dev_priv->display.get_display_clock_speed =
15041 i9xx_misc_get_display_clock_speed;
15042 else if (IS_I915GM(dev))
15043 dev_priv->display.get_display_clock_speed =
15044 i915gm_get_display_clock_speed;
15045 else if (IS_I865G(dev))
15046 dev_priv->display.get_display_clock_speed =
15047 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020015048 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070015049 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015050 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015051 else { /* 830 */
15052 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015053 dev_priv->display.get_display_clock_speed =
15054 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015055 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015056
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015057 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015058 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015059 } else if (IS_GEN6(dev)) {
15060 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015061 } else if (IS_IVYBRIDGE(dev)) {
15062 /* FIXME: detect B0+ stepping and use auto training */
15063 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030015064 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015065 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015066 if (IS_BROADWELL(dev)) {
15067 dev_priv->display.modeset_commit_cdclk =
15068 broadwell_modeset_commit_cdclk;
15069 dev_priv->display.modeset_calc_cdclk =
15070 broadwell_modeset_calc_cdclk;
15071 }
Wayne Boyer666a4532015-12-09 12:29:35 -080015072 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015073 dev_priv->display.modeset_commit_cdclk =
15074 valleyview_modeset_commit_cdclk;
15075 dev_priv->display.modeset_calc_cdclk =
15076 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053015077 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015078 dev_priv->display.modeset_commit_cdclk =
15079 broxton_modeset_commit_cdclk;
15080 dev_priv->display.modeset_calc_cdclk =
15081 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015082 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015083
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015084 switch (INTEL_INFO(dev)->gen) {
15085 case 2:
15086 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15087 break;
15088
15089 case 3:
15090 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15091 break;
15092
15093 case 4:
15094 case 5:
15095 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15096 break;
15097
15098 case 6:
15099 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15100 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015101 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070015102 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015103 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15104 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000015105 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000015106 /* Drop through - unsupported since execlist only. */
15107 default:
15108 /* Default just returns -ENODEV to indicate unsupported */
15109 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015110 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020015111
Ville Syrjäläe39b9992014-09-04 14:53:14 +030015112 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070015113}
15114
Jesse Barnesb690e962010-07-19 13:53:12 -070015115/*
15116 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15117 * resume, or other times. This quirk makes sure that's the case for
15118 * affected systems.
15119 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015120static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015121{
15122 struct drm_i915_private *dev_priv = dev->dev_private;
15123
15124 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015125 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015126}
15127
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015128static void quirk_pipeb_force(struct drm_device *dev)
15129{
15130 struct drm_i915_private *dev_priv = dev->dev_private;
15131
15132 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15133 DRM_INFO("applying pipe b force quirk\n");
15134}
15135
Keith Packard435793d2011-07-12 14:56:22 -070015136/*
15137 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15138 */
15139static void quirk_ssc_force_disable(struct drm_device *dev)
15140{
15141 struct drm_i915_private *dev_priv = dev->dev_private;
15142 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015143 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015144}
15145
Carsten Emde4dca20e2012-03-15 15:56:26 +010015146/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015147 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15148 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015149 */
15150static void quirk_invert_brightness(struct drm_device *dev)
15151{
15152 struct drm_i915_private *dev_priv = dev->dev_private;
15153 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015154 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015155}
15156
Scot Doyle9c72cc62014-07-03 23:27:50 +000015157/* Some VBT's incorrectly indicate no backlight is present */
15158static void quirk_backlight_present(struct drm_device *dev)
15159{
15160 struct drm_i915_private *dev_priv = dev->dev_private;
15161 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15162 DRM_INFO("applying backlight present quirk\n");
15163}
15164
Jesse Barnesb690e962010-07-19 13:53:12 -070015165struct intel_quirk {
15166 int device;
15167 int subsystem_vendor;
15168 int subsystem_device;
15169 void (*hook)(struct drm_device *dev);
15170};
15171
Egbert Eich5f85f172012-10-14 15:46:38 +020015172/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15173struct intel_dmi_quirk {
15174 void (*hook)(struct drm_device *dev);
15175 const struct dmi_system_id (*dmi_id_list)[];
15176};
15177
15178static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15179{
15180 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15181 return 1;
15182}
15183
15184static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15185 {
15186 .dmi_id_list = &(const struct dmi_system_id[]) {
15187 {
15188 .callback = intel_dmi_reverse_brightness,
15189 .ident = "NCR Corporation",
15190 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15191 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15192 },
15193 },
15194 { } /* terminating entry */
15195 },
15196 .hook = quirk_invert_brightness,
15197 },
15198};
15199
Ben Widawskyc43b5632012-04-16 14:07:40 -070015200static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015201 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15202 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15203
Jesse Barnesb690e962010-07-19 13:53:12 -070015204 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15205 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15206
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015207 /* 830 needs to leave pipe A & dpll A up */
15208 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15209
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015210 /* 830 needs to leave pipe B & dpll B up */
15211 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15212
Keith Packard435793d2011-07-12 14:56:22 -070015213 /* Lenovo U160 cannot use SSC on LVDS */
15214 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015215
15216 /* Sony Vaio Y cannot use SSC on LVDS */
15217 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015218
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015219 /* Acer Aspire 5734Z must invert backlight brightness */
15220 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15221
15222 /* Acer/eMachines G725 */
15223 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15224
15225 /* Acer/eMachines e725 */
15226 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15227
15228 /* Acer/Packard Bell NCL20 */
15229 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15230
15231 /* Acer Aspire 4736Z */
15232 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015233
15234 /* Acer Aspire 5336 */
15235 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015236
15237 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15238 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015239
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015240 /* Acer C720 Chromebook (Core i3 4005U) */
15241 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15242
jens steinb2a96012014-10-28 20:25:53 +010015243 /* Apple Macbook 2,1 (Core 2 T7400) */
15244 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15245
Jani Nikula1b9448b02015-11-05 11:49:59 +020015246 /* Apple Macbook 4,1 */
15247 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15248
Scot Doyled4967d82014-07-03 23:27:52 +000015249 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15250 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015251
15252 /* HP Chromebook 14 (Celeron 2955U) */
15253 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015254
15255 /* Dell Chromebook 11 */
15256 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015257
15258 /* Dell Chromebook 11 (2015 version) */
15259 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015260};
15261
15262static void intel_init_quirks(struct drm_device *dev)
15263{
15264 struct pci_dev *d = dev->pdev;
15265 int i;
15266
15267 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15268 struct intel_quirk *q = &intel_quirks[i];
15269
15270 if (d->device == q->device &&
15271 (d->subsystem_vendor == q->subsystem_vendor ||
15272 q->subsystem_vendor == PCI_ANY_ID) &&
15273 (d->subsystem_device == q->subsystem_device ||
15274 q->subsystem_device == PCI_ANY_ID))
15275 q->hook(dev);
15276 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015277 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15278 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15279 intel_dmi_quirks[i].hook(dev);
15280 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015281}
15282
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015283/* Disable the VGA plane that we never use */
15284static void i915_disable_vga(struct drm_device *dev)
15285{
15286 struct drm_i915_private *dev_priv = dev->dev_private;
15287 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015288 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015289
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015290 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015291 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015292 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015293 sr1 = inb(VGA_SR_DATA);
15294 outb(sr1 | 1<<5, VGA_SR_DATA);
15295 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15296 udelay(300);
15297
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015298 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015299 POSTING_READ(vga_reg);
15300}
15301
Daniel Vetterf8175862012-04-10 15:50:11 +020015302void intel_modeset_init_hw(struct drm_device *dev)
15303{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015304 struct drm_i915_private *dev_priv = dev->dev_private;
15305
Ville Syrjäläb6283052015-06-03 15:45:07 +030015306 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015307
15308 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15309
Daniel Vetterf8175862012-04-10 15:50:11 +020015310 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015311 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015312}
15313
Matt Roperd93c0372015-12-03 11:37:41 -080015314/*
15315 * Calculate what we think the watermarks should be for the state we've read
15316 * out of the hardware and then immediately program those watermarks so that
15317 * we ensure the hardware settings match our internal state.
15318 *
15319 * We can calculate what we think WM's should be by creating a duplicate of the
15320 * current state (which was constructed during hardware readout) and running it
15321 * through the atomic check code to calculate new watermark values in the
15322 * state object.
15323 */
15324static void sanitize_watermarks(struct drm_device *dev)
15325{
15326 struct drm_i915_private *dev_priv = to_i915(dev);
15327 struct drm_atomic_state *state;
15328 struct drm_crtc *crtc;
15329 struct drm_crtc_state *cstate;
15330 struct drm_modeset_acquire_ctx ctx;
15331 int ret;
15332 int i;
15333
15334 /* Only supported on platforms that use atomic watermark design */
Matt Roper396e33a2016-01-06 11:34:30 -080015335 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015336 return;
15337
15338 /*
15339 * We need to hold connection_mutex before calling duplicate_state so
15340 * that the connector loop is protected.
15341 */
15342 drm_modeset_acquire_init(&ctx, 0);
15343retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015344 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015345 if (ret == -EDEADLK) {
15346 drm_modeset_backoff(&ctx);
15347 goto retry;
15348 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015349 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015350 }
15351
15352 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15353 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015354 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015355
Matt Roper396e33a2016-01-06 11:34:30 -080015356 /*
15357 * Hardware readout is the only time we don't want to calculate
15358 * intermediate watermarks (since we don't trust the current
15359 * watermarks).
15360 */
15361 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15362
Matt Roperd93c0372015-12-03 11:37:41 -080015363 ret = intel_atomic_check(dev, state);
15364 if (ret) {
15365 /*
15366 * If we fail here, it means that the hardware appears to be
15367 * programmed in a way that shouldn't be possible, given our
15368 * understanding of watermark requirements. This might mean a
15369 * mistake in the hardware readout code or a mistake in the
15370 * watermark calculations for a given platform. Raise a WARN
15371 * so that this is noticeable.
15372 *
15373 * If this actually happens, we'll have to just leave the
15374 * BIOS-programmed watermarks untouched and hope for the best.
15375 */
15376 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015377 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015378 }
15379
15380 /* Write calculated watermark values back */
15381 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15382 for_each_crtc_in_state(state, crtc, cstate, i) {
15383 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15384
Matt Roper396e33a2016-01-06 11:34:30 -080015385 cs->wm.need_postvbl_update = true;
15386 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015387 }
15388
15389 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015390fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015391 drm_modeset_drop_locks(&ctx);
15392 drm_modeset_acquire_fini(&ctx);
15393}
15394
Jesse Barnes79e53942008-11-07 14:24:08 -080015395void intel_modeset_init(struct drm_device *dev)
15396{
Jesse Barnes652c3932009-08-17 13:31:43 -070015397 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015398 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015399 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015400 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015401
15402 drm_mode_config_init(dev);
15403
15404 dev->mode_config.min_width = 0;
15405 dev->mode_config.min_height = 0;
15406
Dave Airlie019d96c2011-09-29 16:20:42 +010015407 dev->mode_config.preferred_depth = 24;
15408 dev->mode_config.prefer_shadow = 1;
15409
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015410 dev->mode_config.allow_fb_modifiers = true;
15411
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015412 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015413
Jesse Barnesb690e962010-07-19 13:53:12 -070015414 intel_init_quirks(dev);
15415
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015416 intel_init_pm(dev);
15417
Ben Widawskye3c74752013-04-05 13:12:39 -070015418 if (INTEL_INFO(dev)->num_pipes == 0)
15419 return;
15420
Lukas Wunner69f92f62015-07-15 13:57:35 +020015421 /*
15422 * There may be no VBT; and if the BIOS enabled SSC we can
15423 * just keep using it to avoid unnecessary flicker. Whereas if the
15424 * BIOS isn't using it, don't assume it will work even if the VBT
15425 * indicates as much.
15426 */
15427 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15428 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15429 DREF_SSC1_ENABLE);
15430
15431 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15432 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15433 bios_lvds_use_ssc ? "en" : "dis",
15434 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15435 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15436 }
15437 }
15438
Jesse Barnese70236a2009-09-21 10:42:27 -070015439 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015440 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015441
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015442 if (IS_GEN2(dev)) {
15443 dev->mode_config.max_width = 2048;
15444 dev->mode_config.max_height = 2048;
15445 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015446 dev->mode_config.max_width = 4096;
15447 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015448 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015449 dev->mode_config.max_width = 8192;
15450 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015451 }
Damien Lespiau068be562014-03-28 14:17:49 +000015452
Ville Syrjälädc41c152014-08-13 11:57:05 +030015453 if (IS_845G(dev) || IS_I865G(dev)) {
15454 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15455 dev->mode_config.cursor_height = 1023;
15456 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015457 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15458 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15459 } else {
15460 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15461 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15462 }
15463
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015464 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015465
Zhao Yakui28c97732009-10-09 11:39:41 +080015466 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015467 INTEL_INFO(dev)->num_pipes,
15468 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015469
Damien Lespiau055e3932014-08-18 13:49:10 +010015470 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015471 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015472 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015473 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015474 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015475 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015476 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015477 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015478 }
15479
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015480 intel_update_czclk(dev_priv);
15481 intel_update_cdclk(dev);
15482
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015483 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015484
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015485 /* Just disable it once at startup */
15486 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015487 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015488
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015489 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015490 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015491 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015492
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015493 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015494 struct intel_initial_plane_config plane_config = {};
15495
Jesse Barnes46f297f2014-03-07 08:57:48 -080015496 if (!crtc->active)
15497 continue;
15498
Jesse Barnes46f297f2014-03-07 08:57:48 -080015499 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015500 * Note that reserving the BIOS fb up front prevents us
15501 * from stuffing other stolen allocations like the ring
15502 * on top. This prevents some ugliness at boot time, and
15503 * can even allow for smooth boot transitions if the BIOS
15504 * fb is large enough for the active pipe configuration.
15505 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015506 dev_priv->display.get_initial_plane_config(crtc,
15507 &plane_config);
15508
15509 /*
15510 * If the fb is shared between multiple heads, we'll
15511 * just get the first one.
15512 */
15513 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015514 }
Matt Roperd93c0372015-12-03 11:37:41 -080015515
15516 /*
15517 * Make sure hardware watermarks really match the state we read out.
15518 * Note that we need to do this after reconstructing the BIOS fb's
15519 * since the watermark calculation done here will use pstate->fb.
15520 */
15521 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015522}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015523
Daniel Vetter7fad7982012-07-04 17:51:47 +020015524static void intel_enable_pipe_a(struct drm_device *dev)
15525{
15526 struct intel_connector *connector;
15527 struct drm_connector *crt = NULL;
15528 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015529 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015530
15531 /* We can't just switch on the pipe A, we need to set things up with a
15532 * proper mode and output configuration. As a gross hack, enable pipe A
15533 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015534 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015535 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15536 crt = &connector->base;
15537 break;
15538 }
15539 }
15540
15541 if (!crt)
15542 return;
15543
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015544 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015545 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015546}
15547
Daniel Vetterfa555832012-10-10 23:14:00 +020015548static bool
15549intel_check_plane_mapping(struct intel_crtc *crtc)
15550{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015551 struct drm_device *dev = crtc->base.dev;
15552 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015553 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015554
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015555 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015556 return true;
15557
Ville Syrjälä649636e2015-09-22 19:50:01 +030015558 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015559
15560 if ((val & DISPLAY_PLANE_ENABLE) &&
15561 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15562 return false;
15563
15564 return true;
15565}
15566
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015567static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15568{
15569 struct drm_device *dev = crtc->base.dev;
15570 struct intel_encoder *encoder;
15571
15572 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15573 return true;
15574
15575 return false;
15576}
15577
Daniel Vetter24929352012-07-02 20:28:59 +020015578static void intel_sanitize_crtc(struct intel_crtc *crtc)
15579{
15580 struct drm_device *dev = crtc->base.dev;
15581 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015582 i915_reg_t reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015583
Daniel Vetter24929352012-07-02 20:28:59 +020015584 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter24929352012-07-02 20:28:59 +020015585 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15586
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015587 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015588 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015589 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015590 struct intel_plane *plane;
15591
Daniel Vetter96256042015-02-13 21:03:42 +010015592 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015593
15594 /* Disable everything but the primary plane */
15595 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15596 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15597 continue;
15598
15599 plane->disable_plane(&plane->base, &crtc->base);
15600 }
Daniel Vetter96256042015-02-13 21:03:42 +010015601 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015602
Daniel Vetter24929352012-07-02 20:28:59 +020015603 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015604 * disable the crtc (and hence change the state) if it is wrong. Note
15605 * that gen4+ has a fixed plane -> pipe mapping. */
15606 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015607 bool plane;
15608
Daniel Vetter24929352012-07-02 20:28:59 +020015609 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15610 crtc->base.base.id);
15611
15612 /* Pipe has the wrong plane attached and the plane is active.
15613 * Temporarily change the plane mapping and disable everything
15614 * ... */
15615 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015616 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015617 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015618 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015619 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015620 }
Daniel Vetter24929352012-07-02 20:28:59 +020015621
Daniel Vetter7fad7982012-07-04 17:51:47 +020015622 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15623 crtc->pipe == PIPE_A && !crtc->active) {
15624 /* BIOS forgot to enable pipe A, this mostly happens after
15625 * resume. Force-enable the pipe to fix this, the update_dpms
15626 * call below we restore the pipe to the right state, but leave
15627 * the required bits on. */
15628 intel_enable_pipe_a(dev);
15629 }
15630
Daniel Vetter24929352012-07-02 20:28:59 +020015631 /* Adjust the state of the output pipe according to whether we
15632 * have active connectors/encoders. */
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015633 if (!intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015634 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015635
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015636 if (crtc->active != crtc->base.state->active) {
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015637 struct intel_encoder *encoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015638
15639 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015640 * functions or because of calls to intel_crtc_disable_noatomic,
15641 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015642 * pipe A quirk. */
15643 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15644 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015645 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015646 crtc->active ? "enabled" : "disabled");
15647
Maarten Lankhorst4be40c92015-07-14 13:45:32 +020015648 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015649 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015650 crtc->base.enabled = crtc->active;
15651
15652 /* Because we only establish the connector -> encoder ->
15653 * crtc links if something is active, this means the
15654 * crtc is now deactivated. Break the links. connector
15655 * -> encoder links are only establish when things are
15656 * actually up, hence no need to break them. */
15657 WARN_ON(crtc->active);
15658
Maarten Lankhorst2d406bb2015-08-05 12:37:09 +020015659 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetter24929352012-07-02 20:28:59 +020015660 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015661 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015662
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015663 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015664 /*
15665 * We start out with underrun reporting disabled to avoid races.
15666 * For correct bookkeeping mark this on active crtcs.
15667 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015668 * Also on gmch platforms we dont have any hardware bits to
15669 * disable the underrun reporting. Which means we need to start
15670 * out with underrun reporting disabled also on inactive pipes,
15671 * since otherwise we'll complain about the garbage we read when
15672 * e.g. coming up after runtime pm.
15673 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015674 * No protection against concurrent access is required - at
15675 * worst a fifo underrun happens which also sets this to false.
15676 */
15677 crtc->cpu_fifo_underrun_disabled = true;
15678 crtc->pch_fifo_underrun_disabled = true;
15679 }
Daniel Vetter24929352012-07-02 20:28:59 +020015680}
15681
15682static void intel_sanitize_encoder(struct intel_encoder *encoder)
15683{
15684 struct intel_connector *connector;
15685 struct drm_device *dev = encoder->base.dev;
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020015686 bool active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015687
15688 /* We need to check both for a crtc link (meaning that the
15689 * encoder is active and trying to read from a pipe) and the
15690 * pipe itself being active. */
15691 bool has_active_crtc = encoder->base.crtc &&
15692 to_intel_crtc(encoder->base.crtc)->active;
15693
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020015694 for_each_intel_connector(dev, connector) {
15695 if (connector->base.encoder != &encoder->base)
15696 continue;
15697
15698 active = true;
15699 break;
15700 }
15701
15702 if (active && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015703 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15704 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015705 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015706
15707 /* Connector is active, but has no active pipe. This is
15708 * fallout from our resume register restoring. Disable
15709 * the encoder manually again. */
15710 if (encoder->base.crtc) {
15711 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15712 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015713 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015714 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015715 if (encoder->post_disable)
15716 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015717 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015718 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015719
15720 /* Inconsistent output/port/pipe state happens presumably due to
15721 * a bug in one of the get_hw_state functions. Or someplace else
15722 * in our code, like the register restore mess on resume. Clamp
15723 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015724 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015725 if (connector->encoder != encoder)
15726 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015727 connector->base.dpms = DRM_MODE_DPMS_OFF;
15728 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015729 }
15730 }
15731 /* Enabled encoders without active connectors will be fixed in
15732 * the crtc fixup. */
15733}
15734
Imre Deak04098752014-02-18 00:02:16 +020015735void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015736{
15737 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015738 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015739
Imre Deak04098752014-02-18 00:02:16 +020015740 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15741 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15742 i915_disable_vga(dev);
15743 }
15744}
15745
15746void i915_redisable_vga(struct drm_device *dev)
15747{
15748 struct drm_i915_private *dev_priv = dev->dev_private;
15749
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015750 /* This function can be called both from intel_modeset_setup_hw_state or
15751 * at a very early point in our resume sequence, where the power well
15752 * structures are not yet restored. Since this function is at a very
15753 * paranoid "someone might have enabled VGA while we were not looking"
15754 * level, just check if the power well is enabled instead of trying to
15755 * follow the "don't touch the power well if we don't need it" policy
15756 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015757 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015758 return;
15759
Imre Deak04098752014-02-18 00:02:16 +020015760 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015761}
15762
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015763static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015764{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015765 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015766
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015767 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015768}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015769
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015770/* FIXME read out full plane state for all planes */
15771static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015772{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015773 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015774 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015775 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015776
Matt Roper19b8d382015-09-24 15:53:17 -070015777 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015778 primary_get_hw_state(to_intel_plane(primary));
15779
15780 if (plane_state->visible)
15781 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015782}
15783
Daniel Vetter30e984d2013-06-05 13:34:17 +020015784static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015785{
15786 struct drm_i915_private *dev_priv = dev->dev_private;
15787 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015788 struct intel_crtc *crtc;
15789 struct intel_encoder *encoder;
15790 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015791 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015792
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015793 dev_priv->active_crtcs = 0;
15794
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015795 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015796 struct intel_crtc_state *crtc_state = crtc->config;
15797 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015798
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015799 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15800 memset(crtc_state, 0, sizeof(*crtc_state));
15801 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015802
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015803 crtc_state->base.active = crtc_state->base.enable =
15804 dev_priv->display.get_pipe_config(crtc, crtc_state);
15805
15806 crtc->base.enabled = crtc_state->base.enable;
15807 crtc->active = crtc_state->base.active;
15808
15809 if (crtc_state->base.active) {
15810 dev_priv->active_crtcs |= 1 << crtc->pipe;
15811
15812 if (IS_BROADWELL(dev_priv)) {
15813 pixclk = ilk_pipe_pixel_rate(crtc_state);
15814
15815 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15816 if (crtc_state->ips_enabled)
15817 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15818 } else if (IS_VALLEYVIEW(dev_priv) ||
15819 IS_CHERRYVIEW(dev_priv) ||
15820 IS_BROXTON(dev_priv))
15821 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15822 else
15823 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15824 }
15825
15826 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015827
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015828 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015829
15830 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15831 crtc->base.base.id,
15832 crtc->active ? "enabled" : "disabled");
15833 }
15834
Daniel Vetter53589012013-06-05 13:34:16 +020015835 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15836 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15837
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015838 pll->on = pll->get_hw_state(dev_priv, pll,
15839 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015840 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015841 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015842 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015843 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015844 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015845 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015846 }
Daniel Vetter53589012013-06-05 13:34:16 +020015847 }
Daniel Vetter53589012013-06-05 13:34:16 +020015848
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015849 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015850 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015851
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015852 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015853 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015854 }
15855
Damien Lespiaub2784e12014-08-05 11:29:37 +010015856 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015857 pipe = 0;
15858
15859 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015860 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15861 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015862 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015863 } else {
15864 encoder->base.crtc = NULL;
15865 }
15866
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015867 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015868 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015869 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015870 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015871 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015872 }
15873
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015874 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015875 if (connector->get_hw_state(connector)) {
15876 connector->base.dpms = DRM_MODE_DPMS_ON;
Daniel Vetter24929352012-07-02 20:28:59 +020015877 connector->base.encoder = &connector->encoder->base;
15878 } else {
15879 connector->base.dpms = DRM_MODE_DPMS_OFF;
15880 connector->base.encoder = NULL;
15881 }
15882 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15883 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015884 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015885 connector->base.encoder ? "enabled" : "disabled");
15886 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015887
15888 for_each_intel_crtc(dev, crtc) {
15889 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15890
15891 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15892 if (crtc->base.state->active) {
15893 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15894 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15895 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15896
15897 /*
15898 * The initial mode needs to be set in order to keep
15899 * the atomic core happy. It wants a valid mode if the
15900 * crtc's enabled, so we do the above call.
15901 *
15902 * At this point some state updated by the connectors
15903 * in their ->detect() callback has not run yet, so
15904 * no recalculation can be done yet.
15905 *
15906 * Even if we could do a recalculation and modeset
15907 * right now it would cause a double modeset if
15908 * fbdev or userspace chooses a different initial mode.
15909 *
15910 * If that happens, someone indicated they wanted a
15911 * mode change, which means it's safe to do a full
15912 * recalculation.
15913 */
15914 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015915
15916 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15917 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015918 }
15919 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015920}
15921
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015922/* Scan out the current hw modeset state,
15923 * and sanitizes it to the current state
15924 */
15925static void
15926intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015927{
15928 struct drm_i915_private *dev_priv = dev->dev_private;
15929 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015930 struct intel_crtc *crtc;
15931 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015932 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015933
15934 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015935
15936 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015937 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015938 intel_sanitize_encoder(encoder);
15939 }
15940
Damien Lespiau055e3932014-08-18 13:49:10 +010015941 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015942 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15943 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015944 intel_dump_pipe_config(crtc, crtc->config,
15945 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015946 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015947
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015948 intel_modeset_update_connector_atomic_state(dev);
15949
Daniel Vetter35c95372013-07-17 06:55:04 +020015950 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15951 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15952
15953 if (!pll->on || pll->active)
15954 continue;
15955
15956 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15957
15958 pll->disable(dev_priv, pll);
15959 pll->on = false;
15960 }
15961
Wayne Boyer666a4532015-12-09 12:29:35 -080015962 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015963 vlv_wm_get_hw_state(dev);
15964 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015965 skl_wm_get_hw_state(dev);
15966 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015967 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015968
15969 for_each_intel_crtc(dev, crtc) {
15970 unsigned long put_domains;
15971
15972 put_domains = modeset_get_crtc_power_domains(&crtc->base);
15973 if (WARN_ON(put_domains))
15974 modeset_put_power_domains(dev_priv, put_domains);
15975 }
15976 intel_display_set_init_power(dev_priv, false);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015977}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015978
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015979void intel_display_resume(struct drm_device *dev)
15980{
15981 struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
15982 struct intel_connector *conn;
15983 struct intel_plane *plane;
15984 struct drm_crtc *crtc;
15985 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015986
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015987 if (!state)
15988 return;
15989
15990 state->acquire_ctx = dev->mode_config.acquire_ctx;
15991
15992 /* preserve complete old state, including dpll */
15993 intel_atomic_get_shared_dpll_state(state);
15994
15995 for_each_crtc(dev, crtc) {
15996 struct drm_crtc_state *crtc_state =
15997 drm_atomic_get_crtc_state(state, crtc);
15998
15999 ret = PTR_ERR_OR_ZERO(crtc_state);
16000 if (ret)
16001 goto err;
16002
16003 /* force a restore */
16004 crtc_state->mode_changed = true;
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010016005 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020016006
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016007 for_each_intel_plane(dev, plane) {
16008 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
16009 if (ret)
16010 goto err;
16011 }
16012
16013 for_each_intel_connector(dev, conn) {
16014 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
16015 if (ret)
16016 goto err;
16017 }
16018
16019 intel_modeset_setup_hw_state(dev);
16020
16021 i915_redisable_vga(dev);
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020016022 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016023 if (!ret)
16024 return;
16025
16026err:
16027 DRM_ERROR("Restoring old state failed with %i\n", ret);
16028 drm_atomic_state_free(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010016029}
16030
16031void intel_modeset_gem_init(struct drm_device *dev)
16032{
Jesse Barnes484b41d2014-03-07 08:57:55 -080016033 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016034 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016035 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016036
Imre Deakae484342014-03-31 15:10:44 +030016037 mutex_lock(&dev->struct_mutex);
16038 intel_init_gt_powersave(dev);
16039 mutex_unlock(&dev->struct_mutex);
16040
Chris Wilson1833b132012-05-09 11:56:28 +010016041 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016042
16043 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016044
16045 /*
16046 * Make sure any fbs we allocated at startup are properly
16047 * pinned & fenced. When we do the allocation it's too early
16048 * for this.
16049 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016050 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016051 obj = intel_fb_obj(c->primary->fb);
16052 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016053 continue;
16054
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016055 mutex_lock(&dev->struct_mutex);
16056 ret = intel_pin_and_fence_fb_obj(c->primary,
16057 c->primary->fb,
Maarten Lankhorst7580d772015-08-18 13:40:06 +020016058 c->primary->state);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016059 mutex_unlock(&dev->struct_mutex);
16060 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016061 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16062 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016063 drm_framebuffer_unreference(c->primary->fb);
16064 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016065 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080016066 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016067 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016068 }
16069 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016070
16071 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016072}
16073
Imre Deak4932e2c2014-02-11 17:12:48 +020016074void intel_connector_unregister(struct intel_connector *intel_connector)
16075{
16076 struct drm_connector *connector = &intel_connector->base;
16077
16078 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010016079 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016080}
16081
Jesse Barnes79e53942008-11-07 14:24:08 -080016082void intel_modeset_cleanup(struct drm_device *dev)
16083{
Jesse Barnes652c3932009-08-17 13:31:43 -070016084 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020016085 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070016086
Imre Deak2eb52522014-11-19 15:30:05 +020016087 intel_disable_gt_powersave(dev);
16088
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016089 intel_backlight_unregister(dev);
16090
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016091 /*
16092 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016093 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016094 * experience fancy races otherwise.
16095 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016096 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016097
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016098 /*
16099 * Due to the hpd irq storm handling the hotplug work can re-arm the
16100 * poll handlers. Hence disable polling after hpd handling is shut down.
16101 */
Keith Packardf87ea762010-10-03 19:36:26 -070016102 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016103
Jesse Barnes723bfd72010-10-07 16:01:13 -070016104 intel_unregister_dsm_handler();
16105
Paulo Zanoni7733b492015-07-07 15:26:04 -030016106 intel_fbc_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016107
Chris Wilson1630fe72011-07-08 12:22:42 +010016108 /* flush any delayed tasks or pending work */
16109 flush_scheduled_work();
16110
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016111 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016112 for_each_intel_connector(dev, connector)
16113 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016114
Jesse Barnes79e53942008-11-07 14:24:08 -080016115 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016116
16117 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030016118
16119 mutex_lock(&dev->struct_mutex);
16120 intel_cleanup_gt_powersave(dev);
16121 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080016122}
16123
Dave Airlie28d52042009-09-21 14:33:58 +100016124/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016125 * Return which encoder is currently attached for connector.
16126 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016127struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016128{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016129 return &intel_attached_encoder(connector)->base;
16130}
Jesse Barnes79e53942008-11-07 14:24:08 -080016131
Chris Wilsondf0e9242010-09-09 16:20:55 +010016132void intel_connector_attach_encoder(struct intel_connector *connector,
16133 struct intel_encoder *encoder)
16134{
16135 connector->encoder = encoder;
16136 drm_mode_connector_attach_encoder(&connector->base,
16137 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016138}
Dave Airlie28d52042009-09-21 14:33:58 +100016139
16140/*
16141 * set vga decode state - true == enable VGA decode
16142 */
16143int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16144{
16145 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016146 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016147 u16 gmch_ctrl;
16148
Chris Wilson75fa0412014-02-07 18:37:02 -020016149 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16150 DRM_ERROR("failed to read control word\n");
16151 return -EIO;
16152 }
16153
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016154 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16155 return 0;
16156
Dave Airlie28d52042009-09-21 14:33:58 +100016157 if (state)
16158 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16159 else
16160 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016161
16162 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16163 DRM_ERROR("failed to write control word\n");
16164 return -EIO;
16165 }
16166
Dave Airlie28d52042009-09-21 14:33:58 +100016167 return 0;
16168}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016169
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016170struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016171
16172 u32 power_well_driver;
16173
Chris Wilson63b66e52013-08-08 15:12:06 +020016174 int num_transcoders;
16175
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016176 struct intel_cursor_error_state {
16177 u32 control;
16178 u32 position;
16179 u32 base;
16180 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016181 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016182
16183 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016184 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016185 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030016186 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016187 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016188
16189 struct intel_plane_error_state {
16190 u32 control;
16191 u32 stride;
16192 u32 size;
16193 u32 pos;
16194 u32 addr;
16195 u32 surface;
16196 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016197 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016198
16199 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016200 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016201 enum transcoder cpu_transcoder;
16202
16203 u32 conf;
16204
16205 u32 htotal;
16206 u32 hblank;
16207 u32 hsync;
16208 u32 vtotal;
16209 u32 vblank;
16210 u32 vsync;
16211 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016212};
16213
16214struct intel_display_error_state *
16215intel_display_capture_error_state(struct drm_device *dev)
16216{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016217 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016218 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016219 int transcoders[] = {
16220 TRANSCODER_A,
16221 TRANSCODER_B,
16222 TRANSCODER_C,
16223 TRANSCODER_EDP,
16224 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016225 int i;
16226
Chris Wilson63b66e52013-08-08 15:12:06 +020016227 if (INTEL_INFO(dev)->num_pipes == 0)
16228 return NULL;
16229
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016230 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016231 if (error == NULL)
16232 return NULL;
16233
Imre Deak190be112013-11-25 17:15:31 +020016234 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016235 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16236
Damien Lespiau055e3932014-08-18 13:49:10 +010016237 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016238 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016239 __intel_display_power_is_enabled(dev_priv,
16240 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016241 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016242 continue;
16243
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016244 error->cursor[i].control = I915_READ(CURCNTR(i));
16245 error->cursor[i].position = I915_READ(CURPOS(i));
16246 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016247
16248 error->plane[i].control = I915_READ(DSPCNTR(i));
16249 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016250 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016251 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016252 error->plane[i].pos = I915_READ(DSPPOS(i));
16253 }
Paulo Zanonica291362013-03-06 20:03:14 -030016254 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16255 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016256 if (INTEL_INFO(dev)->gen >= 4) {
16257 error->plane[i].surface = I915_READ(DSPSURF(i));
16258 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16259 }
16260
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016261 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030016262
Sonika Jindal3abfce72014-07-21 15:23:43 +053016263 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030016264 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016265 }
16266
16267 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16268 if (HAS_DDI(dev_priv->dev))
16269 error->num_transcoders++; /* Account for eDP. */
16270
16271 for (i = 0; i < error->num_transcoders; i++) {
16272 enum transcoder cpu_transcoder = transcoders[i];
16273
Imre Deakddf9c532013-11-27 22:02:02 +020016274 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016275 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016276 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016277 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016278 continue;
16279
Chris Wilson63b66e52013-08-08 15:12:06 +020016280 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16281
16282 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16283 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16284 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16285 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16286 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16287 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16288 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016289 }
16290
16291 return error;
16292}
16293
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016294#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16295
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016296void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016297intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016298 struct drm_device *dev,
16299 struct intel_display_error_state *error)
16300{
Damien Lespiau055e3932014-08-18 13:49:10 +010016301 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016302 int i;
16303
Chris Wilson63b66e52013-08-08 15:12:06 +020016304 if (!error)
16305 return;
16306
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016307 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016308 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016309 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016310 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016311 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016312 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016313 err_printf(m, " Power: %s\n",
16314 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016315 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030016316 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016317
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016318 err_printf(m, "Plane [%d]:\n", i);
16319 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16320 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016321 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016322 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16323 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016324 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016325 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016326 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016327 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016328 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16329 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016330 }
16331
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016332 err_printf(m, "Cursor [%d]:\n", i);
16333 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16334 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16335 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016336 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016337
16338 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010016339 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016340 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016341 err_printf(m, " Power: %s\n",
16342 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020016343 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16344 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16345 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16346 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16347 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16348 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16349 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16350 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016351}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016352
16353void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
16354{
16355 struct intel_crtc *crtc;
16356
16357 for_each_intel_crtc(dev, crtc) {
16358 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016359
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020016360 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016361
16362 work = crtc->unpin_work;
16363
16364 if (work && work->event &&
16365 work->event->base.file_priv == file) {
16366 kfree(work->event);
16367 work->event = NULL;
16368 }
16369
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020016370 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016371 }
16372}