blob: 3b79981735c81cca7b0170de2d4e99128ff06240 [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
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002226unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002227intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01002228 uint64_t fb_format_modifier, unsigned int plane)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002229{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002230 unsigned int tile_height;
2231 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002232
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002233 switch (fb_format_modifier) {
2234 case DRM_FORMAT_MOD_NONE:
2235 tile_height = 1;
2236 break;
2237 case I915_FORMAT_MOD_X_TILED:
2238 tile_height = IS_GEN2(dev) ? 16 : 8;
2239 break;
2240 case I915_FORMAT_MOD_Y_TILED:
2241 tile_height = 32;
2242 break;
2243 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01002244 pixel_bytes = drm_format_plane_cpp(pixel_format, plane);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002245 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002246 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002247 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002248 tile_height = 64;
2249 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002250 case 2:
2251 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002252 tile_height = 32;
2253 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002254 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002255 tile_height = 16;
2256 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002257 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002258 WARN_ONCE(1,
2259 "128-bit pixels are not supported for display!");
2260 tile_height = 16;
2261 break;
2262 }
2263 break;
2264 default:
2265 MISSING_CASE(fb_format_modifier);
2266 tile_height = 1;
2267 break;
2268 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002269
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002270 return tile_height;
2271}
2272
2273unsigned int
2274intel_fb_align_height(struct drm_device *dev, unsigned int height,
2275 uint32_t pixel_format, uint64_t fb_format_modifier)
2276{
2277 return ALIGN(height, intel_tile_height(dev, pixel_format,
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01002278 fb_format_modifier, 0));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002279}
2280
Daniel Vetter75c82a52015-10-14 16:51:04 +02002281static void
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002282intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2283 const struct drm_plane_state *plane_state)
2284{
Daniel Vettera6d09182015-10-14 16:51:05 +02002285 struct intel_rotation_info *info = &view->params.rotation_info;
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002286 unsigned int tile_height, tile_pitch;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002287
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002288 *view = i915_ggtt_view_normal;
2289
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002290 if (!plane_state)
Daniel Vetter75c82a52015-10-14 16:51:04 +02002291 return;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002292
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002293 if (!intel_rotation_90_or_270(plane_state->rotation))
Daniel Vetter75c82a52015-10-14 16:51:04 +02002294 return;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002295
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002296 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002297
2298 info->height = fb->height;
2299 info->pixel_format = fb->pixel_format;
2300 info->pitch = fb->pitches[0];
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002301 info->uv_offset = fb->offsets[1];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002302 info->fb_modifier = fb->modifier[0];
2303
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002304 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01002305 fb->modifier[0], 0);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002306 tile_pitch = PAGE_SIZE / tile_height;
2307 info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2308 info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
2309 info->size = info->width_pages * info->height_pages * PAGE_SIZE;
2310
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002311 if (info->pixel_format == DRM_FORMAT_NV12) {
2312 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
2313 fb->modifier[0], 1);
2314 tile_pitch = PAGE_SIZE / tile_height;
2315 info->width_pages_uv = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2316 info->height_pages_uv = DIV_ROUND_UP(fb->height / 2,
2317 tile_height);
2318 info->size_uv = info->width_pages_uv * info->height_pages_uv *
2319 PAGE_SIZE;
2320 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002321}
2322
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002323static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2324{
2325 if (INTEL_INFO(dev_priv)->gen >= 9)
2326 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002327 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002328 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002329 return 128 * 1024;
2330 else if (INTEL_INFO(dev_priv)->gen >= 4)
2331 return 4 * 1024;
2332 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002333 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002334}
2335
Chris Wilson127bd2a2010-07-23 23:32:05 +01002336int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002337intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2338 struct drm_framebuffer *fb,
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002339 const struct drm_plane_state *plane_state)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002340{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002341 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002342 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002343 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002344 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002345 u32 alignment;
2346 int ret;
2347
Matt Roperebcdd392014-07-09 16:22:11 -07002348 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2349
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002350 switch (fb->modifier[0]) {
2351 case DRM_FORMAT_MOD_NONE:
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002352 alignment = intel_linear_alignment(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002353 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002354 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002355 if (INTEL_INFO(dev)->gen >= 9)
2356 alignment = 256 * 1024;
2357 else {
2358 /* pin() will align the object as required by fence */
2359 alignment = 0;
2360 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002361 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002362 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002363 case I915_FORMAT_MOD_Yf_TILED:
2364 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2365 "Y tiling bo slipped through, driver bug!\n"))
2366 return -EINVAL;
2367 alignment = 1 * 1024 * 1024;
2368 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002369 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002370 MISSING_CASE(fb->modifier[0]);
2371 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002372 }
2373
Daniel Vetter75c82a52015-10-14 16:51:04 +02002374 intel_fill_fb_ggtt_view(&view, fb, plane_state);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002375
Chris Wilson693db182013-03-05 14:52:39 +00002376 /* Note that the w/a also requires 64 PTE of padding following the
2377 * bo. We currently fill all unused PTE with the shadow page and so
2378 * we should always have valid PTE following the scanout preventing
2379 * the VT-d warning.
2380 */
2381 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2382 alignment = 256 * 1024;
2383
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002384 /*
2385 * Global gtt pte registers are special registers which actually forward
2386 * writes to a chunk of system memory. Which means that there is no risk
2387 * that the register values disappear as soon as we call
2388 * intel_runtime_pm_put(), so it is correct to wrap only the
2389 * pin/unpin/fence and not more.
2390 */
2391 intel_runtime_pm_get(dev_priv);
2392
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002393 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2394 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002395 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002396 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002397
2398 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2399 * fence, whereas 965+ only requires a fence if using
2400 * framebuffer compression. For simplicity, we always install
2401 * a fence as the cost is not that onerous.
2402 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002403 if (view.type == I915_GGTT_VIEW_NORMAL) {
2404 ret = i915_gem_object_get_fence(obj);
2405 if (ret == -EDEADLK) {
2406 /*
2407 * -EDEADLK means there are no free fences
2408 * no pending flips.
2409 *
2410 * This is propagated to atomic, but it uses
2411 * -EDEADLK to force a locking recovery, so
2412 * change the returned error to -EBUSY.
2413 */
2414 ret = -EBUSY;
2415 goto err_unpin;
2416 } else if (ret)
2417 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002418
Vivek Kasireddy98072162015-10-29 18:54:38 -07002419 i915_gem_object_pin_fence(obj);
2420 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002421
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002422 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002423 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002424
2425err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002426 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002427err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002428 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002429 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002430}
2431
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002432static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2433 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002434{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002435 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002436 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002437
Matt Roperebcdd392014-07-09 16:22:11 -07002438 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2439
Daniel Vetter75c82a52015-10-14 16:51:04 +02002440 intel_fill_fb_ggtt_view(&view, fb, plane_state);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002441
Vivek Kasireddy98072162015-10-29 18:54:38 -07002442 if (view.type == I915_GGTT_VIEW_NORMAL)
2443 i915_gem_object_unpin_fence(obj);
2444
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002445 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002446}
2447
Daniel Vetterc2c75132012-07-05 12:17:30 +02002448/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2449 * is assumed to be a power-of-two. */
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002450unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2451 int *x, int *y,
Chris Wilsonbc752862013-02-21 20:04:31 +00002452 unsigned int tiling_mode,
2453 unsigned int cpp,
2454 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002455{
Chris Wilsonbc752862013-02-21 20:04:31 +00002456 if (tiling_mode != I915_TILING_NONE) {
2457 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002458
Chris Wilsonbc752862013-02-21 20:04:31 +00002459 tile_rows = *y / 8;
2460 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002461
Chris Wilsonbc752862013-02-21 20:04:31 +00002462 tiles = *x / (512/cpp);
2463 *x %= 512/cpp;
2464
2465 return tile_rows * pitch * 8 + tiles * 4096;
2466 } else {
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002467 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
Chris Wilsonbc752862013-02-21 20:04:31 +00002468 unsigned int offset;
2469
2470 offset = *y * pitch + *x * cpp;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002471 *y = (offset & alignment) / pitch;
2472 *x = ((offset & alignment) - *y * pitch) / cpp;
2473 return offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002474 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002475}
2476
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002477static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002478{
2479 switch (format) {
2480 case DISPPLANE_8BPP:
2481 return DRM_FORMAT_C8;
2482 case DISPPLANE_BGRX555:
2483 return DRM_FORMAT_XRGB1555;
2484 case DISPPLANE_BGRX565:
2485 return DRM_FORMAT_RGB565;
2486 default:
2487 case DISPPLANE_BGRX888:
2488 return DRM_FORMAT_XRGB8888;
2489 case DISPPLANE_RGBX888:
2490 return DRM_FORMAT_XBGR8888;
2491 case DISPPLANE_BGRX101010:
2492 return DRM_FORMAT_XRGB2101010;
2493 case DISPPLANE_RGBX101010:
2494 return DRM_FORMAT_XBGR2101010;
2495 }
2496}
2497
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002498static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2499{
2500 switch (format) {
2501 case PLANE_CTL_FORMAT_RGB_565:
2502 return DRM_FORMAT_RGB565;
2503 default:
2504 case PLANE_CTL_FORMAT_XRGB_8888:
2505 if (rgb_order) {
2506 if (alpha)
2507 return DRM_FORMAT_ABGR8888;
2508 else
2509 return DRM_FORMAT_XBGR8888;
2510 } else {
2511 if (alpha)
2512 return DRM_FORMAT_ARGB8888;
2513 else
2514 return DRM_FORMAT_XRGB8888;
2515 }
2516 case PLANE_CTL_FORMAT_XRGB_2101010:
2517 if (rgb_order)
2518 return DRM_FORMAT_XBGR2101010;
2519 else
2520 return DRM_FORMAT_XRGB2101010;
2521 }
2522}
2523
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002524static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002525intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2526 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002527{
2528 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002529 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002530 struct drm_i915_gem_object *obj = NULL;
2531 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002532 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002533 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2534 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2535 PAGE_SIZE);
2536
2537 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002538
Chris Wilsonff2652e2014-03-10 08:07:02 +00002539 if (plane_config->size == 0)
2540 return false;
2541
Paulo Zanoni3badb492015-09-23 12:52:23 -03002542 /* If the FB is too big, just don't use it since fbdev is not very
2543 * important and we should probably use that space with FBC or other
2544 * features. */
2545 if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
2546 return false;
2547
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002548 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2549 base_aligned,
2550 base_aligned,
2551 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002552 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002553 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002554
Damien Lespiau49af4492015-01-20 12:51:44 +00002555 obj->tiling_mode = plane_config->tiling;
2556 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002557 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002558
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002559 mode_cmd.pixel_format = fb->pixel_format;
2560 mode_cmd.width = fb->width;
2561 mode_cmd.height = fb->height;
2562 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002563 mode_cmd.modifier[0] = fb->modifier[0];
2564 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002565
2566 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002567 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002568 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002569 DRM_DEBUG_KMS("intel fb init failed\n");
2570 goto out_unref_obj;
2571 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002572 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002573
Daniel Vetterf6936e22015-03-26 12:17:05 +01002574 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002575 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002576
2577out_unref_obj:
2578 drm_gem_object_unreference(&obj->base);
2579 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002580 return false;
2581}
2582
Matt Roperafd65eb2015-02-03 13:10:04 -08002583/* Update plane->state->fb to match plane->fb after driver-internal updates */
2584static void
2585update_state_fb(struct drm_plane *plane)
2586{
2587 if (plane->fb == plane->state->fb)
2588 return;
2589
2590 if (plane->state->fb)
2591 drm_framebuffer_unreference(plane->state->fb);
2592 plane->state->fb = plane->fb;
2593 if (plane->state->fb)
2594 drm_framebuffer_reference(plane->state->fb);
2595}
2596
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002597static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002598intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2599 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002600{
2601 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002602 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002603 struct drm_crtc *c;
2604 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002605 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002606 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002607 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002608 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2609 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002610 struct intel_plane_state *intel_state =
2611 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002612 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002613
Damien Lespiau2d140302015-02-05 17:22:18 +00002614 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002615 return;
2616
Daniel Vetterf6936e22015-03-26 12:17:05 +01002617 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002618 fb = &plane_config->fb->base;
2619 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002620 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002621
Damien Lespiau2d140302015-02-05 17:22:18 +00002622 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002623
2624 /*
2625 * Failed to alloc the obj, check to see if we should share
2626 * an fb with another CRTC instead
2627 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002628 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002629 i = to_intel_crtc(c);
2630
2631 if (c == &intel_crtc->base)
2632 continue;
2633
Matt Roper2ff8fde2014-07-08 07:50:07 -07002634 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002635 continue;
2636
Daniel Vetter88595ac2015-03-26 12:42:24 +01002637 fb = c->primary->fb;
2638 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002639 continue;
2640
Daniel Vetter88595ac2015-03-26 12:42:24 +01002641 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002642 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002643 drm_framebuffer_reference(fb);
2644 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002645 }
2646 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002647
Matt Roper200757f2015-12-03 11:37:36 -08002648 /*
2649 * We've failed to reconstruct the BIOS FB. Current display state
2650 * indicates that the primary plane is visible, but has a NULL FB,
2651 * which will lead to problems later if we don't fix it up. The
2652 * simplest solution is to just disable the primary plane now and
2653 * pretend the BIOS never had it enabled.
2654 */
2655 to_intel_plane_state(plane_state)->visible = false;
2656 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2657 intel_pre_disable_primary(&intel_crtc->base);
2658 intel_plane->disable_plane(primary, &intel_crtc->base);
2659
Daniel Vetter88595ac2015-03-26 12:42:24 +01002660 return;
2661
2662valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002663 plane_state->src_x = 0;
2664 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002665 plane_state->src_w = fb->width << 16;
2666 plane_state->src_h = fb->height << 16;
2667
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002668 plane_state->crtc_x = 0;
2669 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002670 plane_state->crtc_w = fb->width;
2671 plane_state->crtc_h = fb->height;
2672
Matt Roper0a8d8a82015-12-03 11:37:38 -08002673 intel_state->src.x1 = plane_state->src_x;
2674 intel_state->src.y1 = plane_state->src_y;
2675 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2676 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2677 intel_state->dst.x1 = plane_state->crtc_x;
2678 intel_state->dst.y1 = plane_state->crtc_y;
2679 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2680 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2681
Daniel Vetter88595ac2015-03-26 12:42:24 +01002682 obj = intel_fb_obj(fb);
2683 if (obj->tiling_mode != I915_TILING_NONE)
2684 dev_priv->preserve_bios_swizzle = true;
2685
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002686 drm_framebuffer_reference(fb);
2687 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002688 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002689 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002690 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002691}
2692
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002693static void i9xx_update_primary_plane(struct drm_plane *primary,
2694 const struct intel_crtc_state *crtc_state,
2695 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002696{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002697 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002698 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002699 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2700 struct drm_framebuffer *fb = plane_state->base.fb;
2701 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002702 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002703 unsigned long linear_offset;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002704 int x = plane_state->src.x1 >> 16;
2705 int y = plane_state->src.y1 >> 16;
Jesse Barnes81255562010-08-02 12:07:50 -07002706 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002707 i915_reg_t reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302708 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002709
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002710 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2711
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002712 dspcntr = DISPPLANE_GAMMA_ENABLE;
2713
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002714 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002715
2716 if (INTEL_INFO(dev)->gen < 4) {
2717 if (intel_crtc->pipe == PIPE_B)
2718 dspcntr |= DISPPLANE_SEL_PIPE_B;
2719
2720 /* pipesrc and dspsize control the size that is scaled from,
2721 * which should always be the user's requested size.
2722 */
2723 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002724 ((crtc_state->pipe_src_h - 1) << 16) |
2725 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002726 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002727 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2728 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002729 ((crtc_state->pipe_src_h - 1) << 16) |
2730 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002731 I915_WRITE(PRIMPOS(plane), 0);
2732 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002733 }
2734
Ville Syrjälä57779d02012-10-31 17:50:14 +02002735 switch (fb->pixel_format) {
2736 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002737 dspcntr |= DISPPLANE_8BPP;
2738 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002739 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002740 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002741 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002742 case DRM_FORMAT_RGB565:
2743 dspcntr |= DISPPLANE_BGRX565;
2744 break;
2745 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002746 dspcntr |= DISPPLANE_BGRX888;
2747 break;
2748 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002749 dspcntr |= DISPPLANE_RGBX888;
2750 break;
2751 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002752 dspcntr |= DISPPLANE_BGRX101010;
2753 break;
2754 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002755 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002756 break;
2757 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002758 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002759 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002760
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002761 if (INTEL_INFO(dev)->gen >= 4 &&
2762 obj->tiling_mode != I915_TILING_NONE)
2763 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002764
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002765 if (IS_G4X(dev))
2766 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2767
Ville Syrjäläb98971272014-08-27 16:51:22 +03002768 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002769
Daniel Vetterc2c75132012-07-05 12:17:30 +02002770 if (INTEL_INFO(dev)->gen >= 4) {
2771 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002772 intel_gen4_compute_page_offset(dev_priv,
2773 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002774 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002775 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002776 linear_offset -= intel_crtc->dspaddr_offset;
2777 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002778 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002779 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002780
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002781 if (plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302782 dspcntr |= DISPPLANE_ROTATE_180;
2783
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002784 x += (crtc_state->pipe_src_w - 1);
2785 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302786
2787 /* Finding the last pixel of the last line of the display
2788 data and adding to linear_offset*/
2789 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002790 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
2791 (crtc_state->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302792 }
2793
Paulo Zanoni2db33662015-09-14 15:20:03 -03002794 intel_crtc->adjusted_x = x;
2795 intel_crtc->adjusted_y = y;
2796
Sonika Jindal48404c12014-08-22 14:06:04 +05302797 I915_WRITE(reg, dspcntr);
2798
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002799 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002800 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002801 I915_WRITE(DSPSURF(plane),
2802 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002803 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002804 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002805 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002806 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002807 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002808}
2809
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002810static void i9xx_disable_primary_plane(struct drm_plane *primary,
2811 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002812{
2813 struct drm_device *dev = crtc->dev;
2814 struct drm_i915_private *dev_priv = dev->dev_private;
2815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002816 int plane = intel_crtc->plane;
2817
2818 I915_WRITE(DSPCNTR(plane), 0);
2819 if (INTEL_INFO(dev_priv)->gen >= 4)
2820 I915_WRITE(DSPSURF(plane), 0);
2821 else
2822 I915_WRITE(DSPADDR(plane), 0);
2823 POSTING_READ(DSPCNTR(plane));
2824}
2825
2826static void ironlake_update_primary_plane(struct drm_plane *primary,
2827 const struct intel_crtc_state *crtc_state,
2828 const struct intel_plane_state *plane_state)
2829{
2830 struct drm_device *dev = primary->dev;
2831 struct drm_i915_private *dev_priv = dev->dev_private;
2832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2833 struct drm_framebuffer *fb = plane_state->base.fb;
2834 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002835 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002836 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002837 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002838 i915_reg_t reg = DSPCNTR(plane);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002839 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2840 int x = plane_state->src.x1 >> 16;
2841 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002842
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002843 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002844 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002845
2846 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2847 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2848
Ville Syrjälä57779d02012-10-31 17:50:14 +02002849 switch (fb->pixel_format) {
2850 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002851 dspcntr |= DISPPLANE_8BPP;
2852 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002853 case DRM_FORMAT_RGB565:
2854 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002855 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002856 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002857 dspcntr |= DISPPLANE_BGRX888;
2858 break;
2859 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002860 dspcntr |= DISPPLANE_RGBX888;
2861 break;
2862 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002863 dspcntr |= DISPPLANE_BGRX101010;
2864 break;
2865 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002866 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002867 break;
2868 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002869 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002870 }
2871
2872 if (obj->tiling_mode != I915_TILING_NONE)
2873 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002874
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002875 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002876 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002877
Ville Syrjäläb98971272014-08-27 16:51:22 +03002878 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002879 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002880 intel_gen4_compute_page_offset(dev_priv,
2881 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002882 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002883 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002884 linear_offset -= intel_crtc->dspaddr_offset;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002885 if (plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302886 dspcntr |= DISPPLANE_ROTATE_180;
2887
2888 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002889 x += (crtc_state->pipe_src_w - 1);
2890 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302891
2892 /* Finding the last pixel of the last line of the display
2893 data and adding to linear_offset*/
2894 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002895 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
2896 (crtc_state->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302897 }
2898 }
2899
Paulo Zanoni2db33662015-09-14 15:20:03 -03002900 intel_crtc->adjusted_x = x;
2901 intel_crtc->adjusted_y = y;
2902
Sonika Jindal48404c12014-08-22 14:06:04 +05302903 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002904
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002905 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002906 I915_WRITE(DSPSURF(plane),
2907 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002908 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002909 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2910 } else {
2911 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2912 I915_WRITE(DSPLINOFF(plane), linear_offset);
2913 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002914 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002915}
2916
Damien Lespiaub3218032015-02-27 11:15:18 +00002917u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2918 uint32_t pixel_format)
2919{
2920 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2921
2922 /*
2923 * The stride is either expressed as a multiple of 64 bytes
2924 * chunks for linear buffers or in number of tiles for tiled
2925 * buffers.
2926 */
2927 switch (fb_modifier) {
2928 case DRM_FORMAT_MOD_NONE:
2929 return 64;
2930 case I915_FORMAT_MOD_X_TILED:
2931 if (INTEL_INFO(dev)->gen == 2)
2932 return 128;
2933 return 512;
2934 case I915_FORMAT_MOD_Y_TILED:
2935 /* No need to check for old gens and Y tiling since this is
2936 * about the display engine and those will be blocked before
2937 * we get here.
2938 */
2939 return 128;
2940 case I915_FORMAT_MOD_Yf_TILED:
2941 if (bits_per_pixel == 8)
2942 return 64;
2943 else
2944 return 128;
2945 default:
2946 MISSING_CASE(fb_modifier);
2947 return 64;
2948 }
2949}
2950
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002951u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2952 struct drm_i915_gem_object *obj,
2953 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002954{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002955 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002956 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002957 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002958
Daniel Vetterce7f1722015-10-14 16:51:06 +02002959 intel_fill_fb_ggtt_view(&view, intel_plane->base.fb,
2960 intel_plane->base.state);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002961
Daniel Vetterce7f1722015-10-14 16:51:06 +02002962 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002963 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002964 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002965 return -1;
2966
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002967 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002968
2969 if (plane == 1) {
Daniel Vettera6d09182015-10-14 16:51:05 +02002970 offset += vma->ggtt_view.params.rotation_info.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002971 PAGE_SIZE;
2972 }
2973
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002974 WARN_ON(upper_32_bits(offset));
2975
2976 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002977}
2978
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002979static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2980{
2981 struct drm_device *dev = intel_crtc->base.dev;
2982 struct drm_i915_private *dev_priv = dev->dev_private;
2983
2984 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2985 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2986 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002987}
2988
Chandra Kondurua1b22782015-04-07 15:28:45 -07002989/*
2990 * This function detaches (aka. unbinds) unused scalers in hardware
2991 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002992static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002993{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002994 struct intel_crtc_scaler_state *scaler_state;
2995 int i;
2996
Chandra Kondurua1b22782015-04-07 15:28:45 -07002997 scaler_state = &intel_crtc->config->scaler_state;
2998
2999 /* loop through and disable scalers that aren't in use */
3000 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003001 if (!scaler_state->scalers[i].in_use)
3002 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07003003 }
3004}
3005
Chandra Konduru6156a452015-04-27 13:48:39 -07003006u32 skl_plane_ctl_format(uint32_t pixel_format)
3007{
Chandra Konduru6156a452015-04-27 13:48:39 -07003008 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01003009 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003010 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07003011 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003012 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07003013 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003014 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003015 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003016 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003017 /*
3018 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3019 * to be already pre-multiplied. We need to add a knob (or a different
3020 * DRM_FORMAT) for user-space to configure that.
3021 */
3022 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003023 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07003024 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003025 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003026 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07003027 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003028 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003029 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003030 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003031 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003032 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003033 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003034 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003035 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003036 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003037 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003038 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003039 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003040 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003041 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003042 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003043
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003044 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003045}
3046
3047u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3048{
Chandra Konduru6156a452015-04-27 13:48:39 -07003049 switch (fb_modifier) {
3050 case DRM_FORMAT_MOD_NONE:
3051 break;
3052 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003053 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003054 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003055 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003056 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003057 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003058 default:
3059 MISSING_CASE(fb_modifier);
3060 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003061
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003062 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003063}
3064
3065u32 skl_plane_ctl_rotation(unsigned int rotation)
3066{
Chandra Konduru6156a452015-04-27 13:48:39 -07003067 switch (rotation) {
3068 case BIT(DRM_ROTATE_0):
3069 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303070 /*
3071 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3072 * while i915 HW rotation is clockwise, thats why this swapping.
3073 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003074 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303075 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003076 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003077 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003078 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303079 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003080 default:
3081 MISSING_CASE(rotation);
3082 }
3083
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003084 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003085}
3086
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003087static void skylake_update_primary_plane(struct drm_plane *plane,
3088 const struct intel_crtc_state *crtc_state,
3089 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003090{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003091 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003092 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003093 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3094 struct drm_framebuffer *fb = plane_state->base.fb;
3095 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003096 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303097 u32 plane_ctl, stride_div, stride;
3098 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003099 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303100 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003101 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003102 int scaler_id = plane_state->scaler_id;
3103 int src_x = plane_state->src.x1 >> 16;
3104 int src_y = plane_state->src.y1 >> 16;
3105 int src_w = drm_rect_width(&plane_state->src) >> 16;
3106 int src_h = drm_rect_height(&plane_state->src) >> 16;
3107 int dst_x = plane_state->dst.x1;
3108 int dst_y = plane_state->dst.y1;
3109 int dst_w = drm_rect_width(&plane_state->dst);
3110 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003111
3112 plane_ctl = PLANE_CTL_ENABLE |
3113 PLANE_CTL_PIPE_GAMMA_ENABLE |
3114 PLANE_CTL_PIPE_CSC_ENABLE;
3115
Chandra Konduru6156a452015-04-27 13:48:39 -07003116 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3117 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003118 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003119 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003120
Damien Lespiaub3218032015-02-27 11:15:18 +00003121 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3122 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003123 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303124
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003125 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003126
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303127 if (intel_rotation_90_or_270(rotation)) {
3128 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003129 tile_height = intel_tile_height(dev, fb->pixel_format,
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01003130 fb->modifier[0], 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303131 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003132 x_offset = stride * tile_height - src_y - src_h;
3133 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003134 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303135 } else {
3136 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003137 x_offset = src_x;
3138 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003139 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303140 }
3141 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003142
Paulo Zanoni2db33662015-09-14 15:20:03 -03003143 intel_crtc->adjusted_x = x_offset;
3144 intel_crtc->adjusted_y = y_offset;
3145
Damien Lespiau70d21f02013-07-03 21:06:04 +01003146 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303147 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3148 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3149 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003150
3151 if (scaler_id >= 0) {
3152 uint32_t ps_ctrl = 0;
3153
3154 WARN_ON(!dst_w || !dst_h);
3155 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3156 crtc_state->scaler_state.scalers[scaler_id].mode;
3157 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3158 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3159 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3160 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3161 I915_WRITE(PLANE_POS(pipe, 0), 0);
3162 } else {
3163 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3164 }
3165
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003166 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003167
3168 POSTING_READ(PLANE_SURF(pipe, 0));
3169}
3170
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003171static void skylake_disable_primary_plane(struct drm_plane *primary,
3172 struct drm_crtc *crtc)
3173{
3174 struct drm_device *dev = crtc->dev;
3175 struct drm_i915_private *dev_priv = dev->dev_private;
3176 int pipe = to_intel_crtc(crtc)->pipe;
3177
3178 if (dev_priv->fbc.deactivate)
3179 dev_priv->fbc.deactivate(dev_priv);
3180
3181 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3182 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3183 POSTING_READ(PLANE_SURF(pipe, 0));
3184}
3185
Jesse Barnes17638cd2011-06-24 12:19:23 -07003186/* Assume fb object is pinned & idle & fenced and just update base pointers */
3187static int
3188intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3189 int x, int y, enum mode_set_atomic state)
3190{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003191 /* Support for kgdboc is disabled, this needs a major rework. */
3192 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003193
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003194 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003195}
3196
Ville Syrjälä75147472014-11-24 18:28:11 +02003197static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003198{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003199 struct drm_crtc *crtc;
3200
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003201 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003202 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3203 enum plane plane = intel_crtc->plane;
3204
3205 intel_prepare_page_flip(dev, plane);
3206 intel_finish_page_flip_plane(dev, plane);
3207 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003208}
3209
3210static void intel_update_primary_planes(struct drm_device *dev)
3211{
Ville Syrjälä75147472014-11-24 18:28:11 +02003212 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003213
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003214 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003215 struct intel_plane *plane = to_intel_plane(crtc->primary);
3216 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003217
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003218 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003219 plane_state = to_intel_plane_state(plane->base.state);
3220
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003221 if (plane_state->visible)
3222 plane->update_plane(&plane->base,
3223 to_intel_crtc_state(crtc->state),
3224 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003225
3226 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003227 }
3228}
3229
Ville Syrjälä75147472014-11-24 18:28:11 +02003230void intel_prepare_reset(struct drm_device *dev)
3231{
3232 /* no reset support for gen2 */
3233 if (IS_GEN2(dev))
3234 return;
3235
3236 /* reset doesn't touch the display */
3237 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3238 return;
3239
3240 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003241 /*
3242 * Disabling the crtcs gracefully seems nicer. Also the
3243 * g33 docs say we should at least disable all the planes.
3244 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003245 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003246}
3247
3248void intel_finish_reset(struct drm_device *dev)
3249{
3250 struct drm_i915_private *dev_priv = to_i915(dev);
3251
3252 /*
3253 * Flips in the rings will be nuked by the reset,
3254 * so complete all pending flips so that user space
3255 * will get its events and not get stuck.
3256 */
3257 intel_complete_page_flips(dev);
3258
3259 /* no reset support for gen2 */
3260 if (IS_GEN2(dev))
3261 return;
3262
3263 /* reset doesn't touch the display */
3264 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3265 /*
3266 * Flips in the rings have been nuked by the reset,
3267 * so update the base address of all primary
3268 * planes to the the last fb to make sure we're
3269 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003270 *
3271 * FIXME: Atomic will make this obsolete since we won't schedule
3272 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003273 */
3274 intel_update_primary_planes(dev);
3275 return;
3276 }
3277
3278 /*
3279 * The display has been reset as well,
3280 * so need a full re-initialization.
3281 */
3282 intel_runtime_pm_disable_interrupts(dev_priv);
3283 intel_runtime_pm_enable_interrupts(dev_priv);
3284
3285 intel_modeset_init_hw(dev);
3286
3287 spin_lock_irq(&dev_priv->irq_lock);
3288 if (dev_priv->display.hpd_irq_setup)
3289 dev_priv->display.hpd_irq_setup(dev);
3290 spin_unlock_irq(&dev_priv->irq_lock);
3291
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003292 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003293
3294 intel_hpd_init(dev_priv);
3295
3296 drm_modeset_unlock_all(dev);
3297}
3298
Chris Wilson7d5e3792014-03-04 13:15:08 +00003299static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3300{
3301 struct drm_device *dev = crtc->dev;
3302 struct drm_i915_private *dev_priv = dev->dev_private;
3303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003304 bool pending;
3305
3306 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3307 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3308 return false;
3309
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003310 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003311 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003312 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003313
3314 return pending;
3315}
3316
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003317static void intel_update_pipe_config(struct intel_crtc *crtc,
3318 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003319{
3320 struct drm_device *dev = crtc->base.dev;
3321 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003322 struct intel_crtc_state *pipe_config =
3323 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003324
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003325 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3326 crtc->base.mode = crtc->base.state->mode;
3327
3328 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3329 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3330 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003331
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003332 if (HAS_DDI(dev))
3333 intel_set_pipe_csc(&crtc->base);
3334
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003335 /*
3336 * Update pipe size and adjust fitter if needed: the reason for this is
3337 * that in compute_mode_changes we check the native mode (not the pfit
3338 * mode) to see if we can flip rather than do a full mode set. In the
3339 * fastboot case, we'll flip, but if we don't update the pipesrc and
3340 * pfit state, we'll end up with a big fb scanned out into the wrong
3341 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003342 */
3343
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003344 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003345 ((pipe_config->pipe_src_w - 1) << 16) |
3346 (pipe_config->pipe_src_h - 1));
3347
3348 /* on skylake this is done by detaching scalers */
3349 if (INTEL_INFO(dev)->gen >= 9) {
3350 skl_detach_scalers(crtc);
3351
3352 if (pipe_config->pch_pfit.enabled)
3353 skylake_pfit_enable(crtc);
3354 } else if (HAS_PCH_SPLIT(dev)) {
3355 if (pipe_config->pch_pfit.enabled)
3356 ironlake_pfit_enable(crtc);
3357 else if (old_crtc_state->pch_pfit.enabled)
3358 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003359 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003360}
3361
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003362static void intel_fdi_normal_train(struct drm_crtc *crtc)
3363{
3364 struct drm_device *dev = crtc->dev;
3365 struct drm_i915_private *dev_priv = dev->dev_private;
3366 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3367 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003368 i915_reg_t reg;
3369 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003370
3371 /* enable normal train */
3372 reg = FDI_TX_CTL(pipe);
3373 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003374 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003375 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3376 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003377 } else {
3378 temp &= ~FDI_LINK_TRAIN_NONE;
3379 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003380 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003381 I915_WRITE(reg, temp);
3382
3383 reg = FDI_RX_CTL(pipe);
3384 temp = I915_READ(reg);
3385 if (HAS_PCH_CPT(dev)) {
3386 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3387 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3388 } else {
3389 temp &= ~FDI_LINK_TRAIN_NONE;
3390 temp |= FDI_LINK_TRAIN_NONE;
3391 }
3392 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3393
3394 /* wait one idle pattern time */
3395 POSTING_READ(reg);
3396 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003397
3398 /* IVB wants error correction enabled */
3399 if (IS_IVYBRIDGE(dev))
3400 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3401 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003402}
3403
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003404/* The FDI link training functions for ILK/Ibexpeak. */
3405static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3406{
3407 struct drm_device *dev = crtc->dev;
3408 struct drm_i915_private *dev_priv = dev->dev_private;
3409 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3410 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003411 i915_reg_t reg;
3412 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003413
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003414 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003415 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003416
Adam Jacksone1a44742010-06-25 15:32:14 -04003417 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3418 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003419 reg = FDI_RX_IMR(pipe);
3420 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003421 temp &= ~FDI_RX_SYMBOL_LOCK;
3422 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003423 I915_WRITE(reg, temp);
3424 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003425 udelay(150);
3426
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003427 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003428 reg = FDI_TX_CTL(pipe);
3429 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003430 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003431 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003432 temp &= ~FDI_LINK_TRAIN_NONE;
3433 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003435
Chris Wilson5eddb702010-09-11 13:48:45 +01003436 reg = FDI_RX_CTL(pipe);
3437 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003438 temp &= ~FDI_LINK_TRAIN_NONE;
3439 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003440 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3441
3442 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003443 udelay(150);
3444
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003445 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003446 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3447 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3448 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003449
Chris Wilson5eddb702010-09-11 13:48:45 +01003450 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003451 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003452 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003453 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3454
3455 if ((temp & FDI_RX_BIT_LOCK)) {
3456 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003457 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003458 break;
3459 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003460 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003461 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003462 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003463
3464 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003465 reg = FDI_TX_CTL(pipe);
3466 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467 temp &= ~FDI_LINK_TRAIN_NONE;
3468 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003470
Chris Wilson5eddb702010-09-11 13:48:45 +01003471 reg = FDI_RX_CTL(pipe);
3472 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473 temp &= ~FDI_LINK_TRAIN_NONE;
3474 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003475 I915_WRITE(reg, temp);
3476
3477 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003478 udelay(150);
3479
Chris Wilson5eddb702010-09-11 13:48:45 +01003480 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003481 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003482 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003483 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3484
3485 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003486 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003487 DRM_DEBUG_KMS("FDI train 2 done.\n");
3488 break;
3489 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003491 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003492 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003493
3494 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003495
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003496}
3497
Akshay Joshi0206e352011-08-16 15:34:10 -04003498static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3500 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3501 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3502 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3503};
3504
3505/* The FDI link training functions for SNB/Cougarpoint. */
3506static void gen6_fdi_link_train(struct drm_crtc *crtc)
3507{
3508 struct drm_device *dev = crtc->dev;
3509 struct drm_i915_private *dev_priv = dev->dev_private;
3510 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3511 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003512 i915_reg_t reg;
3513 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003514
Adam Jacksone1a44742010-06-25 15:32:14 -04003515 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3516 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003517 reg = FDI_RX_IMR(pipe);
3518 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003519 temp &= ~FDI_RX_SYMBOL_LOCK;
3520 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003521 I915_WRITE(reg, temp);
3522
3523 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003524 udelay(150);
3525
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003526 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003527 reg = FDI_TX_CTL(pipe);
3528 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003529 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003530 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003531 temp &= ~FDI_LINK_TRAIN_NONE;
3532 temp |= FDI_LINK_TRAIN_PATTERN_1;
3533 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3534 /* SNB-B */
3535 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003536 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003537
Daniel Vetterd74cf322012-10-26 10:58:13 +02003538 I915_WRITE(FDI_RX_MISC(pipe),
3539 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3540
Chris Wilson5eddb702010-09-11 13:48:45 +01003541 reg = FDI_RX_CTL(pipe);
3542 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003543 if (HAS_PCH_CPT(dev)) {
3544 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3545 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3546 } else {
3547 temp &= ~FDI_LINK_TRAIN_NONE;
3548 temp |= FDI_LINK_TRAIN_PATTERN_1;
3549 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003550 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3551
3552 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003553 udelay(150);
3554
Akshay Joshi0206e352011-08-16 15:34:10 -04003555 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003556 reg = FDI_TX_CTL(pipe);
3557 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003558 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3559 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003560 I915_WRITE(reg, temp);
3561
3562 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003563 udelay(500);
3564
Sean Paulfa37d392012-03-02 12:53:39 -05003565 for (retry = 0; retry < 5; retry++) {
3566 reg = FDI_RX_IIR(pipe);
3567 temp = I915_READ(reg);
3568 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3569 if (temp & FDI_RX_BIT_LOCK) {
3570 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3571 DRM_DEBUG_KMS("FDI train 1 done.\n");
3572 break;
3573 }
3574 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003575 }
Sean Paulfa37d392012-03-02 12:53:39 -05003576 if (retry < 5)
3577 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003578 }
3579 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003580 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003581
3582 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003583 reg = FDI_TX_CTL(pipe);
3584 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003585 temp &= ~FDI_LINK_TRAIN_NONE;
3586 temp |= FDI_LINK_TRAIN_PATTERN_2;
3587 if (IS_GEN6(dev)) {
3588 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3589 /* SNB-B */
3590 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3591 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003592 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003593
Chris Wilson5eddb702010-09-11 13:48:45 +01003594 reg = FDI_RX_CTL(pipe);
3595 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003596 if (HAS_PCH_CPT(dev)) {
3597 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3598 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3599 } else {
3600 temp &= ~FDI_LINK_TRAIN_NONE;
3601 temp |= FDI_LINK_TRAIN_PATTERN_2;
3602 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003603 I915_WRITE(reg, temp);
3604
3605 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003606 udelay(150);
3607
Akshay Joshi0206e352011-08-16 15:34:10 -04003608 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003609 reg = FDI_TX_CTL(pipe);
3610 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003611 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3612 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003613 I915_WRITE(reg, temp);
3614
3615 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003616 udelay(500);
3617
Sean Paulfa37d392012-03-02 12:53:39 -05003618 for (retry = 0; retry < 5; retry++) {
3619 reg = FDI_RX_IIR(pipe);
3620 temp = I915_READ(reg);
3621 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3622 if (temp & FDI_RX_SYMBOL_LOCK) {
3623 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3624 DRM_DEBUG_KMS("FDI train 2 done.\n");
3625 break;
3626 }
3627 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003628 }
Sean Paulfa37d392012-03-02 12:53:39 -05003629 if (retry < 5)
3630 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003631 }
3632 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003633 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003634
3635 DRM_DEBUG_KMS("FDI train done.\n");
3636}
3637
Jesse Barnes357555c2011-04-28 15:09:55 -07003638/* Manual link training for Ivy Bridge A0 parts */
3639static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3640{
3641 struct drm_device *dev = crtc->dev;
3642 struct drm_i915_private *dev_priv = dev->dev_private;
3643 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3644 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003645 i915_reg_t reg;
3646 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003647
3648 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3649 for train result */
3650 reg = FDI_RX_IMR(pipe);
3651 temp = I915_READ(reg);
3652 temp &= ~FDI_RX_SYMBOL_LOCK;
3653 temp &= ~FDI_RX_BIT_LOCK;
3654 I915_WRITE(reg, temp);
3655
3656 POSTING_READ(reg);
3657 udelay(150);
3658
Daniel Vetter01a415f2012-10-27 15:58:40 +02003659 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3660 I915_READ(FDI_RX_IIR(pipe)));
3661
Jesse Barnes139ccd32013-08-19 11:04:55 -07003662 /* Try each vswing and preemphasis setting twice before moving on */
3663 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3664 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003665 reg = FDI_TX_CTL(pipe);
3666 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003667 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3668 temp &= ~FDI_TX_ENABLE;
3669 I915_WRITE(reg, temp);
3670
3671 reg = FDI_RX_CTL(pipe);
3672 temp = I915_READ(reg);
3673 temp &= ~FDI_LINK_TRAIN_AUTO;
3674 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3675 temp &= ~FDI_RX_ENABLE;
3676 I915_WRITE(reg, temp);
3677
3678 /* enable CPU FDI TX and PCH FDI RX */
3679 reg = FDI_TX_CTL(pipe);
3680 temp = I915_READ(reg);
3681 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003682 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003683 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003684 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003685 temp |= snb_b_fdi_train_param[j/2];
3686 temp |= FDI_COMPOSITE_SYNC;
3687 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3688
3689 I915_WRITE(FDI_RX_MISC(pipe),
3690 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3691
3692 reg = FDI_RX_CTL(pipe);
3693 temp = I915_READ(reg);
3694 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3695 temp |= FDI_COMPOSITE_SYNC;
3696 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3697
3698 POSTING_READ(reg);
3699 udelay(1); /* should be 0.5us */
3700
3701 for (i = 0; i < 4; i++) {
3702 reg = FDI_RX_IIR(pipe);
3703 temp = I915_READ(reg);
3704 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3705
3706 if (temp & FDI_RX_BIT_LOCK ||
3707 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3708 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3709 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3710 i);
3711 break;
3712 }
3713 udelay(1); /* should be 0.5us */
3714 }
3715 if (i == 4) {
3716 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3717 continue;
3718 }
3719
3720 /* Train 2 */
3721 reg = FDI_TX_CTL(pipe);
3722 temp = I915_READ(reg);
3723 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3724 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3725 I915_WRITE(reg, temp);
3726
3727 reg = FDI_RX_CTL(pipe);
3728 temp = I915_READ(reg);
3729 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3730 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003731 I915_WRITE(reg, temp);
3732
3733 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003734 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003735
Jesse Barnes139ccd32013-08-19 11:04:55 -07003736 for (i = 0; i < 4; i++) {
3737 reg = FDI_RX_IIR(pipe);
3738 temp = I915_READ(reg);
3739 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003740
Jesse Barnes139ccd32013-08-19 11:04:55 -07003741 if (temp & FDI_RX_SYMBOL_LOCK ||
3742 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3743 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3744 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3745 i);
3746 goto train_done;
3747 }
3748 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003749 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003750 if (i == 4)
3751 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003752 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003753
Jesse Barnes139ccd32013-08-19 11:04:55 -07003754train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003755 DRM_DEBUG_KMS("FDI train done.\n");
3756}
3757
Daniel Vetter88cefb62012-08-12 19:27:14 +02003758static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003759{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003760 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003761 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003762 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003763 i915_reg_t reg;
3764 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003765
Jesse Barnes0e23b992010-09-10 11:10:00 -07003766 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003767 reg = FDI_RX_CTL(pipe);
3768 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003769 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003770 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003771 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003772 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3773
3774 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003775 udelay(200);
3776
3777 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003778 temp = I915_READ(reg);
3779 I915_WRITE(reg, temp | FDI_PCDCLK);
3780
3781 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003782 udelay(200);
3783
Paulo Zanoni20749732012-11-23 15:30:38 -02003784 /* Enable CPU FDI TX PLL, always on for Ironlake */
3785 reg = FDI_TX_CTL(pipe);
3786 temp = I915_READ(reg);
3787 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3788 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003789
Paulo Zanoni20749732012-11-23 15:30:38 -02003790 POSTING_READ(reg);
3791 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003792 }
3793}
3794
Daniel Vetter88cefb62012-08-12 19:27:14 +02003795static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3796{
3797 struct drm_device *dev = intel_crtc->base.dev;
3798 struct drm_i915_private *dev_priv = dev->dev_private;
3799 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003800 i915_reg_t reg;
3801 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003802
3803 /* Switch from PCDclk to Rawclk */
3804 reg = FDI_RX_CTL(pipe);
3805 temp = I915_READ(reg);
3806 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3807
3808 /* Disable CPU FDI TX PLL */
3809 reg = FDI_TX_CTL(pipe);
3810 temp = I915_READ(reg);
3811 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3812
3813 POSTING_READ(reg);
3814 udelay(100);
3815
3816 reg = FDI_RX_CTL(pipe);
3817 temp = I915_READ(reg);
3818 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3819
3820 /* Wait for the clocks to turn off. */
3821 POSTING_READ(reg);
3822 udelay(100);
3823}
3824
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003825static void ironlake_fdi_disable(struct drm_crtc *crtc)
3826{
3827 struct drm_device *dev = crtc->dev;
3828 struct drm_i915_private *dev_priv = dev->dev_private;
3829 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3830 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003831 i915_reg_t reg;
3832 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003833
3834 /* disable CPU FDI tx and PCH FDI rx */
3835 reg = FDI_TX_CTL(pipe);
3836 temp = I915_READ(reg);
3837 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3838 POSTING_READ(reg);
3839
3840 reg = FDI_RX_CTL(pipe);
3841 temp = I915_READ(reg);
3842 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003843 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003844 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3845
3846 POSTING_READ(reg);
3847 udelay(100);
3848
3849 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003850 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003851 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003852
3853 /* still set train pattern 1 */
3854 reg = FDI_TX_CTL(pipe);
3855 temp = I915_READ(reg);
3856 temp &= ~FDI_LINK_TRAIN_NONE;
3857 temp |= FDI_LINK_TRAIN_PATTERN_1;
3858 I915_WRITE(reg, temp);
3859
3860 reg = FDI_RX_CTL(pipe);
3861 temp = I915_READ(reg);
3862 if (HAS_PCH_CPT(dev)) {
3863 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3864 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3865 } else {
3866 temp &= ~FDI_LINK_TRAIN_NONE;
3867 temp |= FDI_LINK_TRAIN_PATTERN_1;
3868 }
3869 /* BPC in FDI rx is consistent with that in PIPECONF */
3870 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003871 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003872 I915_WRITE(reg, temp);
3873
3874 POSTING_READ(reg);
3875 udelay(100);
3876}
3877
Chris Wilson5dce5b932014-01-20 10:17:36 +00003878bool intel_has_pending_fb_unpin(struct drm_device *dev)
3879{
3880 struct intel_crtc *crtc;
3881
3882 /* Note that we don't need to be called with mode_config.lock here
3883 * as our list of CRTC objects is static for the lifetime of the
3884 * device and so cannot disappear as we iterate. Similarly, we can
3885 * happily treat the predicates as racy, atomic checks as userspace
3886 * cannot claim and pin a new fb without at least acquring the
3887 * struct_mutex and so serialising with us.
3888 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003889 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003890 if (atomic_read(&crtc->unpin_work_count) == 0)
3891 continue;
3892
3893 if (crtc->unpin_work)
3894 intel_wait_for_vblank(dev, crtc->pipe);
3895
3896 return true;
3897 }
3898
3899 return false;
3900}
3901
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003902static void page_flip_completed(struct intel_crtc *intel_crtc)
3903{
3904 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3905 struct intel_unpin_work *work = intel_crtc->unpin_work;
3906
3907 /* ensure that the unpin work is consistent wrt ->pending. */
3908 smp_rmb();
3909 intel_crtc->unpin_work = NULL;
3910
3911 if (work->event)
3912 drm_send_vblank_event(intel_crtc->base.dev,
3913 intel_crtc->pipe,
3914 work->event);
3915
3916 drm_crtc_vblank_put(&intel_crtc->base);
3917
3918 wake_up_all(&dev_priv->pending_flip_queue);
3919 queue_work(dev_priv->wq, &work->work);
3920
3921 trace_i915_flip_complete(intel_crtc->plane,
3922 work->pending_flip_obj);
3923}
3924
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003925static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003926{
Chris Wilson0f911282012-04-17 10:05:38 +01003927 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003928 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003929 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003930
Daniel Vetter2c10d572012-12-20 21:24:07 +01003931 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003932
3933 ret = wait_event_interruptible_timeout(
3934 dev_priv->pending_flip_queue,
3935 !intel_crtc_has_pending_flip(crtc),
3936 60*HZ);
3937
3938 if (ret < 0)
3939 return ret;
3940
3941 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003943
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003944 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003945 if (intel_crtc->unpin_work) {
3946 WARN_ONCE(1, "Removing stuck page flip\n");
3947 page_flip_completed(intel_crtc);
3948 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003949 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003950 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003951
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003952 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003953}
3954
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003955static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3956{
3957 u32 temp;
3958
3959 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3960
3961 mutex_lock(&dev_priv->sb_lock);
3962
3963 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3964 temp |= SBI_SSCCTL_DISABLE;
3965 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3966
3967 mutex_unlock(&dev_priv->sb_lock);
3968}
3969
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003970/* Program iCLKIP clock to the desired frequency */
3971static void lpt_program_iclkip(struct drm_crtc *crtc)
3972{
3973 struct drm_device *dev = crtc->dev;
3974 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003975 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003976 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3977 u32 temp;
3978
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003979 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003980
3981 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003982 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003983 auxdiv = 1;
3984 divsel = 0x41;
3985 phaseinc = 0x20;
3986 } else {
3987 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003988 * but the adjusted_mode->crtc_clock in in KHz. To get the
3989 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003990 * convert the virtual clock precision to KHz here for higher
3991 * precision.
3992 */
3993 u32 iclk_virtual_root_freq = 172800 * 1000;
3994 u32 iclk_pi_range = 64;
3995 u32 desired_divisor, msb_divisor_value, pi_value;
3996
Ville Syrjäläa2572f52015-12-04 22:20:21 +02003997 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq, clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003998 msb_divisor_value = desired_divisor / iclk_pi_range;
3999 pi_value = desired_divisor % iclk_pi_range;
4000
4001 auxdiv = 0;
4002 divsel = msb_divisor_value - 2;
4003 phaseinc = pi_value;
4004 }
4005
4006 /* This should not happen with any sane values */
4007 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4008 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4009 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4010 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4011
4012 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 +03004013 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004014 auxdiv,
4015 divsel,
4016 phasedir,
4017 phaseinc);
4018
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004019 mutex_lock(&dev_priv->sb_lock);
4020
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004021 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004022 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004023 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4024 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4025 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4026 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4027 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4028 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004029 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004030
4031 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004032 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004033 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4034 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004035 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004036
4037 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004038 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004039 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004040 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004041
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004042 mutex_unlock(&dev_priv->sb_lock);
4043
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004044 /* Wait for initialization time */
4045 udelay(24);
4046
4047 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4048}
4049
Daniel Vetter275f01b22013-05-03 11:49:47 +02004050static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4051 enum pipe pch_transcoder)
4052{
4053 struct drm_device *dev = crtc->base.dev;
4054 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004055 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004056
4057 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4058 I915_READ(HTOTAL(cpu_transcoder)));
4059 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4060 I915_READ(HBLANK(cpu_transcoder)));
4061 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4062 I915_READ(HSYNC(cpu_transcoder)));
4063
4064 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4065 I915_READ(VTOTAL(cpu_transcoder)));
4066 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4067 I915_READ(VBLANK(cpu_transcoder)));
4068 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4069 I915_READ(VSYNC(cpu_transcoder)));
4070 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4071 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4072}
4073
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004074static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004075{
4076 struct drm_i915_private *dev_priv = dev->dev_private;
4077 uint32_t temp;
4078
4079 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004080 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004081 return;
4082
4083 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4084 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4085
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004086 temp &= ~FDI_BC_BIFURCATION_SELECT;
4087 if (enable)
4088 temp |= FDI_BC_BIFURCATION_SELECT;
4089
4090 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004091 I915_WRITE(SOUTH_CHICKEN1, temp);
4092 POSTING_READ(SOUTH_CHICKEN1);
4093}
4094
4095static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4096{
4097 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004098
4099 switch (intel_crtc->pipe) {
4100 case PIPE_A:
4101 break;
4102 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004103 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004104 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004105 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004106 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004107
4108 break;
4109 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004110 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004111
4112 break;
4113 default:
4114 BUG();
4115 }
4116}
4117
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004118/* Return which DP Port should be selected for Transcoder DP control */
4119static enum port
4120intel_trans_dp_port_sel(struct drm_crtc *crtc)
4121{
4122 struct drm_device *dev = crtc->dev;
4123 struct intel_encoder *encoder;
4124
4125 for_each_encoder_on_crtc(dev, crtc, encoder) {
4126 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4127 encoder->type == INTEL_OUTPUT_EDP)
4128 return enc_to_dig_port(&encoder->base)->port;
4129 }
4130
4131 return -1;
4132}
4133
Jesse Barnesf67a5592011-01-05 10:31:48 -08004134/*
4135 * Enable PCH resources required for PCH ports:
4136 * - PCH PLLs
4137 * - FDI training & RX/TX
4138 * - update transcoder timings
4139 * - DP transcoding bits
4140 * - transcoder
4141 */
4142static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004143{
4144 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004145 struct drm_i915_private *dev_priv = dev->dev_private;
4146 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4147 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004148 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004149
Daniel Vetterab9412b2013-05-03 11:49:46 +02004150 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004151
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004152 if (IS_IVYBRIDGE(dev))
4153 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4154
Daniel Vettercd986ab2012-10-26 10:58:12 +02004155 /* Write the TU size bits before fdi link training, so that error
4156 * detection works. */
4157 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4158 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4159
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004160 /*
4161 * Sometimes spurious CPU pipe underruns happen during FDI
4162 * training, at least with VGA+HDMI cloning. Suppress them.
4163 */
4164 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4165
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004166 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004167 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004168
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004169 /* We need to program the right clock selection before writing the pixel
4170 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004171 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004172 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004173
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004174 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004175 temp |= TRANS_DPLL_ENABLE(pipe);
4176 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004177 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004178 temp |= sel;
4179 else
4180 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004181 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004182 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004183
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004184 /* XXX: pch pll's can be enabled any time before we enable the PCH
4185 * transcoder, and we actually should do this to not upset any PCH
4186 * transcoder that already use the clock when we share it.
4187 *
4188 * Note that enable_shared_dpll tries to do the right thing, but
4189 * get_shared_dpll unconditionally resets the pll - we need that to have
4190 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004191 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004192
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004193 /* set transcoder timing, panel must allow it */
4194 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004195 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004196
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004197 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004198
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004199 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4200
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004201 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004202 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004203 const struct drm_display_mode *adjusted_mode =
4204 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004205 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004206 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004207 temp = I915_READ(reg);
4208 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004209 TRANS_DP_SYNC_MASK |
4210 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004211 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004212 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004213
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004214 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004215 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004216 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004217 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004218
4219 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004220 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004221 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004222 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004223 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004224 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004225 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004226 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004227 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004228 break;
4229 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004230 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004231 }
4232
Chris Wilson5eddb702010-09-11 13:48:45 +01004233 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004234 }
4235
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004236 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004237}
4238
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004239static void lpt_pch_enable(struct drm_crtc *crtc)
4240{
4241 struct drm_device *dev = crtc->dev;
4242 struct drm_i915_private *dev_priv = dev->dev_private;
4243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004244 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004245
Daniel Vetterab9412b2013-05-03 11:49:46 +02004246 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004247
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004248 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004249
Paulo Zanoni0540e482012-10-31 18:12:40 -02004250 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004251 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004252
Paulo Zanoni937bb612012-10-31 18:12:47 -02004253 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004254}
4255
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004256struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4257 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004258{
Daniel Vettere2b78262013-06-07 23:10:03 +02004259 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004260 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004261 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004262 enum intel_dpll_id i;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01004263 int max = dev_priv->num_shared_dpll;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004264
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004265 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4266
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004267 if (HAS_PCH_IBX(dev_priv->dev)) {
4268 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004269 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004270 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004271
Daniel Vetter46edb022013-06-05 13:34:12 +02004272 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4273 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004274
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004275 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004276
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004277 goto found;
4278 }
4279
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304280 if (IS_BROXTON(dev_priv->dev)) {
4281 /* PLL is attached to port in bxt */
4282 struct intel_encoder *encoder;
4283 struct intel_digital_port *intel_dig_port;
4284
4285 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4286 if (WARN_ON(!encoder))
4287 return NULL;
4288
4289 intel_dig_port = enc_to_dig_port(&encoder->base);
4290 /* 1:1 mapping between ports and PLLs */
4291 i = (enum intel_dpll_id)intel_dig_port->port;
4292 pll = &dev_priv->shared_dplls[i];
4293 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4294 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004295 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304296
4297 goto found;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01004298 } else if (INTEL_INFO(dev_priv)->gen < 9 && HAS_DDI(dev_priv))
4299 /* Do not consider SPLL */
4300 max = 2;
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304301
Maarten Lankhorst00490c22015-11-16 14:42:12 +01004302 for (i = 0; i < max; i++) {
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004303 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004304
4305 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004306 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004307 continue;
4308
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004309 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004310 &shared_dpll[i].hw_state,
4311 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004312 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004313 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004314 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004315 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004316 goto found;
4317 }
4318 }
4319
4320 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004321 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4322 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004323 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004324 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4325 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004326 goto found;
4327 }
4328 }
4329
4330 return NULL;
4331
4332found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004333 if (shared_dpll[i].crtc_mask == 0)
4334 shared_dpll[i].hw_state =
4335 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004336
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004337 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004338 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4339 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004340
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004341 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004342
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004343 return pll;
4344}
4345
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004346static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004347{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004348 struct drm_i915_private *dev_priv = to_i915(state->dev);
4349 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004350 struct intel_shared_dpll *pll;
4351 enum intel_dpll_id i;
4352
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004353 if (!to_intel_atomic_state(state)->dpll_set)
4354 return;
4355
4356 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004357 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4358 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004359 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004360 }
4361}
4362
Daniel Vettera1520312013-05-03 11:49:50 +02004363static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004364{
4365 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004366 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004367 u32 temp;
4368
4369 temp = I915_READ(dslreg);
4370 udelay(500);
4371 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004372 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004373 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004374 }
4375}
4376
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004377static int
4378skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4379 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4380 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004381{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004382 struct intel_crtc_scaler_state *scaler_state =
4383 &crtc_state->scaler_state;
4384 struct intel_crtc *intel_crtc =
4385 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004386 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004387
4388 need_scaling = intel_rotation_90_or_270(rotation) ?
4389 (src_h != dst_w || src_w != dst_h):
4390 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004391
4392 /*
4393 * if plane is being disabled or scaler is no more required or force detach
4394 * - free scaler binded to this plane/crtc
4395 * - in order to do this, update crtc->scaler_usage
4396 *
4397 * Here scaler state in crtc_state is set free so that
4398 * scaler can be assigned to other user. Actual register
4399 * update to free the scaler is done in plane/panel-fit programming.
4400 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4401 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004402 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004403 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004404 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004405 scaler_state->scalers[*scaler_id].in_use = 0;
4406
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004407 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4408 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4409 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004410 scaler_state->scaler_users);
4411 *scaler_id = -1;
4412 }
4413 return 0;
4414 }
4415
4416 /* range checks */
4417 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4418 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4419
4420 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4421 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004422 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004423 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004424 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004425 return -EINVAL;
4426 }
4427
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004428 /* mark this plane as a scaler user in crtc_state */
4429 scaler_state->scaler_users |= (1 << scaler_user);
4430 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4431 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4432 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4433 scaler_state->scaler_users);
4434
4435 return 0;
4436}
4437
4438/**
4439 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4440 *
4441 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004442 *
4443 * Return
4444 * 0 - scaler_usage updated successfully
4445 * error - requested scaling cannot be supported or other error condition
4446 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004447int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004448{
4449 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004450 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004451
4452 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4453 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4454
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004455 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004456 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4457 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004458 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004459}
4460
4461/**
4462 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4463 *
4464 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004465 * @plane_state: atomic plane state to update
4466 *
4467 * Return
4468 * 0 - scaler_usage updated successfully
4469 * error - requested scaling cannot be supported or other error condition
4470 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004471static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4472 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004473{
4474
4475 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004476 struct intel_plane *intel_plane =
4477 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004478 struct drm_framebuffer *fb = plane_state->base.fb;
4479 int ret;
4480
4481 bool force_detach = !fb || !plane_state->visible;
4482
4483 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4484 intel_plane->base.base.id, intel_crtc->pipe,
4485 drm_plane_index(&intel_plane->base));
4486
4487 ret = skl_update_scaler(crtc_state, force_detach,
4488 drm_plane_index(&intel_plane->base),
4489 &plane_state->scaler_id,
4490 plane_state->base.rotation,
4491 drm_rect_width(&plane_state->src) >> 16,
4492 drm_rect_height(&plane_state->src) >> 16,
4493 drm_rect_width(&plane_state->dst),
4494 drm_rect_height(&plane_state->dst));
4495
4496 if (ret || plane_state->scaler_id < 0)
4497 return ret;
4498
Chandra Kondurua1b22782015-04-07 15:28:45 -07004499 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004500 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004501 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004502 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004503 return -EINVAL;
4504 }
4505
4506 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004507 switch (fb->pixel_format) {
4508 case DRM_FORMAT_RGB565:
4509 case DRM_FORMAT_XBGR8888:
4510 case DRM_FORMAT_XRGB8888:
4511 case DRM_FORMAT_ABGR8888:
4512 case DRM_FORMAT_ARGB8888:
4513 case DRM_FORMAT_XRGB2101010:
4514 case DRM_FORMAT_XBGR2101010:
4515 case DRM_FORMAT_YUYV:
4516 case DRM_FORMAT_YVYU:
4517 case DRM_FORMAT_UYVY:
4518 case DRM_FORMAT_VYUY:
4519 break;
4520 default:
4521 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4522 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4523 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004524 }
4525
Chandra Kondurua1b22782015-04-07 15:28:45 -07004526 return 0;
4527}
4528
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004529static void skylake_scaler_disable(struct intel_crtc *crtc)
4530{
4531 int i;
4532
4533 for (i = 0; i < crtc->num_scalers; i++)
4534 skl_detach_scaler(crtc, i);
4535}
4536
4537static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004538{
4539 struct drm_device *dev = crtc->base.dev;
4540 struct drm_i915_private *dev_priv = dev->dev_private;
4541 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004542 struct intel_crtc_scaler_state *scaler_state =
4543 &crtc->config->scaler_state;
4544
4545 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4546
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004547 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004548 int id;
4549
4550 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4551 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4552 return;
4553 }
4554
4555 id = scaler_state->scaler_id;
4556 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4557 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4558 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4559 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4560
4561 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004562 }
4563}
4564
Jesse Barnesb074cec2013-04-25 12:55:02 -07004565static void ironlake_pfit_enable(struct intel_crtc *crtc)
4566{
4567 struct drm_device *dev = crtc->base.dev;
4568 struct drm_i915_private *dev_priv = dev->dev_private;
4569 int pipe = crtc->pipe;
4570
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004571 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004572 /* Force use of hard-coded filter coefficients
4573 * as some pre-programmed values are broken,
4574 * e.g. x201.
4575 */
4576 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4577 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4578 PF_PIPE_SEL_IVB(pipe));
4579 else
4580 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004581 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4582 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004583 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004584}
4585
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004586void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004587{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004588 struct drm_device *dev = crtc->base.dev;
4589 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004590
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004591 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004592 return;
4593
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004594 /* We can only enable IPS after we enable a plane and wait for a vblank */
4595 intel_wait_for_vblank(dev, crtc->pipe);
4596
Paulo Zanonid77e4532013-09-24 13:52:55 -03004597 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004598 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004599 mutex_lock(&dev_priv->rps.hw_lock);
4600 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4601 mutex_unlock(&dev_priv->rps.hw_lock);
4602 /* Quoting Art Runyan: "its not safe to expect any particular
4603 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004604 * mailbox." Moreover, the mailbox may return a bogus state,
4605 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004606 */
4607 } else {
4608 I915_WRITE(IPS_CTL, IPS_ENABLE);
4609 /* The bit only becomes 1 in the next vblank, so this wait here
4610 * is essentially intel_wait_for_vblank. If we don't have this
4611 * and don't wait for vblanks until the end of crtc_enable, then
4612 * the HW state readout code will complain that the expected
4613 * IPS_CTL value is not the one we read. */
4614 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4615 DRM_ERROR("Timed out waiting for IPS enable\n");
4616 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004617}
4618
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004619void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004620{
4621 struct drm_device *dev = crtc->base.dev;
4622 struct drm_i915_private *dev_priv = dev->dev_private;
4623
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004624 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004625 return;
4626
4627 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004628 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004629 mutex_lock(&dev_priv->rps.hw_lock);
4630 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4631 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004632 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4633 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4634 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004635 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004636 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004637 POSTING_READ(IPS_CTL);
4638 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004639
4640 /* We need to wait for a vblank before we can disable the plane. */
4641 intel_wait_for_vblank(dev, crtc->pipe);
4642}
4643
4644/** Loads the palette/gamma unit for the CRTC with the prepared values */
4645static void intel_crtc_load_lut(struct drm_crtc *crtc)
4646{
4647 struct drm_device *dev = crtc->dev;
4648 struct drm_i915_private *dev_priv = dev->dev_private;
4649 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4650 enum pipe pipe = intel_crtc->pipe;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004651 int i;
4652 bool reenable_ips = false;
4653
4654 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004655 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004656 return;
4657
Imre Deak50360402015-01-16 00:55:16 -08004658 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Jani Nikulaa65347b2015-11-27 12:21:46 +02004659 if (intel_crtc->config->has_dsi_encoder)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004660 assert_dsi_pll_enabled(dev_priv);
4661 else
4662 assert_pll_enabled(dev_priv, pipe);
4663 }
4664
Paulo Zanonid77e4532013-09-24 13:52:55 -03004665 /* Workaround : Do not read or write the pipe palette/gamma data while
4666 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4667 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004668 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004669 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4670 GAMMA_MODE_MODE_SPLIT)) {
4671 hsw_disable_ips(intel_crtc);
4672 reenable_ips = true;
4673 }
4674
4675 for (i = 0; i < 256; i++) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004676 i915_reg_t palreg;
Ville Syrjäläf65a9c52015-09-18 20:03:28 +03004677
4678 if (HAS_GMCH_DISPLAY(dev))
4679 palreg = PALETTE(pipe, i);
4680 else
4681 palreg = LGC_PALETTE(pipe, i);
4682
4683 I915_WRITE(palreg,
Paulo Zanonid77e4532013-09-24 13:52:55 -03004684 (intel_crtc->lut_r[i] << 16) |
4685 (intel_crtc->lut_g[i] << 8) |
4686 intel_crtc->lut_b[i]);
4687 }
4688
4689 if (reenable_ips)
4690 hsw_enable_ips(intel_crtc);
4691}
4692
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004693static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004694{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004695 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004696 struct drm_device *dev = intel_crtc->base.dev;
4697 struct drm_i915_private *dev_priv = dev->dev_private;
4698
4699 mutex_lock(&dev->struct_mutex);
4700 dev_priv->mm.interruptible = false;
4701 (void) intel_overlay_switch_off(intel_crtc->overlay);
4702 dev_priv->mm.interruptible = true;
4703 mutex_unlock(&dev->struct_mutex);
4704 }
4705
4706 /* Let userspace switch the overlay on again. In most cases userspace
4707 * has to recompute where to put it anyway.
4708 */
4709}
4710
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004711/**
4712 * intel_post_enable_primary - Perform operations after enabling primary plane
4713 * @crtc: the CRTC whose primary plane was just enabled
4714 *
4715 * Performs potentially sleeping operations that must be done after the primary
4716 * plane is enabled, such as updating FBC and IPS. Note that this may be
4717 * called due to an explicit primary plane update, or due to an implicit
4718 * re-enable that is caused when a sprite plane is updated to no longer
4719 * completely hide the primary plane.
4720 */
4721static void
4722intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004723{
4724 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004725 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004726 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4727 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004728
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004729 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004730 * FIXME IPS should be fine as long as one plane is
4731 * enabled, but in practice it seems to have problems
4732 * when going from primary only to sprite only and vice
4733 * versa.
4734 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004735 hsw_enable_ips(intel_crtc);
4736
Daniel Vetterf99d7062014-06-19 16:01:59 +02004737 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004738 * Gen2 reports pipe underruns whenever all planes are disabled.
4739 * So don't enable underrun reporting before at least some planes
4740 * are enabled.
4741 * FIXME: Need to fix the logic to work when we turn off all planes
4742 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004743 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004744 if (IS_GEN2(dev))
4745 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4746
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004747 /* Underruns don't always raise interrupts, so check manually. */
4748 intel_check_cpu_fifo_underruns(dev_priv);
4749 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004750}
4751
4752/**
4753 * intel_pre_disable_primary - Perform operations before disabling primary plane
4754 * @crtc: the CRTC whose primary plane is to be disabled
4755 *
4756 * Performs potentially sleeping operations that must be done before the
4757 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4758 * be called due to an explicit primary plane update, or due to an implicit
4759 * disable that is caused when a sprite plane completely hides the primary
4760 * plane.
4761 */
4762static void
4763intel_pre_disable_primary(struct drm_crtc *crtc)
4764{
4765 struct drm_device *dev = crtc->dev;
4766 struct drm_i915_private *dev_priv = dev->dev_private;
4767 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4768 int pipe = intel_crtc->pipe;
4769
4770 /*
4771 * Gen2 reports pipe underruns whenever all planes are disabled.
4772 * So diasble underrun reporting before all the planes get disabled.
4773 * FIXME: Need to fix the logic to work when we turn off all planes
4774 * but leave the pipe running.
4775 */
4776 if (IS_GEN2(dev))
4777 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4778
4779 /*
4780 * Vblank time updates from the shadow to live plane control register
4781 * are blocked if the memory self-refresh mode is active at that
4782 * moment. So to make sure the plane gets truly disabled, disable
4783 * first the self-refresh mode. The self-refresh enable bit in turn
4784 * will be checked/applied by the HW only at the next frame start
4785 * event which is after the vblank start event, so we need to have a
4786 * wait-for-vblank between disabling the plane and the pipe.
4787 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004788 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004789 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004790 dev_priv->wm.vlv.cxsr = false;
4791 intel_wait_for_vblank(dev, pipe);
4792 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004793
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004794 /*
4795 * FIXME IPS should be fine as long as one plane is
4796 * enabled, but in practice it seems to have problems
4797 * when going from primary only to sprite only and vice
4798 * versa.
4799 */
4800 hsw_disable_ips(intel_crtc);
4801}
4802
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004803static void intel_post_plane_update(struct intel_crtc *crtc)
4804{
4805 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004806 struct intel_crtc_state *pipe_config =
4807 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004808 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004809
4810 if (atomic->wait_vblank)
4811 intel_wait_for_vblank(dev, crtc->pipe);
4812
4813 intel_frontbuffer_flip(dev, atomic->fb_bits);
4814
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004815 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004816
Maarten Lankhorstb9001112015-11-19 16:07:16 +01004817 if (pipe_config->wm_changed && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004818 intel_update_watermarks(&crtc->base);
4819
Paulo Zanonic80ac852015-07-02 19:25:13 -03004820 if (atomic->update_fbc)
Paulo Zanoni754d1132015-10-13 19:13:25 -03004821 intel_fbc_update(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004822
4823 if (atomic->post_enable_primary)
4824 intel_post_enable_primary(&crtc->base);
4825
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004826 memset(atomic, 0, sizeof(*atomic));
4827}
4828
4829static void intel_pre_plane_update(struct intel_crtc *crtc)
4830{
4831 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004832 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004833 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004834 struct intel_crtc_state *pipe_config =
4835 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004836
Paulo Zanonic80ac852015-07-02 19:25:13 -03004837 if (atomic->disable_fbc)
Paulo Zanonid029bca2015-10-15 10:44:46 -03004838 intel_fbc_deactivate(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004839
Rodrigo Vivi066cf552015-06-26 13:55:54 -07004840 if (crtc->atomic.disable_ips)
4841 hsw_disable_ips(crtc);
4842
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004843 if (atomic->pre_disable_primary)
4844 intel_pre_disable_primary(&crtc->base);
Ville Syrjälä852eb002015-06-24 22:00:07 +03004845
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004846 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004847 crtc->wm.cxsr_allowed = false;
4848 intel_set_memory_cxsr(dev_priv, false);
4849 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004850
Matt Roper396e33a2016-01-06 11:34:30 -08004851 /*
4852 * IVB workaround: must disable low power watermarks for at least
4853 * one frame before enabling scaling. LP watermarks can be re-enabled
4854 * when scaling is disabled.
4855 *
4856 * WaCxSRDisabledForSpriteScaling:ivb
4857 */
4858 if (pipe_config->disable_lp_wm) {
4859 ilk_disable_lp_wm(dev);
4860 intel_wait_for_vblank(dev, crtc->pipe);
4861 }
4862
4863 /*
4864 * If we're doing a modeset, we're done. No need to do any pre-vblank
4865 * watermark programming here.
4866 */
4867 if (needs_modeset(&pipe_config->base))
4868 return;
4869
4870 /*
4871 * For platforms that support atomic watermarks, program the
4872 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4873 * will be the intermediate values that are safe for both pre- and
4874 * post- vblank; when vblank happens, the 'active' values will be set
4875 * to the final 'target' values and we'll do this again to get the
4876 * optimal watermarks. For gen9+ platforms, the values we program here
4877 * will be the final target values which will get automatically latched
4878 * at vblank time; no further programming will be necessary.
4879 *
4880 * If a platform hasn't been transitioned to atomic watermarks yet,
4881 * we'll continue to update watermarks the old way, if flags tell
4882 * us to.
4883 */
4884 if (dev_priv->display.initial_watermarks != NULL)
4885 dev_priv->display.initial_watermarks(pipe_config);
4886 else if (pipe_config->wm_changed)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004887 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004888}
4889
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004890static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004891{
4892 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004893 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004894 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004895 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004896
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004897 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004898
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004899 drm_for_each_plane_mask(p, dev, plane_mask)
4900 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004901
Daniel Vetterf99d7062014-06-19 16:01:59 +02004902 /*
4903 * FIXME: Once we grow proper nuclear flip support out of this we need
4904 * to compute the mask of flip planes precisely. For the time being
4905 * consider this a flip to a NULL plane.
4906 */
4907 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004908}
4909
Jesse Barnesf67a5592011-01-05 10:31:48 -08004910static void ironlake_crtc_enable(struct drm_crtc *crtc)
4911{
4912 struct drm_device *dev = crtc->dev;
4913 struct drm_i915_private *dev_priv = dev->dev_private;
4914 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004915 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004916 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004917
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004918 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004919 return;
4920
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004921 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004922 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4923
4924 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004925 intel_prepare_shared_dpll(intel_crtc);
4926
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004927 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304928 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004929
4930 intel_set_pipe_timings(intel_crtc);
4931
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004932 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004933 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004934 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004935 }
4936
4937 ironlake_set_pipeconf(crtc);
4938
Jesse Barnesf67a5592011-01-05 10:31:48 -08004939 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004940
Daniel Vettera72e4c92014-09-30 10:56:47 +02004941 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004942
Daniel Vetterf6736a12013-06-05 13:34:30 +02004943 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004944 if (encoder->pre_enable)
4945 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004946
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004947 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004948 /* Note: FDI PLL enabling _must_ be done before we enable the
4949 * cpu pipes, hence this is separate from all the other fdi/pch
4950 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004951 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004952 } else {
4953 assert_fdi_tx_disabled(dev_priv, pipe);
4954 assert_fdi_rx_disabled(dev_priv, pipe);
4955 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004956
Jesse Barnesb074cec2013-04-25 12:55:02 -07004957 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004958
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004959 /*
4960 * On ILK+ LUT must be loaded before the pipe is running but with
4961 * clocks enabled
4962 */
4963 intel_crtc_load_lut(crtc);
4964
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004965 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004966 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004967
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004968 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004969 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004970
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004971 assert_vblank_disabled(crtc);
4972 drm_crtc_vblank_on(crtc);
4973
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004974 for_each_encoder_on_crtc(dev, crtc, encoder)
4975 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004976
4977 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004978 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004979
4980 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4981 if (intel_crtc->config->has_pch_encoder)
4982 intel_wait_for_vblank(dev, pipe);
4983 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanonid029bca2015-10-15 10:44:46 -03004984
4985 intel_fbc_enable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004986}
4987
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004988/* IPS only exists on ULT machines and is tied to pipe A. */
4989static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4990{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004991 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004992}
4993
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004994static void haswell_crtc_enable(struct drm_crtc *crtc)
4995{
4996 struct drm_device *dev = crtc->dev;
4997 struct drm_i915_private *dev_priv = dev->dev_private;
4998 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4999 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005000 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
5001 struct intel_crtc_state *pipe_config =
5002 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005003
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005004 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005005 return;
5006
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005007 if (intel_crtc->config->has_pch_encoder)
5008 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5009 false);
5010
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005011 if (intel_crtc_to_shared_dpll(intel_crtc))
5012 intel_enable_shared_dpll(intel_crtc);
5013
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005014 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305015 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005016
5017 intel_set_pipe_timings(intel_crtc);
5018
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005019 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5020 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5021 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005022 }
5023
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005024 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005025 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005026 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005027 }
5028
5029 haswell_set_pipeconf(crtc);
5030
5031 intel_set_pipe_csc(crtc);
5032
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005033 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005034
Daniel Vetter6b698512015-11-28 11:05:39 +01005035 if (intel_crtc->config->has_pch_encoder)
5036 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5037 else
5038 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5039
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305040 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005041 if (encoder->pre_enable)
5042 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305043 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005044
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005045 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03005046 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03005047
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_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005050
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005051 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005052 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005053 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005054 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005055
5056 /*
5057 * On ILK+ LUT must be loaded before the pipe is running but with
5058 * clocks enabled
5059 */
5060 intel_crtc_load_lut(crtc);
5061
Paulo Zanoni1f544382012-10-24 11:32:00 -02005062 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02005063 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305064 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005065
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005066 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005067 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005068
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005069 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005070 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005071
Jani Nikulaa65347b2015-11-27 12:21:46 +02005072 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005073 intel_ddi_set_vc_payload_alloc(crtc, true);
5074
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005075 assert_vblank_disabled(crtc);
5076 drm_crtc_vblank_on(crtc);
5077
Jani Nikula8807e552013-08-30 19:40:32 +03005078 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005079 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005080 intel_opregion_notify_encoder(encoder, true);
5081 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005082
Daniel Vetter6b698512015-11-28 11:05:39 +01005083 if (intel_crtc->config->has_pch_encoder) {
5084 intel_wait_for_vblank(dev, pipe);
5085 intel_wait_for_vblank(dev, pipe);
5086 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005087 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5088 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01005089 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005090
Paulo Zanonie4916942013-09-20 16:21:19 -03005091 /* If we change the relative order between pipe/planes enabling, we need
5092 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005093 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5094 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5095 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5096 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5097 }
Paulo Zanonid029bca2015-10-15 10:44:46 -03005098
5099 intel_fbc_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005100}
5101
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005102static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005103{
5104 struct drm_device *dev = crtc->base.dev;
5105 struct drm_i915_private *dev_priv = dev->dev_private;
5106 int pipe = crtc->pipe;
5107
5108 /* To avoid upsetting the power well on haswell only disable the pfit if
5109 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005110 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005111 I915_WRITE(PF_CTL(pipe), 0);
5112 I915_WRITE(PF_WIN_POS(pipe), 0);
5113 I915_WRITE(PF_WIN_SZ(pipe), 0);
5114 }
5115}
5116
Jesse Barnes6be4a602010-09-10 10:26:01 -07005117static void ironlake_crtc_disable(struct drm_crtc *crtc)
5118{
5119 struct drm_device *dev = crtc->dev;
5120 struct drm_i915_private *dev_priv = dev->dev_private;
5121 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005122 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005123 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005124
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005125 if (intel_crtc->config->has_pch_encoder)
5126 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5127
Daniel Vetterea9d7582012-07-10 10:42:52 +02005128 for_each_encoder_on_crtc(dev, crtc, encoder)
5129 encoder->disable(encoder);
5130
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005131 drm_crtc_vblank_off(crtc);
5132 assert_vblank_disabled(crtc);
5133
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005134 /*
5135 * Sometimes spurious CPU pipe underruns happen when the
5136 * pipe is already disabled, but FDI RX/TX is still enabled.
5137 * Happens at least with VGA+HDMI cloning. Suppress them.
5138 */
5139 if (intel_crtc->config->has_pch_encoder)
5140 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5141
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005142 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005143
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005144 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005145
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005146 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005147 ironlake_fdi_disable(crtc);
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005148 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5149 }
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005150
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005151 for_each_encoder_on_crtc(dev, crtc, encoder)
5152 if (encoder->post_disable)
5153 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005154
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005155 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005156 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005157
Daniel Vetterd925c592013-06-05 13:34:04 +02005158 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005159 i915_reg_t reg;
5160 u32 temp;
5161
Daniel Vetterd925c592013-06-05 13:34:04 +02005162 /* disable TRANS_DP_CTL */
5163 reg = TRANS_DP_CTL(pipe);
5164 temp = I915_READ(reg);
5165 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5166 TRANS_DP_PORT_SEL_MASK);
5167 temp |= TRANS_DP_PORT_SEL_NONE;
5168 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005169
Daniel Vetterd925c592013-06-05 13:34:04 +02005170 /* disable DPLL_SEL */
5171 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005172 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005173 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005174 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005175
Daniel Vetterd925c592013-06-05 13:34:04 +02005176 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005177 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005178
5179 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanonid029bca2015-10-15 10:44:46 -03005180
5181 intel_fbc_disable_crtc(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005182}
5183
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005184static void haswell_crtc_disable(struct drm_crtc *crtc)
5185{
5186 struct drm_device *dev = crtc->dev;
5187 struct drm_i915_private *dev_priv = dev->dev_private;
5188 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5189 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005190 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005191
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005192 if (intel_crtc->config->has_pch_encoder)
5193 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5194 false);
5195
Jani Nikula8807e552013-08-30 19:40:32 +03005196 for_each_encoder_on_crtc(dev, crtc, encoder) {
5197 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005198 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005199 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005200
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005201 drm_crtc_vblank_off(crtc);
5202 assert_vblank_disabled(crtc);
5203
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005204 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005205
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005206 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005207 intel_ddi_set_vc_payload_alloc(crtc, false);
5208
Jani Nikulaa65347b2015-11-27 12:21:46 +02005209 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305210 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005211
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005212 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005213 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005214 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005215 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005216
Jani Nikulaa65347b2015-11-27 12:21:46 +02005217 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305218 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005219
Imre Deak97b040a2014-06-25 22:01:50 +03005220 for_each_encoder_on_crtc(dev, crtc, encoder)
5221 if (encoder->post_disable)
5222 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005223
Ville Syrjälä92966a32015-12-08 16:05:48 +02005224 if (intel_crtc->config->has_pch_encoder) {
5225 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005226 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005227 intel_ddi_fdi_disable(crtc);
5228
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005229 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5230 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005231 }
Paulo Zanonid029bca2015-10-15 10:44:46 -03005232
5233 intel_fbc_disable_crtc(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005234}
5235
Jesse Barnes2dd24552013-04-25 12:55:01 -07005236static void i9xx_pfit_enable(struct intel_crtc *crtc)
5237{
5238 struct drm_device *dev = crtc->base.dev;
5239 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005240 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005241
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005242 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005243 return;
5244
Daniel Vetterc0b03412013-05-28 12:05:54 +02005245 /*
5246 * The panel fitter should only be adjusted whilst the pipe is disabled,
5247 * according to register description and PRM.
5248 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005249 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5250 assert_pipe_disabled(dev_priv, crtc->pipe);
5251
Jesse Barnesb074cec2013-04-25 12:55:02 -07005252 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5253 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005254
5255 /* Border color in case we don't scale up to the full screen. Black by
5256 * default, change to something else for debugging. */
5257 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005258}
5259
Dave Airlied05410f2014-06-05 13:22:59 +10005260static enum intel_display_power_domain port_to_power_domain(enum port port)
5261{
5262 switch (port) {
5263 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005264 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005265 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005266 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005267 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005268 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005269 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005270 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005271 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005272 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005273 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005274 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005275 return POWER_DOMAIN_PORT_OTHER;
5276 }
5277}
5278
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005279static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5280{
5281 switch (port) {
5282 case PORT_A:
5283 return POWER_DOMAIN_AUX_A;
5284 case PORT_B:
5285 return POWER_DOMAIN_AUX_B;
5286 case PORT_C:
5287 return POWER_DOMAIN_AUX_C;
5288 case PORT_D:
5289 return POWER_DOMAIN_AUX_D;
5290 case PORT_E:
5291 /* FIXME: Check VBT for actual wiring of PORT E */
5292 return POWER_DOMAIN_AUX_D;
5293 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005294 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005295 return POWER_DOMAIN_AUX_A;
5296 }
5297}
5298
Imre Deak319be8a2014-03-04 19:22:57 +02005299enum intel_display_power_domain
5300intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005301{
Imre Deak319be8a2014-03-04 19:22:57 +02005302 struct drm_device *dev = intel_encoder->base.dev;
5303 struct intel_digital_port *intel_dig_port;
5304
5305 switch (intel_encoder->type) {
5306 case INTEL_OUTPUT_UNKNOWN:
5307 /* Only DDI platforms should ever use this output type */
5308 WARN_ON_ONCE(!HAS_DDI(dev));
5309 case INTEL_OUTPUT_DISPLAYPORT:
5310 case INTEL_OUTPUT_HDMI:
5311 case INTEL_OUTPUT_EDP:
5312 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005313 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005314 case INTEL_OUTPUT_DP_MST:
5315 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5316 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005317 case INTEL_OUTPUT_ANALOG:
5318 return POWER_DOMAIN_PORT_CRT;
5319 case INTEL_OUTPUT_DSI:
5320 return POWER_DOMAIN_PORT_DSI;
5321 default:
5322 return POWER_DOMAIN_PORT_OTHER;
5323 }
5324}
5325
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005326enum intel_display_power_domain
5327intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5328{
5329 struct drm_device *dev = intel_encoder->base.dev;
5330 struct intel_digital_port *intel_dig_port;
5331
5332 switch (intel_encoder->type) {
5333 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005334 case INTEL_OUTPUT_HDMI:
5335 /*
5336 * Only DDI platforms should ever use these output types.
5337 * We can get here after the HDMI detect code has already set
5338 * the type of the shared encoder. Since we can't be sure
5339 * what's the status of the given connectors, play safe and
5340 * run the DP detection too.
5341 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005342 WARN_ON_ONCE(!HAS_DDI(dev));
5343 case INTEL_OUTPUT_DISPLAYPORT:
5344 case INTEL_OUTPUT_EDP:
5345 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5346 return port_to_aux_power_domain(intel_dig_port->port);
5347 case INTEL_OUTPUT_DP_MST:
5348 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5349 return port_to_aux_power_domain(intel_dig_port->port);
5350 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005351 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005352 return POWER_DOMAIN_AUX_A;
5353 }
5354}
5355
Imre Deak319be8a2014-03-04 19:22:57 +02005356static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5357{
5358 struct drm_device *dev = crtc->dev;
5359 struct intel_encoder *intel_encoder;
5360 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5361 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005362 unsigned long mask;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02005363 enum transcoder transcoder = intel_crtc->config->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005364
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005365 if (!crtc->state->active)
5366 return 0;
5367
Imre Deak77d22dc2014-03-05 16:20:52 +02005368 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5369 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005370 if (intel_crtc->config->pch_pfit.enabled ||
5371 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005372 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5373
Imre Deak319be8a2014-03-04 19:22:57 +02005374 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5375 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5376
Imre Deak77d22dc2014-03-05 16:20:52 +02005377 return mask;
5378}
5379
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005380static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
5381{
5382 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5383 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5384 enum intel_display_power_domain domain;
5385 unsigned long domains, new_domains, old_domains;
5386
5387 old_domains = intel_crtc->enabled_power_domains;
5388 intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
5389
5390 domains = new_domains & ~old_domains;
5391
5392 for_each_power_domain(domain, domains)
5393 intel_display_power_get(dev_priv, domain);
5394
5395 return old_domains & ~new_domains;
5396}
5397
5398static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5399 unsigned long domains)
5400{
5401 enum intel_display_power_domain domain;
5402
5403 for_each_power_domain(domain, domains)
5404 intel_display_power_put(dev_priv, domain);
5405}
5406
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005407static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005408{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005409 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005410 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005411 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005412 unsigned long put_domains[I915_MAX_PIPES] = {};
5413 struct drm_crtc_state *crtc_state;
5414 struct drm_crtc *crtc;
5415 int i;
Imre Deak77d22dc2014-03-05 16:20:52 +02005416
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005417 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5418 if (needs_modeset(crtc->state))
5419 put_domains[to_intel_crtc(crtc)->pipe] =
5420 modeset_get_crtc_power_domains(crtc);
Imre Deak77d22dc2014-03-05 16:20:52 +02005421 }
5422
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005423 if (dev_priv->display.modeset_commit_cdclk &&
5424 intel_state->dev_cdclk != dev_priv->cdclk_freq)
5425 dev_priv->display.modeset_commit_cdclk(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005426
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005427 for (i = 0; i < I915_MAX_PIPES; i++)
5428 if (put_domains[i])
5429 modeset_put_power_domains(dev_priv, put_domains[i]);
Imre Deak77d22dc2014-03-05 16:20:52 +02005430}
5431
Mika Kaholaadafdc62015-08-18 14:36:59 +03005432static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5433{
5434 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5435
5436 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5437 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5438 return max_cdclk_freq;
5439 else if (IS_CHERRYVIEW(dev_priv))
5440 return max_cdclk_freq*95/100;
5441 else if (INTEL_INFO(dev_priv)->gen < 4)
5442 return 2*max_cdclk_freq*90/100;
5443 else
5444 return max_cdclk_freq*90/100;
5445}
5446
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005447static void intel_update_max_cdclk(struct drm_device *dev)
5448{
5449 struct drm_i915_private *dev_priv = dev->dev_private;
5450
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005451 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005452 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5453
5454 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5455 dev_priv->max_cdclk_freq = 675000;
5456 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5457 dev_priv->max_cdclk_freq = 540000;
5458 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5459 dev_priv->max_cdclk_freq = 450000;
5460 else
5461 dev_priv->max_cdclk_freq = 337500;
5462 } else if (IS_BROADWELL(dev)) {
5463 /*
5464 * FIXME with extra cooling we can allow
5465 * 540 MHz for ULX and 675 Mhz for ULT.
5466 * How can we know if extra cooling is
5467 * available? PCI ID, VTB, something else?
5468 */
5469 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5470 dev_priv->max_cdclk_freq = 450000;
5471 else if (IS_BDW_ULX(dev))
5472 dev_priv->max_cdclk_freq = 450000;
5473 else if (IS_BDW_ULT(dev))
5474 dev_priv->max_cdclk_freq = 540000;
5475 else
5476 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005477 } else if (IS_CHERRYVIEW(dev)) {
5478 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005479 } else if (IS_VALLEYVIEW(dev)) {
5480 dev_priv->max_cdclk_freq = 400000;
5481 } else {
5482 /* otherwise assume cdclk is fixed */
5483 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5484 }
5485
Mika Kaholaadafdc62015-08-18 14:36:59 +03005486 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5487
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005488 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5489 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005490
5491 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5492 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005493}
5494
5495static void intel_update_cdclk(struct drm_device *dev)
5496{
5497 struct drm_i915_private *dev_priv = dev->dev_private;
5498
5499 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5500 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5501 dev_priv->cdclk_freq);
5502
5503 /*
5504 * Program the gmbus_freq based on the cdclk frequency.
5505 * BSpec erroneously claims we should aim for 4MHz, but
5506 * in fact 1MHz is the correct frequency.
5507 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005508 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005509 /*
5510 * Program the gmbus_freq based on the cdclk frequency.
5511 * BSpec erroneously claims we should aim for 4MHz, but
5512 * in fact 1MHz is the correct frequency.
5513 */
5514 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5515 }
5516
5517 if (dev_priv->max_cdclk_freq == 0)
5518 intel_update_max_cdclk(dev);
5519}
5520
Damien Lespiau70d0c572015-06-04 18:21:29 +01005521static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305522{
5523 struct drm_i915_private *dev_priv = dev->dev_private;
5524 uint32_t divider;
5525 uint32_t ratio;
5526 uint32_t current_freq;
5527 int ret;
5528
5529 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5530 switch (frequency) {
5531 case 144000:
5532 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5533 ratio = BXT_DE_PLL_RATIO(60);
5534 break;
5535 case 288000:
5536 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5537 ratio = BXT_DE_PLL_RATIO(60);
5538 break;
5539 case 384000:
5540 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5541 ratio = BXT_DE_PLL_RATIO(60);
5542 break;
5543 case 576000:
5544 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5545 ratio = BXT_DE_PLL_RATIO(60);
5546 break;
5547 case 624000:
5548 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5549 ratio = BXT_DE_PLL_RATIO(65);
5550 break;
5551 case 19200:
5552 /*
5553 * Bypass frequency with DE PLL disabled. Init ratio, divider
5554 * to suppress GCC warning.
5555 */
5556 ratio = 0;
5557 divider = 0;
5558 break;
5559 default:
5560 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5561
5562 return;
5563 }
5564
5565 mutex_lock(&dev_priv->rps.hw_lock);
5566 /* Inform power controller of upcoming frequency change */
5567 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5568 0x80000000);
5569 mutex_unlock(&dev_priv->rps.hw_lock);
5570
5571 if (ret) {
5572 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5573 ret, frequency);
5574 return;
5575 }
5576
5577 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5578 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5579 current_freq = current_freq * 500 + 1000;
5580
5581 /*
5582 * DE PLL has to be disabled when
5583 * - setting to 19.2MHz (bypass, PLL isn't used)
5584 * - before setting to 624MHz (PLL needs toggling)
5585 * - before setting to any frequency from 624MHz (PLL needs toggling)
5586 */
5587 if (frequency == 19200 || frequency == 624000 ||
5588 current_freq == 624000) {
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),
5592 1))
5593 DRM_ERROR("timout waiting for DE PLL unlock\n");
5594 }
5595
5596 if (frequency != 19200) {
5597 uint32_t val;
5598
5599 val = I915_READ(BXT_DE_PLL_CTL);
5600 val &= ~BXT_DE_PLL_RATIO_MASK;
5601 val |= ratio;
5602 I915_WRITE(BXT_DE_PLL_CTL, val);
5603
5604 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5605 /* Timeout 200us */
5606 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5607 DRM_ERROR("timeout waiting for DE PLL lock\n");
5608
5609 val = I915_READ(CDCLK_CTL);
5610 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5611 val |= divider;
5612 /*
5613 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5614 * enable otherwise.
5615 */
5616 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5617 if (frequency >= 500000)
5618 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5619
5620 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5621 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5622 val |= (frequency - 1000) / 500;
5623 I915_WRITE(CDCLK_CTL, val);
5624 }
5625
5626 mutex_lock(&dev_priv->rps.hw_lock);
5627 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5628 DIV_ROUND_UP(frequency, 25000));
5629 mutex_unlock(&dev_priv->rps.hw_lock);
5630
5631 if (ret) {
5632 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5633 ret, frequency);
5634 return;
5635 }
5636
Damien Lespiaua47871b2015-06-04 18:21:34 +01005637 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305638}
5639
5640void broxton_init_cdclk(struct drm_device *dev)
5641{
5642 struct drm_i915_private *dev_priv = dev->dev_private;
5643 uint32_t val;
5644
5645 /*
5646 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5647 * or else the reset will hang because there is no PCH to respond.
5648 * Move the handshake programming to initialization sequence.
5649 * Previously was left up to BIOS.
5650 */
5651 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5652 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5653 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5654
5655 /* Enable PG1 for cdclk */
5656 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5657
5658 /* check if cd clock is enabled */
5659 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5660 DRM_DEBUG_KMS("Display already initialized\n");
5661 return;
5662 }
5663
5664 /*
5665 * FIXME:
5666 * - The initial CDCLK needs to be read from VBT.
5667 * Need to make this change after VBT has changes for BXT.
5668 * - check if setting the max (or any) cdclk freq is really necessary
5669 * here, it belongs to modeset time
5670 */
5671 broxton_set_cdclk(dev, 624000);
5672
5673 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005674 POSTING_READ(DBUF_CTL);
5675
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305676 udelay(10);
5677
5678 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5679 DRM_ERROR("DBuf power enable timeout!\n");
5680}
5681
5682void broxton_uninit_cdclk(struct drm_device *dev)
5683{
5684 struct drm_i915_private *dev_priv = dev->dev_private;
5685
5686 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005687 POSTING_READ(DBUF_CTL);
5688
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305689 udelay(10);
5690
5691 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5692 DRM_ERROR("DBuf power disable timeout!\n");
5693
5694 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5695 broxton_set_cdclk(dev, 19200);
5696
5697 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5698}
5699
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005700static const struct skl_cdclk_entry {
5701 unsigned int freq;
5702 unsigned int vco;
5703} skl_cdclk_frequencies[] = {
5704 { .freq = 308570, .vco = 8640 },
5705 { .freq = 337500, .vco = 8100 },
5706 { .freq = 432000, .vco = 8640 },
5707 { .freq = 450000, .vco = 8100 },
5708 { .freq = 540000, .vco = 8100 },
5709 { .freq = 617140, .vco = 8640 },
5710 { .freq = 675000, .vco = 8100 },
5711};
5712
5713static unsigned int skl_cdclk_decimal(unsigned int freq)
5714{
5715 return (freq - 1000) / 500;
5716}
5717
5718static unsigned int skl_cdclk_get_vco(unsigned int freq)
5719{
5720 unsigned int i;
5721
5722 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5723 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5724
5725 if (e->freq == freq)
5726 return e->vco;
5727 }
5728
5729 return 8100;
5730}
5731
5732static void
5733skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5734{
5735 unsigned int min_freq;
5736 u32 val;
5737
5738 /* select the minimum CDCLK before enabling DPLL 0 */
5739 val = I915_READ(CDCLK_CTL);
5740 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5741 val |= CDCLK_FREQ_337_308;
5742
5743 if (required_vco == 8640)
5744 min_freq = 308570;
5745 else
5746 min_freq = 337500;
5747
5748 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5749
5750 I915_WRITE(CDCLK_CTL, val);
5751 POSTING_READ(CDCLK_CTL);
5752
5753 /*
5754 * We always enable DPLL0 with the lowest link rate possible, but still
5755 * taking into account the VCO required to operate the eDP panel at the
5756 * desired frequency. The usual DP link rates operate with a VCO of
5757 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5758 * The modeset code is responsible for the selection of the exact link
5759 * rate later on, with the constraint of choosing a frequency that
5760 * works with required_vco.
5761 */
5762 val = I915_READ(DPLL_CTRL1);
5763
5764 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5765 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5766 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5767 if (required_vco == 8640)
5768 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5769 SKL_DPLL0);
5770 else
5771 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5772 SKL_DPLL0);
5773
5774 I915_WRITE(DPLL_CTRL1, val);
5775 POSTING_READ(DPLL_CTRL1);
5776
5777 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5778
5779 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5780 DRM_ERROR("DPLL0 not locked\n");
5781}
5782
5783static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5784{
5785 int ret;
5786 u32 val;
5787
5788 /* inform PCU we want to change CDCLK */
5789 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5790 mutex_lock(&dev_priv->rps.hw_lock);
5791 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5792 mutex_unlock(&dev_priv->rps.hw_lock);
5793
5794 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5795}
5796
5797static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5798{
5799 unsigned int i;
5800
5801 for (i = 0; i < 15; i++) {
5802 if (skl_cdclk_pcu_ready(dev_priv))
5803 return true;
5804 udelay(10);
5805 }
5806
5807 return false;
5808}
5809
5810static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5811{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005812 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005813 u32 freq_select, pcu_ack;
5814
5815 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5816
5817 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5818 DRM_ERROR("failed to inform PCU about cdclk change\n");
5819 return;
5820 }
5821
5822 /* set CDCLK_CTL */
5823 switch(freq) {
5824 case 450000:
5825 case 432000:
5826 freq_select = CDCLK_FREQ_450_432;
5827 pcu_ack = 1;
5828 break;
5829 case 540000:
5830 freq_select = CDCLK_FREQ_540;
5831 pcu_ack = 2;
5832 break;
5833 case 308570:
5834 case 337500:
5835 default:
5836 freq_select = CDCLK_FREQ_337_308;
5837 pcu_ack = 0;
5838 break;
5839 case 617140:
5840 case 675000:
5841 freq_select = CDCLK_FREQ_675_617;
5842 pcu_ack = 3;
5843 break;
5844 }
5845
5846 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5847 POSTING_READ(CDCLK_CTL);
5848
5849 /* inform PCU of the change */
5850 mutex_lock(&dev_priv->rps.hw_lock);
5851 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5852 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005853
5854 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005855}
5856
5857void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5858{
5859 /* disable DBUF power */
5860 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5861 POSTING_READ(DBUF_CTL);
5862
5863 udelay(10);
5864
5865 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5866 DRM_ERROR("DBuf power disable timeout\n");
5867
Imre Deakab96c1ee2015-11-04 19:24:18 +02005868 /* disable DPLL0 */
5869 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5870 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5871 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005872}
5873
5874void skl_init_cdclk(struct drm_i915_private *dev_priv)
5875{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005876 unsigned int required_vco;
5877
Gary Wang39d9b852015-08-28 16:40:34 +08005878 /* DPLL0 not enabled (happens on early BIOS versions) */
5879 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5880 /* enable DPLL0 */
5881 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5882 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005883 }
5884
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005885 /* set CDCLK to the frequency the BIOS chose */
5886 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5887
5888 /* enable DBUF power */
5889 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5890 POSTING_READ(DBUF_CTL);
5891
5892 udelay(10);
5893
5894 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5895 DRM_ERROR("DBuf power enable timeout\n");
5896}
5897
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305898int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5899{
5900 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5901 uint32_t cdctl = I915_READ(CDCLK_CTL);
5902 int freq = dev_priv->skl_boot_cdclk;
5903
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305904 /*
5905 * check if the pre-os intialized the display
5906 * There is SWF18 scratchpad register defined which is set by the
5907 * pre-os which can be used by the OS drivers to check the status
5908 */
5909 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5910 goto sanitize;
5911
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305912 /* Is PLL enabled and locked ? */
5913 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5914 goto sanitize;
5915
5916 /* DPLL okay; verify the cdclock
5917 *
5918 * Noticed in some instances that the freq selection is correct but
5919 * decimal part is programmed wrong from BIOS where pre-os does not
5920 * enable display. Verify the same as well.
5921 */
5922 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5923 /* All well; nothing to sanitize */
5924 return false;
5925sanitize:
5926 /*
5927 * As of now initialize with max cdclk till
5928 * we get dynamic cdclk support
5929 * */
5930 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5931 skl_init_cdclk(dev_priv);
5932
5933 /* we did have to sanitize */
5934 return true;
5935}
5936
Jesse Barnes30a970c2013-11-04 13:48:12 -08005937/* Adjust CDclk dividers to allow high res or save power if possible */
5938static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5939{
5940 struct drm_i915_private *dev_priv = dev->dev_private;
5941 u32 val, cmd;
5942
Vandana Kannan164dfd22014-11-24 13:37:41 +05305943 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5944 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005945
Ville Syrjälädfcab172014-06-13 13:37:47 +03005946 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005947 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005948 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005949 cmd = 1;
5950 else
5951 cmd = 0;
5952
5953 mutex_lock(&dev_priv->rps.hw_lock);
5954 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5955 val &= ~DSPFREQGUAR_MASK;
5956 val |= (cmd << DSPFREQGUAR_SHIFT);
5957 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5958 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5959 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5960 50)) {
5961 DRM_ERROR("timed out waiting for CDclk change\n");
5962 }
5963 mutex_unlock(&dev_priv->rps.hw_lock);
5964
Ville Syrjälä54433e92015-05-26 20:42:31 +03005965 mutex_lock(&dev_priv->sb_lock);
5966
Ville Syrjälädfcab172014-06-13 13:37:47 +03005967 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005968 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005969
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005970 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005971
Jesse Barnes30a970c2013-11-04 13:48:12 -08005972 /* adjust cdclk divider */
5973 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005974 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005975 val |= divider;
5976 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005977
5978 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005979 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005980 50))
5981 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005982 }
5983
Jesse Barnes30a970c2013-11-04 13:48:12 -08005984 /* adjust self-refresh exit latency value */
5985 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5986 val &= ~0x7f;
5987
5988 /*
5989 * For high bandwidth configs, we set a higher latency in the bunit
5990 * so that the core display fetch happens in time to avoid underruns.
5991 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005992 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005993 val |= 4500 / 250; /* 4.5 usec */
5994 else
5995 val |= 3000 / 250; /* 3.0 usec */
5996 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005997
Ville Syrjäläa5805162015-05-26 20:42:30 +03005998 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005999
Ville Syrjäläb6283052015-06-03 15:45:07 +03006000 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006001}
6002
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006003static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6004{
6005 struct drm_i915_private *dev_priv = dev->dev_private;
6006 u32 val, cmd;
6007
Vandana Kannan164dfd22014-11-24 13:37:41 +05306008 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6009 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006010
6011 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006012 case 333333:
6013 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006014 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006015 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006016 break;
6017 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01006018 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006019 return;
6020 }
6021
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02006022 /*
6023 * Specs are full of misinformation, but testing on actual
6024 * hardware has shown that we just need to write the desired
6025 * CCK divider into the Punit register.
6026 */
6027 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6028
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006029 mutex_lock(&dev_priv->rps.hw_lock);
6030 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6031 val &= ~DSPFREQGUAR_MASK_CHV;
6032 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6033 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6034 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6035 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6036 50)) {
6037 DRM_ERROR("timed out waiting for CDclk change\n");
6038 }
6039 mutex_unlock(&dev_priv->rps.hw_lock);
6040
Ville Syrjäläb6283052015-06-03 15:45:07 +03006041 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006042}
6043
Jesse Barnes30a970c2013-11-04 13:48:12 -08006044static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6045 int max_pixclk)
6046{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006047 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006048 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006049
Jesse Barnes30a970c2013-11-04 13:48:12 -08006050 /*
6051 * Really only a few cases to deal with, as only 4 CDclks are supported:
6052 * 200MHz
6053 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006054 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006055 * 400MHz (VLV only)
6056 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6057 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006058 *
6059 * We seem to get an unstable or solid color picture at 200MHz.
6060 * Not sure what's wrong. For now use 200MHz only when all pipes
6061 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08006062 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006063 if (!IS_CHERRYVIEW(dev_priv) &&
6064 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006065 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006066 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006067 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006068 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006069 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006070 else
6071 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006072}
6073
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306074static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
6075 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006076{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306077 /*
6078 * FIXME:
6079 * - remove the guardband, it's not needed on BXT
6080 * - set 19.2MHz bypass frequency if there are no active pipes
6081 */
6082 if (max_pixclk > 576000*9/10)
6083 return 624000;
6084 else if (max_pixclk > 384000*9/10)
6085 return 576000;
6086 else if (max_pixclk > 288000*9/10)
6087 return 384000;
6088 else if (max_pixclk > 144000*9/10)
6089 return 288000;
6090 else
6091 return 144000;
6092}
6093
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006094/* Compute the max pixel clock for new configuration. Uses atomic state if
6095 * that's non-NULL, look at current state otherwise. */
6096static int intel_mode_max_pixclk(struct drm_device *dev,
6097 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006098{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006099 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
6100 struct drm_i915_private *dev_priv = dev->dev_private;
6101 struct drm_crtc *crtc;
6102 struct drm_crtc_state *crtc_state;
6103 unsigned max_pixclk = 0, i;
6104 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006105
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006106 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6107 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006108
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006109 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6110 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006111
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006112 if (crtc_state->enable)
6113 pixclk = crtc_state->adjusted_mode.crtc_clock;
6114
6115 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006116 }
6117
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006118 if (!intel_state->active_crtcs)
6119 return 0;
6120
6121 for_each_pipe(dev_priv, pipe)
6122 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6123
Jesse Barnes30a970c2013-11-04 13:48:12 -08006124 return max_pixclk;
6125}
6126
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006127static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006128{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006129 struct drm_device *dev = state->dev;
6130 struct drm_i915_private *dev_priv = dev->dev_private;
6131 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006132 struct intel_atomic_state *intel_state =
6133 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006134
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006135 if (max_pixclk < 0)
6136 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006137
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006138 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006139 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306140
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006141 if (!intel_state->active_crtcs)
6142 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6143
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006144 return 0;
6145}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006146
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006147static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
6148{
6149 struct drm_device *dev = state->dev;
6150 struct drm_i915_private *dev_priv = dev->dev_private;
6151 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006152 struct intel_atomic_state *intel_state =
6153 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006154
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006155 if (max_pixclk < 0)
6156 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006157
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006158 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006159 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006160
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006161 if (!intel_state->active_crtcs)
6162 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
6163
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006164 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006165}
6166
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006167static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6168{
6169 unsigned int credits, default_credits;
6170
6171 if (IS_CHERRYVIEW(dev_priv))
6172 default_credits = PFI_CREDIT(12);
6173 else
6174 default_credits = PFI_CREDIT(8);
6175
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006176 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006177 /* CHV suggested value is 31 or 63 */
6178 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006179 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006180 else
6181 credits = PFI_CREDIT(15);
6182 } else {
6183 credits = default_credits;
6184 }
6185
6186 /*
6187 * WA - write default credits before re-programming
6188 * FIXME: should we also set the resend bit here?
6189 */
6190 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6191 default_credits);
6192
6193 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6194 credits | PFI_CREDIT_RESEND);
6195
6196 /*
6197 * FIXME is this guaranteed to clear
6198 * immediately or should we poll for it?
6199 */
6200 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6201}
6202
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006203static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006204{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006205 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006206 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006207 struct intel_atomic_state *old_intel_state =
6208 to_intel_atomic_state(old_state);
6209 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006210
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006211 /*
6212 * FIXME: We can end up here with all power domains off, yet
6213 * with a CDCLK frequency other than the minimum. To account
6214 * for this take the PIPE-A power domain, which covers the HW
6215 * blocks needed for the following programming. This can be
6216 * removed once it's guaranteed that we get here either with
6217 * the minimum CDCLK set, or the required power domains
6218 * enabled.
6219 */
6220 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006221
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006222 if (IS_CHERRYVIEW(dev))
6223 cherryview_set_cdclk(dev, req_cdclk);
6224 else
6225 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006226
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006227 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006228
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006229 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006230}
6231
Jesse Barnes89b667f2013-04-18 14:51:36 -07006232static void valleyview_crtc_enable(struct drm_crtc *crtc)
6233{
6234 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006235 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6237 struct intel_encoder *encoder;
6238 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006239
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006240 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006241 return;
6242
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006243 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306244 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006245
6246 intel_set_pipe_timings(intel_crtc);
6247
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006248 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6249 struct drm_i915_private *dev_priv = dev->dev_private;
6250
6251 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6252 I915_WRITE(CHV_CANVAS(pipe), 0);
6253 }
6254
Daniel Vetter5b18e572014-04-24 23:55:06 +02006255 i9xx_set_pipeconf(intel_crtc);
6256
Jesse Barnes89b667f2013-04-18 14:51:36 -07006257 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006258
Daniel Vettera72e4c92014-09-30 10:56:47 +02006259 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006260
Jesse Barnes89b667f2013-04-18 14:51:36 -07006261 for_each_encoder_on_crtc(dev, crtc, encoder)
6262 if (encoder->pre_pll_enable)
6263 encoder->pre_pll_enable(encoder);
6264
Jani Nikulaa65347b2015-11-27 12:21:46 +02006265 if (!intel_crtc->config->has_dsi_encoder) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006266 if (IS_CHERRYVIEW(dev)) {
6267 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006268 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006269 } else {
6270 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006271 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006272 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006273 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006274
6275 for_each_encoder_on_crtc(dev, crtc, encoder)
6276 if (encoder->pre_enable)
6277 encoder->pre_enable(encoder);
6278
Jesse Barnes2dd24552013-04-25 12:55:01 -07006279 i9xx_pfit_enable(intel_crtc);
6280
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006281 intel_crtc_load_lut(crtc);
6282
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006283 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006284
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006285 assert_vblank_disabled(crtc);
6286 drm_crtc_vblank_on(crtc);
6287
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006288 for_each_encoder_on_crtc(dev, crtc, encoder)
6289 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006290}
6291
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006292static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6293{
6294 struct drm_device *dev = crtc->base.dev;
6295 struct drm_i915_private *dev_priv = dev->dev_private;
6296
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006297 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6298 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006299}
6300
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006301static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006302{
6303 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006304 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006306 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006307 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006308
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006309 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006310 return;
6311
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006312 i9xx_set_pll_dividers(intel_crtc);
6313
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006314 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306315 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006316
6317 intel_set_pipe_timings(intel_crtc);
6318
Daniel Vetter5b18e572014-04-24 23:55:06 +02006319 i9xx_set_pipeconf(intel_crtc);
6320
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006321 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006322
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006323 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006324 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006325
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006326 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006327 if (encoder->pre_enable)
6328 encoder->pre_enable(encoder);
6329
Daniel Vetterf6736a12013-06-05 13:34:30 +02006330 i9xx_enable_pll(intel_crtc);
6331
Jesse Barnes2dd24552013-04-25 12:55:01 -07006332 i9xx_pfit_enable(intel_crtc);
6333
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006334 intel_crtc_load_lut(crtc);
6335
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006336 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006337 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006338
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006339 assert_vblank_disabled(crtc);
6340 drm_crtc_vblank_on(crtc);
6341
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006342 for_each_encoder_on_crtc(dev, crtc, encoder)
6343 encoder->enable(encoder);
Paulo Zanonid029bca2015-10-15 10:44:46 -03006344
6345 intel_fbc_enable(intel_crtc);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006346}
6347
Daniel Vetter87476d62013-04-11 16:29:06 +02006348static void i9xx_pfit_disable(struct intel_crtc *crtc)
6349{
6350 struct drm_device *dev = crtc->base.dev;
6351 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006352
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006353 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006354 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006355
6356 assert_pipe_disabled(dev_priv, crtc->pipe);
6357
Daniel Vetter328d8e82013-05-08 10:36:31 +02006358 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6359 I915_READ(PFIT_CONTROL));
6360 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006361}
6362
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006363static void i9xx_crtc_disable(struct drm_crtc *crtc)
6364{
6365 struct drm_device *dev = crtc->dev;
6366 struct drm_i915_private *dev_priv = dev->dev_private;
6367 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006368 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006369 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006370
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006371 /*
6372 * On gen2 planes are double buffered but the pipe isn't, so we must
6373 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006374 * We also need to wait on all gmch platforms because of the
6375 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006376 */
Imre Deak564ed192014-06-13 14:54:21 +03006377 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006378
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006379 for_each_encoder_on_crtc(dev, crtc, encoder)
6380 encoder->disable(encoder);
6381
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006382 drm_crtc_vblank_off(crtc);
6383 assert_vblank_disabled(crtc);
6384
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006385 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006386
Daniel Vetter87476d62013-04-11 16:29:06 +02006387 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006388
Jesse Barnes89b667f2013-04-18 14:51:36 -07006389 for_each_encoder_on_crtc(dev, crtc, encoder)
6390 if (encoder->post_disable)
6391 encoder->post_disable(encoder);
6392
Jani Nikulaa65347b2015-11-27 12:21:46 +02006393 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006394 if (IS_CHERRYVIEW(dev))
6395 chv_disable_pll(dev_priv, pipe);
6396 else if (IS_VALLEYVIEW(dev))
6397 vlv_disable_pll(dev_priv, pipe);
6398 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006399 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006400 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006401
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006402 for_each_encoder_on_crtc(dev, crtc, encoder)
6403 if (encoder->post_pll_disable)
6404 encoder->post_pll_disable(encoder);
6405
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006406 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006407 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Paulo Zanonid029bca2015-10-15 10:44:46 -03006408
6409 intel_fbc_disable_crtc(intel_crtc);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006410}
6411
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006412static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006413{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006414 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006415 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006416 enum intel_display_power_domain domain;
6417 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006418
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006419 if (!intel_crtc->active)
6420 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006421
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006422 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006423 WARN_ON(intel_crtc->unpin_work);
6424
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006425 intel_pre_disable_primary(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006426
6427 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6428 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006429 }
6430
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006431 dev_priv->display.crtc_disable(crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006432 intel_crtc->active = false;
6433 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006434 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006435
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006436 domains = intel_crtc->enabled_power_domains;
6437 for_each_power_domain(domain, domains)
6438 intel_display_power_put(dev_priv, domain);
6439 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006440
6441 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6442 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006443}
6444
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006445/*
6446 * turn all crtc's off, but do not adjust state
6447 * This has to be paired with a call to intel_modeset_setup_hw_state.
6448 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006449int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006450{
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006451 struct drm_mode_config *config = &dev->mode_config;
6452 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
6453 struct drm_atomic_state *state;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006454 struct drm_crtc *crtc;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006455 unsigned crtc_mask = 0;
6456 int ret = 0;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006457
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006458 if (WARN_ON(!ctx))
6459 return 0;
6460
6461 lockdep_assert_held(&ctx->ww_ctx);
6462 state = drm_atomic_state_alloc(dev);
6463 if (WARN_ON(!state))
6464 return -ENOMEM;
6465
6466 state->acquire_ctx = ctx;
6467 state->allow_modeset = true;
6468
6469 for_each_crtc(dev, crtc) {
6470 struct drm_crtc_state *crtc_state =
6471 drm_atomic_get_crtc_state(state, crtc);
6472
6473 ret = PTR_ERR_OR_ZERO(crtc_state);
6474 if (ret)
6475 goto free;
6476
6477 if (!crtc_state->active)
6478 continue;
6479
6480 crtc_state->active = false;
6481 crtc_mask |= 1 << drm_crtc_index(crtc);
6482 }
6483
6484 if (crtc_mask) {
Maarten Lankhorst74c090b2015-07-13 16:30:30 +02006485 ret = drm_atomic_commit(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006486
6487 if (!ret) {
6488 for_each_crtc(dev, crtc)
6489 if (crtc_mask & (1 << drm_crtc_index(crtc)))
6490 crtc->state->active = true;
6491
6492 return ret;
6493 }
6494 }
6495
6496free:
6497 if (ret)
6498 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
6499 drm_atomic_state_free(state);
6500 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006501}
6502
Chris Wilsonea5b2132010-08-04 13:50:23 +01006503void intel_encoder_destroy(struct drm_encoder *encoder)
6504{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006505 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006506
Chris Wilsonea5b2132010-08-04 13:50:23 +01006507 drm_encoder_cleanup(encoder);
6508 kfree(intel_encoder);
6509}
6510
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006511/* Cross check the actual hw state with our own modeset state tracking (and it's
6512 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006513static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006514{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006515 struct drm_crtc *crtc = connector->base.state->crtc;
6516
6517 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6518 connector->base.base.id,
6519 connector->base.name);
6520
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006521 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006522 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006523 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006524
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006525 I915_STATE_WARN(!crtc,
6526 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006527
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006528 if (!crtc)
6529 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006530
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006531 I915_STATE_WARN(!crtc->state->active,
6532 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006533
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006534 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006535 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006536
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006537 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006538 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006539
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006540 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006541 "attached encoder crtc differs from connector crtc\n");
6542 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006543 I915_STATE_WARN(crtc && crtc->state->active,
6544 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006545 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6546 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006547 }
6548}
6549
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006550int intel_connector_init(struct intel_connector *connector)
6551{
6552 struct drm_connector_state *connector_state;
6553
6554 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6555 if (!connector_state)
6556 return -ENOMEM;
6557
6558 connector->base.state = connector_state;
6559 return 0;
6560}
6561
6562struct intel_connector *intel_connector_alloc(void)
6563{
6564 struct intel_connector *connector;
6565
6566 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6567 if (!connector)
6568 return NULL;
6569
6570 if (intel_connector_init(connector) < 0) {
6571 kfree(connector);
6572 return NULL;
6573 }
6574
6575 return connector;
6576}
6577
Daniel Vetterf0947c32012-07-02 13:10:34 +02006578/* Simple connector->get_hw_state implementation for encoders that support only
6579 * one connector and no cloning and hence the encoder state determines the state
6580 * of the connector. */
6581bool intel_connector_get_hw_state(struct intel_connector *connector)
6582{
Daniel Vetter24929352012-07-02 20:28:59 +02006583 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006584 struct intel_encoder *encoder = connector->encoder;
6585
6586 return encoder->get_hw_state(encoder, &pipe);
6587}
6588
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006589static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006590{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006591 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6592 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006593
6594 return 0;
6595}
6596
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006597static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006598 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006599{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006600 struct drm_atomic_state *state = pipe_config->base.state;
6601 struct intel_crtc *other_crtc;
6602 struct intel_crtc_state *other_crtc_state;
6603
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006604 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6605 pipe_name(pipe), pipe_config->fdi_lanes);
6606 if (pipe_config->fdi_lanes > 4) {
6607 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6608 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006609 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006610 }
6611
Paulo Zanonibafb6552013-11-02 21:07:44 -07006612 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006613 if (pipe_config->fdi_lanes > 2) {
6614 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6615 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006616 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006617 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006618 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006619 }
6620 }
6621
6622 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006623 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006624
6625 /* Ivybridge 3 pipe is really complicated */
6626 switch (pipe) {
6627 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006628 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006629 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006630 if (pipe_config->fdi_lanes <= 2)
6631 return 0;
6632
6633 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6634 other_crtc_state =
6635 intel_atomic_get_crtc_state(state, other_crtc);
6636 if (IS_ERR(other_crtc_state))
6637 return PTR_ERR(other_crtc_state);
6638
6639 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006640 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6641 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006642 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006643 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006644 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006645 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006646 if (pipe_config->fdi_lanes > 2) {
6647 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6648 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006649 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006650 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006651
6652 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6653 other_crtc_state =
6654 intel_atomic_get_crtc_state(state, other_crtc);
6655 if (IS_ERR(other_crtc_state))
6656 return PTR_ERR(other_crtc_state);
6657
6658 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006659 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006660 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006661 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006662 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006663 default:
6664 BUG();
6665 }
6666}
6667
Daniel Vettere29c22c2013-02-21 00:00:16 +01006668#define RETRY 1
6669static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006670 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006671{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006672 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006673 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006674 int lane, link_bw, fdi_dotclock, ret;
6675 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006676
Daniel Vettere29c22c2013-02-21 00:00:16 +01006677retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006678 /* FDI is a binary signal running at ~2.7GHz, encoding
6679 * each output octet as 10 bits. The actual frequency
6680 * is stored as a divider into a 100MHz clock, and the
6681 * mode pixel clock is stored in units of 1KHz.
6682 * Hence the bw of each lane in terms of the mode signal
6683 * is:
6684 */
6685 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6686
Damien Lespiau241bfc32013-09-25 16:45:37 +01006687 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006688
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006689 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006690 pipe_config->pipe_bpp);
6691
6692 pipe_config->fdi_lanes = lane;
6693
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006694 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006695 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006696
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006697 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6698 intel_crtc->pipe, pipe_config);
6699 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006700 pipe_config->pipe_bpp -= 2*3;
6701 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6702 pipe_config->pipe_bpp);
6703 needs_recompute = true;
6704 pipe_config->bw_constrained = true;
6705
6706 goto retry;
6707 }
6708
6709 if (needs_recompute)
6710 return RETRY;
6711
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006712 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006713}
6714
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006715static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6716 struct intel_crtc_state *pipe_config)
6717{
6718 if (pipe_config->pipe_bpp > 24)
6719 return false;
6720
6721 /* HSW can handle pixel rate up to cdclk? */
6722 if (IS_HASWELL(dev_priv->dev))
6723 return true;
6724
6725 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006726 * We compare against max which means we must take
6727 * the increased cdclk requirement into account when
6728 * calculating the new cdclk.
6729 *
6730 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006731 */
6732 return ilk_pipe_pixel_rate(pipe_config) <=
6733 dev_priv->max_cdclk_freq * 95 / 100;
6734}
6735
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006736static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006737 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006738{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006739 struct drm_device *dev = crtc->base.dev;
6740 struct drm_i915_private *dev_priv = dev->dev_private;
6741
Jani Nikulad330a952014-01-21 11:24:25 +02006742 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006743 hsw_crtc_supports_ips(crtc) &&
6744 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006745}
6746
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006747static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6748{
6749 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6750
6751 /* GDG double wide on either pipe, otherwise pipe A only */
6752 return INTEL_INFO(dev_priv)->gen < 4 &&
6753 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6754}
6755
Daniel Vettera43f6e02013-06-07 23:10:32 +02006756static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006757 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006758{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006759 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006760 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006761 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006762
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006763 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006764 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006765 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006766
6767 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006768 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006769 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006770 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006771 if (intel_crtc_supports_double_wide(crtc) &&
6772 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006773 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006774 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006775 }
6776
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006777 if (adjusted_mode->crtc_clock > clock_limit) {
6778 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6779 adjusted_mode->crtc_clock, clock_limit,
6780 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006781 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006782 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006783 }
Chris Wilson89749352010-09-12 18:25:19 +01006784
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006785 /*
6786 * Pipe horizontal size must be even in:
6787 * - DVO ganged mode
6788 * - LVDS dual channel mode
6789 * - Double wide pipe
6790 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006791 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006792 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6793 pipe_config->pipe_src_w &= ~1;
6794
Damien Lespiau8693a822013-05-03 18:48:11 +01006795 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6796 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006797 */
6798 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006799 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006800 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006801
Damien Lespiauf5adf942013-06-24 18:29:34 +01006802 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006803 hsw_compute_ips_config(crtc, pipe_config);
6804
Daniel Vetter877d48d2013-04-19 11:24:43 +02006805 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006806 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006807
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006808 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006809}
6810
Ville Syrjälä1652d192015-03-31 14:12:01 +03006811static int skylake_get_display_clock_speed(struct drm_device *dev)
6812{
6813 struct drm_i915_private *dev_priv = to_i915(dev);
6814 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6815 uint32_t cdctl = I915_READ(CDCLK_CTL);
6816 uint32_t linkrate;
6817
Damien Lespiau414355a2015-06-04 18:21:31 +01006818 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006819 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006820
6821 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6822 return 540000;
6823
6824 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006825 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006826
Damien Lespiau71cd8422015-04-30 16:39:17 +01006827 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6828 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006829 /* vco 8640 */
6830 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6831 case CDCLK_FREQ_450_432:
6832 return 432000;
6833 case CDCLK_FREQ_337_308:
6834 return 308570;
6835 case CDCLK_FREQ_675_617:
6836 return 617140;
6837 default:
6838 WARN(1, "Unknown cd freq selection\n");
6839 }
6840 } else {
6841 /* vco 8100 */
6842 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6843 case CDCLK_FREQ_450_432:
6844 return 450000;
6845 case CDCLK_FREQ_337_308:
6846 return 337500;
6847 case CDCLK_FREQ_675_617:
6848 return 675000;
6849 default:
6850 WARN(1, "Unknown cd freq selection\n");
6851 }
6852 }
6853
6854 /* error case, do as if DPLL0 isn't enabled */
6855 return 24000;
6856}
6857
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006858static int broxton_get_display_clock_speed(struct drm_device *dev)
6859{
6860 struct drm_i915_private *dev_priv = to_i915(dev);
6861 uint32_t cdctl = I915_READ(CDCLK_CTL);
6862 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6863 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6864 int cdclk;
6865
6866 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6867 return 19200;
6868
6869 cdclk = 19200 * pll_ratio / 2;
6870
6871 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6872 case BXT_CDCLK_CD2X_DIV_SEL_1:
6873 return cdclk; /* 576MHz or 624MHz */
6874 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6875 return cdclk * 2 / 3; /* 384MHz */
6876 case BXT_CDCLK_CD2X_DIV_SEL_2:
6877 return cdclk / 2; /* 288MHz */
6878 case BXT_CDCLK_CD2X_DIV_SEL_4:
6879 return cdclk / 4; /* 144MHz */
6880 }
6881
6882 /* error case, do as if DE PLL isn't enabled */
6883 return 19200;
6884}
6885
Ville Syrjälä1652d192015-03-31 14:12:01 +03006886static int broadwell_get_display_clock_speed(struct drm_device *dev)
6887{
6888 struct drm_i915_private *dev_priv = dev->dev_private;
6889 uint32_t lcpll = I915_READ(LCPLL_CTL);
6890 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6891
6892 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6893 return 800000;
6894 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6895 return 450000;
6896 else if (freq == LCPLL_CLK_FREQ_450)
6897 return 450000;
6898 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6899 return 540000;
6900 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6901 return 337500;
6902 else
6903 return 675000;
6904}
6905
6906static int haswell_get_display_clock_speed(struct drm_device *dev)
6907{
6908 struct drm_i915_private *dev_priv = dev->dev_private;
6909 uint32_t lcpll = I915_READ(LCPLL_CTL);
6910 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6911
6912 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6913 return 800000;
6914 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6915 return 450000;
6916 else if (freq == LCPLL_CLK_FREQ_450)
6917 return 450000;
6918 else if (IS_HSW_ULT(dev))
6919 return 337500;
6920 else
6921 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006922}
6923
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006924static int valleyview_get_display_clock_speed(struct drm_device *dev)
6925{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006926 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6927 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006928}
6929
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006930static int ilk_get_display_clock_speed(struct drm_device *dev)
6931{
6932 return 450000;
6933}
6934
Jesse Barnese70236a2009-09-21 10:42:27 -07006935static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006936{
Jesse Barnese70236a2009-09-21 10:42:27 -07006937 return 400000;
6938}
Jesse Barnes79e53942008-11-07 14:24:08 -08006939
Jesse Barnese70236a2009-09-21 10:42:27 -07006940static int i915_get_display_clock_speed(struct drm_device *dev)
6941{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006942 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006943}
Jesse Barnes79e53942008-11-07 14:24:08 -08006944
Jesse Barnese70236a2009-09-21 10:42:27 -07006945static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6946{
6947 return 200000;
6948}
Jesse Barnes79e53942008-11-07 14:24:08 -08006949
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006950static int pnv_get_display_clock_speed(struct drm_device *dev)
6951{
6952 u16 gcfgc = 0;
6953
6954 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6955
6956 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6957 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006958 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006959 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006960 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006961 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006962 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006963 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6964 return 200000;
6965 default:
6966 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6967 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006968 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006969 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006970 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006971 }
6972}
6973
Jesse Barnese70236a2009-09-21 10:42:27 -07006974static int i915gm_get_display_clock_speed(struct drm_device *dev)
6975{
6976 u16 gcfgc = 0;
6977
6978 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6979
6980 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006981 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006982 else {
6983 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6984 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006985 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006986 default:
6987 case GC_DISPLAY_CLOCK_190_200_MHZ:
6988 return 190000;
6989 }
6990 }
6991}
Jesse Barnes79e53942008-11-07 14:24:08 -08006992
Jesse Barnese70236a2009-09-21 10:42:27 -07006993static int i865_get_display_clock_speed(struct drm_device *dev)
6994{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006995 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006996}
6997
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006998static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006999{
7000 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007001
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03007002 /*
7003 * 852GM/852GMV only supports 133 MHz and the HPLLCC
7004 * encoding is different :(
7005 * FIXME is this the right way to detect 852GM/852GMV?
7006 */
7007 if (dev->pdev->revision == 0x1)
7008 return 133333;
7009
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007010 pci_bus_read_config_word(dev->pdev->bus,
7011 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7012
Jesse Barnese70236a2009-09-21 10:42:27 -07007013 /* Assume that the hardware is in the high speed state. This
7014 * should be the default.
7015 */
7016 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7017 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007018 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07007019 case GC_CLOCK_100_200:
7020 return 200000;
7021 case GC_CLOCK_166_250:
7022 return 250000;
7023 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007024 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007025 case GC_CLOCK_133_266:
7026 case GC_CLOCK_133_266_2:
7027 case GC_CLOCK_166_266:
7028 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007029 }
7030
7031 /* Shouldn't happen */
7032 return 0;
7033}
7034
7035static int i830_get_display_clock_speed(struct drm_device *dev)
7036{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007037 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08007038}
7039
Ville Syrjälä34edce22015-05-22 11:22:33 +03007040static unsigned int intel_hpll_vco(struct drm_device *dev)
7041{
7042 struct drm_i915_private *dev_priv = dev->dev_private;
7043 static const unsigned int blb_vco[8] = {
7044 [0] = 3200000,
7045 [1] = 4000000,
7046 [2] = 5333333,
7047 [3] = 4800000,
7048 [4] = 6400000,
7049 };
7050 static const unsigned int pnv_vco[8] = {
7051 [0] = 3200000,
7052 [1] = 4000000,
7053 [2] = 5333333,
7054 [3] = 4800000,
7055 [4] = 2666667,
7056 };
7057 static const unsigned int cl_vco[8] = {
7058 [0] = 3200000,
7059 [1] = 4000000,
7060 [2] = 5333333,
7061 [3] = 6400000,
7062 [4] = 3333333,
7063 [5] = 3566667,
7064 [6] = 4266667,
7065 };
7066 static const unsigned int elk_vco[8] = {
7067 [0] = 3200000,
7068 [1] = 4000000,
7069 [2] = 5333333,
7070 [3] = 4800000,
7071 };
7072 static const unsigned int ctg_vco[8] = {
7073 [0] = 3200000,
7074 [1] = 4000000,
7075 [2] = 5333333,
7076 [3] = 6400000,
7077 [4] = 2666667,
7078 [5] = 4266667,
7079 };
7080 const unsigned int *vco_table;
7081 unsigned int vco;
7082 uint8_t tmp = 0;
7083
7084 /* FIXME other chipsets? */
7085 if (IS_GM45(dev))
7086 vco_table = ctg_vco;
7087 else if (IS_G4X(dev))
7088 vco_table = elk_vco;
7089 else if (IS_CRESTLINE(dev))
7090 vco_table = cl_vco;
7091 else if (IS_PINEVIEW(dev))
7092 vco_table = pnv_vco;
7093 else if (IS_G33(dev))
7094 vco_table = blb_vco;
7095 else
7096 return 0;
7097
7098 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7099
7100 vco = vco_table[tmp & 0x7];
7101 if (vco == 0)
7102 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7103 else
7104 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7105
7106 return vco;
7107}
7108
7109static int gm45_get_display_clock_speed(struct drm_device *dev)
7110{
7111 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7112 uint16_t tmp = 0;
7113
7114 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7115
7116 cdclk_sel = (tmp >> 12) & 0x1;
7117
7118 switch (vco) {
7119 case 2666667:
7120 case 4000000:
7121 case 5333333:
7122 return cdclk_sel ? 333333 : 222222;
7123 case 3200000:
7124 return cdclk_sel ? 320000 : 228571;
7125 default:
7126 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7127 return 222222;
7128 }
7129}
7130
7131static int i965gm_get_display_clock_speed(struct drm_device *dev)
7132{
7133 static const uint8_t div_3200[] = { 16, 10, 8 };
7134 static const uint8_t div_4000[] = { 20, 12, 10 };
7135 static const uint8_t div_5333[] = { 24, 16, 14 };
7136 const uint8_t *div_table;
7137 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7138 uint16_t tmp = 0;
7139
7140 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7141
7142 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7143
7144 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7145 goto fail;
7146
7147 switch (vco) {
7148 case 3200000:
7149 div_table = div_3200;
7150 break;
7151 case 4000000:
7152 div_table = div_4000;
7153 break;
7154 case 5333333:
7155 div_table = div_5333;
7156 break;
7157 default:
7158 goto fail;
7159 }
7160
7161 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7162
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007163fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007164 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7165 return 200000;
7166}
7167
7168static int g33_get_display_clock_speed(struct drm_device *dev)
7169{
7170 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7171 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7172 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7173 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7174 const uint8_t *div_table;
7175 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7176 uint16_t tmp = 0;
7177
7178 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7179
7180 cdclk_sel = (tmp >> 4) & 0x7;
7181
7182 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7183 goto fail;
7184
7185 switch (vco) {
7186 case 3200000:
7187 div_table = div_3200;
7188 break;
7189 case 4000000:
7190 div_table = div_4000;
7191 break;
7192 case 4800000:
7193 div_table = div_4800;
7194 break;
7195 case 5333333:
7196 div_table = div_5333;
7197 break;
7198 default:
7199 goto fail;
7200 }
7201
7202 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7203
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007204fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007205 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7206 return 190476;
7207}
7208
Zhenyu Wang2c072452009-06-05 15:38:42 +08007209static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007210intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007211{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007212 while (*num > DATA_LINK_M_N_MASK ||
7213 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007214 *num >>= 1;
7215 *den >>= 1;
7216 }
7217}
7218
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007219static void compute_m_n(unsigned int m, unsigned int n,
7220 uint32_t *ret_m, uint32_t *ret_n)
7221{
7222 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7223 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7224 intel_reduce_m_n_ratio(ret_m, ret_n);
7225}
7226
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007227void
7228intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7229 int pixel_clock, int link_clock,
7230 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007231{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007232 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007233
7234 compute_m_n(bits_per_pixel * pixel_clock,
7235 link_clock * nlanes * 8,
7236 &m_n->gmch_m, &m_n->gmch_n);
7237
7238 compute_m_n(pixel_clock, link_clock,
7239 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007240}
7241
Chris Wilsona7615032011-01-12 17:04:08 +00007242static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7243{
Jani Nikulad330a952014-01-21 11:24:25 +02007244 if (i915.panel_use_ssc >= 0)
7245 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007246 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007247 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007248}
7249
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007250static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7251 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007252{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007253 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007254 struct drm_i915_private *dev_priv = dev->dev_private;
7255 int refclk;
7256
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007257 WARN_ON(!crtc_state->base.state);
7258
Wayne Boyer666a4532015-12-09 12:29:35 -08007259 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007260 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007261 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007262 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007263 refclk = dev_priv->vbt.lvds_ssc_freq;
7264 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007265 } else if (!IS_GEN2(dev)) {
7266 refclk = 96000;
7267 } else {
7268 refclk = 48000;
7269 }
7270
7271 return refclk;
7272}
7273
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007274static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007275{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007276 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007277}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007278
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007279static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7280{
7281 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007282}
7283
Daniel Vetterf47709a2013-03-28 10:42:02 +01007284static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007285 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007286 intel_clock_t *reduced_clock)
7287{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007288 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007289 u32 fp, fp2 = 0;
7290
7291 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007292 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007293 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007294 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007295 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007296 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007297 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007298 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007299 }
7300
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007301 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007302
Daniel Vetterf47709a2013-03-28 10:42:02 +01007303 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007304 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007305 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007306 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007307 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007308 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007309 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007310 }
7311}
7312
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007313static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7314 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007315{
7316 u32 reg_val;
7317
7318 /*
7319 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7320 * and set it to a reasonable value instead.
7321 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007322 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007323 reg_val &= 0xffffff00;
7324 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007325 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007326
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007327 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007328 reg_val &= 0x8cffffff;
7329 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007330 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007331
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007332 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007333 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007334 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007335
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007336 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007337 reg_val &= 0x00ffffff;
7338 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007339 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007340}
7341
Daniel Vetterb5518422013-05-03 11:49:48 +02007342static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7343 struct intel_link_m_n *m_n)
7344{
7345 struct drm_device *dev = crtc->base.dev;
7346 struct drm_i915_private *dev_priv = dev->dev_private;
7347 int pipe = crtc->pipe;
7348
Daniel Vettere3b95f12013-05-03 11:49:49 +02007349 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7350 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7351 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7352 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007353}
7354
7355static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007356 struct intel_link_m_n *m_n,
7357 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007358{
7359 struct drm_device *dev = crtc->base.dev;
7360 struct drm_i915_private *dev_priv = dev->dev_private;
7361 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007362 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007363
7364 if (INTEL_INFO(dev)->gen >= 5) {
7365 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7366 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7367 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7368 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007369 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7370 * for gen < 8) and if DRRS is supported (to make sure the
7371 * registers are not unnecessarily accessed).
7372 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307373 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007374 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007375 I915_WRITE(PIPE_DATA_M2(transcoder),
7376 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7377 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7378 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7379 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7380 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007381 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007382 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7383 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7384 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7385 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007386 }
7387}
7388
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307389void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007390{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307391 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7392
7393 if (m_n == M1_N1) {
7394 dp_m_n = &crtc->config->dp_m_n;
7395 dp_m2_n2 = &crtc->config->dp_m2_n2;
7396 } else if (m_n == M2_N2) {
7397
7398 /*
7399 * M2_N2 registers are not supported. Hence m2_n2 divider value
7400 * needs to be programmed into M1_N1.
7401 */
7402 dp_m_n = &crtc->config->dp_m2_n2;
7403 } else {
7404 DRM_ERROR("Unsupported divider value\n");
7405 return;
7406 }
7407
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007408 if (crtc->config->has_pch_encoder)
7409 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007410 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307411 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007412}
7413
Daniel Vetter251ac862015-06-18 10:30:24 +02007414static void vlv_compute_dpll(struct intel_crtc *crtc,
7415 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007416{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007417 u32 dpll, dpll_md;
7418
7419 /*
7420 * Enable DPIO clock input. We should never disable the reference
7421 * clock for pipe B, since VGA hotplug / manual detection depends
7422 * on it.
7423 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007424 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7425 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007426 /* We should never disable this, set it here for state tracking */
7427 if (crtc->pipe == PIPE_B)
7428 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7429 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007430 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007431
Ville Syrjäläd288f652014-10-28 13:20:22 +02007432 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007433 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007434 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007435}
7436
Ville Syrjäläd288f652014-10-28 13:20:22 +02007437static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007438 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007439{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007440 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007441 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007442 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007443 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007444 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007445 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007446
Ville Syrjäläa5805162015-05-26 20:42:30 +03007447 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007448
Ville Syrjäläd288f652014-10-28 13:20:22 +02007449 bestn = pipe_config->dpll.n;
7450 bestm1 = pipe_config->dpll.m1;
7451 bestm2 = pipe_config->dpll.m2;
7452 bestp1 = pipe_config->dpll.p1;
7453 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007454
Jesse Barnes89b667f2013-04-18 14:51:36 -07007455 /* See eDP HDMI DPIO driver vbios notes doc */
7456
7457 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007458 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007459 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007460
7461 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007462 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007463
7464 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007465 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007466 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007467 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007468
7469 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007470 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007471
7472 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007473 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7474 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7475 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007476 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007477
7478 /*
7479 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7480 * but we don't support that).
7481 * Note: don't use the DAC post divider as it seems unstable.
7482 */
7483 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007484 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007485
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007486 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007487 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007488
Jesse Barnes89b667f2013-04-18 14:51:36 -07007489 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007490 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007491 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7492 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007493 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007494 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007495 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007496 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007497 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007498
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007499 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007500 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007501 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007502 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007503 0x0df40000);
7504 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007505 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007506 0x0df70000);
7507 } else { /* HDMI or VGA */
7508 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007509 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007510 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007511 0x0df70000);
7512 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007513 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007514 0x0df40000);
7515 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007516
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007517 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007518 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007519 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7520 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007521 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007522 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007523
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007524 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007525 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007526}
7527
Daniel Vetter251ac862015-06-18 10:30:24 +02007528static void chv_compute_dpll(struct intel_crtc *crtc,
7529 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007530{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007531 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7532 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007533 DPLL_VCO_ENABLE;
7534 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007535 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007536
Ville Syrjäläd288f652014-10-28 13:20:22 +02007537 pipe_config->dpll_hw_state.dpll_md =
7538 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007539}
7540
Ville Syrjäläd288f652014-10-28 13:20:22 +02007541static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007542 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007543{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007544 struct drm_device *dev = crtc->base.dev;
7545 struct drm_i915_private *dev_priv = dev->dev_private;
7546 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007547 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007548 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307549 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007550 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307551 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307552 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007553
Ville Syrjäläd288f652014-10-28 13:20:22 +02007554 bestn = pipe_config->dpll.n;
7555 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7556 bestm1 = pipe_config->dpll.m1;
7557 bestm2 = pipe_config->dpll.m2 >> 22;
7558 bestp1 = pipe_config->dpll.p1;
7559 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307560 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307561 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307562 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007563
7564 /*
7565 * Enable Refclk and SSC
7566 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007567 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007568 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007569
Ville Syrjäläa5805162015-05-26 20:42:30 +03007570 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007571
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007572 /* p1 and p2 divider */
7573 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7574 5 << DPIO_CHV_S1_DIV_SHIFT |
7575 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7576 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7577 1 << DPIO_CHV_K_DIV_SHIFT);
7578
7579 /* Feedback post-divider - m2 */
7580 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7581
7582 /* Feedback refclk divider - n and m1 */
7583 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7584 DPIO_CHV_M1_DIV_BY_2 |
7585 1 << DPIO_CHV_N_DIV_SHIFT);
7586
7587 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007588 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007589
7590 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307591 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7592 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7593 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7594 if (bestm2_frac)
7595 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7596 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007597
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307598 /* Program digital lock detect threshold */
7599 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7600 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7601 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7602 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7603 if (!bestm2_frac)
7604 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7605 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7606
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007607 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307608 if (vco == 5400000) {
7609 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7610 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7611 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7612 tribuf_calcntr = 0x9;
7613 } else if (vco <= 6200000) {
7614 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7615 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7616 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7617 tribuf_calcntr = 0x9;
7618 } else if (vco <= 6480000) {
7619 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7620 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7621 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7622 tribuf_calcntr = 0x8;
7623 } else {
7624 /* Not supported. Apply the same limits as in the max case */
7625 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7626 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7627 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7628 tribuf_calcntr = 0;
7629 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007630 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7631
Ville Syrjälä968040b2015-03-11 22:52:08 +02007632 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307633 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7634 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7635 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7636
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007637 /* AFC Recal */
7638 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7639 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7640 DPIO_AFC_RECAL);
7641
Ville Syrjäläa5805162015-05-26 20:42:30 +03007642 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007643}
7644
Ville Syrjäläd288f652014-10-28 13:20:22 +02007645/**
7646 * vlv_force_pll_on - forcibly enable just the PLL
7647 * @dev_priv: i915 private structure
7648 * @pipe: pipe PLL to enable
7649 * @dpll: PLL configuration
7650 *
7651 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7652 * in cases where we need the PLL enabled even when @pipe is not going to
7653 * be enabled.
7654 */
7655void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7656 const struct dpll *dpll)
7657{
7658 struct intel_crtc *crtc =
7659 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007660 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007661 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007662 .pixel_multiplier = 1,
7663 .dpll = *dpll,
7664 };
7665
7666 if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007667 chv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007668 chv_prepare_pll(crtc, &pipe_config);
7669 chv_enable_pll(crtc, &pipe_config);
7670 } else {
Daniel Vetter251ac862015-06-18 10:30:24 +02007671 vlv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007672 vlv_prepare_pll(crtc, &pipe_config);
7673 vlv_enable_pll(crtc, &pipe_config);
7674 }
7675}
7676
7677/**
7678 * vlv_force_pll_off - forcibly disable just the PLL
7679 * @dev_priv: i915 private structure
7680 * @pipe: pipe PLL to disable
7681 *
7682 * Disable the PLL for @pipe. To be used in cases where we need
7683 * the PLL enabled even when @pipe is not going to be enabled.
7684 */
7685void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7686{
7687 if (IS_CHERRYVIEW(dev))
7688 chv_disable_pll(to_i915(dev), pipe);
7689 else
7690 vlv_disable_pll(to_i915(dev), pipe);
7691}
7692
Daniel Vetter251ac862015-06-18 10:30:24 +02007693static void i9xx_compute_dpll(struct intel_crtc *crtc,
7694 struct intel_crtc_state *crtc_state,
7695 intel_clock_t *reduced_clock,
7696 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007697{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007698 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007699 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007700 u32 dpll;
7701 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007702 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007703
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007704 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307705
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007706 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7707 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007708
7709 dpll = DPLL_VGA_MODE_DIS;
7710
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007711 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007712 dpll |= DPLLB_MODE_LVDS;
7713 else
7714 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007715
Daniel Vetteref1b4602013-06-01 17:17:04 +02007716 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007717 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007718 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007719 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007720
7721 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007722 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007723
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007724 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007725 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007726
7727 /* compute bitmask from p1 value */
7728 if (IS_PINEVIEW(dev))
7729 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7730 else {
7731 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7732 if (IS_G4X(dev) && reduced_clock)
7733 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7734 }
7735 switch (clock->p2) {
7736 case 5:
7737 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7738 break;
7739 case 7:
7740 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7741 break;
7742 case 10:
7743 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7744 break;
7745 case 14:
7746 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7747 break;
7748 }
7749 if (INTEL_INFO(dev)->gen >= 4)
7750 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7751
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007752 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007753 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007754 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007755 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7756 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7757 else
7758 dpll |= PLL_REF_INPUT_DREFCLK;
7759
7760 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007761 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007762
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007763 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007764 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007765 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007766 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007767 }
7768}
7769
Daniel Vetter251ac862015-06-18 10:30:24 +02007770static void i8xx_compute_dpll(struct intel_crtc *crtc,
7771 struct intel_crtc_state *crtc_state,
7772 intel_clock_t *reduced_clock,
7773 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007774{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007775 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007776 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007777 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007778 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007779
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007780 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307781
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007782 dpll = DPLL_VGA_MODE_DIS;
7783
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007784 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007785 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7786 } else {
7787 if (clock->p1 == 2)
7788 dpll |= PLL_P1_DIVIDE_BY_TWO;
7789 else
7790 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7791 if (clock->p2 == 4)
7792 dpll |= PLL_P2_DIVIDE_BY_4;
7793 }
7794
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007795 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007796 dpll |= DPLL_DVO_2X_MODE;
7797
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007798 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007799 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7800 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7801 else
7802 dpll |= PLL_REF_INPUT_DREFCLK;
7803
7804 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007805 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007806}
7807
Daniel Vetter8a654f32013-06-01 17:16:22 +02007808static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007809{
7810 struct drm_device *dev = intel_crtc->base.dev;
7811 struct drm_i915_private *dev_priv = dev->dev_private;
7812 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007813 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007814 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007815 uint32_t crtc_vtotal, crtc_vblank_end;
7816 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007817
7818 /* We need to be careful not to changed the adjusted mode, for otherwise
7819 * the hw state checker will get angry at the mismatch. */
7820 crtc_vtotal = adjusted_mode->crtc_vtotal;
7821 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007822
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007823 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007824 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007825 crtc_vtotal -= 1;
7826 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007827
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007828 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007829 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7830 else
7831 vsyncshift = adjusted_mode->crtc_hsync_start -
7832 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007833 if (vsyncshift < 0)
7834 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007835 }
7836
7837 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007838 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007839
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007840 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007841 (adjusted_mode->crtc_hdisplay - 1) |
7842 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007843 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007844 (adjusted_mode->crtc_hblank_start - 1) |
7845 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007846 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007847 (adjusted_mode->crtc_hsync_start - 1) |
7848 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7849
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007850 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007851 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007852 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007853 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007854 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007855 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007856 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007857 (adjusted_mode->crtc_vsync_start - 1) |
7858 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7859
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007860 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7861 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7862 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7863 * bits. */
7864 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7865 (pipe == PIPE_B || pipe == PIPE_C))
7866 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7867
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007868 /* pipesrc controls the size that is scaled from, which should
7869 * always be the user's requested size.
7870 */
7871 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007872 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7873 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007874}
7875
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007876static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007877 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007878{
7879 struct drm_device *dev = crtc->base.dev;
7880 struct drm_i915_private *dev_priv = dev->dev_private;
7881 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7882 uint32_t tmp;
7883
7884 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007885 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7886 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007887 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007888 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7889 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007890 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007891 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7892 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007893
7894 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007895 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7896 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007897 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007898 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7899 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007900 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007901 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7902 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007903
7904 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007905 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7906 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7907 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007908 }
7909
7910 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007911 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7912 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7913
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007914 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7915 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007916}
7917
Daniel Vetterf6a83282014-02-11 15:28:57 -08007918void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007919 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007920{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007921 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7922 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7923 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7924 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007925
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007926 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7927 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7928 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7929 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007930
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007931 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007932 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007933
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007934 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7935 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007936
7937 mode->hsync = drm_mode_hsync(mode);
7938 mode->vrefresh = drm_mode_vrefresh(mode);
7939 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007940}
7941
Daniel Vetter84b046f2013-02-19 18:48:54 +01007942static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7943{
7944 struct drm_device *dev = intel_crtc->base.dev;
7945 struct drm_i915_private *dev_priv = dev->dev_private;
7946 uint32_t pipeconf;
7947
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007948 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007949
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007950 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7951 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7952 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007953
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007954 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007955 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007956
Daniel Vetterff9ce462013-04-24 14:57:17 +02007957 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007958 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007959 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007960 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007961 pipeconf |= PIPECONF_DITHER_EN |
7962 PIPECONF_DITHER_TYPE_SP;
7963
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007964 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007965 case 18:
7966 pipeconf |= PIPECONF_6BPC;
7967 break;
7968 case 24:
7969 pipeconf |= PIPECONF_8BPC;
7970 break;
7971 case 30:
7972 pipeconf |= PIPECONF_10BPC;
7973 break;
7974 default:
7975 /* Case prevented by intel_choose_pipe_bpp_dither. */
7976 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007977 }
7978 }
7979
7980 if (HAS_PIPE_CXSR(dev)) {
7981 if (intel_crtc->lowfreq_avail) {
7982 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7983 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7984 } else {
7985 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007986 }
7987 }
7988
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007989 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007990 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007991 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007992 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7993 else
7994 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7995 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007996 pipeconf |= PIPECONF_PROGRESSIVE;
7997
Wayne Boyer666a4532015-12-09 12:29:35 -08007998 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7999 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02008000 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03008001
Daniel Vetter84b046f2013-02-19 18:48:54 +01008002 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8003 POSTING_READ(PIPECONF(intel_crtc->pipe));
8004}
8005
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008006static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8007 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008008{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008009 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008010 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07008011 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008012 intel_clock_t clock;
8013 bool ok;
Ma Lingd4906092009-03-18 20:13:27 +08008014 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008015 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008016 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008017 struct drm_connector_state *connector_state;
8018 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008019
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008020 memset(&crtc_state->dpll_hw_state, 0,
8021 sizeof(crtc_state->dpll_hw_state));
8022
Jani Nikulaa65347b2015-11-27 12:21:46 +02008023 if (crtc_state->has_dsi_encoder)
Daniel Vetter5b18e572014-04-24 23:55:06 +02008024 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008025
Jani Nikulaa65347b2015-11-27 12:21:46 +02008026 for_each_connector_in_state(state, connector, connector_state, i) {
8027 if (connector_state->crtc == &crtc->base)
8028 num_connectors++;
8029 }
8030
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008031 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008032 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03008033
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008034 /*
8035 * Returns a set of divisors for the desired target clock with
8036 * the given refclk, or FALSE. The returned values represent
8037 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
8038 * 2) / p1 / p2.
8039 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008040 limit = intel_limit(crtc_state, refclk);
8041 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008042 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008043 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03008044 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008045 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8046 return -EINVAL;
8047 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008048
Jani Nikulaf2335332013-09-13 11:03:09 +03008049 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008050 crtc_state->dpll.n = clock.n;
8051 crtc_state->dpll.m1 = clock.m1;
8052 crtc_state->dpll.m2 = clock.m2;
8053 crtc_state->dpll.p1 = clock.p1;
8054 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008055 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008056
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008057 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008058 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02008059 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008060 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02008061 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008062 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02008063 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008064 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008065 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02008066 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008067 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008068
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008069 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008070}
8071
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008072static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008073 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008074{
8075 struct drm_device *dev = crtc->base.dev;
8076 struct drm_i915_private *dev_priv = dev->dev_private;
8077 uint32_t tmp;
8078
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008079 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8080 return;
8081
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008082 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008083 if (!(tmp & PFIT_ENABLE))
8084 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008085
Daniel Vetter06922822013-07-11 13:35:40 +02008086 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008087 if (INTEL_INFO(dev)->gen < 4) {
8088 if (crtc->pipe != PIPE_B)
8089 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008090 } else {
8091 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8092 return;
8093 }
8094
Daniel Vetter06922822013-07-11 13:35:40 +02008095 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008096 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
8097 if (INTEL_INFO(dev)->gen < 5)
8098 pipe_config->gmch_pfit.lvds_border_bits =
8099 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
8100}
8101
Jesse Barnesacbec812013-09-20 11:29:32 -07008102static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008103 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008104{
8105 struct drm_device *dev = crtc->base.dev;
8106 struct drm_i915_private *dev_priv = dev->dev_private;
8107 int pipe = pipe_config->cpu_transcoder;
8108 intel_clock_t clock;
8109 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008110 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008111
Shobhit Kumarf573de52014-07-30 20:32:37 +05308112 /* In case of MIPI DPLL will not even be used */
8113 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8114 return;
8115
Ville Syrjäläa5805162015-05-26 20:42:30 +03008116 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008117 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008118 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008119
8120 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8121 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8122 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8123 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8124 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8125
Imre Deakdccbea32015-06-22 23:35:51 +03008126 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008127}
8128
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008129static void
8130i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8131 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008132{
8133 struct drm_device *dev = crtc->base.dev;
8134 struct drm_i915_private *dev_priv = dev->dev_private;
8135 u32 val, base, offset;
8136 int pipe = crtc->pipe, plane = crtc->plane;
8137 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008138 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008139 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008140 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008141
Damien Lespiau42a7b082015-02-05 19:35:13 +00008142 val = I915_READ(DSPCNTR(plane));
8143 if (!(val & DISPLAY_PLANE_ENABLE))
8144 return;
8145
Damien Lespiaud9806c92015-01-21 14:07:19 +00008146 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008147 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008148 DRM_DEBUG_KMS("failed to alloc fb\n");
8149 return;
8150 }
8151
Damien Lespiau1b842c82015-01-21 13:50:54 +00008152 fb = &intel_fb->base;
8153
Daniel Vetter18c52472015-02-10 17:16:09 +00008154 if (INTEL_INFO(dev)->gen >= 4) {
8155 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008156 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008157 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8158 }
8159 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008160
8161 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008162 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008163 fb->pixel_format = fourcc;
8164 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008165
8166 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008167 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008168 offset = I915_READ(DSPTILEOFF(plane));
8169 else
8170 offset = I915_READ(DSPLINOFF(plane));
8171 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8172 } else {
8173 base = I915_READ(DSPADDR(plane));
8174 }
8175 plane_config->base = base;
8176
8177 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008178 fb->width = ((val >> 16) & 0xfff) + 1;
8179 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008180
8181 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008182 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008183
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008184 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008185 fb->pixel_format,
8186 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008187
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008188 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008189
Damien Lespiau2844a922015-01-20 12:51:48 +00008190 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8191 pipe_name(pipe), plane, fb->width, fb->height,
8192 fb->bits_per_pixel, base, fb->pitches[0],
8193 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008194
Damien Lespiau2d140302015-02-05 17:22:18 +00008195 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008196}
8197
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008198static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008199 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008200{
8201 struct drm_device *dev = crtc->base.dev;
8202 struct drm_i915_private *dev_priv = dev->dev_private;
8203 int pipe = pipe_config->cpu_transcoder;
8204 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8205 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008206 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008207 int refclk = 100000;
8208
Ville Syrjäläa5805162015-05-26 20:42:30 +03008209 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008210 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8211 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8212 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8213 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008214 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008215 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008216
8217 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008218 clock.m2 = (pll_dw0 & 0xff) << 22;
8219 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8220 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008221 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8222 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8223 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8224
Imre Deakdccbea32015-06-22 23:35:51 +03008225 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008226}
8227
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008228static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008229 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008230{
8231 struct drm_device *dev = crtc->base.dev;
8232 struct drm_i915_private *dev_priv = dev->dev_private;
8233 uint32_t tmp;
8234
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008235 if (!intel_display_power_is_enabled(dev_priv,
8236 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008237 return false;
8238
Daniel Vettere143a212013-07-04 12:01:15 +02008239 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008240 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008241
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008242 tmp = I915_READ(PIPECONF(crtc->pipe));
8243 if (!(tmp & PIPECONF_ENABLE))
8244 return false;
8245
Wayne Boyer666a4532015-12-09 12:29:35 -08008246 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008247 switch (tmp & PIPECONF_BPC_MASK) {
8248 case PIPECONF_6BPC:
8249 pipe_config->pipe_bpp = 18;
8250 break;
8251 case PIPECONF_8BPC:
8252 pipe_config->pipe_bpp = 24;
8253 break;
8254 case PIPECONF_10BPC:
8255 pipe_config->pipe_bpp = 30;
8256 break;
8257 default:
8258 break;
8259 }
8260 }
8261
Wayne Boyer666a4532015-12-09 12:29:35 -08008262 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8263 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008264 pipe_config->limited_color_range = true;
8265
Ville Syrjälä282740f2013-09-04 18:30:03 +03008266 if (INTEL_INFO(dev)->gen < 4)
8267 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8268
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008269 intel_get_pipe_timings(crtc, pipe_config);
8270
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008271 i9xx_get_pfit_config(crtc, pipe_config);
8272
Daniel Vetter6c49f242013-06-06 12:45:25 +02008273 if (INTEL_INFO(dev)->gen >= 4) {
8274 tmp = I915_READ(DPLL_MD(crtc->pipe));
8275 pipe_config->pixel_multiplier =
8276 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8277 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008278 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008279 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8280 tmp = I915_READ(DPLL(crtc->pipe));
8281 pipe_config->pixel_multiplier =
8282 ((tmp & SDVO_MULTIPLIER_MASK)
8283 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8284 } else {
8285 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8286 * port and will be fixed up in the encoder->get_config
8287 * function. */
8288 pipe_config->pixel_multiplier = 1;
8289 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008290 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008291 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008292 /*
8293 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8294 * on 830. Filter it out here so that we don't
8295 * report errors due to that.
8296 */
8297 if (IS_I830(dev))
8298 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8299
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008300 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8301 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008302 } else {
8303 /* Mask out read-only status bits. */
8304 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8305 DPLL_PORTC_READY_MASK |
8306 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008307 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008308
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008309 if (IS_CHERRYVIEW(dev))
8310 chv_crtc_clock_get(crtc, pipe_config);
8311 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008312 vlv_crtc_clock_get(crtc, pipe_config);
8313 else
8314 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008315
Ville Syrjälä0f646142015-08-26 19:39:18 +03008316 /*
8317 * Normally the dotclock is filled in by the encoder .get_config()
8318 * but in case the pipe is enabled w/o any ports we need a sane
8319 * default.
8320 */
8321 pipe_config->base.adjusted_mode.crtc_clock =
8322 pipe_config->port_clock / pipe_config->pixel_multiplier;
8323
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008324 return true;
8325}
8326
Paulo Zanonidde86e22012-12-01 12:04:25 -02008327static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008328{
8329 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008330 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008331 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008332 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008333 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008334 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008335 bool has_ck505 = false;
8336 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008337
8338 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008339 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008340 switch (encoder->type) {
8341 case INTEL_OUTPUT_LVDS:
8342 has_panel = true;
8343 has_lvds = true;
8344 break;
8345 case INTEL_OUTPUT_EDP:
8346 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008347 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008348 has_cpu_edp = true;
8349 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008350 default:
8351 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008352 }
8353 }
8354
Keith Packard99eb6a02011-09-26 14:29:12 -07008355 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008356 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008357 can_ssc = has_ck505;
8358 } else {
8359 has_ck505 = false;
8360 can_ssc = true;
8361 }
8362
Imre Deak2de69052013-05-08 13:14:04 +03008363 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8364 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008365
8366 /* Ironlake: try to setup display ref clock before DPLL
8367 * enabling. This is only under driver's control after
8368 * PCH B stepping, previous chipset stepping should be
8369 * ignoring this setting.
8370 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008371 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008372
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008373 /* As we must carefully and slowly disable/enable each source in turn,
8374 * compute the final state we want first and check if we need to
8375 * make any changes at all.
8376 */
8377 final = val;
8378 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008379 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008380 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008381 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008382 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8383
8384 final &= ~DREF_SSC_SOURCE_MASK;
8385 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8386 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008387
Keith Packard199e5d72011-09-22 12:01:57 -07008388 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008389 final |= DREF_SSC_SOURCE_ENABLE;
8390
8391 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8392 final |= DREF_SSC1_ENABLE;
8393
8394 if (has_cpu_edp) {
8395 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8396 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8397 else
8398 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8399 } else
8400 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8401 } else {
8402 final |= DREF_SSC_SOURCE_DISABLE;
8403 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8404 }
8405
8406 if (final == val)
8407 return;
8408
8409 /* Always enable nonspread source */
8410 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8411
8412 if (has_ck505)
8413 val |= DREF_NONSPREAD_CK505_ENABLE;
8414 else
8415 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8416
8417 if (has_panel) {
8418 val &= ~DREF_SSC_SOURCE_MASK;
8419 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008420
Keith Packard199e5d72011-09-22 12:01:57 -07008421 /* SSC must be turned on before enabling the CPU output */
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 panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008424 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008425 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008426 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008427
8428 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008429 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008430 POSTING_READ(PCH_DREF_CONTROL);
8431 udelay(200);
8432
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008433 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008434
8435 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008436 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008437 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008438 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008439 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008440 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008441 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008442 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008443 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008444
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008445 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008446 POSTING_READ(PCH_DREF_CONTROL);
8447 udelay(200);
8448 } else {
8449 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8450
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008451 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008452
8453 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008454 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008455
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008456 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008457 POSTING_READ(PCH_DREF_CONTROL);
8458 udelay(200);
8459
8460 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008461 val &= ~DREF_SSC_SOURCE_MASK;
8462 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008463
8464 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008465 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008466
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008467 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008468 POSTING_READ(PCH_DREF_CONTROL);
8469 udelay(200);
8470 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008471
8472 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008473}
8474
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008475static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008476{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008477 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008478
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008479 tmp = I915_READ(SOUTH_CHICKEN2);
8480 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8481 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008482
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008483 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8484 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8485 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008486
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008487 tmp = I915_READ(SOUTH_CHICKEN2);
8488 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8489 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008490
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008491 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8492 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8493 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008494}
8495
8496/* WaMPhyProgramming:hsw */
8497static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8498{
8499 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008500
8501 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8502 tmp &= ~(0xFF << 24);
8503 tmp |= (0x12 << 24);
8504 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8505
Paulo Zanonidde86e22012-12-01 12:04:25 -02008506 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8507 tmp |= (1 << 11);
8508 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8509
8510 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8511 tmp |= (1 << 11);
8512 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8513
Paulo Zanonidde86e22012-12-01 12:04:25 -02008514 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8515 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8516 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8517
8518 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8519 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8520 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8521
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008522 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8523 tmp &= ~(7 << 13);
8524 tmp |= (5 << 13);
8525 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008526
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008527 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8528 tmp &= ~(7 << 13);
8529 tmp |= (5 << 13);
8530 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008531
8532 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8533 tmp &= ~0xFF;
8534 tmp |= 0x1C;
8535 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8536
8537 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8538 tmp &= ~0xFF;
8539 tmp |= 0x1C;
8540 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8541
8542 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8543 tmp &= ~(0xFF << 16);
8544 tmp |= (0x1C << 16);
8545 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8546
8547 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8548 tmp &= ~(0xFF << 16);
8549 tmp |= (0x1C << 16);
8550 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8551
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008552 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8553 tmp |= (1 << 27);
8554 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008555
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008556 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8557 tmp |= (1 << 27);
8558 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008559
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008560 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8561 tmp &= ~(0xF << 28);
8562 tmp |= (4 << 28);
8563 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008564
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008565 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8566 tmp &= ~(0xF << 28);
8567 tmp |= (4 << 28);
8568 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008569}
8570
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008571/* Implements 3 different sequences from BSpec chapter "Display iCLK
8572 * Programming" based on the parameters passed:
8573 * - Sequence to enable CLKOUT_DP
8574 * - Sequence to enable CLKOUT_DP without spread
8575 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8576 */
8577static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8578 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008579{
8580 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008581 uint32_t reg, tmp;
8582
8583 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8584 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008585 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008586 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008587
Ville Syrjäläa5805162015-05-26 20:42:30 +03008588 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008589
8590 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8591 tmp &= ~SBI_SSCCTL_DISABLE;
8592 tmp |= SBI_SSCCTL_PATHALT;
8593 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8594
8595 udelay(24);
8596
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008597 if (with_spread) {
8598 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8599 tmp &= ~SBI_SSCCTL_PATHALT;
8600 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008601
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008602 if (with_fdi) {
8603 lpt_reset_fdi_mphy(dev_priv);
8604 lpt_program_fdi_mphy(dev_priv);
8605 }
8606 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008607
Ville Syrjäläc2699522015-08-27 23:55:59 +03008608 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008609 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8610 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8611 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008612
Ville Syrjäläa5805162015-05-26 20:42:30 +03008613 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008614}
8615
Paulo Zanoni47701c32013-07-23 11:19:25 -03008616/* Sequence to disable CLKOUT_DP */
8617static void lpt_disable_clkout_dp(struct drm_device *dev)
8618{
8619 struct drm_i915_private *dev_priv = dev->dev_private;
8620 uint32_t reg, tmp;
8621
Ville Syrjäläa5805162015-05-26 20:42:30 +03008622 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008623
Ville Syrjäläc2699522015-08-27 23:55:59 +03008624 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008625 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8626 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8627 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8628
8629 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8630 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8631 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8632 tmp |= SBI_SSCCTL_PATHALT;
8633 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8634 udelay(32);
8635 }
8636 tmp |= SBI_SSCCTL_DISABLE;
8637 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8638 }
8639
Ville Syrjäläa5805162015-05-26 20:42:30 +03008640 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008641}
8642
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008643#define BEND_IDX(steps) ((50 + (steps)) / 5)
8644
8645static const uint16_t sscdivintphase[] = {
8646 [BEND_IDX( 50)] = 0x3B23,
8647 [BEND_IDX( 45)] = 0x3B23,
8648 [BEND_IDX( 40)] = 0x3C23,
8649 [BEND_IDX( 35)] = 0x3C23,
8650 [BEND_IDX( 30)] = 0x3D23,
8651 [BEND_IDX( 25)] = 0x3D23,
8652 [BEND_IDX( 20)] = 0x3E23,
8653 [BEND_IDX( 15)] = 0x3E23,
8654 [BEND_IDX( 10)] = 0x3F23,
8655 [BEND_IDX( 5)] = 0x3F23,
8656 [BEND_IDX( 0)] = 0x0025,
8657 [BEND_IDX( -5)] = 0x0025,
8658 [BEND_IDX(-10)] = 0x0125,
8659 [BEND_IDX(-15)] = 0x0125,
8660 [BEND_IDX(-20)] = 0x0225,
8661 [BEND_IDX(-25)] = 0x0225,
8662 [BEND_IDX(-30)] = 0x0325,
8663 [BEND_IDX(-35)] = 0x0325,
8664 [BEND_IDX(-40)] = 0x0425,
8665 [BEND_IDX(-45)] = 0x0425,
8666 [BEND_IDX(-50)] = 0x0525,
8667};
8668
8669/*
8670 * Bend CLKOUT_DP
8671 * steps -50 to 50 inclusive, in steps of 5
8672 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8673 * change in clock period = -(steps / 10) * 5.787 ps
8674 */
8675static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8676{
8677 uint32_t tmp;
8678 int idx = BEND_IDX(steps);
8679
8680 if (WARN_ON(steps % 5 != 0))
8681 return;
8682
8683 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8684 return;
8685
8686 mutex_lock(&dev_priv->sb_lock);
8687
8688 if (steps % 10 != 0)
8689 tmp = 0xAAAAAAAB;
8690 else
8691 tmp = 0x00000000;
8692 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8693
8694 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8695 tmp &= 0xffff0000;
8696 tmp |= sscdivintphase[idx];
8697 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8698
8699 mutex_unlock(&dev_priv->sb_lock);
8700}
8701
8702#undef BEND_IDX
8703
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008704static void lpt_init_pch_refclk(struct drm_device *dev)
8705{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008706 struct intel_encoder *encoder;
8707 bool has_vga = false;
8708
Damien Lespiaub2784e12014-08-05 11:29:37 +01008709 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008710 switch (encoder->type) {
8711 case INTEL_OUTPUT_ANALOG:
8712 has_vga = true;
8713 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008714 default:
8715 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008716 }
8717 }
8718
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008719 if (has_vga) {
8720 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008721 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008722 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008723 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008724 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008725}
8726
Paulo Zanonidde86e22012-12-01 12:04:25 -02008727/*
8728 * Initialize reference clocks when the driver loads
8729 */
8730void intel_init_pch_refclk(struct drm_device *dev)
8731{
8732 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8733 ironlake_init_pch_refclk(dev);
8734 else if (HAS_PCH_LPT(dev))
8735 lpt_init_pch_refclk(dev);
8736}
8737
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008738static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008739{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008740 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008741 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008742 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008743 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008744 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008745 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008746 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008747 bool is_lvds = false;
8748
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008749 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008750 if (connector_state->crtc != crtc_state->base.crtc)
8751 continue;
8752
8753 encoder = to_intel_encoder(connector_state->best_encoder);
8754
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008755 switch (encoder->type) {
8756 case INTEL_OUTPUT_LVDS:
8757 is_lvds = true;
8758 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008759 default:
8760 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008761 }
8762 num_connectors++;
8763 }
8764
8765 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008766 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008767 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008768 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008769 }
8770
8771 return 120000;
8772}
8773
Daniel Vetter6ff93602013-04-19 11:24:36 +02008774static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008775{
8776 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8778 int pipe = intel_crtc->pipe;
8779 uint32_t val;
8780
Daniel Vetter78114072013-06-13 00:54:57 +02008781 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008782
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008783 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008784 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008785 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008786 break;
8787 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008788 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008789 break;
8790 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008791 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008792 break;
8793 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008794 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008795 break;
8796 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008797 /* Case prevented by intel_choose_pipe_bpp_dither. */
8798 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008799 }
8800
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008801 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008802 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8803
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008804 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008805 val |= PIPECONF_INTERLACED_ILK;
8806 else
8807 val |= PIPECONF_PROGRESSIVE;
8808
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008809 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008810 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008811
Paulo Zanonic8203562012-09-12 10:06:29 -03008812 I915_WRITE(PIPECONF(pipe), val);
8813 POSTING_READ(PIPECONF(pipe));
8814}
8815
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008816/*
8817 * Set up the pipe CSC unit.
8818 *
8819 * Currently only full range RGB to limited range RGB conversion
8820 * is supported, but eventually this should handle various
8821 * RGB<->YCbCr scenarios as well.
8822 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008823static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008824{
8825 struct drm_device *dev = crtc->dev;
8826 struct drm_i915_private *dev_priv = dev->dev_private;
8827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8828 int pipe = intel_crtc->pipe;
8829 uint16_t coeff = 0x7800; /* 1.0 */
8830
8831 /*
8832 * TODO: Check what kind of values actually come out of the pipe
8833 * with these coeff/postoff values and adjust to get the best
8834 * accuracy. Perhaps we even need to take the bpc value into
8835 * consideration.
8836 */
8837
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008838 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008839 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8840
8841 /*
8842 * GY/GU and RY/RU should be the other way around according
8843 * to BSpec, but reality doesn't agree. Just set them up in
8844 * a way that results in the correct picture.
8845 */
8846 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8847 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8848
8849 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8850 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8851
8852 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8853 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8854
8855 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8856 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8857 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8858
8859 if (INTEL_INFO(dev)->gen > 6) {
8860 uint16_t postoff = 0;
8861
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008862 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008863 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008864
8865 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8866 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8867 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8868
8869 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8870 } else {
8871 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8872
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008873 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008874 mode |= CSC_BLACK_SCREEN_OFFSET;
8875
8876 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8877 }
8878}
8879
Daniel Vetter6ff93602013-04-19 11:24:36 +02008880static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008881{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008882 struct drm_device *dev = crtc->dev;
8883 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008884 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008885 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008886 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008887 uint32_t val;
8888
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008889 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008890
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008891 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008892 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8893
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008894 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008895 val |= PIPECONF_INTERLACED_ILK;
8896 else
8897 val |= PIPECONF_PROGRESSIVE;
8898
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008899 I915_WRITE(PIPECONF(cpu_transcoder), val);
8900 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008901
8902 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8903 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008904
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308905 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008906 val = 0;
8907
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008908 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008909 case 18:
8910 val |= PIPEMISC_DITHER_6_BPC;
8911 break;
8912 case 24:
8913 val |= PIPEMISC_DITHER_8_BPC;
8914 break;
8915 case 30:
8916 val |= PIPEMISC_DITHER_10_BPC;
8917 break;
8918 case 36:
8919 val |= PIPEMISC_DITHER_12_BPC;
8920 break;
8921 default:
8922 /* Case prevented by pipe_config_set_bpp. */
8923 BUG();
8924 }
8925
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008926 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008927 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8928
8929 I915_WRITE(PIPEMISC(pipe), val);
8930 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008931}
8932
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008933static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008934 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008935 intel_clock_t *clock,
8936 bool *has_reduced_clock,
8937 intel_clock_t *reduced_clock)
8938{
8939 struct drm_device *dev = crtc->dev;
8940 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008941 int refclk;
8942 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008943 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008944
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008945 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008946
8947 /*
8948 * Returns a set of divisors for the desired target clock with the given
8949 * refclk, or FALSE. The returned values represent the clock equation:
8950 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8951 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008952 limit = intel_limit(crtc_state, refclk);
8953 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008954 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008955 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008956 if (!ret)
8957 return false;
8958
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008959 return true;
8960}
8961
Paulo Zanonid4b19312012-11-29 11:29:32 -02008962int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8963{
8964 /*
8965 * Account for spread spectrum to avoid
8966 * oversubscribing the link. Max center spread
8967 * is 2.5%; use 5% for safety's sake.
8968 */
8969 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008970 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008971}
8972
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008973static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008974{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008975 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008976}
8977
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008978static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008979 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008980 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008981 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008982{
8983 struct drm_crtc *crtc = &intel_crtc->base;
8984 struct drm_device *dev = crtc->dev;
8985 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008986 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008987 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008988 struct drm_connector_state *connector_state;
8989 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008990 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008991 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008992 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008993
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008994 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008995 if (connector_state->crtc != crtc_state->base.crtc)
8996 continue;
8997
8998 encoder = to_intel_encoder(connector_state->best_encoder);
8999
9000 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009001 case INTEL_OUTPUT_LVDS:
9002 is_lvds = true;
9003 break;
9004 case INTEL_OUTPUT_SDVO:
9005 case INTEL_OUTPUT_HDMI:
9006 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009007 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02009008 default:
9009 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009010 }
9011
9012 num_connectors++;
9013 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009014
Chris Wilsonc1858122010-12-03 21:35:48 +00009015 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07009016 factor = 21;
9017 if (is_lvds) {
9018 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009019 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02009020 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07009021 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009022 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07009023 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00009024
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009025 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02009026 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00009027
Daniel Vetter9a7c7892013-04-04 22:20:34 +02009028 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
9029 *fp2 |= FP_CB_TUNE;
9030
Chris Wilson5eddb702010-09-11 13:48:45 +01009031 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08009032
Eric Anholta07d6782011-03-30 13:01:08 -07009033 if (is_lvds)
9034 dpll |= DPLLB_MODE_LVDS;
9035 else
9036 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009037
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009038 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02009039 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009040
9041 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02009042 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009043 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02009044 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08009045
Eric Anholta07d6782011-03-30 13:01:08 -07009046 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009047 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009048 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009049 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009050
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009051 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07009052 case 5:
9053 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9054 break;
9055 case 7:
9056 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9057 break;
9058 case 10:
9059 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9060 break;
9061 case 14:
9062 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9063 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009064 }
9065
Daniel Vetterb4c09f32013-04-30 14:01:42 +02009066 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05009067 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08009068 else
9069 dpll |= PLL_REF_INPUT_DREFCLK;
9070
Daniel Vetter959e16d2013-06-05 13:34:21 +02009071 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009072}
9073
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009074static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9075 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08009076{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009077 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009078 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009079 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03009080 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01009081 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009082 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009083
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03009084 memset(&crtc_state->dpll_hw_state, 0,
9085 sizeof(crtc_state->dpll_hw_state));
9086
Ville Syrjälä7905df22015-11-25 16:35:30 +02009087 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08009088
Paulo Zanoni5dc52982012-10-05 12:05:56 -03009089 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
9090 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
9091
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009092 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03009093 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009094 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009095 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9096 return -EINVAL;
9097 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01009098 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009099 if (!crtc_state->clock_set) {
9100 crtc_state->dpll.n = clock.n;
9101 crtc_state->dpll.m1 = clock.m1;
9102 crtc_state->dpll.m2 = clock.m2;
9103 crtc_state->dpll.p1 = clock.p1;
9104 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009105 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009106
Paulo Zanoni5dc52982012-10-05 12:05:56 -03009107 /* 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 +02009108 if (crtc_state->has_pch_encoder) {
9109 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009110 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009111 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009112
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009113 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009114 &fp, &reduced_clock,
9115 has_reduced_clock ? &fp2 : NULL);
9116
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009117 crtc_state->dpll_hw_state.dpll = dpll;
9118 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009119 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009120 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009121 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009122 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009123
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009124 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009125 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03009126 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009127 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07009128 return -EINVAL;
9129 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009130 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009131
Rodrigo Viviab585de2015-03-24 12:40:09 -07009132 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009133 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02009134 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009135 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009136
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009137 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009138}
9139
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009140static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9141 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009142{
9143 struct drm_device *dev = crtc->base.dev;
9144 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009145 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009146
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009147 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9148 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9149 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9150 & ~TU_SIZE_MASK;
9151 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9152 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9153 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9154}
9155
9156static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9157 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009158 struct intel_link_m_n *m_n,
9159 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009160{
9161 struct drm_device *dev = crtc->base.dev;
9162 struct drm_i915_private *dev_priv = dev->dev_private;
9163 enum pipe pipe = crtc->pipe;
9164
9165 if (INTEL_INFO(dev)->gen >= 5) {
9166 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9167 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9168 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9169 & ~TU_SIZE_MASK;
9170 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9171 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9172 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009173 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9174 * gen < 8) and if DRRS is supported (to make sure the
9175 * registers are not unnecessarily read).
9176 */
9177 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009178 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009179 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9180 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9181 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9182 & ~TU_SIZE_MASK;
9183 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9184 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9185 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9186 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009187 } else {
9188 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9189 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9190 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9191 & ~TU_SIZE_MASK;
9192 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9193 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9194 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9195 }
9196}
9197
9198void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009199 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009200{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009201 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009202 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9203 else
9204 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009205 &pipe_config->dp_m_n,
9206 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009207}
9208
Daniel Vetter72419202013-04-04 13:28:53 +02009209static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009210 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009211{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009212 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009213 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009214}
9215
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009216static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009217 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009218{
9219 struct drm_device *dev = crtc->base.dev;
9220 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009221 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9222 uint32_t ps_ctrl = 0;
9223 int id = -1;
9224 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009225
Chandra Kondurua1b22782015-04-07 15:28:45 -07009226 /* find scaler attached to this pipe */
9227 for (i = 0; i < crtc->num_scalers; i++) {
9228 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9229 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9230 id = i;
9231 pipe_config->pch_pfit.enabled = true;
9232 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9233 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9234 break;
9235 }
9236 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009237
Chandra Kondurua1b22782015-04-07 15:28:45 -07009238 scaler_state->scaler_id = id;
9239 if (id >= 0) {
9240 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9241 } else {
9242 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009243 }
9244}
9245
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009246static void
9247skylake_get_initial_plane_config(struct intel_crtc *crtc,
9248 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009249{
9250 struct drm_device *dev = crtc->base.dev;
9251 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009252 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009253 int pipe = crtc->pipe;
9254 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009255 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009256 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009257 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009258
Damien Lespiaud9806c92015-01-21 14:07:19 +00009259 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009260 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009261 DRM_DEBUG_KMS("failed to alloc fb\n");
9262 return;
9263 }
9264
Damien Lespiau1b842c82015-01-21 13:50:54 +00009265 fb = &intel_fb->base;
9266
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009267 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009268 if (!(val & PLANE_CTL_ENABLE))
9269 goto error;
9270
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009271 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9272 fourcc = skl_format_to_fourcc(pixel_format,
9273 val & PLANE_CTL_ORDER_RGBX,
9274 val & PLANE_CTL_ALPHA_MASK);
9275 fb->pixel_format = fourcc;
9276 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9277
Damien Lespiau40f46282015-02-27 11:15:21 +00009278 tiling = val & PLANE_CTL_TILED_MASK;
9279 switch (tiling) {
9280 case PLANE_CTL_TILED_LINEAR:
9281 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9282 break;
9283 case PLANE_CTL_TILED_X:
9284 plane_config->tiling = I915_TILING_X;
9285 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9286 break;
9287 case PLANE_CTL_TILED_Y:
9288 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9289 break;
9290 case PLANE_CTL_TILED_YF:
9291 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9292 break;
9293 default:
9294 MISSING_CASE(tiling);
9295 goto error;
9296 }
9297
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009298 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9299 plane_config->base = base;
9300
9301 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9302
9303 val = I915_READ(PLANE_SIZE(pipe, 0));
9304 fb->height = ((val >> 16) & 0xfff) + 1;
9305 fb->width = ((val >> 0) & 0x1fff) + 1;
9306
9307 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009308 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9309 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009310 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9311
9312 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009313 fb->pixel_format,
9314 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009315
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009316 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009317
9318 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9319 pipe_name(pipe), fb->width, fb->height,
9320 fb->bits_per_pixel, base, fb->pitches[0],
9321 plane_config->size);
9322
Damien Lespiau2d140302015-02-05 17:22:18 +00009323 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009324 return;
9325
9326error:
9327 kfree(fb);
9328}
9329
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009330static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009331 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009332{
9333 struct drm_device *dev = crtc->base.dev;
9334 struct drm_i915_private *dev_priv = dev->dev_private;
9335 uint32_t tmp;
9336
9337 tmp = I915_READ(PF_CTL(crtc->pipe));
9338
9339 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009340 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009341 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9342 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009343
9344 /* We currently do not free assignements of panel fitters on
9345 * ivb/hsw (since we don't use the higher upscaling modes which
9346 * differentiates them) so just WARN about this case for now. */
9347 if (IS_GEN7(dev)) {
9348 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9349 PF_PIPE_SEL_IVB(crtc->pipe));
9350 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009351 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009352}
9353
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009354static void
9355ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9356 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009357{
9358 struct drm_device *dev = crtc->base.dev;
9359 struct drm_i915_private *dev_priv = dev->dev_private;
9360 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009361 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009362 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009363 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009364 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009365 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009366
Damien Lespiau42a7b082015-02-05 19:35:13 +00009367 val = I915_READ(DSPCNTR(pipe));
9368 if (!(val & DISPLAY_PLANE_ENABLE))
9369 return;
9370
Damien Lespiaud9806c92015-01-21 14:07:19 +00009371 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009372 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009373 DRM_DEBUG_KMS("failed to alloc fb\n");
9374 return;
9375 }
9376
Damien Lespiau1b842c82015-01-21 13:50:54 +00009377 fb = &intel_fb->base;
9378
Daniel Vetter18c52472015-02-10 17:16:09 +00009379 if (INTEL_INFO(dev)->gen >= 4) {
9380 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009381 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009382 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9383 }
9384 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009385
9386 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009387 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009388 fb->pixel_format = fourcc;
9389 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009390
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009391 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009392 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009393 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009394 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009395 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009396 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009397 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009398 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009399 }
9400 plane_config->base = base;
9401
9402 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009403 fb->width = ((val >> 16) & 0xfff) + 1;
9404 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009405
9406 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009407 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009408
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009409 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009410 fb->pixel_format,
9411 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009412
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009413 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009414
Damien Lespiau2844a922015-01-20 12:51:48 +00009415 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9416 pipe_name(pipe), fb->width, fb->height,
9417 fb->bits_per_pixel, base, fb->pitches[0],
9418 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009419
Damien Lespiau2d140302015-02-05 17:22:18 +00009420 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009421}
9422
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009423static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009424 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009425{
9426 struct drm_device *dev = crtc->base.dev;
9427 struct drm_i915_private *dev_priv = dev->dev_private;
9428 uint32_t tmp;
9429
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009430 if (!intel_display_power_is_enabled(dev_priv,
9431 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009432 return false;
9433
Daniel Vettere143a212013-07-04 12:01:15 +02009434 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009435 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009436
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009437 tmp = I915_READ(PIPECONF(crtc->pipe));
9438 if (!(tmp & PIPECONF_ENABLE))
9439 return false;
9440
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009441 switch (tmp & PIPECONF_BPC_MASK) {
9442 case PIPECONF_6BPC:
9443 pipe_config->pipe_bpp = 18;
9444 break;
9445 case PIPECONF_8BPC:
9446 pipe_config->pipe_bpp = 24;
9447 break;
9448 case PIPECONF_10BPC:
9449 pipe_config->pipe_bpp = 30;
9450 break;
9451 case PIPECONF_12BPC:
9452 pipe_config->pipe_bpp = 36;
9453 break;
9454 default:
9455 break;
9456 }
9457
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009458 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9459 pipe_config->limited_color_range = true;
9460
Daniel Vetterab9412b2013-05-03 11:49:46 +02009461 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009462 struct intel_shared_dpll *pll;
9463
Daniel Vetter88adfff2013-03-28 10:42:01 +01009464 pipe_config->has_pch_encoder = true;
9465
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009466 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9467 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9468 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009469
9470 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009471
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009472 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009473 pipe_config->shared_dpll =
9474 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009475 } else {
9476 tmp = I915_READ(PCH_DPLL_SEL);
9477 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9478 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9479 else
9480 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9481 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009482
9483 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9484
9485 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9486 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009487
9488 tmp = pipe_config->dpll_hw_state.dpll;
9489 pipe_config->pixel_multiplier =
9490 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9491 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009492
9493 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009494 } else {
9495 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009496 }
9497
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009498 intel_get_pipe_timings(crtc, pipe_config);
9499
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009500 ironlake_get_pfit_config(crtc, pipe_config);
9501
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009502 return true;
9503}
9504
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009505static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9506{
9507 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009508 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009509
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009510 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009511 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009512 pipe_name(crtc->pipe));
9513
Rob Clarke2c719b2014-12-15 13:56:32 -05009514 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9515 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009516 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9517 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009518 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9519 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009520 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009521 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009522 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009523 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009524 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009525 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009526 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009527 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009528 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009529
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009530 /*
9531 * In theory we can still leave IRQs enabled, as long as only the HPD
9532 * interrupts remain enabled. We used to check for that, but since it's
9533 * gen-specific and since we only disable LCPLL after we fully disable
9534 * the interrupts, the check below should be enough.
9535 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009536 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009537}
9538
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009539static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9540{
9541 struct drm_device *dev = dev_priv->dev;
9542
9543 if (IS_HASWELL(dev))
9544 return I915_READ(D_COMP_HSW);
9545 else
9546 return I915_READ(D_COMP_BDW);
9547}
9548
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009549static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9550{
9551 struct drm_device *dev = dev_priv->dev;
9552
9553 if (IS_HASWELL(dev)) {
9554 mutex_lock(&dev_priv->rps.hw_lock);
9555 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9556 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009557 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009558 mutex_unlock(&dev_priv->rps.hw_lock);
9559 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009560 I915_WRITE(D_COMP_BDW, val);
9561 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009562 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009563}
9564
9565/*
9566 * This function implements pieces of two sequences from BSpec:
9567 * - Sequence for display software to disable LCPLL
9568 * - Sequence for display software to allow package C8+
9569 * The steps implemented here are just the steps that actually touch the LCPLL
9570 * register. Callers should take care of disabling all the display engine
9571 * functions, doing the mode unset, fixing interrupts, etc.
9572 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009573static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9574 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009575{
9576 uint32_t val;
9577
9578 assert_can_disable_lcpll(dev_priv);
9579
9580 val = I915_READ(LCPLL_CTL);
9581
9582 if (switch_to_fclk) {
9583 val |= LCPLL_CD_SOURCE_FCLK;
9584 I915_WRITE(LCPLL_CTL, val);
9585
9586 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9587 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9588 DRM_ERROR("Switching to FCLK failed\n");
9589
9590 val = I915_READ(LCPLL_CTL);
9591 }
9592
9593 val |= LCPLL_PLL_DISABLE;
9594 I915_WRITE(LCPLL_CTL, val);
9595 POSTING_READ(LCPLL_CTL);
9596
9597 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9598 DRM_ERROR("LCPLL still locked\n");
9599
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009600 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009601 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009602 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009603 ndelay(100);
9604
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009605 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9606 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009607 DRM_ERROR("D_COMP RCOMP still in progress\n");
9608
9609 if (allow_power_down) {
9610 val = I915_READ(LCPLL_CTL);
9611 val |= LCPLL_POWER_DOWN_ALLOW;
9612 I915_WRITE(LCPLL_CTL, val);
9613 POSTING_READ(LCPLL_CTL);
9614 }
9615}
9616
9617/*
9618 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9619 * source.
9620 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009621static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009622{
9623 uint32_t val;
9624
9625 val = I915_READ(LCPLL_CTL);
9626
9627 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9628 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9629 return;
9630
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009631 /*
9632 * Make sure we're not on PC8 state before disabling PC8, otherwise
9633 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009634 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009635 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009636
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009637 if (val & LCPLL_POWER_DOWN_ALLOW) {
9638 val &= ~LCPLL_POWER_DOWN_ALLOW;
9639 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009640 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009641 }
9642
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009643 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009644 val |= D_COMP_COMP_FORCE;
9645 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009646 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009647
9648 val = I915_READ(LCPLL_CTL);
9649 val &= ~LCPLL_PLL_DISABLE;
9650 I915_WRITE(LCPLL_CTL, val);
9651
9652 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9653 DRM_ERROR("LCPLL not locked yet\n");
9654
9655 if (val & LCPLL_CD_SOURCE_FCLK) {
9656 val = I915_READ(LCPLL_CTL);
9657 val &= ~LCPLL_CD_SOURCE_FCLK;
9658 I915_WRITE(LCPLL_CTL, val);
9659
9660 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9661 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9662 DRM_ERROR("Switching back to LCPLL failed\n");
9663 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009664
Mika Kuoppala59bad942015-01-16 11:34:40 +02009665 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009666 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009667}
9668
Paulo Zanoni765dab672014-03-07 20:08:18 -03009669/*
9670 * Package states C8 and deeper are really deep PC states that can only be
9671 * reached when all the devices on the system allow it, so even if the graphics
9672 * device allows PC8+, it doesn't mean the system will actually get to these
9673 * states. Our driver only allows PC8+ when going into runtime PM.
9674 *
9675 * The requirements for PC8+ are that all the outputs are disabled, the power
9676 * well is disabled and most interrupts are disabled, and these are also
9677 * requirements for runtime PM. When these conditions are met, we manually do
9678 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9679 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9680 * hang the machine.
9681 *
9682 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9683 * the state of some registers, so when we come back from PC8+ we need to
9684 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9685 * need to take care of the registers kept by RC6. Notice that this happens even
9686 * if we don't put the device in PCI D3 state (which is what currently happens
9687 * because of the runtime PM support).
9688 *
9689 * For more, read "Display Sequences for Package C8" on the hardware
9690 * documentation.
9691 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009692void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009693{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009694 struct drm_device *dev = dev_priv->dev;
9695 uint32_t val;
9696
Paulo Zanonic67a4702013-08-19 13:18:09 -03009697 DRM_DEBUG_KMS("Enabling package C8+\n");
9698
Ville Syrjäläc2699522015-08-27 23:55:59 +03009699 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009700 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9701 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9702 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9703 }
9704
9705 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009706 hsw_disable_lcpll(dev_priv, true, true);
9707}
9708
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009709void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009710{
9711 struct drm_device *dev = dev_priv->dev;
9712 uint32_t val;
9713
Paulo Zanonic67a4702013-08-19 13:18:09 -03009714 DRM_DEBUG_KMS("Disabling package C8+\n");
9715
9716 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009717 lpt_init_pch_refclk(dev);
9718
Ville Syrjäläc2699522015-08-27 23:55:59 +03009719 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009720 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9721 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9722 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9723 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009724}
9725
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009726static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309727{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009728 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009729 struct intel_atomic_state *old_intel_state =
9730 to_intel_atomic_state(old_state);
9731 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309732
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009733 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309734}
9735
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009736/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009737static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009738{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009739 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9740 struct drm_i915_private *dev_priv = state->dev->dev_private;
9741 struct drm_crtc *crtc;
9742 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009743 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009744 unsigned max_pixel_rate = 0, i;
9745 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009746
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009747 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9748 sizeof(intel_state->min_pixclk));
9749
9750 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009751 int pixel_rate;
9752
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009753 crtc_state = to_intel_crtc_state(cstate);
9754 if (!crtc_state->base.enable) {
9755 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009756 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009757 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009758
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009759 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009760
9761 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009762 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009763 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9764
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009765 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009766 }
9767
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009768 if (!intel_state->active_crtcs)
9769 return 0;
9770
9771 for_each_pipe(dev_priv, pipe)
9772 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9773
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009774 return max_pixel_rate;
9775}
9776
9777static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9778{
9779 struct drm_i915_private *dev_priv = dev->dev_private;
9780 uint32_t val, data;
9781 int ret;
9782
9783 if (WARN((I915_READ(LCPLL_CTL) &
9784 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9785 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9786 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9787 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9788 "trying to change cdclk frequency with cdclk not enabled\n"))
9789 return;
9790
9791 mutex_lock(&dev_priv->rps.hw_lock);
9792 ret = sandybridge_pcode_write(dev_priv,
9793 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9794 mutex_unlock(&dev_priv->rps.hw_lock);
9795 if (ret) {
9796 DRM_ERROR("failed to inform pcode about cdclk change\n");
9797 return;
9798 }
9799
9800 val = I915_READ(LCPLL_CTL);
9801 val |= LCPLL_CD_SOURCE_FCLK;
9802 I915_WRITE(LCPLL_CTL, val);
9803
9804 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9805 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9806 DRM_ERROR("Switching to FCLK failed\n");
9807
9808 val = I915_READ(LCPLL_CTL);
9809 val &= ~LCPLL_CLK_FREQ_MASK;
9810
9811 switch (cdclk) {
9812 case 450000:
9813 val |= LCPLL_CLK_FREQ_450;
9814 data = 0;
9815 break;
9816 case 540000:
9817 val |= LCPLL_CLK_FREQ_54O_BDW;
9818 data = 1;
9819 break;
9820 case 337500:
9821 val |= LCPLL_CLK_FREQ_337_5_BDW;
9822 data = 2;
9823 break;
9824 case 675000:
9825 val |= LCPLL_CLK_FREQ_675_BDW;
9826 data = 3;
9827 break;
9828 default:
9829 WARN(1, "invalid cdclk frequency\n");
9830 return;
9831 }
9832
9833 I915_WRITE(LCPLL_CTL, val);
9834
9835 val = I915_READ(LCPLL_CTL);
9836 val &= ~LCPLL_CD_SOURCE_FCLK;
9837 I915_WRITE(LCPLL_CTL, val);
9838
9839 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9840 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9841 DRM_ERROR("Switching back to LCPLL failed\n");
9842
9843 mutex_lock(&dev_priv->rps.hw_lock);
9844 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9845 mutex_unlock(&dev_priv->rps.hw_lock);
9846
9847 intel_update_cdclk(dev);
9848
9849 WARN(cdclk != dev_priv->cdclk_freq,
9850 "cdclk requested %d kHz but got %d kHz\n",
9851 cdclk, dev_priv->cdclk_freq);
9852}
9853
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009854static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009855{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009856 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009857 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009858 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009859 int cdclk;
9860
9861 /*
9862 * FIXME should also account for plane ratio
9863 * once 64bpp pixel formats are supported.
9864 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009865 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009866 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009867 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009868 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009869 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009870 cdclk = 450000;
9871 else
9872 cdclk = 337500;
9873
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009874 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009875 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9876 cdclk, dev_priv->max_cdclk_freq);
9877 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009878 }
9879
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009880 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9881 if (!intel_state->active_crtcs)
9882 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009883
9884 return 0;
9885}
9886
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009887static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009888{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009889 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009890 struct intel_atomic_state *old_intel_state =
9891 to_intel_atomic_state(old_state);
9892 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009893
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009894 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009895}
9896
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009897static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9898 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009899{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009900 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009901 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009902
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009903 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009904
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009905 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009906}
9907
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309908static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9909 enum port port,
9910 struct intel_crtc_state *pipe_config)
9911{
9912 switch (port) {
9913 case PORT_A:
9914 pipe_config->ddi_pll_sel = SKL_DPLL0;
9915 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9916 break;
9917 case PORT_B:
9918 pipe_config->ddi_pll_sel = SKL_DPLL1;
9919 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9920 break;
9921 case PORT_C:
9922 pipe_config->ddi_pll_sel = SKL_DPLL2;
9923 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9924 break;
9925 default:
9926 DRM_ERROR("Incorrect port type\n");
9927 }
9928}
9929
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009930static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9931 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009932 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009933{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009934 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009935
9936 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9937 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9938
9939 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009940 case SKL_DPLL0:
9941 /*
9942 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9943 * of the shared DPLL framework and thus needs to be read out
9944 * separately
9945 */
9946 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9947 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9948 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009949 case SKL_DPLL1:
9950 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9951 break;
9952 case SKL_DPLL2:
9953 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9954 break;
9955 case SKL_DPLL3:
9956 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9957 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009958 }
9959}
9960
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009961static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9962 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009963 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009964{
9965 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9966
9967 switch (pipe_config->ddi_pll_sel) {
9968 case PORT_CLK_SEL_WRPLL1:
9969 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9970 break;
9971 case PORT_CLK_SEL_WRPLL2:
9972 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9973 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009974 case PORT_CLK_SEL_SPLL:
9975 pipe_config->shared_dpll = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009976 break;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009977 }
9978}
9979
Daniel Vetter26804af2014-06-25 22:01:55 +03009980static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009981 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009982{
9983 struct drm_device *dev = crtc->base.dev;
9984 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009985 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009986 enum port port;
9987 uint32_t tmp;
9988
9989 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9990
9991 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9992
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009993 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009994 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309995 else if (IS_BROXTON(dev))
9996 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009997 else
9998 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009999
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010000 if (pipe_config->shared_dpll >= 0) {
10001 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
10002
10003 WARN_ON(!pll->get_hw_state(dev_priv, pll,
10004 &pipe_config->dpll_hw_state));
10005 }
10006
Daniel Vetter26804af2014-06-25 22:01:55 +030010007 /*
10008 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10009 * DDI E. So just check whether this pipe is wired to DDI E and whether
10010 * the PCH transcoder is on.
10011 */
Damien Lespiauca370452013-12-03 13:56:24 +000010012 if (INTEL_INFO(dev)->gen < 9 &&
10013 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010014 pipe_config->has_pch_encoder = true;
10015
10016 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10017 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10018 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10019
10020 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10021 }
10022}
10023
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010024static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010025 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010026{
10027 struct drm_device *dev = crtc->base.dev;
10028 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010029 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010030 uint32_t tmp;
10031
Daniel Vetterf458ebb2014-09-30 10:56:39 +020010032 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +020010033 POWER_DOMAIN_PIPE(crtc->pipe)))
10034 return false;
10035
Daniel Vettere143a212013-07-04 12:01:15 +020010036 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010037 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
10038
Daniel Vettereccb1402013-05-22 00:50:22 +020010039 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10040 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10041 enum pipe trans_edp_pipe;
10042 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10043 default:
10044 WARN(1, "unknown pipe linked to edp transcoder\n");
10045 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10046 case TRANS_DDI_EDP_INPUT_A_ON:
10047 trans_edp_pipe = PIPE_A;
10048 break;
10049 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10050 trans_edp_pipe = PIPE_B;
10051 break;
10052 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10053 trans_edp_pipe = PIPE_C;
10054 break;
10055 }
10056
10057 if (trans_edp_pipe == crtc->pipe)
10058 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10059 }
10060
Daniel Vetterf458ebb2014-09-30 10:56:39 +020010061 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +020010062 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -030010063 return false;
10064
Daniel Vettereccb1402013-05-22 00:50:22 +020010065 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010066 if (!(tmp & PIPECONF_ENABLE))
10067 return false;
10068
Daniel Vetter26804af2014-06-25 22:01:55 +030010069 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010070
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010071 intel_get_pipe_timings(crtc, pipe_config);
10072
Chandra Kondurua1b22782015-04-07 15:28:45 -070010073 if (INTEL_INFO(dev)->gen >= 9) {
10074 skl_init_scalers(dev, crtc, pipe_config);
10075 }
10076
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010077 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010078
10079 if (INTEL_INFO(dev)->gen >= 9) {
10080 pipe_config->scaler_state.scaler_id = -1;
10081 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10082 }
10083
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010084 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010085 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010086 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010087 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010088 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010089 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010090
Jesse Barnese59150d2014-01-07 13:30:45 -080010091 if (IS_HASWELL(dev))
10092 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10093 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010094
Clint Taylorebb69c92014-09-30 10:30:22 -070010095 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
10096 pipe_config->pixel_multiplier =
10097 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10098 } else {
10099 pipe_config->pixel_multiplier = 1;
10100 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010101
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010102 return true;
10103}
10104
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010105static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10106 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010107{
10108 struct drm_device *dev = crtc->dev;
10109 struct drm_i915_private *dev_priv = dev->dev_private;
10110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010111 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010112
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010113 if (plane_state && plane_state->visible) {
10114 unsigned int width = plane_state->base.crtc_w;
10115 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010116 unsigned int stride = roundup_pow_of_two(width) * 4;
10117
10118 switch (stride) {
10119 default:
10120 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10121 width, stride);
10122 stride = 256;
10123 /* fallthrough */
10124 case 256:
10125 case 512:
10126 case 1024:
10127 case 2048:
10128 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010129 }
10130
Ville Syrjälädc41c152014-08-13 11:57:05 +030010131 cntl |= CURSOR_ENABLE |
10132 CURSOR_GAMMA_ENABLE |
10133 CURSOR_FORMAT_ARGB |
10134 CURSOR_STRIDE(stride);
10135
10136 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010137 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010138
Ville Syrjälädc41c152014-08-13 11:57:05 +030010139 if (intel_crtc->cursor_cntl != 0 &&
10140 (intel_crtc->cursor_base != base ||
10141 intel_crtc->cursor_size != size ||
10142 intel_crtc->cursor_cntl != cntl)) {
10143 /* On these chipsets we can only modify the base/size/stride
10144 * whilst the cursor is disabled.
10145 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010146 I915_WRITE(CURCNTR(PIPE_A), 0);
10147 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010148 intel_crtc->cursor_cntl = 0;
10149 }
10150
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010151 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010152 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010153 intel_crtc->cursor_base = base;
10154 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010155
10156 if (intel_crtc->cursor_size != size) {
10157 I915_WRITE(CURSIZE, size);
10158 intel_crtc->cursor_size = size;
10159 }
10160
Chris Wilson4b0e3332014-05-30 16:35:26 +030010161 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010162 I915_WRITE(CURCNTR(PIPE_A), cntl);
10163 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010164 intel_crtc->cursor_cntl = cntl;
10165 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010166}
10167
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010168static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10169 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010170{
10171 struct drm_device *dev = crtc->dev;
10172 struct drm_i915_private *dev_priv = dev->dev_private;
10173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10174 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010175 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010176
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010177 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010178 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010179 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010180 case 64:
10181 cntl |= CURSOR_MODE_64_ARGB_AX;
10182 break;
10183 case 128:
10184 cntl |= CURSOR_MODE_128_ARGB_AX;
10185 break;
10186 case 256:
10187 cntl |= CURSOR_MODE_256_ARGB_AX;
10188 break;
10189 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010190 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010191 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010192 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010193 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010194
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010195 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010196 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010197
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010198 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10199 cntl |= CURSOR_ROTATE_180;
10200 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010201
Chris Wilson4b0e3332014-05-30 16:35:26 +030010202 if (intel_crtc->cursor_cntl != cntl) {
10203 I915_WRITE(CURCNTR(pipe), cntl);
10204 POSTING_READ(CURCNTR(pipe));
10205 intel_crtc->cursor_cntl = cntl;
10206 }
10207
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010208 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010209 I915_WRITE(CURBASE(pipe), base);
10210 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010211
10212 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010213}
10214
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010215/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010216static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010217 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010218{
10219 struct drm_device *dev = crtc->dev;
10220 struct drm_i915_private *dev_priv = dev->dev_private;
10221 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10222 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010223 u32 base = intel_crtc->cursor_addr;
10224 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010225
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010226 if (plane_state) {
10227 int x = plane_state->base.crtc_x;
10228 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010229
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010230 if (x < 0) {
10231 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10232 x = -x;
10233 }
10234 pos |= x << CURSOR_X_SHIFT;
10235
10236 if (y < 0) {
10237 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10238 y = -y;
10239 }
10240 pos |= y << CURSOR_Y_SHIFT;
10241
10242 /* ILK+ do this automagically */
10243 if (HAS_GMCH_DISPLAY(dev) &&
10244 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10245 base += (plane_state->base.crtc_h *
10246 plane_state->base.crtc_w - 1) * 4;
10247 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010248 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010249
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010250 I915_WRITE(CURPOS(pipe), pos);
10251
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010252 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010253 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010254 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010255 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010256}
10257
Ville Syrjälädc41c152014-08-13 11:57:05 +030010258static bool cursor_size_ok(struct drm_device *dev,
10259 uint32_t width, uint32_t height)
10260{
10261 if (width == 0 || height == 0)
10262 return false;
10263
10264 /*
10265 * 845g/865g are special in that they are only limited by
10266 * the width of their cursors, the height is arbitrary up to
10267 * the precision of the register. Everything else requires
10268 * square cursors, limited to a few power-of-two sizes.
10269 */
10270 if (IS_845G(dev) || IS_I865G(dev)) {
10271 if ((width & 63) != 0)
10272 return false;
10273
10274 if (width > (IS_845G(dev) ? 64 : 512))
10275 return false;
10276
10277 if (height > 1023)
10278 return false;
10279 } else {
10280 switch (width | height) {
10281 case 256:
10282 case 128:
10283 if (IS_GEN2(dev))
10284 return false;
10285 case 64:
10286 break;
10287 default:
10288 return false;
10289 }
10290 }
10291
10292 return true;
10293}
10294
Jesse Barnes79e53942008-11-07 14:24:08 -080010295static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010296 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010297{
James Simmons72034252010-08-03 01:33:19 +010010298 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010300
James Simmons72034252010-08-03 01:33:19 +010010301 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010302 intel_crtc->lut_r[i] = red[i] >> 8;
10303 intel_crtc->lut_g[i] = green[i] >> 8;
10304 intel_crtc->lut_b[i] = blue[i] >> 8;
10305 }
10306
10307 intel_crtc_load_lut(crtc);
10308}
10309
Jesse Barnes79e53942008-11-07 14:24:08 -080010310/* VESA 640x480x72Hz mode to set on the pipe */
10311static struct drm_display_mode load_detect_mode = {
10312 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10313 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10314};
10315
Daniel Vettera8bb6812014-02-10 18:00:39 +010010316struct drm_framebuffer *
10317__intel_framebuffer_create(struct drm_device *dev,
10318 struct drm_mode_fb_cmd2 *mode_cmd,
10319 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010320{
10321 struct intel_framebuffer *intel_fb;
10322 int ret;
10323
10324 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010325 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010326 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010327
10328 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010329 if (ret)
10330 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010331
10332 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010333
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010334err:
10335 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010336 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010337}
10338
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010339static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010340intel_framebuffer_create(struct drm_device *dev,
10341 struct drm_mode_fb_cmd2 *mode_cmd,
10342 struct drm_i915_gem_object *obj)
10343{
10344 struct drm_framebuffer *fb;
10345 int ret;
10346
10347 ret = i915_mutex_lock_interruptible(dev);
10348 if (ret)
10349 return ERR_PTR(ret);
10350 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10351 mutex_unlock(&dev->struct_mutex);
10352
10353 return fb;
10354}
10355
Chris Wilsond2dff872011-04-19 08:36:26 +010010356static u32
10357intel_framebuffer_pitch_for_width(int width, int bpp)
10358{
10359 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10360 return ALIGN(pitch, 64);
10361}
10362
10363static u32
10364intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10365{
10366 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010367 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010368}
10369
10370static struct drm_framebuffer *
10371intel_framebuffer_create_for_mode(struct drm_device *dev,
10372 struct drm_display_mode *mode,
10373 int depth, int bpp)
10374{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010375 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010376 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010377 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010378
10379 obj = i915_gem_alloc_object(dev,
10380 intel_framebuffer_size_for_mode(mode, bpp));
10381 if (obj == NULL)
10382 return ERR_PTR(-ENOMEM);
10383
10384 mode_cmd.width = mode->hdisplay;
10385 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010386 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10387 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010388 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010389
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010390 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10391 if (IS_ERR(fb))
10392 drm_gem_object_unreference_unlocked(&obj->base);
10393
10394 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010395}
10396
10397static struct drm_framebuffer *
10398mode_fits_in_fbdev(struct drm_device *dev,
10399 struct drm_display_mode *mode)
10400{
Daniel Vetter06957262015-08-10 13:34:08 +020010401#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010402 struct drm_i915_private *dev_priv = dev->dev_private;
10403 struct drm_i915_gem_object *obj;
10404 struct drm_framebuffer *fb;
10405
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010406 if (!dev_priv->fbdev)
10407 return NULL;
10408
10409 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010410 return NULL;
10411
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010412 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010413 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010414
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010415 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010416 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10417 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010418 return NULL;
10419
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010420 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010421 return NULL;
10422
10423 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010424#else
10425 return NULL;
10426#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010427}
10428
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010429static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10430 struct drm_crtc *crtc,
10431 struct drm_display_mode *mode,
10432 struct drm_framebuffer *fb,
10433 int x, int y)
10434{
10435 struct drm_plane_state *plane_state;
10436 int hdisplay, vdisplay;
10437 int ret;
10438
10439 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10440 if (IS_ERR(plane_state))
10441 return PTR_ERR(plane_state);
10442
10443 if (mode)
10444 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10445 else
10446 hdisplay = vdisplay = 0;
10447
10448 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10449 if (ret)
10450 return ret;
10451 drm_atomic_set_fb_for_plane(plane_state, fb);
10452 plane_state->crtc_x = 0;
10453 plane_state->crtc_y = 0;
10454 plane_state->crtc_w = hdisplay;
10455 plane_state->crtc_h = vdisplay;
10456 plane_state->src_x = x << 16;
10457 plane_state->src_y = y << 16;
10458 plane_state->src_w = hdisplay << 16;
10459 plane_state->src_h = vdisplay << 16;
10460
10461 return 0;
10462}
10463
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010464bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010465 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010466 struct intel_load_detect_pipe *old,
10467 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010468{
10469 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010470 struct intel_encoder *intel_encoder =
10471 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010472 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010473 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010474 struct drm_crtc *crtc = NULL;
10475 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010476 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010477 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010478 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010479 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010480 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010481 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010482
Chris Wilsond2dff872011-04-19 08:36:26 +010010483 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010484 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010485 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010486
Rob Clark51fd3712013-11-19 12:10:12 -050010487retry:
10488 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10489 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010490 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010491
Jesse Barnes79e53942008-11-07 14:24:08 -080010492 /*
10493 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010494 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010495 * - if the connector already has an assigned crtc, use it (but make
10496 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010497 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010498 * - try to find the first unused crtc that can drive this connector,
10499 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010500 */
10501
10502 /* See if we already have a CRTC for this connector */
10503 if (encoder->crtc) {
10504 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010505
Rob Clark51fd3712013-11-19 12:10:12 -050010506 ret = drm_modeset_lock(&crtc->mutex, ctx);
10507 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010508 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010509 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10510 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010511 goto fail;
Daniel Vetter7b240562012-12-12 00:35:33 +010010512
Daniel Vetter24218aa2012-08-12 19:27:11 +020010513 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010514 old->load_detect_temp = false;
10515
10516 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010517 if (connector->dpms != DRM_MODE_DPMS_ON)
10518 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010519
Chris Wilson71731882011-04-19 23:10:58 +010010520 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010521 }
10522
10523 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010524 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010525 i++;
10526 if (!(encoder->possible_crtcs & (1 << i)))
10527 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010528 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010529 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +030010530
10531 crtc = possible_crtc;
10532 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010533 }
10534
10535 /*
10536 * If we didn't find an unused CRTC, don't use any.
10537 */
10538 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010539 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010540 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010541 }
10542
Rob Clark51fd3712013-11-19 12:10:12 -050010543 ret = drm_modeset_lock(&crtc->mutex, ctx);
10544 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010545 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010546 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10547 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010548 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010549
10550 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +020010551 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010552 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010553 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010554
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010555 state = drm_atomic_state_alloc(dev);
10556 if (!state)
10557 return false;
10558
10559 state->acquire_ctx = ctx;
10560
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010561 connector_state = drm_atomic_get_connector_state(state, connector);
10562 if (IS_ERR(connector_state)) {
10563 ret = PTR_ERR(connector_state);
10564 goto fail;
10565 }
10566
10567 connector_state->crtc = crtc;
10568 connector_state->best_encoder = &intel_encoder->base;
10569
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010570 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10571 if (IS_ERR(crtc_state)) {
10572 ret = PTR_ERR(crtc_state);
10573 goto fail;
10574 }
10575
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010576 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010577
Chris Wilson64927112011-04-20 07:25:26 +010010578 if (!mode)
10579 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010580
Chris Wilsond2dff872011-04-19 08:36:26 +010010581 /* We need a framebuffer large enough to accommodate all accesses
10582 * that the plane may generate whilst we perform load detection.
10583 * We can not rely on the fbcon either being present (we get called
10584 * during its initialisation to detect all boot displays, or it may
10585 * not even exist) or that it is large enough to satisfy the
10586 * requested mode.
10587 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010588 fb = mode_fits_in_fbdev(dev, mode);
10589 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010590 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010591 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10592 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010593 } else
10594 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010595 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010596 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010597 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010598 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010599
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010600 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10601 if (ret)
10602 goto fail;
10603
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010604 drm_mode_copy(&crtc_state->base.mode, mode);
10605
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020010606 if (drm_atomic_commit(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010607 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010608 if (old->release_fb)
10609 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010610 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010611 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010612 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010613
Jesse Barnes79e53942008-11-07 14:24:08 -080010614 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010615 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010616 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010617
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010618fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010619 drm_atomic_state_free(state);
10620 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010621
Rob Clark51fd3712013-11-19 12:10:12 -050010622 if (ret == -EDEADLK) {
10623 drm_modeset_backoff(ctx);
10624 goto retry;
10625 }
10626
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010627 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010628}
10629
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010630void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010631 struct intel_load_detect_pipe *old,
10632 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010633{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010634 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010635 struct intel_encoder *intel_encoder =
10636 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010637 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010638 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010639 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010640 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010641 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010642 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010643 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010644
Chris Wilsond2dff872011-04-19 08:36:26 +010010645 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010646 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010647 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010648
Chris Wilson8261b192011-04-19 23:18:09 +010010649 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010650 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010651 if (!state)
10652 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010653
10654 state->acquire_ctx = ctx;
10655
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010656 connector_state = drm_atomic_get_connector_state(state, connector);
10657 if (IS_ERR(connector_state))
10658 goto fail;
10659
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010660 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10661 if (IS_ERR(crtc_state))
10662 goto fail;
10663
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010664 connector_state->best_encoder = NULL;
10665 connector_state->crtc = NULL;
10666
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010667 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010668
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010669 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10670 0, 0);
10671 if (ret)
10672 goto fail;
10673
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020010674 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010675 if (ret)
10676 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010677
Daniel Vetter36206362012-12-10 20:42:17 +010010678 if (old->release_fb) {
10679 drm_framebuffer_unregister_private(old->release_fb);
10680 drm_framebuffer_unreference(old->release_fb);
10681 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010682
Chris Wilson0622a532011-04-21 09:32:11 +010010683 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010684 }
10685
Eric Anholtc751ce42010-03-25 11:48:48 -070010686 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010687 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10688 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010689
10690 return;
10691fail:
10692 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10693 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010694}
10695
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010696static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010697 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010698{
10699 struct drm_i915_private *dev_priv = dev->dev_private;
10700 u32 dpll = pipe_config->dpll_hw_state.dpll;
10701
10702 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010703 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010704 else if (HAS_PCH_SPLIT(dev))
10705 return 120000;
10706 else if (!IS_GEN2(dev))
10707 return 96000;
10708 else
10709 return 48000;
10710}
10711
Jesse Barnes79e53942008-11-07 14:24:08 -080010712/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010713static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010714 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010715{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010716 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010717 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010718 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010719 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010720 u32 fp;
10721 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010722 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010723 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010724
10725 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010726 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010727 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010728 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010729
10730 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010731 if (IS_PINEVIEW(dev)) {
10732 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10733 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010734 } else {
10735 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10736 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10737 }
10738
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010739 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010740 if (IS_PINEVIEW(dev))
10741 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10742 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010743 else
10744 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010745 DPLL_FPA01_P1_POST_DIV_SHIFT);
10746
10747 switch (dpll & DPLL_MODE_MASK) {
10748 case DPLLB_MODE_DAC_SERIAL:
10749 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10750 5 : 10;
10751 break;
10752 case DPLLB_MODE_LVDS:
10753 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10754 7 : 14;
10755 break;
10756 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010757 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010758 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010759 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010760 }
10761
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010762 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010763 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010764 else
Imre Deakdccbea32015-06-22 23:35:51 +030010765 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010766 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010767 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010768 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010769
10770 if (is_lvds) {
10771 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10772 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010773
10774 if (lvds & LVDS_CLKB_POWER_UP)
10775 clock.p2 = 7;
10776 else
10777 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010778 } else {
10779 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10780 clock.p1 = 2;
10781 else {
10782 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10783 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10784 }
10785 if (dpll & PLL_P2_DIVIDE_BY_4)
10786 clock.p2 = 4;
10787 else
10788 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010789 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010790
Imre Deakdccbea32015-06-22 23:35:51 +030010791 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010792 }
10793
Ville Syrjälä18442d02013-09-13 16:00:08 +030010794 /*
10795 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010796 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010797 * encoder's get_config() function.
10798 */
Imre Deakdccbea32015-06-22 23:35:51 +030010799 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010800}
10801
Ville Syrjälä6878da02013-09-13 15:59:11 +030010802int intel_dotclock_calculate(int link_freq,
10803 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010804{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010805 /*
10806 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010807 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010808 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010809 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010810 *
10811 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010812 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010813 */
10814
Ville Syrjälä6878da02013-09-13 15:59:11 +030010815 if (!m_n->link_n)
10816 return 0;
10817
10818 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10819}
10820
Ville Syrjälä18442d02013-09-13 16:00:08 +030010821static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010822 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010823{
10824 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010825
10826 /* read out port_clock from the DPLL */
10827 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010828
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010829 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010830 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010831 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010832 * agree once we know their relationship in the encoder's
10833 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010834 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010835 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010836 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10837 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010838}
10839
10840/** Returns the currently programmed mode of the given pipe. */
10841struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10842 struct drm_crtc *crtc)
10843{
Jesse Barnes548f2452011-02-17 10:40:53 -080010844 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010846 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010847 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010848 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010849 int htot = I915_READ(HTOTAL(cpu_transcoder));
10850 int hsync = I915_READ(HSYNC(cpu_transcoder));
10851 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10852 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010853 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010854
10855 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10856 if (!mode)
10857 return NULL;
10858
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010859 /*
10860 * Construct a pipe_config sufficient for getting the clock info
10861 * back out of crtc_clock_get.
10862 *
10863 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10864 * to use a real value here instead.
10865 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010866 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010867 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010868 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10869 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10870 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010871 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10872
Ville Syrjälä773ae032013-09-23 17:48:20 +030010873 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010874 mode->hdisplay = (htot & 0xffff) + 1;
10875 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10876 mode->hsync_start = (hsync & 0xffff) + 1;
10877 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10878 mode->vdisplay = (vtot & 0xffff) + 1;
10879 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10880 mode->vsync_start = (vsync & 0xffff) + 1;
10881 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10882
10883 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010884
10885 return mode;
10886}
10887
Chris Wilsonf047e392012-07-21 12:31:41 +010010888void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010889{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010890 struct drm_i915_private *dev_priv = dev->dev_private;
10891
Chris Wilsonf62a0072014-02-21 17:55:39 +000010892 if (dev_priv->mm.busy)
10893 return;
10894
Paulo Zanoni43694d62014-03-07 20:08:08 -030010895 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010896 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010897 if (INTEL_INFO(dev)->gen >= 6)
10898 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010899 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010900}
10901
10902void intel_mark_idle(struct drm_device *dev)
10903{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010904 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010905
Chris Wilsonf62a0072014-02-21 17:55:39 +000010906 if (!dev_priv->mm.busy)
10907 return;
10908
10909 dev_priv->mm.busy = false;
10910
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010911 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010912 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010913
Paulo Zanoni43694d62014-03-07 20:08:08 -030010914 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010915}
10916
Jesse Barnes79e53942008-11-07 14:24:08 -080010917static void intel_crtc_destroy(struct drm_crtc *crtc)
10918{
10919 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010920 struct drm_device *dev = crtc->dev;
10921 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010922
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010923 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010924 work = intel_crtc->unpin_work;
10925 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010926 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010927
10928 if (work) {
10929 cancel_work_sync(&work->work);
10930 kfree(work);
10931 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010932
10933 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010934
Jesse Barnes79e53942008-11-07 14:24:08 -080010935 kfree(intel_crtc);
10936}
10937
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010938static void intel_unpin_work_fn(struct work_struct *__work)
10939{
10940 struct intel_unpin_work *work =
10941 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010942 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10943 struct drm_device *dev = crtc->base.dev;
10944 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010945
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010946 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010947 intel_unpin_fb_obj(work->old_fb, primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010948 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010949
John Harrisonf06cc1b2014-11-24 18:49:37 +000010950 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010951 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010952 mutex_unlock(&dev->struct_mutex);
10953
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010954 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010955 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010956
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010957 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10958 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010959
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010960 kfree(work);
10961}
10962
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010963static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010964 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010965{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010966 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10967 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010968 unsigned long flags;
10969
10970 /* Ignore early vblank irqs */
10971 if (intel_crtc == NULL)
10972 return;
10973
Daniel Vetterf3260382014-09-15 14:55:23 +020010974 /*
10975 * This is called both by irq handlers and the reset code (to complete
10976 * lost pageflips) so needs the full irqsave spinlocks.
10977 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010978 spin_lock_irqsave(&dev->event_lock, flags);
10979 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010980
10981 /* Ensure we don't miss a work->pending update ... */
10982 smp_rmb();
10983
10984 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010985 spin_unlock_irqrestore(&dev->event_lock, flags);
10986 return;
10987 }
10988
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010989 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010990
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010991 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010992}
10993
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010994void intel_finish_page_flip(struct drm_device *dev, int pipe)
10995{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010996 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010997 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10998
Mario Kleiner49b14a52010-12-09 07:00:07 +010010999 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011000}
11001
11002void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
11003{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011004 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011005 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
11006
Mario Kleiner49b14a52010-12-09 07:00:07 +010011007 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011008}
11009
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011010/* Is 'a' after or equal to 'b'? */
11011static bool g4x_flip_count_after_eq(u32 a, u32 b)
11012{
11013 return !((a - b) & 0x80000000);
11014}
11015
11016static bool page_flip_finished(struct intel_crtc *crtc)
11017{
11018 struct drm_device *dev = crtc->base.dev;
11019 struct drm_i915_private *dev_priv = dev->dev_private;
11020
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030011021 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
11022 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
11023 return true;
11024
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011025 /*
11026 * The relevant registers doen't exist on pre-ctg.
11027 * As the flip done interrupt doesn't trigger for mmio
11028 * flips on gmch platforms, a flip count check isn't
11029 * really needed there. But since ctg has the registers,
11030 * include it in the check anyway.
11031 */
11032 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11033 return true;
11034
11035 /*
11036 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11037 * used the same base address. In that case the mmio flip might
11038 * have completed, but the CS hasn't even executed the flip yet.
11039 *
11040 * A flip count check isn't enough as the CS might have updated
11041 * the base address just after start of vblank, but before we
11042 * managed to process the interrupt. This means we'd complete the
11043 * CS flip too soon.
11044 *
11045 * Combining both checks should get us a good enough result. It may
11046 * still happen that the CS flip has been executed, but has not
11047 * yet actually completed. But in case the base address is the same
11048 * anyway, we don't really care.
11049 */
11050 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11051 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030011052 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011053 crtc->unpin_work->flip_count);
11054}
11055
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011056void intel_prepare_page_flip(struct drm_device *dev, int plane)
11057{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011058 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011059 struct intel_crtc *intel_crtc =
11060 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
11061 unsigned long flags;
11062
Daniel Vetterf3260382014-09-15 14:55:23 +020011063
11064 /*
11065 * This is called both by irq handlers and the reset code (to complete
11066 * lost pageflips) so needs the full irqsave spinlocks.
11067 *
11068 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000011069 * generate a page-flip completion irq, i.e. every modeset
11070 * is also accompanied by a spurious intel_prepare_page_flip().
11071 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011072 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011073 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000011074 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011075 spin_unlock_irqrestore(&dev->event_lock, flags);
11076}
11077
Chris Wilson60426392015-10-10 10:44:32 +010011078static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011079{
11080 /* Ensure that the work item is consistent when activating it ... */
11081 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010011082 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011083 /* and that it is marked active as soon as the irq could fire. */
11084 smp_wmb();
11085}
11086
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011087static int intel_gen2_queue_flip(struct drm_device *dev,
11088 struct drm_crtc *crtc,
11089 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011090 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011091 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011092 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011093{
John Harrison6258fbe2015-05-29 17:43:48 +010011094 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011096 u32 flip_mask;
11097 int ret;
11098
John Harrison5fb9de12015-05-29 17:44:07 +010011099 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011100 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011101 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011102
11103 /* Can't queue multiple flips, so wait for the previous
11104 * one to finish before executing the next.
11105 */
11106 if (intel_crtc->plane)
11107 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11108 else
11109 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011110 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11111 intel_ring_emit(ring, MI_NOOP);
11112 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11113 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11114 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011115 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011116 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011117
Chris Wilson60426392015-10-10 10:44:32 +010011118 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011119 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011120}
11121
11122static int intel_gen3_queue_flip(struct drm_device *dev,
11123 struct drm_crtc *crtc,
11124 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011125 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011126 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011127 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011128{
John Harrison6258fbe2015-05-29 17:43:48 +010011129 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011130 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011131 u32 flip_mask;
11132 int ret;
11133
John Harrison5fb9de12015-05-29 17:44:07 +010011134 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011135 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011136 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011137
11138 if (intel_crtc->plane)
11139 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11140 else
11141 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011142 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11143 intel_ring_emit(ring, MI_NOOP);
11144 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11145 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11146 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011147 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011148 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011149
Chris Wilson60426392015-10-10 10:44:32 +010011150 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011151 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011152}
11153
11154static int intel_gen4_queue_flip(struct drm_device *dev,
11155 struct drm_crtc *crtc,
11156 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011157 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011158 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011159 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011160{
John Harrison6258fbe2015-05-29 17:43:48 +010011161 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011162 struct drm_i915_private *dev_priv = dev->dev_private;
11163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11164 uint32_t pf, pipesrc;
11165 int ret;
11166
John Harrison5fb9de12015-05-29 17:44:07 +010011167 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011168 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011169 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011170
11171 /* i965+ uses the linear or tiled offsets from the
11172 * Display Registers (which do not change across a page-flip)
11173 * so we need only reprogram the base address.
11174 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020011175 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11176 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11177 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011178 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011179 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011180
11181 /* XXX Enabling the panel-fitter across page-flip is so far
11182 * untested on non-native modes, so ignore it for now.
11183 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11184 */
11185 pf = 0;
11186 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011187 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011188
Chris Wilson60426392015-10-10 10:44:32 +010011189 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011190 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011191}
11192
11193static int intel_gen6_queue_flip(struct drm_device *dev,
11194 struct drm_crtc *crtc,
11195 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011196 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011197 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011198 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011199{
John Harrison6258fbe2015-05-29 17:43:48 +010011200 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011201 struct drm_i915_private *dev_priv = dev->dev_private;
11202 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11203 uint32_t pf, pipesrc;
11204 int ret;
11205
John Harrison5fb9de12015-05-29 17:44:07 +010011206 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011207 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011208 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011209
Daniel Vetter6d90c952012-04-26 23:28:05 +020011210 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11211 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11212 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011213 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011214
Chris Wilson99d9acd2012-04-17 20:37:00 +010011215 /* Contrary to the suggestions in the documentation,
11216 * "Enable Panel Fitter" does not seem to be required when page
11217 * flipping with a non-native mode, and worse causes a normal
11218 * modeset to fail.
11219 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11220 */
11221 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011222 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011223 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011224
Chris Wilson60426392015-10-10 10:44:32 +010011225 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011226 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011227}
11228
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011229static int intel_gen7_queue_flip(struct drm_device *dev,
11230 struct drm_crtc *crtc,
11231 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011232 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011233 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011234 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011235{
John Harrison6258fbe2015-05-29 17:43:48 +010011236 struct intel_engine_cs *ring = req->ring;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011237 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011238 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011239 int len, ret;
11240
Robin Schroereba905b2014-05-18 02:24:50 +020011241 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011242 case PLANE_A:
11243 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11244 break;
11245 case PLANE_B:
11246 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11247 break;
11248 case PLANE_C:
11249 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11250 break;
11251 default:
11252 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011253 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011254 }
11255
Chris Wilsonffe74d72013-08-26 20:58:12 +010011256 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011257 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011258 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011259 /*
11260 * On Gen 8, SRM is now taking an extra dword to accommodate
11261 * 48bits addresses, and we need a NOOP for the batch size to
11262 * stay even.
11263 */
11264 if (IS_GEN8(dev))
11265 len += 2;
11266 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011267
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011268 /*
11269 * BSpec MI_DISPLAY_FLIP for IVB:
11270 * "The full packet must be contained within the same cache line."
11271 *
11272 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11273 * cacheline, if we ever start emitting more commands before
11274 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11275 * then do the cacheline alignment, and finally emit the
11276 * MI_DISPLAY_FLIP.
11277 */
John Harrisonbba09b12015-05-29 17:44:06 +010011278 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011279 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011280 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011281
John Harrison5fb9de12015-05-29 17:44:07 +010011282 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011283 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011284 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011285
Chris Wilsonffe74d72013-08-26 20:58:12 +010011286 /* Unmask the flip-done completion message. Note that the bspec says that
11287 * we should do this for both the BCS and RCS, and that we must not unmask
11288 * more than one flip event at any time (or ensure that one flip message
11289 * can be sent by waiting for flip-done prior to queueing new flips).
11290 * Experimentation says that BCS works despite DERRMR masking all
11291 * flip-done completion events and that unmasking all planes at once
11292 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11293 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11294 */
11295 if (ring->id == RCS) {
11296 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
Ville Syrjäläf92a9162015-11-04 23:20:07 +020011297 intel_ring_emit_reg(ring, DERRMR);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011298 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11299 DERRMR_PIPEB_PRI_FLIP_DONE |
11300 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011301 if (IS_GEN8(dev))
Arun Siluveryf1afe242015-08-04 16:22:20 +010011302 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011303 MI_SRM_LRM_GLOBAL_GTT);
11304 else
Arun Siluveryf1afe242015-08-04 16:22:20 +010011305 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011306 MI_SRM_LRM_GLOBAL_GTT);
Ville Syrjäläf92a9162015-11-04 23:20:07 +020011307 intel_ring_emit_reg(ring, DERRMR);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011308 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011309 if (IS_GEN8(dev)) {
11310 intel_ring_emit(ring, 0);
11311 intel_ring_emit(ring, MI_NOOP);
11312 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011313 }
11314
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011315 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011316 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011317 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011318 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011319
Chris Wilson60426392015-10-10 10:44:32 +010011320 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011321 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011322}
11323
Sourab Gupta84c33a62014-06-02 16:47:17 +053011324static bool use_mmio_flip(struct intel_engine_cs *ring,
11325 struct drm_i915_gem_object *obj)
11326{
11327 /*
11328 * This is not being used for older platforms, because
11329 * non-availability of flip done interrupt forces us to use
11330 * CS flips. Older platforms derive flip done using some clever
11331 * tricks involving the flip_pending status bits and vblank irqs.
11332 * So using MMIO flips there would disrupt this mechanism.
11333 */
11334
Chris Wilson8e09bf82014-07-08 10:40:30 +010011335 if (ring == NULL)
11336 return true;
11337
Sourab Gupta84c33a62014-06-02 16:47:17 +053011338 if (INTEL_INFO(ring->dev)->gen < 5)
11339 return false;
11340
11341 if (i915.use_mmio_flip < 0)
11342 return false;
11343 else if (i915.use_mmio_flip > 0)
11344 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011345 else if (i915.enable_execlists)
11346 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011347 else if (obj->base.dma_buf &&
11348 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11349 false))
11350 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011351 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011352 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011353}
11354
Chris Wilson60426392015-10-10 10:44:32 +010011355static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011356 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011357 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011358{
11359 struct drm_device *dev = intel_crtc->base.dev;
11360 struct drm_i915_private *dev_priv = dev->dev_private;
11361 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011362 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011363 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011364
11365 ctl = I915_READ(PLANE_CTL(pipe, 0));
11366 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011367 switch (fb->modifier[0]) {
11368 case DRM_FORMAT_MOD_NONE:
11369 break;
11370 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011371 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011372 break;
11373 case I915_FORMAT_MOD_Y_TILED:
11374 ctl |= PLANE_CTL_TILED_Y;
11375 break;
11376 case I915_FORMAT_MOD_Yf_TILED:
11377 ctl |= PLANE_CTL_TILED_YF;
11378 break;
11379 default:
11380 MISSING_CASE(fb->modifier[0]);
11381 }
Damien Lespiauff944562014-11-20 14:58:16 +000011382
11383 /*
11384 * The stride is either expressed as a multiple of 64 bytes chunks for
11385 * linear buffers or in number of tiles for tiled buffers.
11386 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011387 if (intel_rotation_90_or_270(rotation)) {
11388 /* stride = Surface height in tiles */
11389 tile_height = intel_tile_height(dev, fb->pixel_format,
11390 fb->modifier[0], 0);
11391 stride = DIV_ROUND_UP(fb->height, tile_height);
11392 } else {
11393 stride = fb->pitches[0] /
11394 intel_fb_stride_alignment(dev, fb->modifier[0],
11395 fb->pixel_format);
11396 }
Damien Lespiauff944562014-11-20 14:58:16 +000011397
11398 /*
11399 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11400 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11401 */
11402 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11403 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11404
Chris Wilson60426392015-10-10 10:44:32 +010011405 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011406 POSTING_READ(PLANE_SURF(pipe, 0));
11407}
11408
Chris Wilson60426392015-10-10 10:44:32 +010011409static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11410 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011411{
11412 struct drm_device *dev = intel_crtc->base.dev;
11413 struct drm_i915_private *dev_priv = dev->dev_private;
11414 struct intel_framebuffer *intel_fb =
11415 to_intel_framebuffer(intel_crtc->base.primary->fb);
11416 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011417 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011418 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011419
Sourab Gupta84c33a62014-06-02 16:47:17 +053011420 dspcntr = I915_READ(reg);
11421
Damien Lespiauc5d97472014-10-25 00:11:11 +010011422 if (obj->tiling_mode != I915_TILING_NONE)
11423 dspcntr |= DISPPLANE_TILED;
11424 else
11425 dspcntr &= ~DISPPLANE_TILED;
11426
Sourab Gupta84c33a62014-06-02 16:47:17 +053011427 I915_WRITE(reg, dspcntr);
11428
Chris Wilson60426392015-10-10 10:44:32 +010011429 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011430 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011431}
11432
11433/*
11434 * XXX: This is the temporary way to update the plane registers until we get
11435 * around to using the usual plane update functions for MMIO flips
11436 */
Chris Wilson60426392015-10-10 10:44:32 +010011437static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011438{
Chris Wilson60426392015-10-10 10:44:32 +010011439 struct intel_crtc *crtc = mmio_flip->crtc;
11440 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011441
Chris Wilson60426392015-10-10 10:44:32 +010011442 spin_lock_irq(&crtc->base.dev->event_lock);
11443 work = crtc->unpin_work;
11444 spin_unlock_irq(&crtc->base.dev->event_lock);
11445 if (work == NULL)
11446 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011447
Chris Wilson60426392015-10-10 10:44:32 +010011448 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011449
Chris Wilson60426392015-10-10 10:44:32 +010011450 intel_pipe_update_start(crtc);
11451
11452 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011453 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011454 else
11455 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011456 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011457
Chris Wilson60426392015-10-10 10:44:32 +010011458 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011459}
11460
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011461static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011462{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011463 struct intel_mmio_flip *mmio_flip =
11464 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011465 struct intel_framebuffer *intel_fb =
11466 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11467 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011468
Chris Wilson60426392015-10-10 10:44:32 +010011469 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011470 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011471 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011472 false, NULL,
11473 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011474 i915_gem_request_unreference__unlocked(mmio_flip->req);
11475 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011476
Alex Goinsfd8e0582015-11-25 18:43:38 -080011477 /* For framebuffer backed by dmabuf, wait for fence */
11478 if (obj->base.dma_buf)
11479 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11480 false, false,
11481 MAX_SCHEDULE_TIMEOUT) < 0);
11482
Chris Wilson60426392015-10-10 10:44:32 +010011483 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011484 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011485}
11486
11487static int intel_queue_mmio_flip(struct drm_device *dev,
11488 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011489 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011490{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011491 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011492
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011493 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11494 if (mmio_flip == NULL)
11495 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011496
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011497 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011498 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011499 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011500 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011501
11502 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11503 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011504
Sourab Gupta84c33a62014-06-02 16:47:17 +053011505 return 0;
11506}
11507
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011508static int intel_default_queue_flip(struct drm_device *dev,
11509 struct drm_crtc *crtc,
11510 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011511 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011512 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011513 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011514{
11515 return -ENODEV;
11516}
11517
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011518static bool __intel_pageflip_stall_check(struct drm_device *dev,
11519 struct drm_crtc *crtc)
11520{
11521 struct drm_i915_private *dev_priv = dev->dev_private;
11522 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11523 struct intel_unpin_work *work = intel_crtc->unpin_work;
11524 u32 addr;
11525
11526 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11527 return true;
11528
Chris Wilson908565c2015-08-12 13:08:22 +010011529 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11530 return false;
11531
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011532 if (!work->enable_stall_check)
11533 return false;
11534
11535 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011536 if (work->flip_queued_req &&
11537 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011538 return false;
11539
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011540 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011541 }
11542
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011543 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011544 return false;
11545
11546 /* Potential stall - if we see that the flip has happened,
11547 * assume a missed interrupt. */
11548 if (INTEL_INFO(dev)->gen >= 4)
11549 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11550 else
11551 addr = I915_READ(DSPADDR(intel_crtc->plane));
11552
11553 /* There is a potential issue here with a false positive after a flip
11554 * to the same address. We could address this by checking for a
11555 * non-incrementing frame counter.
11556 */
11557 return addr == work->gtt_offset;
11558}
11559
11560void intel_check_page_flip(struct drm_device *dev, int pipe)
11561{
11562 struct drm_i915_private *dev_priv = dev->dev_private;
11563 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11564 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011565 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011566
Dave Gordon6c51d462015-03-06 15:34:26 +000011567 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011568
11569 if (crtc == NULL)
11570 return;
11571
Daniel Vetterf3260382014-09-15 14:55:23 +020011572 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011573 work = intel_crtc->unpin_work;
11574 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011575 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011576 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011577 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011578 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011579 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011580 if (work != NULL &&
11581 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11582 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011583 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011584}
11585
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011586static int intel_crtc_page_flip(struct drm_crtc *crtc,
11587 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011588 struct drm_pending_vblank_event *event,
11589 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011590{
11591 struct drm_device *dev = crtc->dev;
11592 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011593 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011594 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011596 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011597 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011598 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011599 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011600 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011601 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011602 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011603
Matt Roper2ff8fde2014-07-08 07:50:07 -070011604 /*
11605 * drm_mode_page_flip_ioctl() should already catch this, but double
11606 * check to be safe. In the future we may enable pageflipping from
11607 * a disabled primary plane.
11608 */
11609 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11610 return -EBUSY;
11611
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011612 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011613 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011614 return -EINVAL;
11615
11616 /*
11617 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11618 * Note that pitch changes could also affect these register.
11619 */
11620 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011621 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11622 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011623 return -EINVAL;
11624
Chris Wilsonf900db42014-02-20 09:26:13 +000011625 if (i915_terminally_wedged(&dev_priv->gpu_error))
11626 goto out_hang;
11627
Daniel Vetterb14c5672013-09-19 12:18:32 +020011628 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011629 if (work == NULL)
11630 return -ENOMEM;
11631
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011632 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011633 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011634 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011635 INIT_WORK(&work->work, intel_unpin_work_fn);
11636
Daniel Vetter87b6b102014-05-15 15:33:46 +020011637 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011638 if (ret)
11639 goto free_work;
11640
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011641 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011642 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011643 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011644 /* Before declaring the flip queue wedged, check if
11645 * the hardware completed the operation behind our backs.
11646 */
11647 if (__intel_pageflip_stall_check(dev, crtc)) {
11648 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11649 page_flip_completed(intel_crtc);
11650 } else {
11651 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011652 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011653
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011654 drm_crtc_vblank_put(crtc);
11655 kfree(work);
11656 return -EBUSY;
11657 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011658 }
11659 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011660 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011661
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011662 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11663 flush_workqueue(dev_priv->wq);
11664
Jesse Barnes75dfca82010-02-10 15:09:44 -080011665 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011666 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011667 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011668
Matt Roperf4510a22014-04-01 15:22:40 -070011669 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011670 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011671
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011672 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011673
Chris Wilson89ed88b2015-02-16 14:31:49 +000011674 ret = i915_mutex_lock_interruptible(dev);
11675 if (ret)
11676 goto cleanup;
11677
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011678 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011679 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011680
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011681 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030011682 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011683
Wayne Boyer666a4532015-12-09 12:29:35 -080011684 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011685 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011686 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011687 /* vlv: DISPLAY_FLIP fails to change tiling */
11688 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011689 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011690 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011691 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011692 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011693 if (ring == NULL || ring->id != RCS)
11694 ring = &dev_priv->ring[BCS];
11695 } else {
11696 ring = &dev_priv->ring[RCS];
11697 }
11698
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011699 mmio_flip = use_mmio_flip(ring, obj);
11700
11701 /* When using CS flips, we want to emit semaphores between rings.
11702 * However, when using mmio flips we will create a task to do the
11703 * synchronisation, so all we want here is to pin the framebuffer
11704 * into the display plane and skip any waits.
11705 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011706 if (!mmio_flip) {
11707 ret = i915_gem_object_sync(obj, ring, &request);
11708 if (ret)
11709 goto cleanup_pending;
11710 }
11711
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011712 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011713 crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011714 if (ret)
11715 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011716
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011717 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11718 obj, 0);
11719 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011720
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011721 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011722 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011723 if (ret)
11724 goto cleanup_unpin;
11725
John Harrisonf06cc1b2014-11-24 18:49:37 +000011726 i915_gem_request_assign(&work->flip_queued_req,
11727 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011728 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011729 if (!request) {
11730 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11731 if (ret)
11732 goto cleanup_unpin;
11733 }
11734
11735 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011736 page_flip_flags);
11737 if (ret)
11738 goto cleanup_unpin;
11739
John Harrison6258fbe2015-05-29 17:43:48 +010011740 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011741 }
11742
John Harrison91af1272015-06-18 13:14:56 +010011743 if (request)
John Harrison75289872015-05-29 17:43:49 +010011744 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011745
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011746 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011747 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011748
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011749 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011750 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011751 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011752
Paulo Zanonid029bca2015-10-15 10:44:46 -030011753 intel_fbc_deactivate(intel_crtc);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011754 intel_frontbuffer_flip_prepare(dev,
11755 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011756
Jesse Barnese5510fa2010-07-01 16:48:37 -070011757 trace_i915_flip_request(intel_crtc->plane, obj);
11758
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011759 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011760
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011761cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011762 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011763cleanup_pending:
John Harrison91af1272015-06-18 13:14:56 +010011764 if (request)
11765 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011766 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011767 mutex_unlock(&dev->struct_mutex);
11768cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011769 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011770 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011771
Chris Wilson89ed88b2015-02-16 14:31:49 +000011772 drm_gem_object_unreference_unlocked(&obj->base);
11773 drm_framebuffer_unreference(work->old_fb);
11774
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011775 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011776 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011777 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011778
Daniel Vetter87b6b102014-05-15 15:33:46 +020011779 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011780free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011781 kfree(work);
11782
Chris Wilsonf900db42014-02-20 09:26:13 +000011783 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011784 struct drm_atomic_state *state;
11785 struct drm_plane_state *plane_state;
11786
Chris Wilsonf900db42014-02-20 09:26:13 +000011787out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011788 state = drm_atomic_state_alloc(dev);
11789 if (!state)
11790 return -ENOMEM;
11791 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11792
11793retry:
11794 plane_state = drm_atomic_get_plane_state(state, primary);
11795 ret = PTR_ERR_OR_ZERO(plane_state);
11796 if (!ret) {
11797 drm_atomic_set_fb_for_plane(plane_state, fb);
11798
11799 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11800 if (!ret)
11801 ret = drm_atomic_commit(state);
11802 }
11803
11804 if (ret == -EDEADLK) {
11805 drm_modeset_backoff(state->acquire_ctx);
11806 drm_atomic_state_clear(state);
11807 goto retry;
11808 }
11809
11810 if (ret)
11811 drm_atomic_state_free(state);
11812
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011813 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011814 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011815 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011816 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011817 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011818 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011819 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011820}
11821
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011822
11823/**
11824 * intel_wm_need_update - Check whether watermarks need updating
11825 * @plane: drm plane
11826 * @state: new plane state
11827 *
11828 * Check current plane state versus the new one to determine whether
11829 * watermarks need to be recalculated.
11830 *
11831 * Returns true or false.
11832 */
11833static bool intel_wm_need_update(struct drm_plane *plane,
11834 struct drm_plane_state *state)
11835{
Matt Roperd21fbe82015-09-24 15:53:12 -070011836 struct intel_plane_state *new = to_intel_plane_state(state);
11837 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11838
11839 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011840 if (new->visible != cur->visible)
11841 return true;
11842
11843 if (!cur->base.fb || !new->base.fb)
11844 return false;
11845
11846 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11847 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011848 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11849 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11850 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11851 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011852 return true;
11853
11854 return false;
11855}
11856
Matt Roperd21fbe82015-09-24 15:53:12 -070011857static bool needs_scaling(struct intel_plane_state *state)
11858{
11859 int src_w = drm_rect_width(&state->src) >> 16;
11860 int src_h = drm_rect_height(&state->src) >> 16;
11861 int dst_w = drm_rect_width(&state->dst);
11862 int dst_h = drm_rect_height(&state->dst);
11863
11864 return (src_w != dst_w || src_h != dst_h);
11865}
11866
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011867int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11868 struct drm_plane_state *plane_state)
11869{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011870 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011871 struct drm_crtc *crtc = crtc_state->crtc;
11872 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11873 struct drm_plane *plane = plane_state->plane;
11874 struct drm_device *dev = crtc->dev;
11875 struct drm_i915_private *dev_priv = dev->dev_private;
11876 struct intel_plane_state *old_plane_state =
11877 to_intel_plane_state(plane->state);
11878 int idx = intel_crtc->base.base.id, ret;
11879 int i = drm_plane_index(plane);
11880 bool mode_changed = needs_modeset(crtc_state);
11881 bool was_crtc_enabled = crtc->state->active;
11882 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011883 bool turn_off, turn_on, visible, was_visible;
11884 struct drm_framebuffer *fb = plane_state->fb;
11885
11886 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11887 plane->type != DRM_PLANE_TYPE_CURSOR) {
11888 ret = skl_update_scaler_plane(
11889 to_intel_crtc_state(crtc_state),
11890 to_intel_plane_state(plane_state));
11891 if (ret)
11892 return ret;
11893 }
11894
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011895 was_visible = old_plane_state->visible;
11896 visible = to_intel_plane_state(plane_state)->visible;
11897
11898 if (!was_crtc_enabled && WARN_ON(was_visible))
11899 was_visible = false;
11900
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011901 /*
11902 * Visibility is calculated as if the crtc was on, but
11903 * after scaler setup everything depends on it being off
11904 * when the crtc isn't active.
11905 */
11906 if (!is_crtc_enabled)
11907 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011908
11909 if (!was_visible && !visible)
11910 return 0;
11911
11912 turn_off = was_visible && (!visible || mode_changed);
11913 turn_on = visible && (!was_visible || mode_changed);
11914
11915 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11916 plane->base.id, fb ? fb->base.id : -1);
11917
11918 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11919 plane->base.id, was_visible, visible,
11920 turn_off, turn_on, mode_changed);
11921
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011922 if (turn_on || turn_off) {
11923 pipe_config->wm_changed = true;
11924
Ville Syrjälä852eb002015-06-24 22:00:07 +030011925 /* must disable cxsr around plane enable/disable */
11926 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11927 if (is_crtc_enabled)
11928 intel_crtc->atomic.wait_vblank = true;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011929 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011930 }
11931 } else if (intel_wm_need_update(plane, plane_state)) {
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011932 pipe_config->wm_changed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011933 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011934
Matt Roper396e33a2016-01-06 11:34:30 -080011935 /* Pre-gen9 platforms need two-step watermark updates */
11936 if (pipe_config->wm_changed && INTEL_INFO(dev)->gen < 9 &&
11937 dev_priv->display.optimize_watermarks)
11938 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11939
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011940 if (visible || was_visible)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011941 intel_crtc->atomic.fb_bits |=
11942 to_intel_plane(plane)->frontbuffer_bit;
11943
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011944 switch (plane->type) {
11945 case DRM_PLANE_TYPE_PRIMARY:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011946 intel_crtc->atomic.pre_disable_primary = turn_off;
11947 intel_crtc->atomic.post_enable_primary = turn_on;
11948
Rodrigo Vivi066cf552015-06-26 13:55:54 -070011949 if (turn_off) {
11950 /*
11951 * FIXME: Actually if we will still have any other
11952 * plane enabled on the pipe we could let IPS enabled
11953 * still, but for now lets consider that when we make
11954 * primary invisible by setting DSPCNTR to 0 on
11955 * update_primary_plane function IPS needs to be
11956 * disable.
11957 */
11958 intel_crtc->atomic.disable_ips = true;
11959
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011960 intel_crtc->atomic.disable_fbc = true;
Rodrigo Vivi066cf552015-06-26 13:55:54 -070011961 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011962
11963 /*
11964 * FBC does not work on some platforms for rotated
11965 * planes, so disable it when rotation is not 0 and
11966 * update it when rotation is set back to 0.
11967 *
11968 * FIXME: This is redundant with the fbc update done in
11969 * the primary plane enable function except that that
11970 * one is done too late. We eventually need to unify
11971 * this.
11972 */
11973
11974 if (visible &&
11975 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11976 dev_priv->fbc.crtc == intel_crtc &&
11977 plane_state->rotation != BIT(DRM_ROTATE_0))
11978 intel_crtc->atomic.disable_fbc = true;
11979
11980 /*
11981 * BDW signals flip done immediately if the plane
11982 * is disabled, even if the plane enable is already
11983 * armed to occur at the next vblank :(
11984 */
11985 if (turn_on && IS_BROADWELL(dev))
11986 intel_crtc->atomic.wait_vblank = true;
11987
11988 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11989 break;
11990 case DRM_PLANE_TYPE_CURSOR:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011991 break;
11992 case DRM_PLANE_TYPE_OVERLAY:
Matt Roperd21fbe82015-09-24 15:53:12 -070011993 /*
11994 * WaCxSRDisabledForSpriteScaling:ivb
11995 *
11996 * cstate->update_wm was already set above, so this flag will
11997 * take effect when we commit and program watermarks.
11998 */
11999 if (IS_IVYBRIDGE(dev) &&
12000 needs_scaling(to_intel_plane_state(plane_state)) &&
12001 !needs_scaling(old_plane_state)) {
12002 to_intel_crtc_state(crtc_state)->disable_lp_wm = true;
12003 } else if (turn_off && !mode_changed) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012004 intel_crtc->atomic.wait_vblank = true;
12005 intel_crtc->atomic.update_sprite_watermarks |=
12006 1 << i;
12007 }
Matt Roperd21fbe82015-09-24 15:53:12 -070012008
12009 break;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012010 }
12011 return 0;
12012}
12013
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012014static bool encoders_cloneable(const struct intel_encoder *a,
12015 const struct intel_encoder *b)
12016{
12017 /* masks could be asymmetric, so check both ways */
12018 return a == b || (a->cloneable & (1 << b->type) &&
12019 b->cloneable & (1 << a->type));
12020}
12021
12022static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12023 struct intel_crtc *crtc,
12024 struct intel_encoder *encoder)
12025{
12026 struct intel_encoder *source_encoder;
12027 struct drm_connector *connector;
12028 struct drm_connector_state *connector_state;
12029 int i;
12030
12031 for_each_connector_in_state(state, connector, connector_state, i) {
12032 if (connector_state->crtc != &crtc->base)
12033 continue;
12034
12035 source_encoder =
12036 to_intel_encoder(connector_state->best_encoder);
12037 if (!encoders_cloneable(encoder, source_encoder))
12038 return false;
12039 }
12040
12041 return true;
12042}
12043
12044static bool check_encoder_cloning(struct drm_atomic_state *state,
12045 struct intel_crtc *crtc)
12046{
12047 struct intel_encoder *encoder;
12048 struct drm_connector *connector;
12049 struct drm_connector_state *connector_state;
12050 int i;
12051
12052 for_each_connector_in_state(state, connector, connector_state, i) {
12053 if (connector_state->crtc != &crtc->base)
12054 continue;
12055
12056 encoder = to_intel_encoder(connector_state->best_encoder);
12057 if (!check_single_encoder_cloning(state, crtc, encoder))
12058 return false;
12059 }
12060
12061 return true;
12062}
12063
12064static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12065 struct drm_crtc_state *crtc_state)
12066{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012067 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012068 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012069 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012070 struct intel_crtc_state *pipe_config =
12071 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012072 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012073 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012074 bool mode_changed = needs_modeset(crtc_state);
12075
12076 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12077 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12078 return -EINVAL;
12079 }
12080
Ville Syrjälä852eb002015-06-24 22:00:07 +030012081 if (mode_changed && !crtc_state->active)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012082 pipe_config->wm_changed = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012083
Maarten Lankhorstad421372015-06-15 12:33:42 +020012084 if (mode_changed && crtc_state->enable &&
12085 dev_priv->display.crtc_compute_clock &&
12086 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
12087 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12088 pipe_config);
12089 if (ret)
12090 return ret;
12091 }
12092
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012093 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012094 if (dev_priv->display.compute_pipe_wm) {
12095 ret = dev_priv->display.compute_pipe_wm(intel_crtc, state);
Matt Roper396e33a2016-01-06 11:34:30 -080012096 if (ret) {
12097 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012098 return ret;
Matt Roper396e33a2016-01-06 11:34:30 -080012099 }
12100 }
12101
12102 if (dev_priv->display.compute_intermediate_wm &&
12103 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12104 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12105 return 0;
12106
12107 /*
12108 * Calculate 'intermediate' watermarks that satisfy both the
12109 * old state and the new state. We can program these
12110 * immediately.
12111 */
12112 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12113 intel_crtc,
12114 pipe_config);
12115 if (ret) {
12116 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12117 return ret;
12118 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070012119 }
12120
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012121 if (INTEL_INFO(dev)->gen >= 9) {
12122 if (mode_changed)
12123 ret = skl_update_scaler_crtc(pipe_config);
12124
12125 if (!ret)
12126 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12127 pipe_config);
12128 }
12129
12130 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012131}
12132
Jani Nikula65b38e02015-04-13 11:26:56 +030012133static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012134 .mode_set_base_atomic = intel_pipe_set_base_atomic,
12135 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080012136 .atomic_begin = intel_begin_crtc_commit,
12137 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012138 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012139};
12140
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012141static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12142{
12143 struct intel_connector *connector;
12144
12145 for_each_intel_connector(dev, connector) {
12146 if (connector->base.encoder) {
12147 connector->base.state->best_encoder =
12148 connector->base.encoder;
12149 connector->base.state->crtc =
12150 connector->base.encoder->crtc;
12151 } else {
12152 connector->base.state->best_encoder = NULL;
12153 connector->base.state->crtc = NULL;
12154 }
12155 }
12156}
12157
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012158static void
Robin Schroereba905b2014-05-18 02:24:50 +020012159connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012160 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012161{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012162 int bpp = pipe_config->pipe_bpp;
12163
12164 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12165 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012166 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012167
12168 /* Don't use an invalid EDID bpc value */
12169 if (connector->base.display_info.bpc &&
12170 connector->base.display_info.bpc * 3 < bpp) {
12171 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12172 bpp, connector->base.display_info.bpc*3);
12173 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12174 }
12175
12176 /* Clamp bpp to 8 on screens without EDID 1.4 */
12177 if (connector->base.display_info.bpc == 0 && bpp > 24) {
12178 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
12179 bpp);
12180 pipe_config->pipe_bpp = 24;
12181 }
12182}
12183
12184static int
12185compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012186 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012187{
12188 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012189 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012190 struct drm_connector *connector;
12191 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012192 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012193
Wayne Boyer666a4532015-12-09 12:29:35 -080012194 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012195 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012196 else if (INTEL_INFO(dev)->gen >= 5)
12197 bpp = 12*3;
12198 else
12199 bpp = 8*3;
12200
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012201
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012202 pipe_config->pipe_bpp = bpp;
12203
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012204 state = pipe_config->base.state;
12205
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012206 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012207 for_each_connector_in_state(state, connector, connector_state, i) {
12208 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012209 continue;
12210
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012211 connected_sink_compute_bpp(to_intel_connector(connector),
12212 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012213 }
12214
12215 return bpp;
12216}
12217
Daniel Vetter644db712013-09-19 14:53:58 +020012218static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12219{
12220 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12221 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012222 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012223 mode->crtc_hdisplay, mode->crtc_hsync_start,
12224 mode->crtc_hsync_end, mode->crtc_htotal,
12225 mode->crtc_vdisplay, mode->crtc_vsync_start,
12226 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12227}
12228
Daniel Vetterc0b03412013-05-28 12:05:54 +020012229static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012230 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012231 const char *context)
12232{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012233 struct drm_device *dev = crtc->base.dev;
12234 struct drm_plane *plane;
12235 struct intel_plane *intel_plane;
12236 struct intel_plane_state *state;
12237 struct drm_framebuffer *fb;
12238
12239 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12240 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012241
12242 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
12243 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12244 pipe_config->pipe_bpp, pipe_config->dither);
12245 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12246 pipe_config->has_pch_encoder,
12247 pipe_config->fdi_lanes,
12248 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12249 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12250 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012251 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 +030012252 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012253 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012254 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12255 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12256 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012257
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012258 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 -070012259 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012260 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012261 pipe_config->dp_m2_n2.gmch_m,
12262 pipe_config->dp_m2_n2.gmch_n,
12263 pipe_config->dp_m2_n2.link_m,
12264 pipe_config->dp_m2_n2.link_n,
12265 pipe_config->dp_m2_n2.tu);
12266
Daniel Vetter55072d12014-11-20 16:10:28 +010012267 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12268 pipe_config->has_audio,
12269 pipe_config->has_infoframe);
12270
Daniel Vetterc0b03412013-05-28 12:05:54 +020012271 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012272 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012273 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012274 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12275 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012276 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012277 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12278 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012279 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12280 crtc->num_scalers,
12281 pipe_config->scaler_state.scaler_users,
12282 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012283 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12284 pipe_config->gmch_pfit.control,
12285 pipe_config->gmch_pfit.pgm_ratios,
12286 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012287 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012288 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012289 pipe_config->pch_pfit.size,
12290 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012291 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012292 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012293
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012294 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012295 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012296 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012297 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012298 pipe_config->ddi_pll_sel,
12299 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012300 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012301 pipe_config->dpll_hw_state.pll0,
12302 pipe_config->dpll_hw_state.pll1,
12303 pipe_config->dpll_hw_state.pll2,
12304 pipe_config->dpll_hw_state.pll3,
12305 pipe_config->dpll_hw_state.pll6,
12306 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012307 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012308 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012309 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012310 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012311 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12312 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12313 pipe_config->ddi_pll_sel,
12314 pipe_config->dpll_hw_state.ctrl1,
12315 pipe_config->dpll_hw_state.cfgcr1,
12316 pipe_config->dpll_hw_state.cfgcr2);
12317 } else if (HAS_DDI(dev)) {
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012318 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012319 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012320 pipe_config->dpll_hw_state.wrpll,
12321 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012322 } else {
12323 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12324 "fp0: 0x%x, fp1: 0x%x\n",
12325 pipe_config->dpll_hw_state.dpll,
12326 pipe_config->dpll_hw_state.dpll_md,
12327 pipe_config->dpll_hw_state.fp0,
12328 pipe_config->dpll_hw_state.fp1);
12329 }
12330
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012331 DRM_DEBUG_KMS("planes on this crtc\n");
12332 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12333 intel_plane = to_intel_plane(plane);
12334 if (intel_plane->pipe != crtc->pipe)
12335 continue;
12336
12337 state = to_intel_plane_state(plane->state);
12338 fb = state->base.fb;
12339 if (!fb) {
12340 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12341 "disabled, scaler_id = %d\n",
12342 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12343 plane->base.id, intel_plane->pipe,
12344 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12345 drm_plane_index(plane), state->scaler_id);
12346 continue;
12347 }
12348
12349 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12350 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12351 plane->base.id, intel_plane->pipe,
12352 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12353 drm_plane_index(plane));
12354 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12355 fb->base.id, fb->width, fb->height, fb->pixel_format);
12356 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12357 state->scaler_id,
12358 state->src.x1 >> 16, state->src.y1 >> 16,
12359 drm_rect_width(&state->src) >> 16,
12360 drm_rect_height(&state->src) >> 16,
12361 state->dst.x1, state->dst.y1,
12362 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12363 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012364}
12365
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012366static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012367{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012368 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012369 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012370 unsigned int used_ports = 0;
12371
12372 /*
12373 * Walk the connector list instead of the encoder
12374 * list to detect the problem on ddi platforms
12375 * where there's just one encoder per digital port.
12376 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012377 drm_for_each_connector(connector, dev) {
12378 struct drm_connector_state *connector_state;
12379 struct intel_encoder *encoder;
12380
12381 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12382 if (!connector_state)
12383 connector_state = connector->state;
12384
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012385 if (!connector_state->best_encoder)
12386 continue;
12387
12388 encoder = to_intel_encoder(connector_state->best_encoder);
12389
12390 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012391
12392 switch (encoder->type) {
12393 unsigned int port_mask;
12394 case INTEL_OUTPUT_UNKNOWN:
12395 if (WARN_ON(!HAS_DDI(dev)))
12396 break;
12397 case INTEL_OUTPUT_DISPLAYPORT:
12398 case INTEL_OUTPUT_HDMI:
12399 case INTEL_OUTPUT_EDP:
12400 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12401
12402 /* the same port mustn't appear more than once */
12403 if (used_ports & port_mask)
12404 return false;
12405
12406 used_ports |= port_mask;
12407 default:
12408 break;
12409 }
12410 }
12411
12412 return true;
12413}
12414
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012415static void
12416clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12417{
12418 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012419 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012420 struct intel_dpll_hw_state dpll_hw_state;
12421 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012422 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012423 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012424
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012425 /* FIXME: before the switch to atomic started, a new pipe_config was
12426 * kzalloc'd. Code that depends on any field being zero should be
12427 * fixed, so that the crtc_state can be safely duplicated. For now,
12428 * only fields that are know to not cause problems are preserved. */
12429
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012430 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012431 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012432 shared_dpll = crtc_state->shared_dpll;
12433 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012434 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012435 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012436
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012437 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012438
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012439 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012440 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012441 crtc_state->shared_dpll = shared_dpll;
12442 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012443 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012444 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012445}
12446
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012447static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012448intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012449 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012450{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012451 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012452 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012453 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012454 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012455 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012456 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012457 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012458
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012459 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012460
Daniel Vettere143a212013-07-04 12:01:15 +020012461 pipe_config->cpu_transcoder =
12462 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012463
Imre Deak2960bc92013-07-30 13:36:32 +030012464 /*
12465 * Sanitize sync polarity flags based on requested ones. If neither
12466 * positive or negative polarity is requested, treat this as meaning
12467 * negative polarity.
12468 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012469 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012470 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012471 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012472
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012473 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012474 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012475 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012476
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012477 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12478 pipe_config);
12479 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012480 goto fail;
12481
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012482 /*
12483 * Determine the real pipe dimensions. Note that stereo modes can
12484 * increase the actual pipe size due to the frame doubling and
12485 * insertion of additional space for blanks between the frame. This
12486 * is stored in the crtc timings. We use the requested mode to do this
12487 * computation to clearly distinguish it from the adjusted mode, which
12488 * can be changed by the connectors in the below retry loop.
12489 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012490 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012491 &pipe_config->pipe_src_w,
12492 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012493
Daniel Vettere29c22c2013-02-21 00:00:16 +010012494encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012495 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012496 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012497 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012498
Daniel Vetter135c81b2013-07-21 21:37:09 +020012499 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012500 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12501 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012502
Daniel Vetter7758a112012-07-08 19:40:39 +020012503 /* Pass our mode to the connectors and the CRTC to give them a chance to
12504 * adjust it according to limitations or connector properties, and also
12505 * a chance to reject the mode entirely.
12506 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012507 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012508 if (connector_state->crtc != crtc)
12509 continue;
12510
12511 encoder = to_intel_encoder(connector_state->best_encoder);
12512
Daniel Vetterefea6e82013-07-21 21:36:59 +020012513 if (!(encoder->compute_config(encoder, pipe_config))) {
12514 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012515 goto fail;
12516 }
12517 }
12518
Daniel Vetterff9a6752013-06-01 17:16:21 +020012519 /* Set default port clock if not overwritten by the encoder. Needs to be
12520 * done afterwards in case the encoder adjusts the mode. */
12521 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012522 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012523 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012524
Daniel Vettera43f6e02013-06-07 23:10:32 +020012525 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012526 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012527 DRM_DEBUG_KMS("CRTC fixup failed\n");
12528 goto fail;
12529 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012530
12531 if (ret == RETRY) {
12532 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12533 ret = -EINVAL;
12534 goto fail;
12535 }
12536
12537 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12538 retry = false;
12539 goto encoder_retry;
12540 }
12541
Daniel Vettere8fa4272015-08-12 11:43:34 +020012542 /* Dithering seems to not pass-through bits correctly when it should, so
12543 * only enable it on 6bpc panels. */
12544 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012545 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012546 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012547
Daniel Vetter7758a112012-07-08 19:40:39 +020012548fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012549 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012550}
12551
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012552static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012553intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012554{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012555 struct drm_crtc *crtc;
12556 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012557 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012558
Ville Syrjälä76688512014-01-10 11:28:06 +020012559 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012560 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012561 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012562
12563 /* Update hwmode for vblank functions */
12564 if (crtc->state->active)
12565 crtc->hwmode = crtc->state->adjusted_mode;
12566 else
12567 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012568
12569 /*
12570 * Update legacy state to satisfy fbc code. This can
12571 * be removed when fbc uses the atomic state.
12572 */
12573 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12574 struct drm_plane_state *plane_state = crtc->primary->state;
12575
12576 crtc->primary->fb = plane_state->fb;
12577 crtc->x = plane_state->src_x >> 16;
12578 crtc->y = plane_state->src_y >> 16;
12579 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012580 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012581}
12582
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012583static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012584{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012585 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012586
12587 if (clock1 == clock2)
12588 return true;
12589
12590 if (!clock1 || !clock2)
12591 return false;
12592
12593 diff = abs(clock1 - clock2);
12594
12595 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12596 return true;
12597
12598 return false;
12599}
12600
Daniel Vetter25c5b262012-07-08 22:08:04 +020012601#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12602 list_for_each_entry((intel_crtc), \
12603 &(dev)->mode_config.crtc_list, \
12604 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012605 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012606
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012607static bool
12608intel_compare_m_n(unsigned int m, unsigned int n,
12609 unsigned int m2, unsigned int n2,
12610 bool exact)
12611{
12612 if (m == m2 && n == n2)
12613 return true;
12614
12615 if (exact || !m || !n || !m2 || !n2)
12616 return false;
12617
12618 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12619
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012620 if (n > n2) {
12621 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012622 m2 <<= 1;
12623 n2 <<= 1;
12624 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012625 } else if (n < n2) {
12626 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012627 m <<= 1;
12628 n <<= 1;
12629 }
12630 }
12631
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012632 if (n != n2)
12633 return false;
12634
12635 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012636}
12637
12638static bool
12639intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12640 struct intel_link_m_n *m2_n2,
12641 bool adjust)
12642{
12643 if (m_n->tu == m2_n2->tu &&
12644 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12645 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12646 intel_compare_m_n(m_n->link_m, m_n->link_n,
12647 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12648 if (adjust)
12649 *m2_n2 = *m_n;
12650
12651 return true;
12652 }
12653
12654 return false;
12655}
12656
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012657static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012658intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012659 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012660 struct intel_crtc_state *pipe_config,
12661 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012662{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012663 bool ret = true;
12664
12665#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12666 do { \
12667 if (!adjust) \
12668 DRM_ERROR(fmt, ##__VA_ARGS__); \
12669 else \
12670 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12671 } while (0)
12672
Daniel Vetter66e985c2013-06-05 13:34:20 +020012673#define PIPE_CONF_CHECK_X(name) \
12674 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012675 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012676 "(expected 0x%08x, found 0x%08x)\n", \
12677 current_config->name, \
12678 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012679 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012680 }
12681
Daniel Vetter08a24032013-04-19 11:25:34 +020012682#define PIPE_CONF_CHECK_I(name) \
12683 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012684 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012685 "(expected %i, found %i)\n", \
12686 current_config->name, \
12687 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012688 ret = false; \
12689 }
12690
12691#define PIPE_CONF_CHECK_M_N(name) \
12692 if (!intel_compare_link_m_n(&current_config->name, \
12693 &pipe_config->name,\
12694 adjust)) { \
12695 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12696 "(expected tu %i gmch %i/%i link %i/%i, " \
12697 "found tu %i, gmch %i/%i link %i/%i)\n", \
12698 current_config->name.tu, \
12699 current_config->name.gmch_m, \
12700 current_config->name.gmch_n, \
12701 current_config->name.link_m, \
12702 current_config->name.link_n, \
12703 pipe_config->name.tu, \
12704 pipe_config->name.gmch_m, \
12705 pipe_config->name.gmch_n, \
12706 pipe_config->name.link_m, \
12707 pipe_config->name.link_n); \
12708 ret = false; \
12709 }
12710
12711#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12712 if (!intel_compare_link_m_n(&current_config->name, \
12713 &pipe_config->name, adjust) && \
12714 !intel_compare_link_m_n(&current_config->alt_name, \
12715 &pipe_config->name, adjust)) { \
12716 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12717 "(expected tu %i gmch %i/%i link %i/%i, " \
12718 "or tu %i gmch %i/%i link %i/%i, " \
12719 "found tu %i, gmch %i/%i link %i/%i)\n", \
12720 current_config->name.tu, \
12721 current_config->name.gmch_m, \
12722 current_config->name.gmch_n, \
12723 current_config->name.link_m, \
12724 current_config->name.link_n, \
12725 current_config->alt_name.tu, \
12726 current_config->alt_name.gmch_m, \
12727 current_config->alt_name.gmch_n, \
12728 current_config->alt_name.link_m, \
12729 current_config->alt_name.link_n, \
12730 pipe_config->name.tu, \
12731 pipe_config->name.gmch_m, \
12732 pipe_config->name.gmch_n, \
12733 pipe_config->name.link_m, \
12734 pipe_config->name.link_n); \
12735 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012736 }
12737
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012738/* This is required for BDW+ where there is only one set of registers for
12739 * switching between high and low RR.
12740 * This macro can be used whenever a comparison has to be made between one
12741 * hw state and multiple sw state variables.
12742 */
12743#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12744 if ((current_config->name != pipe_config->name) && \
12745 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012746 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012747 "(expected %i or %i, found %i)\n", \
12748 current_config->name, \
12749 current_config->alt_name, \
12750 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012751 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012752 }
12753
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012754#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12755 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012756 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012757 "(expected %i, found %i)\n", \
12758 current_config->name & (mask), \
12759 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012760 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012761 }
12762
Ville Syrjälä5e550652013-09-06 23:29:07 +030012763#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12764 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012765 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012766 "(expected %i, found %i)\n", \
12767 current_config->name, \
12768 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012769 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012770 }
12771
Daniel Vetterbb760062013-06-06 14:55:52 +020012772#define PIPE_CONF_QUIRK(quirk) \
12773 ((current_config->quirks | pipe_config->quirks) & (quirk))
12774
Daniel Vettereccb1402013-05-22 00:50:22 +020012775 PIPE_CONF_CHECK_I(cpu_transcoder);
12776
Daniel Vetter08a24032013-04-19 11:25:34 +020012777 PIPE_CONF_CHECK_I(has_pch_encoder);
12778 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012779 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012780
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012781 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012782 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012783
12784 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012785 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012786
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012787 if (current_config->has_drrs)
12788 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12789 } else
12790 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012791
Jani Nikulaa65347b2015-11-27 12:21:46 +020012792 PIPE_CONF_CHECK_I(has_dsi_encoder);
12793
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012794 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12795 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12796 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12797 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12798 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12799 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012800
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012801 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12802 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12803 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12804 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12805 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12806 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012807
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012808 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012809 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012810 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012811 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012812 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012813 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012814
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012815 PIPE_CONF_CHECK_I(has_audio);
12816
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012817 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012818 DRM_MODE_FLAG_INTERLACE);
12819
Daniel Vetterbb760062013-06-06 14:55:52 +020012820 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012821 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012822 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012823 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012824 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012825 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012826 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012827 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012828 DRM_MODE_FLAG_NVSYNC);
12829 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012830
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012831 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012832 /* pfit ratios are autocomputed by the hw on gen4+ */
12833 if (INTEL_INFO(dev)->gen < 4)
12834 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012835 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012836
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012837 if (!adjust) {
12838 PIPE_CONF_CHECK_I(pipe_src_w);
12839 PIPE_CONF_CHECK_I(pipe_src_h);
12840
12841 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12842 if (current_config->pch_pfit.enabled) {
12843 PIPE_CONF_CHECK_X(pch_pfit.pos);
12844 PIPE_CONF_CHECK_X(pch_pfit.size);
12845 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012846
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012847 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12848 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012849
Jesse Barnese59150d2014-01-07 13:30:45 -080012850 /* BDW+ don't expose a synchronous way to read the state */
12851 if (IS_HASWELL(dev))
12852 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012853
Ville Syrjälä282740f2013-09-04 18:30:03 +030012854 PIPE_CONF_CHECK_I(double_wide);
12855
Daniel Vetter26804af2014-06-25 22:01:55 +030012856 PIPE_CONF_CHECK_X(ddi_pll_sel);
12857
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012858 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012859 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012860 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012861 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12862 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012863 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012864 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012865 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12866 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12867 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012868
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012869 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12870 PIPE_CONF_CHECK_I(pipe_bpp);
12871
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012872 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012873 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012874
Daniel Vetter66e985c2013-06-05 13:34:20 +020012875#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012876#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012877#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012878#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012879#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012880#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012881#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012882
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012883 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012884}
12885
Damien Lespiau08db6652014-11-04 17:06:52 +000012886static void check_wm_state(struct drm_device *dev)
12887{
12888 struct drm_i915_private *dev_priv = dev->dev_private;
12889 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12890 struct intel_crtc *intel_crtc;
12891 int plane;
12892
12893 if (INTEL_INFO(dev)->gen < 9)
12894 return;
12895
12896 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12897 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12898
12899 for_each_intel_crtc(dev, intel_crtc) {
12900 struct skl_ddb_entry *hw_entry, *sw_entry;
12901 const enum pipe pipe = intel_crtc->pipe;
12902
12903 if (!intel_crtc->active)
12904 continue;
12905
12906 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012907 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012908 hw_entry = &hw_ddb.plane[pipe][plane];
12909 sw_entry = &sw_ddb->plane[pipe][plane];
12910
12911 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12912 continue;
12913
12914 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12915 "(expected (%u,%u), found (%u,%u))\n",
12916 pipe_name(pipe), plane + 1,
12917 sw_entry->start, sw_entry->end,
12918 hw_entry->start, hw_entry->end);
12919 }
12920
12921 /* cursor */
Matt Roper4969d332015-09-24 15:53:10 -070012922 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12923 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
Damien Lespiau08db6652014-11-04 17:06:52 +000012924
12925 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12926 continue;
12927
12928 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12929 "(expected (%u,%u), found (%u,%u))\n",
12930 pipe_name(pipe),
12931 sw_entry->start, sw_entry->end,
12932 hw_entry->start, hw_entry->end);
12933 }
12934}
12935
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012936static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012937check_connector_state(struct drm_device *dev,
12938 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012939{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012940 struct drm_connector_state *old_conn_state;
12941 struct drm_connector *connector;
12942 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012943
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012944 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12945 struct drm_encoder *encoder = connector->encoder;
12946 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012947
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012948 /* This also checks the encoder/connector hw state with the
12949 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012950 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012951
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012952 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012953 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012954 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012955}
12956
12957static void
12958check_encoder_state(struct drm_device *dev)
12959{
12960 struct intel_encoder *encoder;
12961 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012962
Damien Lespiaub2784e12014-08-05 11:29:37 +010012963 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012964 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012965 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012966
12967 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12968 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012969 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012970
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012971 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012972 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012973 continue;
12974 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012975
12976 I915_STATE_WARN(connector->base.state->crtc !=
12977 encoder->base.crtc,
12978 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012979 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012980
Rob Clarke2c719b2014-12-15 13:56:32 -050012981 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012982 "encoder's enabled state mismatch "
12983 "(expected %i, found %i)\n",
12984 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012985
12986 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012987 bool active;
12988
12989 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012990 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012991 "encoder detached but still enabled on pipe %c.\n",
12992 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012993 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012994 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012995}
12996
12997static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012998check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012999{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013000 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013001 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013002 struct drm_crtc_state *old_crtc_state;
13003 struct drm_crtc *crtc;
13004 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013005
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013006 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
13007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13008 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020013009 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013010
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013011 if (!needs_modeset(crtc->state) &&
13012 !to_intel_crtc_state(crtc->state)->update_pipe)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013013 continue;
13014
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013015 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
13016 pipe_config = to_intel_crtc_state(old_crtc_state);
13017 memset(pipe_config, 0, sizeof(*pipe_config));
13018 pipe_config->base.crtc = crtc;
13019 pipe_config->base.state = old_state;
13020
13021 DRM_DEBUG_KMS("[CRTC:%d]\n",
13022 crtc->base.id);
13023
13024 active = dev_priv->display.get_pipe_config(intel_crtc,
13025 pipe_config);
13026
13027 /* hw state is inconsistent with the pipe quirk */
13028 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13029 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13030 active = crtc->state->active;
13031
13032 I915_STATE_WARN(crtc->state->active != active,
13033 "crtc active state doesn't match with hw state "
13034 "(expected %i, found %i)\n", crtc->state->active, active);
13035
13036 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
13037 "transitional active state does not match atomic hw state "
13038 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
13039
13040 for_each_encoder_on_crtc(dev, crtc, encoder) {
13041 enum pipe pipe;
13042
13043 active = encoder->get_hw_state(encoder, &pipe);
13044 I915_STATE_WARN(active != crtc->state->active,
13045 "[ENCODER:%i] active %i with crtc active %i\n",
13046 encoder->base.base.id, active, crtc->state->active);
13047
13048 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13049 "Encoder connected to wrong pipe %c\n",
13050 pipe_name(pipe));
13051
13052 if (active)
13053 encoder->get_config(encoder, pipe_config);
13054 }
13055
13056 if (!crtc->state->active)
13057 continue;
13058
13059 sw_config = to_intel_crtc_state(crtc->state);
13060 if (!intel_pipe_config_compare(dev, sw_config,
13061 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050013062 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013063 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020013064 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013065 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020013066 "[sw state]");
13067 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013068 }
13069}
13070
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013071static void
13072check_shared_dpll_state(struct drm_device *dev)
13073{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013074 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013075 struct intel_crtc *crtc;
13076 struct intel_dpll_hw_state dpll_hw_state;
13077 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013078
13079 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13080 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13081 int enabled_crtcs = 0, active_crtcs = 0;
13082 bool active;
13083
13084 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13085
13086 DRM_DEBUG_KMS("%s\n", pll->name);
13087
13088 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
13089
Rob Clarke2c719b2014-12-15 13:56:32 -050013090 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020013091 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013092 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050013093 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020013094 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050013095 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020013096 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050013097 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020013098 "pll on state mismatch (expected %i, found %i)\n",
13099 pll->on, active);
13100
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013101 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080013102 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020013103 enabled_crtcs++;
13104 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13105 active_crtcs++;
13106 }
Rob Clarke2c719b2014-12-15 13:56:32 -050013107 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020013108 "pll active crtcs mismatch (expected %i, found %i)\n",
13109 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050013110 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020013111 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013112 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013113
Rob Clarke2c719b2014-12-15 13:56:32 -050013114 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020013115 sizeof(dpll_hw_state)),
13116 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020013117 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013118}
13119
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013120static void
13121intel_modeset_check_state(struct drm_device *dev,
13122 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013123{
Damien Lespiau08db6652014-11-04 17:06:52 +000013124 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013125 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013126 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013127 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013128 check_shared_dpll_state(dev);
13129}
13130
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020013131void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030013132 int dotclock)
13133{
13134 /*
13135 * FDI already provided one idea for the dotclock.
13136 * Yell if the encoder disagrees.
13137 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013138 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030013139 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013140 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030013141}
13142
Ville Syrjälä80715b22014-05-15 20:23:23 +030013143static void update_scanline_offset(struct intel_crtc *crtc)
13144{
13145 struct drm_device *dev = crtc->base.dev;
13146
13147 /*
13148 * The scanline counter increments at the leading edge of hsync.
13149 *
13150 * On most platforms it starts counting from vtotal-1 on the
13151 * first active line. That means the scanline counter value is
13152 * always one less than what we would expect. Ie. just after
13153 * start of vblank, which also occurs at start of hsync (on the
13154 * last active line), the scanline counter will read vblank_start-1.
13155 *
13156 * On gen2 the scanline counter starts counting from 1 instead
13157 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13158 * to keep the value positive), instead of adding one.
13159 *
13160 * On HSW+ the behaviour of the scanline counter depends on the output
13161 * type. For DP ports it behaves like most other platforms, but on HDMI
13162 * there's an extra 1 line difference. So we need to add two instead of
13163 * one to the value.
13164 */
13165 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013166 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013167 int vtotal;
13168
Ville Syrjälä124abe02015-09-08 13:40:45 +030013169 vtotal = adjusted_mode->crtc_vtotal;
13170 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013171 vtotal /= 2;
13172
13173 crtc->scanline_offset = vtotal - 1;
13174 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013175 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013176 crtc->scanline_offset = 2;
13177 } else
13178 crtc->scanline_offset = 1;
13179}
13180
Maarten Lankhorstad421372015-06-15 12:33:42 +020013181static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013182{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013183 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013184 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013185 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013186 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013187 struct intel_crtc_state *intel_crtc_state;
13188 struct drm_crtc *crtc;
13189 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013190 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013191
13192 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013193 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013194
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013195 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020013196 int dpll;
13197
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013198 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030013199 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013200 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013201
Maarten Lankhorstad421372015-06-15 12:33:42 +020013202 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013203 continue;
13204
Maarten Lankhorstad421372015-06-15 12:33:42 +020013205 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013206
Maarten Lankhorstad421372015-06-15 12:33:42 +020013207 if (!shared_dpll)
13208 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13209
13210 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013211 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013212}
13213
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013214/*
13215 * This implements the workaround described in the "notes" section of the mode
13216 * set sequence documentation. When going from no pipes or single pipe to
13217 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13218 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13219 */
13220static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13221{
13222 struct drm_crtc_state *crtc_state;
13223 struct intel_crtc *intel_crtc;
13224 struct drm_crtc *crtc;
13225 struct intel_crtc_state *first_crtc_state = NULL;
13226 struct intel_crtc_state *other_crtc_state = NULL;
13227 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13228 int i;
13229
13230 /* look at all crtc's that are going to be enabled in during modeset */
13231 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13232 intel_crtc = to_intel_crtc(crtc);
13233
13234 if (!crtc_state->active || !needs_modeset(crtc_state))
13235 continue;
13236
13237 if (first_crtc_state) {
13238 other_crtc_state = to_intel_crtc_state(crtc_state);
13239 break;
13240 } else {
13241 first_crtc_state = to_intel_crtc_state(crtc_state);
13242 first_pipe = intel_crtc->pipe;
13243 }
13244 }
13245
13246 /* No workaround needed? */
13247 if (!first_crtc_state)
13248 return 0;
13249
13250 /* w/a possibly needed, check how many crtc's are already enabled. */
13251 for_each_intel_crtc(state->dev, intel_crtc) {
13252 struct intel_crtc_state *pipe_config;
13253
13254 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13255 if (IS_ERR(pipe_config))
13256 return PTR_ERR(pipe_config);
13257
13258 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13259
13260 if (!pipe_config->base.active ||
13261 needs_modeset(&pipe_config->base))
13262 continue;
13263
13264 /* 2 or more enabled crtcs means no need for w/a */
13265 if (enabled_pipe != INVALID_PIPE)
13266 return 0;
13267
13268 enabled_pipe = intel_crtc->pipe;
13269 }
13270
13271 if (enabled_pipe != INVALID_PIPE)
13272 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13273 else if (other_crtc_state)
13274 other_crtc_state->hsw_workaround_pipe = first_pipe;
13275
13276 return 0;
13277}
13278
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013279static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13280{
13281 struct drm_crtc *crtc;
13282 struct drm_crtc_state *crtc_state;
13283 int ret = 0;
13284
13285 /* add all active pipes to the state */
13286 for_each_crtc(state->dev, crtc) {
13287 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13288 if (IS_ERR(crtc_state))
13289 return PTR_ERR(crtc_state);
13290
13291 if (!crtc_state->active || needs_modeset(crtc_state))
13292 continue;
13293
13294 crtc_state->mode_changed = true;
13295
13296 ret = drm_atomic_add_affected_connectors(state, crtc);
13297 if (ret)
13298 break;
13299
13300 ret = drm_atomic_add_affected_planes(state, crtc);
13301 if (ret)
13302 break;
13303 }
13304
13305 return ret;
13306}
13307
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013308static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013309{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013310 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13311 struct drm_i915_private *dev_priv = state->dev->dev_private;
13312 struct drm_crtc *crtc;
13313 struct drm_crtc_state *crtc_state;
13314 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013315
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013316 if (!check_digital_port_conflicts(state)) {
13317 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13318 return -EINVAL;
13319 }
13320
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013321 intel_state->modeset = true;
13322 intel_state->active_crtcs = dev_priv->active_crtcs;
13323
13324 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13325 if (crtc_state->active)
13326 intel_state->active_crtcs |= 1 << i;
13327 else
13328 intel_state->active_crtcs &= ~(1 << i);
13329 }
13330
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013331 /*
13332 * See if the config requires any additional preparation, e.g.
13333 * to adjust global state with pipes off. We need to do this
13334 * here so we can get the modeset_pipe updated config for the new
13335 * mode set on this crtc. For other crtcs we need to use the
13336 * adjusted_mode bits in the crtc directly.
13337 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013338 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013339 ret = dev_priv->display.modeset_calc_cdclk(state);
13340
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013341 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013342 ret = intel_modeset_all_pipes(state);
13343
13344 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013345 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013346 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013347 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013348
Maarten Lankhorstad421372015-06-15 12:33:42 +020013349 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013350
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013351 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013352 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013353
Maarten Lankhorstad421372015-06-15 12:33:42 +020013354 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013355}
13356
Matt Roperaa363132015-09-24 15:53:18 -070013357/*
13358 * Handle calculation of various watermark data at the end of the atomic check
13359 * phase. The code here should be run after the per-crtc and per-plane 'check'
13360 * handlers to ensure that all derived state has been updated.
13361 */
13362static void calc_watermark_data(struct drm_atomic_state *state)
13363{
13364 struct drm_device *dev = state->dev;
13365 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13366 struct drm_crtc *crtc;
13367 struct drm_crtc_state *cstate;
13368 struct drm_plane *plane;
13369 struct drm_plane_state *pstate;
13370
13371 /*
13372 * Calculate watermark configuration details now that derived
13373 * plane/crtc state is all properly updated.
13374 */
13375 drm_for_each_crtc(crtc, dev) {
13376 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13377 crtc->state;
13378
13379 if (cstate->active)
13380 intel_state->wm_config.num_pipes_active++;
13381 }
13382 drm_for_each_legacy_plane(plane, dev) {
13383 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13384 plane->state;
13385
13386 if (!to_intel_plane_state(pstate)->visible)
13387 continue;
13388
13389 intel_state->wm_config.sprites_enabled = true;
13390 if (pstate->crtc_w != pstate->src_w >> 16 ||
13391 pstate->crtc_h != pstate->src_h >> 16)
13392 intel_state->wm_config.sprites_scaled = true;
13393 }
13394}
13395
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013396/**
13397 * intel_atomic_check - validate state object
13398 * @dev: drm device
13399 * @state: state to validate
13400 */
13401static int intel_atomic_check(struct drm_device *dev,
13402 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013403{
Matt Roperaa363132015-09-24 15:53:18 -070013404 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013405 struct drm_crtc *crtc;
13406 struct drm_crtc_state *crtc_state;
13407 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013408 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013409
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013410 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013411 if (ret)
13412 return ret;
13413
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013414 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013415 struct intel_crtc_state *pipe_config =
13416 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013417
Maarten Lankhorstba8af3e2015-11-16 12:49:14 +010013418 memset(&to_intel_crtc(crtc)->atomic, 0,
13419 sizeof(struct intel_crtc_atomic_commit));
13420
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013421 /* Catch I915_MODE_FLAG_INHERITED */
13422 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13423 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013424
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013425 if (!crtc_state->enable) {
13426 if (needs_modeset(crtc_state))
13427 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013428 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013429 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013430
Daniel Vetter26495482015-07-15 14:15:52 +020013431 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013432 continue;
13433
Daniel Vetter26495482015-07-15 14:15:52 +020013434 /* FIXME: For only active_changed we shouldn't need to do any
13435 * state recomputation at all. */
13436
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013437 ret = drm_atomic_add_affected_connectors(state, crtc);
13438 if (ret)
13439 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013440
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013441 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013442 if (ret)
13443 return ret;
13444
Jani Nikula73831232015-11-19 10:26:30 +020013445 if (i915.fastboot &&
13446 intel_pipe_config_compare(state->dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013447 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013448 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013449 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013450 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013451 }
13452
13453 if (needs_modeset(crtc_state)) {
13454 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013455
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013456 ret = drm_atomic_add_affected_planes(state, crtc);
13457 if (ret)
13458 return ret;
13459 }
13460
Daniel Vetter26495482015-07-15 14:15:52 +020013461 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13462 needs_modeset(crtc_state) ?
13463 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013464 }
13465
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013466 if (any_ms) {
13467 ret = intel_modeset_checks(state);
13468
13469 if (ret)
13470 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013471 } else
Matt Roperaa363132015-09-24 15:53:18 -070013472 intel_state->cdclk = to_i915(state->dev)->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013473
Matt Roperaa363132015-09-24 15:53:18 -070013474 ret = drm_atomic_helper_check_planes(state->dev, state);
13475 if (ret)
13476 return ret;
13477
13478 calc_watermark_data(state);
13479
13480 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013481}
13482
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013483static int intel_atomic_prepare_commit(struct drm_device *dev,
13484 struct drm_atomic_state *state,
13485 bool async)
13486{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013487 struct drm_i915_private *dev_priv = dev->dev_private;
13488 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013489 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013490 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013491 struct drm_crtc *crtc;
13492 int i, ret;
13493
13494 if (async) {
13495 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13496 return -EINVAL;
13497 }
13498
13499 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13500 ret = intel_crtc_wait_for_pending_flips(crtc);
13501 if (ret)
13502 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013503
13504 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13505 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013506 }
13507
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013508 ret = mutex_lock_interruptible(&dev->struct_mutex);
13509 if (ret)
13510 return ret;
13511
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013512 ret = drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013513 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13514 u32 reset_counter;
13515
13516 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13517 mutex_unlock(&dev->struct_mutex);
13518
13519 for_each_plane_in_state(state, plane, plane_state, i) {
13520 struct intel_plane_state *intel_plane_state =
13521 to_intel_plane_state(plane_state);
13522
13523 if (!intel_plane_state->wait_req)
13524 continue;
13525
13526 ret = __i915_wait_request(intel_plane_state->wait_req,
13527 reset_counter, true,
13528 NULL, NULL);
13529
13530 /* Swallow -EIO errors to allow updates during hw lockup. */
13531 if (ret == -EIO)
13532 ret = 0;
13533
13534 if (ret)
13535 break;
13536 }
13537
13538 if (!ret)
13539 return 0;
13540
13541 mutex_lock(&dev->struct_mutex);
13542 drm_atomic_helper_cleanup_planes(dev, state);
13543 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013544
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013545 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013546 return ret;
13547}
13548
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013549/**
13550 * intel_atomic_commit - commit validated state object
13551 * @dev: DRM device
13552 * @state: the top-level driver state object
13553 * @async: asynchronous commit
13554 *
13555 * This function commits a top-level state object that has been validated
13556 * with drm_atomic_helper_check().
13557 *
13558 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13559 * we can only handle plane-related operations and do not yet support
13560 * asynchronous commit.
13561 *
13562 * RETURNS
13563 * Zero for success or -errno.
13564 */
13565static int intel_atomic_commit(struct drm_device *dev,
13566 struct drm_atomic_state *state,
13567 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013568{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013569 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013570 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013571 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013572 struct drm_crtc *crtc;
Matt Roper396e33a2016-01-06 11:34:30 -080013573 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013574 int ret = 0, i;
13575 bool hw_check = intel_state->modeset;
Daniel Vettera6778b32012-07-02 09:56:42 +020013576
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013577 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013578 if (ret) {
13579 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013580 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013581 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013582
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013583 drm_atomic_helper_swap_state(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013584 dev_priv->wm.config = to_intel_atomic_state(state)->wm_config;
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013585
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013586 if (intel_state->modeset) {
13587 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13588 sizeof(intel_state->min_pixclk));
13589 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013590 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013591 }
13592
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013593 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013594 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13595
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013596 if (!needs_modeset(crtc->state))
13597 continue;
13598
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013599 intel_pre_plane_update(intel_crtc);
Daniel Vetter460da9162013-03-27 00:44:51 +010013600
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013601 if (crtc_state->active) {
13602 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13603 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013604 intel_crtc->active = false;
13605 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013606
13607 /*
13608 * Underruns don't always raise
13609 * interrupts, so check manually.
13610 */
13611 intel_check_cpu_fifo_underruns(dev_priv);
13612 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013613
13614 if (!crtc->state->active)
13615 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013616 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013617 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013618
Daniel Vetterea9d7582012-07-10 10:42:52 +020013619 /* Only after disabling all output pipelines that will be changed can we
13620 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013621 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013622
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013623 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013624 intel_shared_dpll_commit(state);
13625
13626 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013627 modeset_update_crtc_power_domains(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013628 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013629
Daniel Vettera6778b32012-07-02 09:56:42 +020013630 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013631 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013632 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13633 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013634 bool update_pipe = !modeset &&
13635 to_intel_crtc_state(crtc->state)->update_pipe;
13636 unsigned long put_domains = 0;
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013637
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013638 if (modeset)
13639 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
13640
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013641 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013642 update_scanline_offset(to_intel_crtc(crtc));
13643 dev_priv->display.crtc_enable(crtc);
13644 }
13645
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013646 if (update_pipe) {
13647 put_domains = modeset_get_crtc_power_domains(crtc);
13648
13649 /* make sure intel_modeset_check_state runs */
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013650 hw_check = true;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013651 }
13652
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013653 if (!modeset)
13654 intel_pre_plane_update(intel_crtc);
13655
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013656 if (crtc->state->active &&
13657 (crtc->state->planes_changed || update_pipe))
Maarten Lankhorst62852622015-09-23 16:29:38 +020013658 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013659
13660 if (put_domains)
13661 modeset_put_power_domains(dev_priv, put_domains);
13662
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013663 intel_post_plane_update(intel_crtc);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013664
13665 if (modeset)
13666 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013667 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013668
Daniel Vettera6778b32012-07-02 09:56:42 +020013669 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013670
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013671 drm_atomic_helper_wait_for_vblanks(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013672
Matt Roper396e33a2016-01-06 11:34:30 -080013673 /*
13674 * Now that the vblank has passed, we can go ahead and program the
13675 * optimal watermarks on platforms that need two-step watermark
13676 * programming.
13677 *
13678 * TODO: Move this (and other cleanup) to an async worker eventually.
13679 */
13680 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13681 intel_cstate = to_intel_crtc_state(crtc->state);
13682
13683 if (dev_priv->display.optimize_watermarks)
13684 dev_priv->display.optimize_watermarks(intel_cstate);
13685 }
13686
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013687 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013688 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013689 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013690
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013691 if (hw_check)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013692 intel_modeset_check_state(dev, state);
13693
13694 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013695
Mika Kuoppala75714942015-12-16 09:26:48 +020013696 /* As one of the primary mmio accessors, KMS has a high likelihood
13697 * of triggering bugs in unclaimed access. After we finish
13698 * modesetting, see if an error has been flagged, and if so
13699 * enable debugging for the next modeset - and hope we catch
13700 * the culprit.
13701 *
13702 * XXX note that we assume display power is on at this point.
13703 * This might hold true now but we need to add pm helper to check
13704 * unclaimed only when the hardware is on, as atomic commits
13705 * can happen also when the device is completely off.
13706 */
13707 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13708
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013709 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013710}
13711
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013712void intel_crtc_restore_mode(struct drm_crtc *crtc)
13713{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013714 struct drm_device *dev = crtc->dev;
13715 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013716 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013717 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013718
13719 state = drm_atomic_state_alloc(dev);
13720 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013721 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013722 crtc->base.id);
13723 return;
13724 }
13725
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013726 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013727
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013728retry:
13729 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13730 ret = PTR_ERR_OR_ZERO(crtc_state);
13731 if (!ret) {
13732 if (!crtc_state->active)
13733 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013734
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013735 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013736 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013737 }
13738
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013739 if (ret == -EDEADLK) {
13740 drm_atomic_state_clear(state);
13741 drm_modeset_backoff(state->acquire_ctx);
13742 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013743 }
13744
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013745 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013746out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013747 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013748}
13749
Daniel Vetter25c5b262012-07-08 22:08:04 +020013750#undef for_each_intel_crtc_masked
13751
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013752static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013753 .gamma_set = intel_crtc_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013754 .set_config = drm_atomic_helper_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013755 .destroy = intel_crtc_destroy,
13756 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013757 .atomic_duplicate_state = intel_crtc_duplicate_state,
13758 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013759};
13760
Daniel Vetter53589012013-06-05 13:34:16 +020013761static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13762 struct intel_shared_dpll *pll,
13763 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013764{
Daniel Vetter53589012013-06-05 13:34:16 +020013765 uint32_t val;
13766
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013767 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013768 return false;
13769
Daniel Vetter53589012013-06-05 13:34:16 +020013770 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013771 hw_state->dpll = val;
13772 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13773 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013774
13775 return val & DPLL_VCO_ENABLE;
13776}
13777
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013778static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13779 struct intel_shared_dpll *pll)
13780{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013781 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13782 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013783}
13784
Daniel Vettere7b903d2013-06-05 13:34:14 +020013785static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13786 struct intel_shared_dpll *pll)
13787{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013788 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013789 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013790
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013791 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013792
13793 /* Wait for the clocks to stabilize. */
13794 POSTING_READ(PCH_DPLL(pll->id));
13795 udelay(150);
13796
13797 /* The pixel multiplier can only be updated once the
13798 * DPLL is enabled and the clocks are stable.
13799 *
13800 * So write it again.
13801 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013802 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013803 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013804 udelay(200);
13805}
13806
13807static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13808 struct intel_shared_dpll *pll)
13809{
13810 struct drm_device *dev = dev_priv->dev;
13811 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013812
13813 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013814 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013815 if (intel_crtc_to_shared_dpll(crtc) == pll)
13816 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13817 }
13818
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013819 I915_WRITE(PCH_DPLL(pll->id), 0);
13820 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013821 udelay(200);
13822}
13823
Daniel Vetter46edb022013-06-05 13:34:12 +020013824static char *ibx_pch_dpll_names[] = {
13825 "PCH DPLL A",
13826 "PCH DPLL B",
13827};
13828
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013829static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013830{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013831 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013832 int i;
13833
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013834 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013835
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013836 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013837 dev_priv->shared_dplls[i].id = i;
13838 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013839 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013840 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13841 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013842 dev_priv->shared_dplls[i].get_hw_state =
13843 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013844 }
13845}
13846
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013847static void intel_shared_dpll_init(struct drm_device *dev)
13848{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013849 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013850
Daniel Vetter9cd86932014-06-25 22:01:57 +030013851 if (HAS_DDI(dev))
13852 intel_ddi_pll_init(dev);
13853 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013854 ibx_pch_dpll_init(dev);
13855 else
13856 dev_priv->num_shared_dpll = 0;
13857
13858 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013859}
13860
Matt Roper6beb8c232014-12-01 15:40:14 -080013861/**
13862 * intel_prepare_plane_fb - Prepare fb for usage on plane
13863 * @plane: drm plane to prepare for
13864 * @fb: framebuffer to prepare for presentation
13865 *
13866 * Prepares a framebuffer for usage on a display plane. Generally this
13867 * involves pinning the underlying object and updating the frontbuffer tracking
13868 * bits. Some older platforms need special physical address handling for
13869 * cursor planes.
13870 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013871 * Must be called with struct_mutex held.
13872 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013873 * Returns 0 on success, negative error code on failure.
13874 */
13875int
13876intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013877 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013878{
13879 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013880 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013881 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013882 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013883 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013884 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013885
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013886 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013887 return 0;
13888
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013889 if (old_obj) {
13890 struct drm_crtc_state *crtc_state =
13891 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13892
13893 /* Big Hammer, we also need to ensure that any pending
13894 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13895 * current scanout is retired before unpinning the old
13896 * framebuffer. Note that we rely on userspace rendering
13897 * into the buffer attached to the pipe they are waiting
13898 * on. If not, userspace generates a GPU hang with IPEHR
13899 * point to the MI_WAIT_FOR_EVENT.
13900 *
13901 * This should only fail upon a hung GPU, in which case we
13902 * can safely continue.
13903 */
13904 if (needs_modeset(crtc_state))
13905 ret = i915_gem_object_wait_rendering(old_obj, true);
13906
13907 /* Swallow -EIO errors to allow updates during hw lockup. */
13908 if (ret && ret != -EIO)
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013909 return ret;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013910 }
13911
Alex Goins3c28ff22015-11-25 18:43:39 -080013912 /* For framebuffer backed by dmabuf, wait for fence */
13913 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013914 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013915
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013916 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13917 false, true,
13918 MAX_SCHEDULE_TIMEOUT);
13919 if (lret == -ERESTARTSYS)
13920 return lret;
13921
13922 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013923 }
13924
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013925 if (!obj) {
13926 ret = 0;
13927 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013928 INTEL_INFO(dev)->cursor_needs_physical) {
13929 int align = IS_I830(dev) ? 16 * 1024 : 256;
13930 ret = i915_gem_object_attach_phys(obj, align);
13931 if (ret)
13932 DRM_DEBUG_KMS("failed to attach phys object\n");
13933 } else {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013934 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state);
Matt Roper6beb8c232014-12-01 15:40:14 -080013935 }
13936
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013937 if (ret == 0) {
13938 if (obj) {
13939 struct intel_plane_state *plane_state =
13940 to_intel_plane_state(new_state);
13941
13942 i915_gem_request_assign(&plane_state->wait_req,
13943 obj->last_write_req);
13944 }
13945
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013946 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013947 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013948
Matt Roper6beb8c232014-12-01 15:40:14 -080013949 return ret;
13950}
13951
Matt Roper38f3ce32014-12-02 07:45:25 -080013952/**
13953 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13954 * @plane: drm plane to clean up for
13955 * @fb: old framebuffer that was on plane
13956 *
13957 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013958 *
13959 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013960 */
13961void
13962intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013963 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013964{
13965 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013966 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013967 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013968 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13969 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013970
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013971 old_intel_state = to_intel_plane_state(old_state);
13972
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013973 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013974 return;
13975
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013976 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13977 !INTEL_INFO(dev)->cursor_needs_physical))
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013978 intel_unpin_fb_obj(old_state->fb, old_state);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013979
13980 /* prepare_fb aborted? */
13981 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13982 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13983 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013984
13985 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
13986
Matt Roper465c1202014-05-29 08:06:54 -070013987}
13988
Chandra Konduru6156a452015-04-27 13:48:39 -070013989int
13990skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13991{
13992 int max_scale;
13993 struct drm_device *dev;
13994 struct drm_i915_private *dev_priv;
13995 int crtc_clock, cdclk;
13996
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013997 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013998 return DRM_PLANE_HELPER_NO_SCALING;
13999
14000 dev = intel_crtc->base.dev;
14001 dev_priv = dev->dev_private;
14002 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014003 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014004
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014005 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014006 return DRM_PLANE_HELPER_NO_SCALING;
14007
14008 /*
14009 * skl max scale is lower of:
14010 * close to 3 but not 3, -1 is for that purpose
14011 * or
14012 * cdclk/crtc_clock
14013 */
14014 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14015
14016 return max_scale;
14017}
14018
Matt Roper465c1202014-05-29 08:06:54 -070014019static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014020intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014021 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014022 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014023{
Matt Roper2b875c22014-12-01 15:40:13 -080014024 struct drm_crtc *crtc = state->base.crtc;
14025 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014026 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014027 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14028 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014029
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014030 /* use scaler when colorkey is not required */
14031 if (INTEL_INFO(plane->dev)->gen >= 9 &&
Maarten Lankhorst818ed962015-06-15 12:33:54 +020014032 state->ckey.flags == I915_SET_COLORKEY_NONE) {
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014033 min_scale = 1;
14034 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053014035 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014036 }
Sonika Jindald8106362015-04-10 14:37:28 +053014037
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014038 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14039 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014040 min_scale, max_scale,
14041 can_position, true,
14042 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014043}
14044
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020014045static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14046 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080014047{
14048 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080014049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020014050 struct intel_crtc_state *old_intel_state =
14051 to_intel_crtc_state(old_crtc_state);
14052 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030014053
Matt Roperc34c9ee2014-12-23 10:41:50 -080014054 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020014055 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020014056
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020014057 if (modeset)
14058 return;
14059
14060 if (to_intel_crtc_state(crtc->state)->update_pipe)
14061 intel_update_pipe_config(intel_crtc, old_intel_state);
14062 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020014063 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080014064}
14065
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020014066static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14067 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080014068{
Matt Roper32b7eee2014-12-24 07:59:06 -080014069 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080014070
Maarten Lankhorst62852622015-09-23 16:29:38 +020014071 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014072}
14073
Matt Ropercf4c7c12014-12-04 10:27:42 -080014074/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014075 * intel_plane_destroy - destroy a plane
14076 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014077 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014078 * Common destruction function for all types of planes (primary, cursor,
14079 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014080 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014081void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014082{
14083 struct intel_plane *intel_plane = to_intel_plane(plane);
14084 drm_plane_cleanup(plane);
14085 kfree(intel_plane);
14086}
14087
Matt Roper65a3fea2015-01-21 16:35:42 -080014088const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014089 .update_plane = drm_atomic_helper_update_plane,
14090 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014091 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014092 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014093 .atomic_get_property = intel_plane_atomic_get_property,
14094 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014095 .atomic_duplicate_state = intel_plane_duplicate_state,
14096 .atomic_destroy_state = intel_plane_destroy_state,
14097
Matt Roper465c1202014-05-29 08:06:54 -070014098};
14099
14100static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14101 int pipe)
14102{
14103 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080014104 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070014105 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014106 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070014107
14108 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
14109 if (primary == NULL)
14110 return NULL;
14111
Matt Roper8e7d6882015-01-21 16:35:41 -080014112 state = intel_create_plane_state(&primary->base);
14113 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014114 kfree(primary);
14115 return NULL;
14116 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014117 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014118
Matt Roper465c1202014-05-29 08:06:54 -070014119 primary->can_scale = false;
14120 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014121 if (INTEL_INFO(dev)->gen >= 9) {
14122 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014123 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014124 }
Matt Roper465c1202014-05-29 08:06:54 -070014125 primary->pipe = pipe;
14126 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014127 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014128 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014129 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14130 primary->plane = !pipe;
14131
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014132 if (INTEL_INFO(dev)->gen >= 9) {
14133 intel_primary_formats = skl_primary_formats;
14134 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014135
14136 primary->update_plane = skylake_update_primary_plane;
14137 primary->disable_plane = skylake_disable_primary_plane;
14138 } else if (HAS_PCH_SPLIT(dev)) {
14139 intel_primary_formats = i965_primary_formats;
14140 num_formats = ARRAY_SIZE(i965_primary_formats);
14141
14142 primary->update_plane = ironlake_update_primary_plane;
14143 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014144 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014145 intel_primary_formats = i965_primary_formats;
14146 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014147
14148 primary->update_plane = i9xx_update_primary_plane;
14149 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014150 } else {
14151 intel_primary_formats = i8xx_primary_formats;
14152 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014153
14154 primary->update_plane = i9xx_update_primary_plane;
14155 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014156 }
14157
14158 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014159 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070014160 intel_primary_formats, num_formats,
14161 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053014162
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014163 if (INTEL_INFO(dev)->gen >= 4)
14164 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014165
Matt Roperea2c67b2014-12-23 10:41:52 -080014166 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14167
Matt Roper465c1202014-05-29 08:06:54 -070014168 return &primary->base;
14169}
14170
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014171void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14172{
14173 if (!dev->mode_config.rotation_property) {
14174 unsigned long flags = BIT(DRM_ROTATE_0) |
14175 BIT(DRM_ROTATE_180);
14176
14177 if (INTEL_INFO(dev)->gen >= 9)
14178 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14179
14180 dev->mode_config.rotation_property =
14181 drm_mode_create_rotation_property(dev, flags);
14182 }
14183 if (dev->mode_config.rotation_property)
14184 drm_object_attach_property(&plane->base.base,
14185 dev->mode_config.rotation_property,
14186 plane->base.state->rotation);
14187}
14188
Matt Roper3d7d6512014-06-10 08:28:13 -070014189static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014190intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014191 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014192 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014193{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014194 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014195 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014196 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014197 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014198 unsigned stride;
14199 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014200
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014201 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14202 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014203 DRM_PLANE_HELPER_NO_SCALING,
14204 DRM_PLANE_HELPER_NO_SCALING,
14205 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014206 if (ret)
14207 return ret;
14208
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014209 /* if we want to turn off the cursor ignore width and height */
14210 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014211 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014212
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014213 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014214 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014215 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14216 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014217 return -EINVAL;
14218 }
14219
Matt Roperea2c67b2014-12-23 10:41:52 -080014220 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14221 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014222 DRM_DEBUG_KMS("buffer is too small\n");
14223 return -ENOMEM;
14224 }
14225
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014226 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014227 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014228 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014229 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014230
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014231 /*
14232 * There's something wrong with the cursor on CHV pipe C.
14233 * If it straddles the left edge of the screen then
14234 * moving it away from the edge or disabling it often
14235 * results in a pipe underrun, and often that can lead to
14236 * dead pipe (constant underrun reported, and it scans
14237 * out just a solid color). To recover from that, the
14238 * display power well must be turned off and on again.
14239 * Refuse the put the cursor into that compromised position.
14240 */
14241 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14242 state->visible && state->base.crtc_x < 0) {
14243 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14244 return -EINVAL;
14245 }
14246
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014247 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014248}
14249
Matt Roperf4a2cf22014-12-01 15:40:12 -080014250static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014251intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014252 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014253{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14255
14256 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014257 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014258}
14259
14260static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014261intel_update_cursor_plane(struct drm_plane *plane,
14262 const struct intel_crtc_state *crtc_state,
14263 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014264{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014265 struct drm_crtc *crtc = crtc_state->base.crtc;
14266 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014267 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014268 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014269 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014270
Matt Roperf4a2cf22014-12-01 15:40:12 -080014271 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014272 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014273 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014274 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014275 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014276 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014277
Gustavo Padovana912f122014-12-01 15:40:10 -080014278 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014279 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014280}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014281
Matt Roper3d7d6512014-06-10 08:28:13 -070014282static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14283 int pipe)
14284{
14285 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014286 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014287
14288 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14289 if (cursor == NULL)
14290 return NULL;
14291
Matt Roper8e7d6882015-01-21 16:35:41 -080014292 state = intel_create_plane_state(&cursor->base);
14293 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014294 kfree(cursor);
14295 return NULL;
14296 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014297 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014298
Matt Roper3d7d6512014-06-10 08:28:13 -070014299 cursor->can_scale = false;
14300 cursor->max_downscale = 1;
14301 cursor->pipe = pipe;
14302 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014303 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014304 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014305 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014306 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014307
14308 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014309 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014310 intel_cursor_formats,
14311 ARRAY_SIZE(intel_cursor_formats),
14312 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014313
14314 if (INTEL_INFO(dev)->gen >= 4) {
14315 if (!dev->mode_config.rotation_property)
14316 dev->mode_config.rotation_property =
14317 drm_mode_create_rotation_property(dev,
14318 BIT(DRM_ROTATE_0) |
14319 BIT(DRM_ROTATE_180));
14320 if (dev->mode_config.rotation_property)
14321 drm_object_attach_property(&cursor->base.base,
14322 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014323 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014324 }
14325
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014326 if (INTEL_INFO(dev)->gen >=9)
14327 state->scaler_id = -1;
14328
Matt Roperea2c67b2014-12-23 10:41:52 -080014329 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14330
Matt Roper3d7d6512014-06-10 08:28:13 -070014331 return &cursor->base;
14332}
14333
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014334static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14335 struct intel_crtc_state *crtc_state)
14336{
14337 int i;
14338 struct intel_scaler *intel_scaler;
14339 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14340
14341 for (i = 0; i < intel_crtc->num_scalers; i++) {
14342 intel_scaler = &scaler_state->scalers[i];
14343 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014344 intel_scaler->mode = PS_SCALER_MODE_DYN;
14345 }
14346
14347 scaler_state->scaler_id = -1;
14348}
14349
Hannes Ederb358d0a2008-12-18 21:18:47 +010014350static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014351{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014352 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014353 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014354 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014355 struct drm_plane *primary = NULL;
14356 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014357 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014358
Daniel Vetter955382f2013-09-19 14:05:45 +020014359 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014360 if (intel_crtc == NULL)
14361 return;
14362
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014363 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14364 if (!crtc_state)
14365 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014366 intel_crtc->config = crtc_state;
14367 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014368 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014369
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014370 /* initialize shared scalers */
14371 if (INTEL_INFO(dev)->gen >= 9) {
14372 if (pipe == PIPE_C)
14373 intel_crtc->num_scalers = 1;
14374 else
14375 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14376
14377 skl_init_scalers(dev, intel_crtc, crtc_state);
14378 }
14379
Matt Roper465c1202014-05-29 08:06:54 -070014380 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014381 if (!primary)
14382 goto fail;
14383
14384 cursor = intel_cursor_plane_create(dev, pipe);
14385 if (!cursor)
14386 goto fail;
14387
Matt Roper465c1202014-05-29 08:06:54 -070014388 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014389 cursor, &intel_crtc_funcs);
14390 if (ret)
14391 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014392
14393 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014394 for (i = 0; i < 256; i++) {
14395 intel_crtc->lut_r[i] = i;
14396 intel_crtc->lut_g[i] = i;
14397 intel_crtc->lut_b[i] = i;
14398 }
14399
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014400 /*
14401 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014402 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014403 */
Jesse Barnes80824002009-09-10 15:28:06 -070014404 intel_crtc->pipe = pipe;
14405 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014406 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014407 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014408 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014409 }
14410
Chris Wilson4b0e3332014-05-30 16:35:26 +030014411 intel_crtc->cursor_base = ~0;
14412 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014413 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014414
Ville Syrjälä852eb002015-06-24 22:00:07 +030014415 intel_crtc->wm.cxsr_allowed = true;
14416
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014417 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14418 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14419 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14420 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14421
Jesse Barnes79e53942008-11-07 14:24:08 -080014422 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014423
14424 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014425 return;
14426
14427fail:
14428 if (primary)
14429 drm_plane_cleanup(primary);
14430 if (cursor)
14431 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014432 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014433 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014434}
14435
Jesse Barnes752aa882013-10-31 18:55:49 +020014436enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14437{
14438 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014439 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014440
Rob Clark51fd3712013-11-19 12:10:12 -050014441 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014442
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014443 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014444 return INVALID_PIPE;
14445
14446 return to_intel_crtc(encoder->crtc)->pipe;
14447}
14448
Carl Worth08d7b3d2009-04-29 14:43:54 -070014449int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014450 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014451{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014452 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014453 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014454 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014455
Rob Clark7707e652014-07-17 23:30:04 -040014456 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014457
Rob Clark7707e652014-07-17 23:30:04 -040014458 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014459 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014460 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014461 }
14462
Rob Clark7707e652014-07-17 23:30:04 -040014463 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014464 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014465
Daniel Vetterc05422d2009-08-11 16:05:30 +020014466 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014467}
14468
Daniel Vetter66a92782012-07-12 20:08:18 +020014469static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014470{
Daniel Vetter66a92782012-07-12 20:08:18 +020014471 struct drm_device *dev = encoder->base.dev;
14472 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014473 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014474 int entry = 0;
14475
Damien Lespiaub2784e12014-08-05 11:29:37 +010014476 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014477 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014478 index_mask |= (1 << entry);
14479
Jesse Barnes79e53942008-11-07 14:24:08 -080014480 entry++;
14481 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014482
Jesse Barnes79e53942008-11-07 14:24:08 -080014483 return index_mask;
14484}
14485
Chris Wilson4d302442010-12-14 19:21:29 +000014486static bool has_edp_a(struct drm_device *dev)
14487{
14488 struct drm_i915_private *dev_priv = dev->dev_private;
14489
14490 if (!IS_MOBILE(dev))
14491 return false;
14492
14493 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14494 return false;
14495
Damien Lespiaue3589902014-02-07 19:12:50 +000014496 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014497 return false;
14498
14499 return true;
14500}
14501
Jesse Barnes84b4e042014-06-25 08:24:29 -070014502static bool intel_crt_present(struct drm_device *dev)
14503{
14504 struct drm_i915_private *dev_priv = dev->dev_private;
14505
Damien Lespiau884497e2013-12-03 13:56:23 +000014506 if (INTEL_INFO(dev)->gen >= 9)
14507 return false;
14508
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014509 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014510 return false;
14511
14512 if (IS_CHERRYVIEW(dev))
14513 return false;
14514
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014515 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14516 return false;
14517
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014518 /* DDI E can't be used if DDI A requires 4 lanes */
14519 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14520 return false;
14521
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014522 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014523 return false;
14524
14525 return true;
14526}
14527
Jesse Barnes79e53942008-11-07 14:24:08 -080014528static void intel_setup_outputs(struct drm_device *dev)
14529{
Eric Anholt725e30a2009-01-22 13:01:02 -080014530 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014531 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014532 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014533
Daniel Vetterc9093352013-06-06 22:22:47 +020014534 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014535
Jesse Barnes84b4e042014-06-25 08:24:29 -070014536 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014537 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014538
Vandana Kannanc776eb22014-08-19 12:05:01 +053014539 if (IS_BROXTON(dev)) {
14540 /*
14541 * FIXME: Broxton doesn't support port detection via the
14542 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14543 * detect the ports.
14544 */
14545 intel_ddi_init(dev, PORT_A);
14546 intel_ddi_init(dev, PORT_B);
14547 intel_ddi_init(dev, PORT_C);
14548 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014549 int found;
14550
Jesse Barnesde31fac2015-03-06 15:53:32 -080014551 /*
14552 * Haswell uses DDI functions to detect digital outputs.
14553 * On SKL pre-D0 the strap isn't connected, so we assume
14554 * it's there.
14555 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014556 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014557 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014558 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014559 intel_ddi_init(dev, PORT_A);
14560
14561 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14562 * register */
14563 found = I915_READ(SFUSE_STRAP);
14564
14565 if (found & SFUSE_STRAP_DDIB_DETECTED)
14566 intel_ddi_init(dev, PORT_B);
14567 if (found & SFUSE_STRAP_DDIC_DETECTED)
14568 intel_ddi_init(dev, PORT_C);
14569 if (found & SFUSE_STRAP_DDID_DETECTED)
14570 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014571 /*
14572 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14573 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014574 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014575 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14576 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14577 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14578 intel_ddi_init(dev, PORT_E);
14579
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014580 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014581 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014582 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014583
14584 if (has_edp_a(dev))
14585 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014586
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014587 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014588 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014589 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014590 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014591 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014592 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014593 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014594 }
14595
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014596 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014597 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014598
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014599 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014600 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014601
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014602 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014603 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014604
Daniel Vetter270b3042012-10-27 15:52:05 +020014605 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014606 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014607 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014608 /*
14609 * The DP_DETECTED bit is the latched state of the DDC
14610 * SDA pin at boot. However since eDP doesn't require DDC
14611 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14612 * eDP ports may have been muxed to an alternate function.
14613 * Thus we can't rely on the DP_DETECTED bit alone to detect
14614 * eDP ports. Consult the VBT as well as DP_DETECTED to
14615 * detect eDP ports.
14616 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014617 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014618 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014619 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14620 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014621 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014622 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014623
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014624 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014625 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014626 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14627 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014628 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014629 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014630
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014631 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014632 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014633 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14634 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14635 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14636 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014637 }
14638
Jani Nikula3cfca972013-08-27 15:12:26 +030014639 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014640 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014641 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014642
Paulo Zanonie2debe92013-02-18 19:00:27 -030014643 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014644 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014645 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014646 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014647 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014648 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014649 }
Ma Ling27185ae2009-08-24 13:50:23 +080014650
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014651 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014652 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014653 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014654
14655 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014656
Paulo Zanonie2debe92013-02-18 19:00:27 -030014657 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014658 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014659 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014660 }
Ma Ling27185ae2009-08-24 13:50:23 +080014661
Paulo Zanonie2debe92013-02-18 19:00:27 -030014662 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014663
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014664 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014665 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014666 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014667 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014668 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014669 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014670 }
Ma Ling27185ae2009-08-24 13:50:23 +080014671
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014672 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014673 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014674 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014675 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014676 intel_dvo_init(dev);
14677
Zhenyu Wang103a1962009-11-27 11:44:36 +080014678 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014679 intel_tv_init(dev);
14680
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014681 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014682
Damien Lespiaub2784e12014-08-05 11:29:37 +010014683 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014684 encoder->base.possible_crtcs = encoder->crtc_mask;
14685 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014686 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014687 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014688
Paulo Zanonidde86e22012-12-01 12:04:25 -020014689 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014690
14691 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014692}
14693
14694static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14695{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014696 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014697 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014698
Daniel Vetteref2d6332014-02-10 18:00:38 +010014699 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014700 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014701 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014702 drm_gem_object_unreference(&intel_fb->obj->base);
14703 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014704 kfree(intel_fb);
14705}
14706
14707static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014708 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014709 unsigned int *handle)
14710{
14711 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014712 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014713
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014714 if (obj->userptr.mm) {
14715 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14716 return -EINVAL;
14717 }
14718
Chris Wilson05394f32010-11-08 19:18:58 +000014719 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014720}
14721
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014722static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14723 struct drm_file *file,
14724 unsigned flags, unsigned color,
14725 struct drm_clip_rect *clips,
14726 unsigned num_clips)
14727{
14728 struct drm_device *dev = fb->dev;
14729 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14730 struct drm_i915_gem_object *obj = intel_fb->obj;
14731
14732 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014733 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014734 mutex_unlock(&dev->struct_mutex);
14735
14736 return 0;
14737}
14738
Jesse Barnes79e53942008-11-07 14:24:08 -080014739static const struct drm_framebuffer_funcs intel_fb_funcs = {
14740 .destroy = intel_user_framebuffer_destroy,
14741 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014742 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014743};
14744
Damien Lespiaub3218032015-02-27 11:15:18 +000014745static
14746u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14747 uint32_t pixel_format)
14748{
14749 u32 gen = INTEL_INFO(dev)->gen;
14750
14751 if (gen >= 9) {
14752 /* "The stride in bytes must not exceed the of the size of 8K
14753 * pixels and 32K bytes."
14754 */
14755 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014756 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014757 return 32*1024;
14758 } else if (gen >= 4) {
14759 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14760 return 16*1024;
14761 else
14762 return 32*1024;
14763 } else if (gen >= 3) {
14764 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14765 return 8*1024;
14766 else
14767 return 16*1024;
14768 } else {
14769 /* XXX DSPC is limited to 4k tiled */
14770 return 8*1024;
14771 }
14772}
14773
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014774static int intel_framebuffer_init(struct drm_device *dev,
14775 struct intel_framebuffer *intel_fb,
14776 struct drm_mode_fb_cmd2 *mode_cmd,
14777 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014778{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014779 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014780 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014781 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014782
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014783 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14784
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014785 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14786 /* Enforce that fb modifier and tiling mode match, but only for
14787 * X-tiled. This is needed for FBC. */
14788 if (!!(obj->tiling_mode == I915_TILING_X) !=
14789 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14790 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14791 return -EINVAL;
14792 }
14793 } else {
14794 if (obj->tiling_mode == I915_TILING_X)
14795 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14796 else if (obj->tiling_mode == I915_TILING_Y) {
14797 DRM_DEBUG("No Y tiling for legacy addfb\n");
14798 return -EINVAL;
14799 }
14800 }
14801
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014802 /* Passed in modifier sanity checking. */
14803 switch (mode_cmd->modifier[0]) {
14804 case I915_FORMAT_MOD_Y_TILED:
14805 case I915_FORMAT_MOD_Yf_TILED:
14806 if (INTEL_INFO(dev)->gen < 9) {
14807 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14808 mode_cmd->modifier[0]);
14809 return -EINVAL;
14810 }
14811 case DRM_FORMAT_MOD_NONE:
14812 case I915_FORMAT_MOD_X_TILED:
14813 break;
14814 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014815 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14816 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014817 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014818 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014819
Damien Lespiaub3218032015-02-27 11:15:18 +000014820 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14821 mode_cmd->pixel_format);
14822 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14823 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14824 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014825 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014826 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014827
Damien Lespiaub3218032015-02-27 11:15:18 +000014828 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14829 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014830 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014831 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14832 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014833 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014834 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014835 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014836 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014837
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014838 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014839 mode_cmd->pitches[0] != obj->stride) {
14840 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14841 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014842 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014843 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014844
Ville Syrjälä57779d02012-10-31 17:50:14 +020014845 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014846 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014847 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014848 case DRM_FORMAT_RGB565:
14849 case DRM_FORMAT_XRGB8888:
14850 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014851 break;
14852 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014853 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014854 DRM_DEBUG("unsupported pixel format: %s\n",
14855 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014856 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014857 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014858 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014859 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014860 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14861 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014862 DRM_DEBUG("unsupported pixel format: %s\n",
14863 drm_get_format_name(mode_cmd->pixel_format));
14864 return -EINVAL;
14865 }
14866 break;
14867 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014868 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014869 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014870 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014871 DRM_DEBUG("unsupported pixel format: %s\n",
14872 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014873 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014874 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014875 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014876 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014877 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014878 DRM_DEBUG("unsupported pixel format: %s\n",
14879 drm_get_format_name(mode_cmd->pixel_format));
14880 return -EINVAL;
14881 }
14882 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014883 case DRM_FORMAT_YUYV:
14884 case DRM_FORMAT_UYVY:
14885 case DRM_FORMAT_YVYU:
14886 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014887 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014888 DRM_DEBUG("unsupported pixel format: %s\n",
14889 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014890 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014891 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014892 break;
14893 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014894 DRM_DEBUG("unsupported pixel format: %s\n",
14895 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014896 return -EINVAL;
14897 }
14898
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014899 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14900 if (mode_cmd->offsets[0] != 0)
14901 return -EINVAL;
14902
Damien Lespiauec2c9812015-01-20 12:51:45 +000014903 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014904 mode_cmd->pixel_format,
14905 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014906 /* FIXME drm helper for size checks (especially planar formats)? */
14907 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14908 return -EINVAL;
14909
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014910 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14911 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014912 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014913
Jesse Barnes79e53942008-11-07 14:24:08 -080014914 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14915 if (ret) {
14916 DRM_ERROR("framebuffer init failed %d\n", ret);
14917 return ret;
14918 }
14919
Jesse Barnes79e53942008-11-07 14:24:08 -080014920 return 0;
14921}
14922
Jesse Barnes79e53942008-11-07 14:24:08 -080014923static struct drm_framebuffer *
14924intel_user_framebuffer_create(struct drm_device *dev,
14925 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020014926 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014927{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014928 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014929 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014930 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014931
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014932 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014933 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014934 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014935 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014936
Daniel Vetter92907cb2015-11-23 09:04:05 +010014937 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014938 if (IS_ERR(fb))
14939 drm_gem_object_unreference_unlocked(&obj->base);
14940
14941 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014942}
14943
Daniel Vetter06957262015-08-10 13:34:08 +020014944#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014945static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014946{
14947}
14948#endif
14949
Jesse Barnes79e53942008-11-07 14:24:08 -080014950static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014951 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014952 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014953 .atomic_check = intel_atomic_check,
14954 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014955 .atomic_state_alloc = intel_atomic_state_alloc,
14956 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014957};
14958
Jesse Barnese70236a2009-09-21 10:42:27 -070014959/* Set up chip specific display functions */
14960static void intel_init_display(struct drm_device *dev)
14961{
14962 struct drm_i915_private *dev_priv = dev->dev_private;
14963
Daniel Vetteree9300b2013-06-03 22:40:22 +020014964 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14965 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014966 else if (IS_CHERRYVIEW(dev))
14967 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014968 else if (IS_VALLEYVIEW(dev))
14969 dev_priv->display.find_dpll = vlv_find_best_dpll;
14970 else if (IS_PINEVIEW(dev))
14971 dev_priv->display.find_dpll = pnv_find_best_dpll;
14972 else
14973 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14974
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014975 if (INTEL_INFO(dev)->gen >= 9) {
14976 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014977 dev_priv->display.get_initial_plane_config =
14978 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014979 dev_priv->display.crtc_compute_clock =
14980 haswell_crtc_compute_clock;
14981 dev_priv->display.crtc_enable = haswell_crtc_enable;
14982 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014983 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014984 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014985 dev_priv->display.get_initial_plane_config =
14986 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014987 dev_priv->display.crtc_compute_clock =
14988 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014989 dev_priv->display.crtc_enable = haswell_crtc_enable;
14990 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014991 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014992 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014993 dev_priv->display.get_initial_plane_config =
14994 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014995 dev_priv->display.crtc_compute_clock =
14996 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014997 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14998 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Wayne Boyer666a4532015-12-09 12:29:35 -080014999 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015000 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015001 dev_priv->display.get_initial_plane_config =
15002 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015003 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015004 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15005 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015006 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015007 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015008 dev_priv->display.get_initial_plane_config =
15009 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015010 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015011 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15012 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015013 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015014
Jesse Barnese70236a2009-09-21 10:42:27 -070015015 /* Returns the core display clock speed */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070015016 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015017 dev_priv->display.get_display_clock_speed =
15018 skylake_get_display_clock_speed;
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015019 else if (IS_BROXTON(dev))
15020 dev_priv->display.get_display_clock_speed =
15021 broxton_get_display_clock_speed;
Ville Syrjälä1652d192015-03-31 14:12:01 +030015022 else if (IS_BROADWELL(dev))
15023 dev_priv->display.get_display_clock_speed =
15024 broadwell_get_display_clock_speed;
15025 else if (IS_HASWELL(dev))
15026 dev_priv->display.get_display_clock_speed =
15027 haswell_get_display_clock_speed;
Wayne Boyer666a4532015-12-09 12:29:35 -080015028 else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015029 dev_priv->display.get_display_clock_speed =
15030 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015031 else if (IS_GEN5(dev))
15032 dev_priv->display.get_display_clock_speed =
15033 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030015034 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030015035 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070015036 dev_priv->display.get_display_clock_speed =
15037 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030015038 else if (IS_GM45(dev))
15039 dev_priv->display.get_display_clock_speed =
15040 gm45_get_display_clock_speed;
15041 else if (IS_CRESTLINE(dev))
15042 dev_priv->display.get_display_clock_speed =
15043 i965gm_get_display_clock_speed;
15044 else if (IS_PINEVIEW(dev))
15045 dev_priv->display.get_display_clock_speed =
15046 pnv_get_display_clock_speed;
15047 else if (IS_G33(dev) || IS_G4X(dev))
15048 dev_priv->display.get_display_clock_speed =
15049 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070015050 else if (IS_I915G(dev))
15051 dev_priv->display.get_display_clock_speed =
15052 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020015053 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070015054 dev_priv->display.get_display_clock_speed =
15055 i9xx_misc_get_display_clock_speed;
15056 else if (IS_I915GM(dev))
15057 dev_priv->display.get_display_clock_speed =
15058 i915gm_get_display_clock_speed;
15059 else if (IS_I865G(dev))
15060 dev_priv->display.get_display_clock_speed =
15061 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020015062 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070015063 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015064 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015065 else { /* 830 */
15066 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015067 dev_priv->display.get_display_clock_speed =
15068 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015069 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015070
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015071 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015072 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015073 } else if (IS_GEN6(dev)) {
15074 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015075 } else if (IS_IVYBRIDGE(dev)) {
15076 /* FIXME: detect B0+ stepping and use auto training */
15077 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030015078 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015079 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015080 if (IS_BROADWELL(dev)) {
15081 dev_priv->display.modeset_commit_cdclk =
15082 broadwell_modeset_commit_cdclk;
15083 dev_priv->display.modeset_calc_cdclk =
15084 broadwell_modeset_calc_cdclk;
15085 }
Wayne Boyer666a4532015-12-09 12:29:35 -080015086 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015087 dev_priv->display.modeset_commit_cdclk =
15088 valleyview_modeset_commit_cdclk;
15089 dev_priv->display.modeset_calc_cdclk =
15090 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053015091 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015092 dev_priv->display.modeset_commit_cdclk =
15093 broxton_modeset_commit_cdclk;
15094 dev_priv->display.modeset_calc_cdclk =
15095 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015096 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015097
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015098 switch (INTEL_INFO(dev)->gen) {
15099 case 2:
15100 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15101 break;
15102
15103 case 3:
15104 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15105 break;
15106
15107 case 4:
15108 case 5:
15109 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15110 break;
15111
15112 case 6:
15113 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15114 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015115 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070015116 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015117 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15118 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000015119 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000015120 /* Drop through - unsupported since execlist only. */
15121 default:
15122 /* Default just returns -ENODEV to indicate unsupported */
15123 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015124 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020015125
Ville Syrjäläe39b9992014-09-04 14:53:14 +030015126 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070015127}
15128
Jesse Barnesb690e962010-07-19 13:53:12 -070015129/*
15130 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15131 * resume, or other times. This quirk makes sure that's the case for
15132 * affected systems.
15133 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015134static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015135{
15136 struct drm_i915_private *dev_priv = dev->dev_private;
15137
15138 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015139 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015140}
15141
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015142static void quirk_pipeb_force(struct drm_device *dev)
15143{
15144 struct drm_i915_private *dev_priv = dev->dev_private;
15145
15146 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15147 DRM_INFO("applying pipe b force quirk\n");
15148}
15149
Keith Packard435793d2011-07-12 14:56:22 -070015150/*
15151 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15152 */
15153static void quirk_ssc_force_disable(struct drm_device *dev)
15154{
15155 struct drm_i915_private *dev_priv = dev->dev_private;
15156 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015157 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015158}
15159
Carsten Emde4dca20e2012-03-15 15:56:26 +010015160/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015161 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15162 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015163 */
15164static void quirk_invert_brightness(struct drm_device *dev)
15165{
15166 struct drm_i915_private *dev_priv = dev->dev_private;
15167 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015168 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015169}
15170
Scot Doyle9c72cc62014-07-03 23:27:50 +000015171/* Some VBT's incorrectly indicate no backlight is present */
15172static void quirk_backlight_present(struct drm_device *dev)
15173{
15174 struct drm_i915_private *dev_priv = dev->dev_private;
15175 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15176 DRM_INFO("applying backlight present quirk\n");
15177}
15178
Jesse Barnesb690e962010-07-19 13:53:12 -070015179struct intel_quirk {
15180 int device;
15181 int subsystem_vendor;
15182 int subsystem_device;
15183 void (*hook)(struct drm_device *dev);
15184};
15185
Egbert Eich5f85f172012-10-14 15:46:38 +020015186/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15187struct intel_dmi_quirk {
15188 void (*hook)(struct drm_device *dev);
15189 const struct dmi_system_id (*dmi_id_list)[];
15190};
15191
15192static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15193{
15194 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15195 return 1;
15196}
15197
15198static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15199 {
15200 .dmi_id_list = &(const struct dmi_system_id[]) {
15201 {
15202 .callback = intel_dmi_reverse_brightness,
15203 .ident = "NCR Corporation",
15204 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15205 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15206 },
15207 },
15208 { } /* terminating entry */
15209 },
15210 .hook = quirk_invert_brightness,
15211 },
15212};
15213
Ben Widawskyc43b5632012-04-16 14:07:40 -070015214static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015215 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15216 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15217
Jesse Barnesb690e962010-07-19 13:53:12 -070015218 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15219 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15220
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015221 /* 830 needs to leave pipe A & dpll A up */
15222 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15223
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015224 /* 830 needs to leave pipe B & dpll B up */
15225 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15226
Keith Packard435793d2011-07-12 14:56:22 -070015227 /* Lenovo U160 cannot use SSC on LVDS */
15228 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015229
15230 /* Sony Vaio Y cannot use SSC on LVDS */
15231 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015232
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015233 /* Acer Aspire 5734Z must invert backlight brightness */
15234 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15235
15236 /* Acer/eMachines G725 */
15237 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15238
15239 /* Acer/eMachines e725 */
15240 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15241
15242 /* Acer/Packard Bell NCL20 */
15243 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15244
15245 /* Acer Aspire 4736Z */
15246 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015247
15248 /* Acer Aspire 5336 */
15249 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015250
15251 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15252 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015253
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015254 /* Acer C720 Chromebook (Core i3 4005U) */
15255 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15256
jens steinb2a96012014-10-28 20:25:53 +010015257 /* Apple Macbook 2,1 (Core 2 T7400) */
15258 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15259
Jani Nikula1b9448b2015-11-05 11:49:59 +020015260 /* Apple Macbook 4,1 */
15261 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15262
Scot Doyled4967d82014-07-03 23:27:52 +000015263 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15264 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015265
15266 /* HP Chromebook 14 (Celeron 2955U) */
15267 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015268
15269 /* Dell Chromebook 11 */
15270 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015271
15272 /* Dell Chromebook 11 (2015 version) */
15273 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015274};
15275
15276static void intel_init_quirks(struct drm_device *dev)
15277{
15278 struct pci_dev *d = dev->pdev;
15279 int i;
15280
15281 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15282 struct intel_quirk *q = &intel_quirks[i];
15283
15284 if (d->device == q->device &&
15285 (d->subsystem_vendor == q->subsystem_vendor ||
15286 q->subsystem_vendor == PCI_ANY_ID) &&
15287 (d->subsystem_device == q->subsystem_device ||
15288 q->subsystem_device == PCI_ANY_ID))
15289 q->hook(dev);
15290 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015291 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15292 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15293 intel_dmi_quirks[i].hook(dev);
15294 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015295}
15296
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015297/* Disable the VGA plane that we never use */
15298static void i915_disable_vga(struct drm_device *dev)
15299{
15300 struct drm_i915_private *dev_priv = dev->dev_private;
15301 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015302 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015303
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015304 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015305 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015306 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015307 sr1 = inb(VGA_SR_DATA);
15308 outb(sr1 | 1<<5, VGA_SR_DATA);
15309 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15310 udelay(300);
15311
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015312 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015313 POSTING_READ(vga_reg);
15314}
15315
Daniel Vetterf8175862012-04-10 15:50:11 +020015316void intel_modeset_init_hw(struct drm_device *dev)
15317{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015318 struct drm_i915_private *dev_priv = dev->dev_private;
15319
Ville Syrjäläb6283052015-06-03 15:45:07 +030015320 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015321
15322 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15323
Daniel Vetterf8175862012-04-10 15:50:11 +020015324 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015325 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015326}
15327
Matt Roperd93c0372015-12-03 11:37:41 -080015328/*
15329 * Calculate what we think the watermarks should be for the state we've read
15330 * out of the hardware and then immediately program those watermarks so that
15331 * we ensure the hardware settings match our internal state.
15332 *
15333 * We can calculate what we think WM's should be by creating a duplicate of the
15334 * current state (which was constructed during hardware readout) and running it
15335 * through the atomic check code to calculate new watermark values in the
15336 * state object.
15337 */
15338static void sanitize_watermarks(struct drm_device *dev)
15339{
15340 struct drm_i915_private *dev_priv = to_i915(dev);
15341 struct drm_atomic_state *state;
15342 struct drm_crtc *crtc;
15343 struct drm_crtc_state *cstate;
15344 struct drm_modeset_acquire_ctx ctx;
15345 int ret;
15346 int i;
15347
15348 /* Only supported on platforms that use atomic watermark design */
Matt Roper396e33a2016-01-06 11:34:30 -080015349 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015350 return;
15351
15352 /*
15353 * We need to hold connection_mutex before calling duplicate_state so
15354 * that the connector loop is protected.
15355 */
15356 drm_modeset_acquire_init(&ctx, 0);
15357retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015358 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015359 if (ret == -EDEADLK) {
15360 drm_modeset_backoff(&ctx);
15361 goto retry;
15362 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015363 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015364 }
15365
15366 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15367 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015368 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015369
Matt Roper396e33a2016-01-06 11:34:30 -080015370 /*
15371 * Hardware readout is the only time we don't want to calculate
15372 * intermediate watermarks (since we don't trust the current
15373 * watermarks).
15374 */
15375 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15376
Matt Roperd93c0372015-12-03 11:37:41 -080015377 ret = intel_atomic_check(dev, state);
15378 if (ret) {
15379 /*
15380 * If we fail here, it means that the hardware appears to be
15381 * programmed in a way that shouldn't be possible, given our
15382 * understanding of watermark requirements. This might mean a
15383 * mistake in the hardware readout code or a mistake in the
15384 * watermark calculations for a given platform. Raise a WARN
15385 * so that this is noticeable.
15386 *
15387 * If this actually happens, we'll have to just leave the
15388 * BIOS-programmed watermarks untouched and hope for the best.
15389 */
15390 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015391 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015392 }
15393
15394 /* Write calculated watermark values back */
15395 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15396 for_each_crtc_in_state(state, crtc, cstate, i) {
15397 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15398
Matt Roper396e33a2016-01-06 11:34:30 -080015399 cs->wm.need_postvbl_update = true;
15400 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015401 }
15402
15403 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015404fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015405 drm_modeset_drop_locks(&ctx);
15406 drm_modeset_acquire_fini(&ctx);
15407}
15408
Jesse Barnes79e53942008-11-07 14:24:08 -080015409void intel_modeset_init(struct drm_device *dev)
15410{
Jesse Barnes652c3932009-08-17 13:31:43 -070015411 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015412 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015413 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015414 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015415
15416 drm_mode_config_init(dev);
15417
15418 dev->mode_config.min_width = 0;
15419 dev->mode_config.min_height = 0;
15420
Dave Airlie019d96c2011-09-29 16:20:42 +010015421 dev->mode_config.preferred_depth = 24;
15422 dev->mode_config.prefer_shadow = 1;
15423
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015424 dev->mode_config.allow_fb_modifiers = true;
15425
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015426 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015427
Jesse Barnesb690e962010-07-19 13:53:12 -070015428 intel_init_quirks(dev);
15429
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015430 intel_init_pm(dev);
15431
Ben Widawskye3c74752013-04-05 13:12:39 -070015432 if (INTEL_INFO(dev)->num_pipes == 0)
15433 return;
15434
Lukas Wunner69f92f62015-07-15 13:57:35 +020015435 /*
15436 * There may be no VBT; and if the BIOS enabled SSC we can
15437 * just keep using it to avoid unnecessary flicker. Whereas if the
15438 * BIOS isn't using it, don't assume it will work even if the VBT
15439 * indicates as much.
15440 */
15441 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15442 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15443 DREF_SSC1_ENABLE);
15444
15445 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15446 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15447 bios_lvds_use_ssc ? "en" : "dis",
15448 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15449 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15450 }
15451 }
15452
Jesse Barnese70236a2009-09-21 10:42:27 -070015453 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015454 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015455
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015456 if (IS_GEN2(dev)) {
15457 dev->mode_config.max_width = 2048;
15458 dev->mode_config.max_height = 2048;
15459 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015460 dev->mode_config.max_width = 4096;
15461 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015462 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015463 dev->mode_config.max_width = 8192;
15464 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015465 }
Damien Lespiau068be562014-03-28 14:17:49 +000015466
Ville Syrjälädc41c152014-08-13 11:57:05 +030015467 if (IS_845G(dev) || IS_I865G(dev)) {
15468 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15469 dev->mode_config.cursor_height = 1023;
15470 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015471 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15472 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15473 } else {
15474 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15475 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15476 }
15477
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015478 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015479
Zhao Yakui28c97732009-10-09 11:39:41 +080015480 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015481 INTEL_INFO(dev)->num_pipes,
15482 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015483
Damien Lespiau055e3932014-08-18 13:49:10 +010015484 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015485 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015486 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015487 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015488 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015489 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015490 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015491 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015492 }
15493
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015494 intel_update_czclk(dev_priv);
15495 intel_update_cdclk(dev);
15496
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015497 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015498
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015499 /* Just disable it once at startup */
15500 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015501 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015502
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015503 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015504 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015505 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015506
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015507 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015508 struct intel_initial_plane_config plane_config = {};
15509
Jesse Barnes46f297f2014-03-07 08:57:48 -080015510 if (!crtc->active)
15511 continue;
15512
Jesse Barnes46f297f2014-03-07 08:57:48 -080015513 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015514 * Note that reserving the BIOS fb up front prevents us
15515 * from stuffing other stolen allocations like the ring
15516 * on top. This prevents some ugliness at boot time, and
15517 * can even allow for smooth boot transitions if the BIOS
15518 * fb is large enough for the active pipe configuration.
15519 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015520 dev_priv->display.get_initial_plane_config(crtc,
15521 &plane_config);
15522
15523 /*
15524 * If the fb is shared between multiple heads, we'll
15525 * just get the first one.
15526 */
15527 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015528 }
Matt Roperd93c0372015-12-03 11:37:41 -080015529
15530 /*
15531 * Make sure hardware watermarks really match the state we read out.
15532 * Note that we need to do this after reconstructing the BIOS fb's
15533 * since the watermark calculation done here will use pstate->fb.
15534 */
15535 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015536}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015537
Daniel Vetter7fad7982012-07-04 17:51:47 +020015538static void intel_enable_pipe_a(struct drm_device *dev)
15539{
15540 struct intel_connector *connector;
15541 struct drm_connector *crt = NULL;
15542 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015543 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015544
15545 /* We can't just switch on the pipe A, we need to set things up with a
15546 * proper mode and output configuration. As a gross hack, enable pipe A
15547 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015548 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015549 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15550 crt = &connector->base;
15551 break;
15552 }
15553 }
15554
15555 if (!crt)
15556 return;
15557
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015558 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015559 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015560}
15561
Daniel Vetterfa555832012-10-10 23:14:00 +020015562static bool
15563intel_check_plane_mapping(struct intel_crtc *crtc)
15564{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015565 struct drm_device *dev = crtc->base.dev;
15566 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015567 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015568
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015569 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015570 return true;
15571
Ville Syrjälä649636e2015-09-22 19:50:01 +030015572 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015573
15574 if ((val & DISPLAY_PLANE_ENABLE) &&
15575 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15576 return false;
15577
15578 return true;
15579}
15580
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015581static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15582{
15583 struct drm_device *dev = crtc->base.dev;
15584 struct intel_encoder *encoder;
15585
15586 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15587 return true;
15588
15589 return false;
15590}
15591
Daniel Vetter24929352012-07-02 20:28:59 +020015592static void intel_sanitize_crtc(struct intel_crtc *crtc)
15593{
15594 struct drm_device *dev = crtc->base.dev;
15595 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015596 i915_reg_t reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015597
Daniel Vetter24929352012-07-02 20:28:59 +020015598 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter24929352012-07-02 20:28:59 +020015599 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15600
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015601 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015602 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015603 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015604 struct intel_plane *plane;
15605
Daniel Vetter96256042015-02-13 21:03:42 +010015606 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015607
15608 /* Disable everything but the primary plane */
15609 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15610 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15611 continue;
15612
15613 plane->disable_plane(&plane->base, &crtc->base);
15614 }
Daniel Vetter96256042015-02-13 21:03:42 +010015615 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015616
Daniel Vetter24929352012-07-02 20:28:59 +020015617 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015618 * disable the crtc (and hence change the state) if it is wrong. Note
15619 * that gen4+ has a fixed plane -> pipe mapping. */
15620 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015621 bool plane;
15622
Daniel Vetter24929352012-07-02 20:28:59 +020015623 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15624 crtc->base.base.id);
15625
15626 /* Pipe has the wrong plane attached and the plane is active.
15627 * Temporarily change the plane mapping and disable everything
15628 * ... */
15629 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015630 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015631 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015632 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015633 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015634 }
Daniel Vetter24929352012-07-02 20:28:59 +020015635
Daniel Vetter7fad7982012-07-04 17:51:47 +020015636 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15637 crtc->pipe == PIPE_A && !crtc->active) {
15638 /* BIOS forgot to enable pipe A, this mostly happens after
15639 * resume. Force-enable the pipe to fix this, the update_dpms
15640 * call below we restore the pipe to the right state, but leave
15641 * the required bits on. */
15642 intel_enable_pipe_a(dev);
15643 }
15644
Daniel Vetter24929352012-07-02 20:28:59 +020015645 /* Adjust the state of the output pipe according to whether we
15646 * have active connectors/encoders. */
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015647 if (!intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015648 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015649
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015650 if (crtc->active != crtc->base.state->active) {
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015651 struct intel_encoder *encoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015652
15653 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015654 * functions or because of calls to intel_crtc_disable_noatomic,
15655 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015656 * pipe A quirk. */
15657 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15658 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015659 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015660 crtc->active ? "enabled" : "disabled");
15661
Maarten Lankhorst4be40c92015-07-14 13:45:32 +020015662 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015663 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015664 crtc->base.enabled = crtc->active;
15665
15666 /* Because we only establish the connector -> encoder ->
15667 * crtc links if something is active, this means the
15668 * crtc is now deactivated. Break the links. connector
15669 * -> encoder links are only establish when things are
15670 * actually up, hence no need to break them. */
15671 WARN_ON(crtc->active);
15672
Maarten Lankhorst2d406bb2015-08-05 12:37:09 +020015673 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetter24929352012-07-02 20:28:59 +020015674 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015675 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015676
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015677 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015678 /*
15679 * We start out with underrun reporting disabled to avoid races.
15680 * For correct bookkeeping mark this on active crtcs.
15681 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015682 * Also on gmch platforms we dont have any hardware bits to
15683 * disable the underrun reporting. Which means we need to start
15684 * out with underrun reporting disabled also on inactive pipes,
15685 * since otherwise we'll complain about the garbage we read when
15686 * e.g. coming up after runtime pm.
15687 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015688 * No protection against concurrent access is required - at
15689 * worst a fifo underrun happens which also sets this to false.
15690 */
15691 crtc->cpu_fifo_underrun_disabled = true;
15692 crtc->pch_fifo_underrun_disabled = true;
15693 }
Daniel Vetter24929352012-07-02 20:28:59 +020015694}
15695
15696static void intel_sanitize_encoder(struct intel_encoder *encoder)
15697{
15698 struct intel_connector *connector;
15699 struct drm_device *dev = encoder->base.dev;
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020015700 bool active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015701
15702 /* We need to check both for a crtc link (meaning that the
15703 * encoder is active and trying to read from a pipe) and the
15704 * pipe itself being active. */
15705 bool has_active_crtc = encoder->base.crtc &&
15706 to_intel_crtc(encoder->base.crtc)->active;
15707
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020015708 for_each_intel_connector(dev, connector) {
15709 if (connector->base.encoder != &encoder->base)
15710 continue;
15711
15712 active = true;
15713 break;
15714 }
15715
15716 if (active && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015717 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15718 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015719 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015720
15721 /* Connector is active, but has no active pipe. This is
15722 * fallout from our resume register restoring. Disable
15723 * the encoder manually again. */
15724 if (encoder->base.crtc) {
15725 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15726 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015727 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015728 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015729 if (encoder->post_disable)
15730 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015731 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015732 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015733
15734 /* Inconsistent output/port/pipe state happens presumably due to
15735 * a bug in one of the get_hw_state functions. Or someplace else
15736 * in our code, like the register restore mess on resume. Clamp
15737 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015738 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015739 if (connector->encoder != encoder)
15740 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015741 connector->base.dpms = DRM_MODE_DPMS_OFF;
15742 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015743 }
15744 }
15745 /* Enabled encoders without active connectors will be fixed in
15746 * the crtc fixup. */
15747}
15748
Imre Deak04098752014-02-18 00:02:16 +020015749void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015750{
15751 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015752 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015753
Imre Deak04098752014-02-18 00:02:16 +020015754 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15755 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15756 i915_disable_vga(dev);
15757 }
15758}
15759
15760void i915_redisable_vga(struct drm_device *dev)
15761{
15762 struct drm_i915_private *dev_priv = dev->dev_private;
15763
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015764 /* This function can be called both from intel_modeset_setup_hw_state or
15765 * at a very early point in our resume sequence, where the power well
15766 * structures are not yet restored. Since this function is at a very
15767 * paranoid "someone might have enabled VGA while we were not looking"
15768 * level, just check if the power well is enabled instead of trying to
15769 * follow the "don't touch the power well if we don't need it" policy
15770 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015771 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015772 return;
15773
Imre Deak04098752014-02-18 00:02:16 +020015774 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015775}
15776
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015777static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015778{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015779 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015780
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015781 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015782}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015783
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015784/* FIXME read out full plane state for all planes */
15785static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015786{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015787 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015788 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015789 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015790
Matt Roper19b8d382015-09-24 15:53:17 -070015791 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015792 primary_get_hw_state(to_intel_plane(primary));
15793
15794 if (plane_state->visible)
15795 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015796}
15797
Daniel Vetter30e984d2013-06-05 13:34:17 +020015798static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015799{
15800 struct drm_i915_private *dev_priv = dev->dev_private;
15801 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015802 struct intel_crtc *crtc;
15803 struct intel_encoder *encoder;
15804 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015805 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015806
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015807 dev_priv->active_crtcs = 0;
15808
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015809 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015810 struct intel_crtc_state *crtc_state = crtc->config;
15811 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015812
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015813 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15814 memset(crtc_state, 0, sizeof(*crtc_state));
15815 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015816
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015817 crtc_state->base.active = crtc_state->base.enable =
15818 dev_priv->display.get_pipe_config(crtc, crtc_state);
15819
15820 crtc->base.enabled = crtc_state->base.enable;
15821 crtc->active = crtc_state->base.active;
15822
15823 if (crtc_state->base.active) {
15824 dev_priv->active_crtcs |= 1 << crtc->pipe;
15825
15826 if (IS_BROADWELL(dev_priv)) {
15827 pixclk = ilk_pipe_pixel_rate(crtc_state);
15828
15829 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15830 if (crtc_state->ips_enabled)
15831 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15832 } else if (IS_VALLEYVIEW(dev_priv) ||
15833 IS_CHERRYVIEW(dev_priv) ||
15834 IS_BROXTON(dev_priv))
15835 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15836 else
15837 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15838 }
15839
15840 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015841
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015842 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015843
15844 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15845 crtc->base.base.id,
15846 crtc->active ? "enabled" : "disabled");
15847 }
15848
Daniel Vetter53589012013-06-05 13:34:16 +020015849 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15850 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15851
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015852 pll->on = pll->get_hw_state(dev_priv, pll,
15853 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015854 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015855 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015856 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015857 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015858 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015859 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015860 }
Daniel Vetter53589012013-06-05 13:34:16 +020015861 }
Daniel Vetter53589012013-06-05 13:34:16 +020015862
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015863 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015864 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015865
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015866 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015867 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015868 }
15869
Damien Lespiaub2784e12014-08-05 11:29:37 +010015870 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015871 pipe = 0;
15872
15873 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015874 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15875 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015876 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015877 } else {
15878 encoder->base.crtc = NULL;
15879 }
15880
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015881 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015882 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015883 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015884 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015885 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015886 }
15887
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015888 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015889 if (connector->get_hw_state(connector)) {
15890 connector->base.dpms = DRM_MODE_DPMS_ON;
Daniel Vetter24929352012-07-02 20:28:59 +020015891 connector->base.encoder = &connector->encoder->base;
15892 } else {
15893 connector->base.dpms = DRM_MODE_DPMS_OFF;
15894 connector->base.encoder = NULL;
15895 }
15896 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15897 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015898 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015899 connector->base.encoder ? "enabled" : "disabled");
15900 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015901
15902 for_each_intel_crtc(dev, crtc) {
15903 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15904
15905 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15906 if (crtc->base.state->active) {
15907 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15908 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15909 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15910
15911 /*
15912 * The initial mode needs to be set in order to keep
15913 * the atomic core happy. It wants a valid mode if the
15914 * crtc's enabled, so we do the above call.
15915 *
15916 * At this point some state updated by the connectors
15917 * in their ->detect() callback has not run yet, so
15918 * no recalculation can be done yet.
15919 *
15920 * Even if we could do a recalculation and modeset
15921 * right now it would cause a double modeset if
15922 * fbdev or userspace chooses a different initial mode.
15923 *
15924 * If that happens, someone indicated they wanted a
15925 * mode change, which means it's safe to do a full
15926 * recalculation.
15927 */
15928 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015929
15930 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15931 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015932 }
15933 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015934}
15935
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015936/* Scan out the current hw modeset state,
15937 * and sanitizes it to the current state
15938 */
15939static void
15940intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015941{
15942 struct drm_i915_private *dev_priv = dev->dev_private;
15943 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015944 struct intel_crtc *crtc;
15945 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015946 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015947
15948 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015949
15950 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015951 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015952 intel_sanitize_encoder(encoder);
15953 }
15954
Damien Lespiau055e3932014-08-18 13:49:10 +010015955 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015956 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15957 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015958 intel_dump_pipe_config(crtc, crtc->config,
15959 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015960 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015961
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015962 intel_modeset_update_connector_atomic_state(dev);
15963
Daniel Vetter35c95372013-07-17 06:55:04 +020015964 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15965 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15966
15967 if (!pll->on || pll->active)
15968 continue;
15969
15970 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15971
15972 pll->disable(dev_priv, pll);
15973 pll->on = false;
15974 }
15975
Wayne Boyer666a4532015-12-09 12:29:35 -080015976 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015977 vlv_wm_get_hw_state(dev);
15978 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015979 skl_wm_get_hw_state(dev);
15980 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015981 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015982
15983 for_each_intel_crtc(dev, crtc) {
15984 unsigned long put_domains;
15985
15986 put_domains = modeset_get_crtc_power_domains(&crtc->base);
15987 if (WARN_ON(put_domains))
15988 modeset_put_power_domains(dev_priv, put_domains);
15989 }
15990 intel_display_set_init_power(dev_priv, false);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015991}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015992
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015993void intel_display_resume(struct drm_device *dev)
15994{
15995 struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
15996 struct intel_connector *conn;
15997 struct intel_plane *plane;
15998 struct drm_crtc *crtc;
15999 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020016000
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016001 if (!state)
16002 return;
16003
16004 state->acquire_ctx = dev->mode_config.acquire_ctx;
16005
16006 /* preserve complete old state, including dpll */
16007 intel_atomic_get_shared_dpll_state(state);
16008
16009 for_each_crtc(dev, crtc) {
16010 struct drm_crtc_state *crtc_state =
16011 drm_atomic_get_crtc_state(state, crtc);
16012
16013 ret = PTR_ERR_OR_ZERO(crtc_state);
16014 if (ret)
16015 goto err;
16016
16017 /* force a restore */
16018 crtc_state->mode_changed = true;
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010016019 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020016020
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016021 for_each_intel_plane(dev, plane) {
16022 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
16023 if (ret)
16024 goto err;
16025 }
16026
16027 for_each_intel_connector(dev, conn) {
16028 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
16029 if (ret)
16030 goto err;
16031 }
16032
16033 intel_modeset_setup_hw_state(dev);
16034
16035 i915_redisable_vga(dev);
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020016036 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016037 if (!ret)
16038 return;
16039
16040err:
16041 DRM_ERROR("Restoring old state failed with %i\n", ret);
16042 drm_atomic_state_free(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010016043}
16044
16045void intel_modeset_gem_init(struct drm_device *dev)
16046{
Jesse Barnes484b41d2014-03-07 08:57:55 -080016047 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016048 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016049 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016050
Imre Deakae484342014-03-31 15:10:44 +030016051 mutex_lock(&dev->struct_mutex);
16052 intel_init_gt_powersave(dev);
16053 mutex_unlock(&dev->struct_mutex);
16054
Chris Wilson1833b132012-05-09 11:56:28 +010016055 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016056
16057 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016058
16059 /*
16060 * Make sure any fbs we allocated at startup are properly
16061 * pinned & fenced. When we do the allocation it's too early
16062 * for this.
16063 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016064 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016065 obj = intel_fb_obj(c->primary->fb);
16066 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016067 continue;
16068
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016069 mutex_lock(&dev->struct_mutex);
16070 ret = intel_pin_and_fence_fb_obj(c->primary,
16071 c->primary->fb,
Maarten Lankhorst7580d772015-08-18 13:40:06 +020016072 c->primary->state);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016073 mutex_unlock(&dev->struct_mutex);
16074 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016075 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16076 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016077 drm_framebuffer_unreference(c->primary->fb);
16078 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016079 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080016080 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016081 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016082 }
16083 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016084
16085 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016086}
16087
Imre Deak4932e2c2014-02-11 17:12:48 +020016088void intel_connector_unregister(struct intel_connector *intel_connector)
16089{
16090 struct drm_connector *connector = &intel_connector->base;
16091
16092 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010016093 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016094}
16095
Jesse Barnes79e53942008-11-07 14:24:08 -080016096void intel_modeset_cleanup(struct drm_device *dev)
16097{
Jesse Barnes652c3932009-08-17 13:31:43 -070016098 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020016099 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070016100
Imre Deak2eb52522014-11-19 15:30:05 +020016101 intel_disable_gt_powersave(dev);
16102
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016103 intel_backlight_unregister(dev);
16104
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016105 /*
16106 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016107 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016108 * experience fancy races otherwise.
16109 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016110 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016111
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016112 /*
16113 * Due to the hpd irq storm handling the hotplug work can re-arm the
16114 * poll handlers. Hence disable polling after hpd handling is shut down.
16115 */
Keith Packardf87ea762010-10-03 19:36:26 -070016116 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016117
Jesse Barnes723bfd72010-10-07 16:01:13 -070016118 intel_unregister_dsm_handler();
16119
Paulo Zanoni7733b492015-07-07 15:26:04 -030016120 intel_fbc_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016121
Chris Wilson1630fe72011-07-08 12:22:42 +010016122 /* flush any delayed tasks or pending work */
16123 flush_scheduled_work();
16124
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016125 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016126 for_each_intel_connector(dev, connector)
16127 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016128
Jesse Barnes79e53942008-11-07 14:24:08 -080016129 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016130
16131 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030016132
16133 mutex_lock(&dev->struct_mutex);
16134 intel_cleanup_gt_powersave(dev);
16135 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080016136}
16137
Dave Airlie28d52042009-09-21 14:33:58 +100016138/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016139 * Return which encoder is currently attached for connector.
16140 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016141struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016142{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016143 return &intel_attached_encoder(connector)->base;
16144}
Jesse Barnes79e53942008-11-07 14:24:08 -080016145
Chris Wilsondf0e9242010-09-09 16:20:55 +010016146void intel_connector_attach_encoder(struct intel_connector *connector,
16147 struct intel_encoder *encoder)
16148{
16149 connector->encoder = encoder;
16150 drm_mode_connector_attach_encoder(&connector->base,
16151 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016152}
Dave Airlie28d52042009-09-21 14:33:58 +100016153
16154/*
16155 * set vga decode state - true == enable VGA decode
16156 */
16157int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16158{
16159 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016160 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016161 u16 gmch_ctrl;
16162
Chris Wilson75fa0412014-02-07 18:37:02 -020016163 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16164 DRM_ERROR("failed to read control word\n");
16165 return -EIO;
16166 }
16167
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016168 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16169 return 0;
16170
Dave Airlie28d52042009-09-21 14:33:58 +100016171 if (state)
16172 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16173 else
16174 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016175
16176 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16177 DRM_ERROR("failed to write control word\n");
16178 return -EIO;
16179 }
16180
Dave Airlie28d52042009-09-21 14:33:58 +100016181 return 0;
16182}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016183
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016184struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016185
16186 u32 power_well_driver;
16187
Chris Wilson63b66e52013-08-08 15:12:06 +020016188 int num_transcoders;
16189
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016190 struct intel_cursor_error_state {
16191 u32 control;
16192 u32 position;
16193 u32 base;
16194 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016195 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016196
16197 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016198 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016199 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030016200 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016201 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016202
16203 struct intel_plane_error_state {
16204 u32 control;
16205 u32 stride;
16206 u32 size;
16207 u32 pos;
16208 u32 addr;
16209 u32 surface;
16210 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016211 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016212
16213 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016214 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016215 enum transcoder cpu_transcoder;
16216
16217 u32 conf;
16218
16219 u32 htotal;
16220 u32 hblank;
16221 u32 hsync;
16222 u32 vtotal;
16223 u32 vblank;
16224 u32 vsync;
16225 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016226};
16227
16228struct intel_display_error_state *
16229intel_display_capture_error_state(struct drm_device *dev)
16230{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016231 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016232 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016233 int transcoders[] = {
16234 TRANSCODER_A,
16235 TRANSCODER_B,
16236 TRANSCODER_C,
16237 TRANSCODER_EDP,
16238 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016239 int i;
16240
Chris Wilson63b66e52013-08-08 15:12:06 +020016241 if (INTEL_INFO(dev)->num_pipes == 0)
16242 return NULL;
16243
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016244 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016245 if (error == NULL)
16246 return NULL;
16247
Imre Deak190be112013-11-25 17:15:31 +020016248 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016249 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16250
Damien Lespiau055e3932014-08-18 13:49:10 +010016251 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016252 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016253 __intel_display_power_is_enabled(dev_priv,
16254 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016255 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016256 continue;
16257
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016258 error->cursor[i].control = I915_READ(CURCNTR(i));
16259 error->cursor[i].position = I915_READ(CURPOS(i));
16260 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016261
16262 error->plane[i].control = I915_READ(DSPCNTR(i));
16263 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016264 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016265 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016266 error->plane[i].pos = I915_READ(DSPPOS(i));
16267 }
Paulo Zanonica291362013-03-06 20:03:14 -030016268 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16269 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016270 if (INTEL_INFO(dev)->gen >= 4) {
16271 error->plane[i].surface = I915_READ(DSPSURF(i));
16272 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16273 }
16274
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016275 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016276
Sonika Jindal3abfce72014-07-21 15:23:43 +053016277 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030016278 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016279 }
16280
16281 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16282 if (HAS_DDI(dev_priv->dev))
16283 error->num_transcoders++; /* Account for eDP. */
16284
16285 for (i = 0; i < error->num_transcoders; i++) {
16286 enum transcoder cpu_transcoder = transcoders[i];
16287
Imre Deakddf9c532013-11-27 22:02:02 +020016288 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016289 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016290 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016291 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016292 continue;
16293
Chris Wilson63b66e52013-08-08 15:12:06 +020016294 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16295
16296 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16297 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16298 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16299 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16300 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16301 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16302 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016303 }
16304
16305 return error;
16306}
16307
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016308#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16309
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016310void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016311intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016312 struct drm_device *dev,
16313 struct intel_display_error_state *error)
16314{
Damien Lespiau055e3932014-08-18 13:49:10 +010016315 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016316 int i;
16317
Chris Wilson63b66e52013-08-08 15:12:06 +020016318 if (!error)
16319 return;
16320
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016321 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016322 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016323 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016324 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016325 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016326 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016327 err_printf(m, " Power: %s\n",
16328 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016329 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016330 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016331
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016332 err_printf(m, "Plane [%d]:\n", i);
16333 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16334 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016335 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016336 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16337 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016338 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016339 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016340 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016341 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016342 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16343 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016344 }
16345
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016346 err_printf(m, "Cursor [%d]:\n", i);
16347 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16348 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16349 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016350 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016351
16352 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010016353 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016354 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016355 err_printf(m, " Power: %s\n",
16356 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020016357 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16358 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16359 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16360 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16361 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16362 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16363 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16364 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016365}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016366
16367void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
16368{
16369 struct intel_crtc *crtc;
16370
16371 for_each_intel_crtc(dev, crtc) {
16372 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016373
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020016374 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016375
16376 work = crtc->unpin_work;
16377
16378 if (work && work->event &&
16379 work->event->base.file_priv == file) {
16380 kfree(work->event);
16381 work->event = NULL;
16382 }
16383
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020016384 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016385 }
16386}