blob: 124342bdbe903f0437df7483ef38f37aa35a5c9a [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 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200227intel_fdi_link_freq(struct drm_i915_private *dev_priv,
228 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100229{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200230 if (HAS_DDI(dev_priv))
231 return pipe_config->port_clock; /* SPLL */
232 else if (IS_GEN5(dev_priv))
233 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200234 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200235 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100236}
237
Daniel Vetter5d536e22013-07-06 12:52:06 +0200238static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400239 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200240 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200241 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400242 .m = { .min = 96, .max = 140 },
243 .m1 = { .min = 18, .max = 26 },
244 .m2 = { .min = 6, .max = 16 },
245 .p = { .min = 4, .max = 128 },
246 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700247 .p2 = { .dot_limit = 165000,
248 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700249};
250
Daniel Vetter5d536e22013-07-06 12:52:06 +0200251static const intel_limit_t intel_limits_i8xx_dvo = {
252 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200253 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200254 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200255 .m = { .min = 96, .max = 140 },
256 .m1 = { .min = 18, .max = 26 },
257 .m2 = { .min = 6, .max = 16 },
258 .p = { .min = 4, .max = 128 },
259 .p1 = { .min = 2, .max = 33 },
260 .p2 = { .dot_limit = 165000,
261 .p2_slow = 4, .p2_fast = 4 },
262};
263
Keith Packarde4b36692009-06-05 19:22:17 -0700264static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400265 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200266 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200267 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400268 .m = { .min = 96, .max = 140 },
269 .m1 = { .min = 18, .max = 26 },
270 .m2 = { .min = 6, .max = 16 },
271 .p = { .min = 4, .max = 128 },
272 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700273 .p2 = { .dot_limit = 165000,
274 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700275};
Eric Anholt273e27c2011-03-30 13:01:10 -0700276
Keith Packarde4b36692009-06-05 19:22:17 -0700277static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400278 .dot = { .min = 20000, .max = 400000 },
279 .vco = { .min = 1400000, .max = 2800000 },
280 .n = { .min = 1, .max = 6 },
281 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100282 .m1 = { .min = 8, .max = 18 },
283 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400284 .p = { .min = 5, .max = 80 },
285 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700286 .p2 = { .dot_limit = 200000,
287 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700288};
289
290static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400291 .dot = { .min = 20000, .max = 400000 },
292 .vco = { .min = 1400000, .max = 2800000 },
293 .n = { .min = 1, .max = 6 },
294 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100295 .m1 = { .min = 8, .max = 18 },
296 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400297 .p = { .min = 7, .max = 98 },
298 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700299 .p2 = { .dot_limit = 112000,
300 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700301};
302
Eric Anholt273e27c2011-03-30 13:01:10 -0700303
Keith Packarde4b36692009-06-05 19:22:17 -0700304static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700305 .dot = { .min = 25000, .max = 270000 },
306 .vco = { .min = 1750000, .max = 3500000},
307 .n = { .min = 1, .max = 4 },
308 .m = { .min = 104, .max = 138 },
309 .m1 = { .min = 17, .max = 23 },
310 .m2 = { .min = 5, .max = 11 },
311 .p = { .min = 10, .max = 30 },
312 .p1 = { .min = 1, .max = 3},
313 .p2 = { .dot_limit = 270000,
314 .p2_slow = 10,
315 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800316 },
Keith Packarde4b36692009-06-05 19:22:17 -0700317};
318
319static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700320 .dot = { .min = 22000, .max = 400000 },
321 .vco = { .min = 1750000, .max = 3500000},
322 .n = { .min = 1, .max = 4 },
323 .m = { .min = 104, .max = 138 },
324 .m1 = { .min = 16, .max = 23 },
325 .m2 = { .min = 5, .max = 11 },
326 .p = { .min = 5, .max = 80 },
327 .p1 = { .min = 1, .max = 8},
328 .p2 = { .dot_limit = 165000,
329 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700330};
331
332static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700333 .dot = { .min = 20000, .max = 115000 },
334 .vco = { .min = 1750000, .max = 3500000 },
335 .n = { .min = 1, .max = 3 },
336 .m = { .min = 104, .max = 138 },
337 .m1 = { .min = 17, .max = 23 },
338 .m2 = { .min = 5, .max = 11 },
339 .p = { .min = 28, .max = 112 },
340 .p1 = { .min = 2, .max = 8 },
341 .p2 = { .dot_limit = 0,
342 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800343 },
Keith Packarde4b36692009-06-05 19:22:17 -0700344};
345
346static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700347 .dot = { .min = 80000, .max = 224000 },
348 .vco = { .min = 1750000, .max = 3500000 },
349 .n = { .min = 1, .max = 3 },
350 .m = { .min = 104, .max = 138 },
351 .m1 = { .min = 17, .max = 23 },
352 .m2 = { .min = 5, .max = 11 },
353 .p = { .min = 14, .max = 42 },
354 .p1 = { .min = 2, .max = 6 },
355 .p2 = { .dot_limit = 0,
356 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800357 },
Keith Packarde4b36692009-06-05 19:22:17 -0700358};
359
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500360static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400361 .dot = { .min = 20000, .max = 400000},
362 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700363 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400364 .n = { .min = 3, .max = 6 },
365 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700366 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400367 .m1 = { .min = 0, .max = 0 },
368 .m2 = { .min = 0, .max = 254 },
369 .p = { .min = 5, .max = 80 },
370 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700371 .p2 = { .dot_limit = 200000,
372 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700373};
374
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500375static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400376 .dot = { .min = 20000, .max = 400000 },
377 .vco = { .min = 1700000, .max = 3500000 },
378 .n = { .min = 3, .max = 6 },
379 .m = { .min = 2, .max = 256 },
380 .m1 = { .min = 0, .max = 0 },
381 .m2 = { .min = 0, .max = 254 },
382 .p = { .min = 7, .max = 112 },
383 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700384 .p2 = { .dot_limit = 112000,
385 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700386};
387
Eric Anholt273e27c2011-03-30 13:01:10 -0700388/* Ironlake / Sandybridge
389 *
390 * We calculate clock using (register_value + 2) for N/M1/M2, so here
391 * the range value for them is (actual_value - 2).
392 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800393static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700394 .dot = { .min = 25000, .max = 350000 },
395 .vco = { .min = 1760000, .max = 3510000 },
396 .n = { .min = 1, .max = 5 },
397 .m = { .min = 79, .max = 127 },
398 .m1 = { .min = 12, .max = 22 },
399 .m2 = { .min = 5, .max = 9 },
400 .p = { .min = 5, .max = 80 },
401 .p1 = { .min = 1, .max = 8 },
402 .p2 = { .dot_limit = 225000,
403 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700404};
405
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800406static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700407 .dot = { .min = 25000, .max = 350000 },
408 .vco = { .min = 1760000, .max = 3510000 },
409 .n = { .min = 1, .max = 3 },
410 .m = { .min = 79, .max = 118 },
411 .m1 = { .min = 12, .max = 22 },
412 .m2 = { .min = 5, .max = 9 },
413 .p = { .min = 28, .max = 112 },
414 .p1 = { .min = 2, .max = 8 },
415 .p2 = { .dot_limit = 225000,
416 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800417};
418
419static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700420 .dot = { .min = 25000, .max = 350000 },
421 .vco = { .min = 1760000, .max = 3510000 },
422 .n = { .min = 1, .max = 3 },
423 .m = { .min = 79, .max = 127 },
424 .m1 = { .min = 12, .max = 22 },
425 .m2 = { .min = 5, .max = 9 },
426 .p = { .min = 14, .max = 56 },
427 .p1 = { .min = 2, .max = 8 },
428 .p2 = { .dot_limit = 225000,
429 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800430};
431
Eric Anholt273e27c2011-03-30 13:01:10 -0700432/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800433static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700434 .dot = { .min = 25000, .max = 350000 },
435 .vco = { .min = 1760000, .max = 3510000 },
436 .n = { .min = 1, .max = 2 },
437 .m = { .min = 79, .max = 126 },
438 .m1 = { .min = 12, .max = 22 },
439 .m2 = { .min = 5, .max = 9 },
440 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400441 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700442 .p2 = { .dot_limit = 225000,
443 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800444};
445
446static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700447 .dot = { .min = 25000, .max = 350000 },
448 .vco = { .min = 1760000, .max = 3510000 },
449 .n = { .min = 1, .max = 3 },
450 .m = { .min = 79, .max = 126 },
451 .m1 = { .min = 12, .max = 22 },
452 .m2 = { .min = 5, .max = 9 },
453 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400454 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700455 .p2 = { .dot_limit = 225000,
456 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800457};
458
Ville Syrjälädc730512013-09-24 21:26:30 +0300459static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300460 /*
461 * These are the data rate limits (measured in fast clocks)
462 * since those are the strictest limits we have. The fast
463 * clock and actual rate limits are more relaxed, so checking
464 * them would make no difference.
465 */
466 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200467 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700468 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700469 .m1 = { .min = 2, .max = 3 },
470 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300471 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300472 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700473};
474
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300475static const intel_limit_t intel_limits_chv = {
476 /*
477 * These are the data rate limits (measured in fast clocks)
478 * since those are the strictest limits we have. The fast
479 * clock and actual rate limits are more relaxed, so checking
480 * them would make no difference.
481 */
482 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200483 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300484 .n = { .min = 1, .max = 1 },
485 .m1 = { .min = 2, .max = 2 },
486 .m2 = { .min = 24 << 22, .max = 175 << 22 },
487 .p1 = { .min = 2, .max = 4 },
488 .p2 = { .p2_slow = 1, .p2_fast = 14 },
489};
490
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200491static const intel_limit_t intel_limits_bxt = {
492 /* FIXME: find real dot limits */
493 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530494 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200495 .n = { .min = 1, .max = 1 },
496 .m1 = { .min = 2, .max = 2 },
497 /* FIXME: find real m2 limits */
498 .m2 = { .min = 2 << 22, .max = 255 << 22 },
499 .p1 = { .min = 2, .max = 4 },
500 .p2 = { .p2_slow = 1, .p2_fast = 20 },
501};
502
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200503static bool
504needs_modeset(struct drm_crtc_state *state)
505{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200506 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200507}
508
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300509/**
510 * Returns whether any output on the specified pipe is of the specified type
511 */
Damien Lespiau40935612014-10-29 11:16:59 +0000512bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300513{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300514 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300515 struct intel_encoder *encoder;
516
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300517 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300518 if (encoder->type == type)
519 return true;
520
521 return false;
522}
523
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200524/**
525 * Returns whether any output on the specified pipe will have the specified
526 * type after a staged modeset is complete, i.e., the same as
527 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
528 * encoder->crtc.
529 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200530static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
531 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200532{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200533 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300534 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200535 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200536 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200537 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200538
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300539 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200540 if (connector_state->crtc != crtc_state->base.crtc)
541 continue;
542
543 num_connectors++;
544
545 encoder = to_intel_encoder(connector_state->best_encoder);
546 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200547 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200548 }
549
550 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200551
552 return false;
553}
554
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200555static const intel_limit_t *
556intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800557{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200558 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800559 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800560
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200561 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100562 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000563 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800564 limit = &intel_limits_ironlake_dual_lvds_100m;
565 else
566 limit = &intel_limits_ironlake_dual_lvds;
567 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000568 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800569 limit = &intel_limits_ironlake_single_lvds_100m;
570 else
571 limit = &intel_limits_ironlake_single_lvds;
572 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200573 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800574 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800575
576 return limit;
577}
578
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200579static const intel_limit_t *
580intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800581{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200582 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800583 const intel_limit_t *limit;
584
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200585 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100586 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700587 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800588 else
Keith Packarde4b36692009-06-05 19:22:17 -0700589 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200590 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
591 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700592 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200593 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700594 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800595 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700596 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800597
598 return limit;
599}
600
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200601static const intel_limit_t *
602intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800603{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200604 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800605 const intel_limit_t *limit;
606
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200607 if (IS_BROXTON(dev))
608 limit = &intel_limits_bxt;
609 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200610 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800611 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200612 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500613 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200614 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500615 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800616 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500617 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300618 } else if (IS_CHERRYVIEW(dev)) {
619 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700620 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300621 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100622 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200623 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100624 limit = &intel_limits_i9xx_lvds;
625 else
626 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800627 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200628 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700629 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200630 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700631 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200632 else
633 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800634 }
635 return limit;
636}
637
Imre Deakdccbea32015-06-22 23:35:51 +0300638/*
639 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
640 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
641 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
642 * The helpers' return value is the rate of the clock that is fed to the
643 * display engine's pipe which can be the above fast dot clock rate or a
644 * divided-down version of it.
645 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500646/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300647static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800648{
Shaohua Li21778322009-02-23 15:19:16 +0800649 clock->m = clock->m2 + 2;
650 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200651 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300652 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300653 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
654 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300655
656 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800657}
658
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200659static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
660{
661 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
662}
663
Imre Deakdccbea32015-06-22 23:35:51 +0300664static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800665{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200666 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800667 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200668 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300669 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300670 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
671 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300672
673 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800674}
675
Imre Deakdccbea32015-06-22 23:35:51 +0300676static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300677{
678 clock->m = clock->m1 * clock->m2;
679 clock->p = clock->p1 * clock->p2;
680 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300681 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300682 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
683 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300684
685 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300686}
687
Imre Deakdccbea32015-06-22 23:35:51 +0300688int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300689{
690 clock->m = clock->m1 * clock->m2;
691 clock->p = clock->p1 * clock->p2;
692 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300693 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300694 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
695 clock->n << 22);
696 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300697
698 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300699}
700
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800701#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800702/**
703 * Returns whether the given set of divisors are valid for a given refclk with
704 * the given connectors.
705 */
706
Chris Wilson1b894b52010-12-14 20:04:54 +0000707static bool intel_PLL_is_valid(struct drm_device *dev,
708 const intel_limit_t *limit,
709 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800710{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300711 if (clock->n < limit->n.min || limit->n.max < clock->n)
712 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800713 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400714 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800715 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400716 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800717 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400718 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300719
Wayne Boyer666a4532015-12-09 12:29:35 -0800720 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
721 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300722 if (clock->m1 <= clock->m2)
723 INTELPllInvalid("m1 <= m2\n");
724
Wayne Boyer666a4532015-12-09 12:29:35 -0800725 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300726 if (clock->p < limit->p.min || limit->p.max < clock->p)
727 INTELPllInvalid("p out of range\n");
728 if (clock->m < limit->m.min || limit->m.max < clock->m)
729 INTELPllInvalid("m out of range\n");
730 }
731
Jesse Barnes79e53942008-11-07 14:24:08 -0800732 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400733 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800734 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
735 * connector, etc., rather than just a single range.
736 */
737 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400738 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800739
740 return true;
741}
742
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300743static int
744i9xx_select_p2_div(const intel_limit_t *limit,
745 const struct intel_crtc_state *crtc_state,
746 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800747{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300748 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800749
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200750 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800751 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100752 * For LVDS just rely on its current settings for dual-channel.
753 * We haven't figured out how to reliably set up different
754 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800755 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100756 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300757 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800758 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300759 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800760 } else {
761 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300762 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800763 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300764 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800765 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300766}
767
768static bool
769i9xx_find_best_dpll(const intel_limit_t *limit,
770 struct intel_crtc_state *crtc_state,
771 int target, int refclk, intel_clock_t *match_clock,
772 intel_clock_t *best_clock)
773{
774 struct drm_device *dev = crtc_state->base.crtc->dev;
775 intel_clock_t clock;
776 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800777
Akshay Joshi0206e352011-08-16 15:34:10 -0400778 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800779
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300780 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
781
Zhao Yakui42158662009-11-20 11:24:18 +0800782 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
783 clock.m1++) {
784 for (clock.m2 = limit->m2.min;
785 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200786 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800787 break;
788 for (clock.n = limit->n.min;
789 clock.n <= limit->n.max; clock.n++) {
790 for (clock.p1 = limit->p1.min;
791 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800792 int this_err;
793
Imre Deakdccbea32015-06-22 23:35:51 +0300794 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000795 if (!intel_PLL_is_valid(dev, limit,
796 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800797 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800798 if (match_clock &&
799 clock.p != match_clock->p)
800 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800801
802 this_err = abs(clock.dot - target);
803 if (this_err < err) {
804 *best_clock = clock;
805 err = this_err;
806 }
807 }
808 }
809 }
810 }
811
812 return (err != target);
813}
814
Ma Lingd4906092009-03-18 20:13:27 +0800815static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200816pnv_find_best_dpll(const intel_limit_t *limit,
817 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200818 int target, int refclk, intel_clock_t *match_clock,
819 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200820{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300821 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200822 intel_clock_t clock;
823 int err = target;
824
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200825 memset(best_clock, 0, sizeof(*best_clock));
826
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300827 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
828
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200829 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
830 clock.m1++) {
831 for (clock.m2 = limit->m2.min;
832 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200833 for (clock.n = limit->n.min;
834 clock.n <= limit->n.max; clock.n++) {
835 for (clock.p1 = limit->p1.min;
836 clock.p1 <= limit->p1.max; clock.p1++) {
837 int this_err;
838
Imre Deakdccbea32015-06-22 23:35:51 +0300839 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800840 if (!intel_PLL_is_valid(dev, limit,
841 &clock))
842 continue;
843 if (match_clock &&
844 clock.p != match_clock->p)
845 continue;
846
847 this_err = abs(clock.dot - target);
848 if (this_err < err) {
849 *best_clock = clock;
850 err = this_err;
851 }
852 }
853 }
854 }
855 }
856
857 return (err != target);
858}
859
Ma Lingd4906092009-03-18 20:13:27 +0800860static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200861g4x_find_best_dpll(const intel_limit_t *limit,
862 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200863 int target, int refclk, intel_clock_t *match_clock,
864 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800865{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300866 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800867 intel_clock_t clock;
868 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300869 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400870 /* approximately equals target * 0.00585 */
871 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800872
873 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300874
875 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
876
Ma Lingd4906092009-03-18 20:13:27 +0800877 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200878 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800879 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200880 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800881 for (clock.m1 = limit->m1.max;
882 clock.m1 >= limit->m1.min; clock.m1--) {
883 for (clock.m2 = limit->m2.max;
884 clock.m2 >= limit->m2.min; clock.m2--) {
885 for (clock.p1 = limit->p1.max;
886 clock.p1 >= limit->p1.min; clock.p1--) {
887 int this_err;
888
Imre Deakdccbea32015-06-22 23:35:51 +0300889 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000890 if (!intel_PLL_is_valid(dev, limit,
891 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800892 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000893
894 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800895 if (this_err < err_most) {
896 *best_clock = clock;
897 err_most = this_err;
898 max_n = clock.n;
899 found = true;
900 }
901 }
902 }
903 }
904 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800905 return found;
906}
Ma Lingd4906092009-03-18 20:13:27 +0800907
Imre Deakd5dd62b2015-03-17 11:40:03 +0200908/*
909 * Check if the calculated PLL configuration is more optimal compared to the
910 * best configuration and error found so far. Return the calculated error.
911 */
912static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
913 const intel_clock_t *calculated_clock,
914 const intel_clock_t *best_clock,
915 unsigned int best_error_ppm,
916 unsigned int *error_ppm)
917{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200918 /*
919 * For CHV ignore the error and consider only the P value.
920 * Prefer a bigger P value based on HW requirements.
921 */
922 if (IS_CHERRYVIEW(dev)) {
923 *error_ppm = 0;
924
925 return calculated_clock->p > best_clock->p;
926 }
927
Imre Deak24be4e42015-03-17 11:40:04 +0200928 if (WARN_ON_ONCE(!target_freq))
929 return false;
930
Imre Deakd5dd62b2015-03-17 11:40:03 +0200931 *error_ppm = div_u64(1000000ULL *
932 abs(target_freq - calculated_clock->dot),
933 target_freq);
934 /*
935 * Prefer a better P value over a better (smaller) error if the error
936 * is small. Ensure this preference for future configurations too by
937 * setting the error to 0.
938 */
939 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
940 *error_ppm = 0;
941
942 return true;
943 }
944
945 return *error_ppm + 10 < best_error_ppm;
946}
947
Zhenyu Wang2c072452009-06-05 15:38:42 +0800948static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200949vlv_find_best_dpll(const intel_limit_t *limit,
950 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200951 int target, int refclk, intel_clock_t *match_clock,
952 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700953{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200954 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300955 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300956 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300957 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300958 /* min update 19.2 MHz */
959 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300960 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700961
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300962 target *= 5; /* fast clock */
963
964 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700965
966 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300967 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300968 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300969 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300970 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300971 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700972 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300973 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200974 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300975
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300976 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
977 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300978
Imre Deakdccbea32015-06-22 23:35:51 +0300979 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300980
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300981 if (!intel_PLL_is_valid(dev, limit,
982 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300983 continue;
984
Imre Deakd5dd62b2015-03-17 11:40:03 +0200985 if (!vlv_PLL_is_optimal(dev, target,
986 &clock,
987 best_clock,
988 bestppm, &ppm))
989 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300990
Imre Deakd5dd62b2015-03-17 11:40:03 +0200991 *best_clock = clock;
992 bestppm = ppm;
993 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700994 }
995 }
996 }
997 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700998
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300999 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07001000}
Keith Packarda4fc5ed2009-04-07 16:16:42 -07001001
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001002static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02001003chv_find_best_dpll(const intel_limit_t *limit,
1004 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001005 int target, int refclk, intel_clock_t *match_clock,
1006 intel_clock_t *best_clock)
1007{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02001008 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03001009 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +02001010 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001011 intel_clock_t clock;
1012 uint64_t m2;
1013 int found = false;
1014
1015 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +02001016 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001017
1018 /*
1019 * Based on hardware doc, the n always set to 1, and m1 always
1020 * set to 2. If requires to support 200Mhz refclk, we need to
1021 * revisit this because n may not 1 anymore.
1022 */
1023 clock.n = 1, clock.m1 = 2;
1024 target *= 5; /* fast clock */
1025
1026 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1027 for (clock.p2 = limit->p2.p2_fast;
1028 clock.p2 >= limit->p2.p2_slow;
1029 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001030 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001031
1032 clock.p = clock.p1 * clock.p2;
1033
1034 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1035 clock.n) << 22, refclk * clock.m1);
1036
1037 if (m2 > INT_MAX/clock.m1)
1038 continue;
1039
1040 clock.m2 = m2;
1041
Imre Deakdccbea32015-06-22 23:35:51 +03001042 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001043
1044 if (!intel_PLL_is_valid(dev, limit, &clock))
1045 continue;
1046
Imre Deak9ca3ba02015-03-17 11:40:05 +02001047 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1048 best_error_ppm, &error_ppm))
1049 continue;
1050
1051 *best_clock = clock;
1052 best_error_ppm = error_ppm;
1053 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001054 }
1055 }
1056
1057 return found;
1058}
1059
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001060bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1061 intel_clock_t *best_clock)
1062{
1063 int refclk = i9xx_get_refclk(crtc_state, 0);
1064
1065 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
1066 target_clock, refclk, NULL, best_clock);
1067}
1068
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001069bool intel_crtc_active(struct drm_crtc *crtc)
1070{
1071 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1072
1073 /* Be paranoid as we can arrive here with only partial
1074 * state retrieved from the hardware during setup.
1075 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001076 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001077 * as Haswell has gained clock readout/fastboot support.
1078 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001079 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001080 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001081 *
1082 * FIXME: The intel_crtc->active here should be switched to
1083 * crtc->state->active once we have proper CRTC states wired up
1084 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001085 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001086 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001087 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001088}
1089
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001090enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1091 enum pipe pipe)
1092{
1093 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1095
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001096 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001097}
1098
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001099static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1100{
1101 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001102 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001103 u32 line1, line2;
1104 u32 line_mask;
1105
1106 if (IS_GEN2(dev))
1107 line_mask = DSL_LINEMASK_GEN2;
1108 else
1109 line_mask = DSL_LINEMASK_GEN3;
1110
1111 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001112 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001113 line2 = I915_READ(reg) & line_mask;
1114
1115 return line1 == line2;
1116}
1117
Keith Packardab7ad7f2010-10-03 00:33:06 -07001118/*
1119 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001120 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001121 *
1122 * After disabling a pipe, we can't wait for vblank in the usual way,
1123 * spinning on the vblank interrupt status bit, since we won't actually
1124 * see an interrupt when the pipe is disabled.
1125 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001126 * On Gen4 and above:
1127 * wait for the pipe register state bit to turn off
1128 *
1129 * Otherwise:
1130 * wait for the display line value to settle (it usually
1131 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001132 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001133 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001134static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001135{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001136 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001137 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001138 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001139 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001140
Keith Packardab7ad7f2010-10-03 00:33:06 -07001141 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001142 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001143
Keith Packardab7ad7f2010-10-03 00:33:06 -07001144 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001145 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1146 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001147 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001148 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001149 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001150 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001151 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001152 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001153}
1154
Jesse Barnesb24e7172011-01-04 15:09:30 -08001155/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001156void assert_pll(struct drm_i915_private *dev_priv,
1157 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001158{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001159 u32 val;
1160 bool cur_state;
1161
Ville Syrjälä649636e2015-09-22 19:50:01 +03001162 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001163 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001164 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001165 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001166 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001167}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001168
Jani Nikula23538ef2013-08-27 15:12:22 +03001169/* XXX: the dsi pll is shared between MIPI DSI ports */
1170static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1171{
1172 u32 val;
1173 bool cur_state;
1174
Ville Syrjäläa5805162015-05-26 20:42:30 +03001175 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001176 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001177 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001178
1179 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001180 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001181 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001182 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001183}
1184#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1185#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1186
Daniel Vetter55607e82013-06-16 21:42:39 +02001187struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001188intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001189{
Daniel Vettere2b78262013-06-07 23:10:03 +02001190 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1191
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001192 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001193 return NULL;
1194
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001195 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001196}
1197
Jesse Barnesb24e7172011-01-04 15:09:30 -08001198/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001199void assert_shared_dpll(struct drm_i915_private *dev_priv,
1200 struct intel_shared_dpll *pll,
1201 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001202{
Jesse Barnes040484a2011-01-03 12:14:26 -08001203 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001204 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001205
Jani Nikula87ad3212016-01-14 12:53:34 +02001206 if (WARN(!pll, "asserting DPLL %s with no DPLL\n", onoff(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001207 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001208
Daniel Vetter53589012013-06-05 13:34:16 +02001209 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001210 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001211 "%s assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001212 pll->name, onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001213}
Jesse Barnes040484a2011-01-03 12:14:26 -08001214
1215static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1216 enum pipe pipe, bool state)
1217{
Jesse Barnes040484a2011-01-03 12:14:26 -08001218 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001219 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1220 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001221
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001222 if (HAS_DDI(dev_priv->dev)) {
1223 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001224 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001225 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001226 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001227 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001228 cur_state = !!(val & FDI_TX_ENABLE);
1229 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001230 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001231 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001232 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001233}
1234#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1235#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1236
1237static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1238 enum pipe pipe, bool state)
1239{
Jesse Barnes040484a2011-01-03 12:14:26 -08001240 u32 val;
1241 bool cur_state;
1242
Ville Syrjälä649636e2015-09-22 19:50:01 +03001243 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001244 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001245 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001246 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001247 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001248}
1249#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1250#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1251
1252static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1253 enum pipe pipe)
1254{
Jesse Barnes040484a2011-01-03 12:14:26 -08001255 u32 val;
1256
1257 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001258 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001259 return;
1260
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001261 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001262 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001263 return;
1264
Ville Syrjälä649636e2015-09-22 19:50:01 +03001265 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001266 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 -08001267}
1268
Daniel Vetter55607e82013-06-16 21:42:39 +02001269void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1270 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001271{
Jesse Barnes040484a2011-01-03 12:14:26 -08001272 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001273 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001274
Ville Syrjälä649636e2015-09-22 19:50:01 +03001275 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001276 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001277 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001278 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001279 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001280}
1281
Daniel Vetterb680c372014-09-19 18:27:27 +02001282void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1283 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001284{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001285 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001286 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001287 u32 val;
1288 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001289 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001290
Jani Nikulabedd4db2014-08-22 15:04:13 +03001291 if (WARN_ON(HAS_DDI(dev)))
1292 return;
1293
1294 if (HAS_PCH_SPLIT(dev)) {
1295 u32 port_sel;
1296
Jesse Barnesea0760c2011-01-04 15:09:32 -08001297 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001298 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1299
1300 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1301 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1302 panel_pipe = PIPE_B;
1303 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001304 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001305 /* presumably write lock depends on pipe, not port select */
1306 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1307 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001308 } else {
1309 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001310 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1311 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001312 }
1313
1314 val = I915_READ(pp_reg);
1315 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001316 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001317 locked = false;
1318
Rob Clarke2c719b2014-12-15 13:56:32 -05001319 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001320 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001321 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001322}
1323
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001324static void assert_cursor(struct drm_i915_private *dev_priv,
1325 enum pipe pipe, bool state)
1326{
1327 struct drm_device *dev = dev_priv->dev;
1328 bool cur_state;
1329
Paulo Zanonid9d82082014-02-27 16:30:56 -03001330 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001331 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001332 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001333 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001334
Rob Clarke2c719b2014-12-15 13:56:32 -05001335 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001336 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001337 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001338}
1339#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1340#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1341
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001342void assert_pipe(struct drm_i915_private *dev_priv,
1343 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001344{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001345 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001346 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1347 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001348 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001349
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001350 /* if we need the pipe quirk it must be always on */
1351 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1352 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001353 state = true;
1354
Imre Deak4feed0e2016-02-12 18:55:14 +02001355 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1356 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001357 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001358 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001359
1360 intel_display_power_put(dev_priv, power_domain);
1361 } else {
1362 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001363 }
1364
Rob Clarke2c719b2014-12-15 13:56:32 -05001365 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001366 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001367 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001368}
1369
Chris Wilson931872f2012-01-16 23:01:13 +00001370static void assert_plane(struct drm_i915_private *dev_priv,
1371 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001372{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001373 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001374 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001375
Ville Syrjälä649636e2015-09-22 19:50:01 +03001376 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001377 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001378 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001379 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001380 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001381}
1382
Chris Wilson931872f2012-01-16 23:01:13 +00001383#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1384#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1385
Jesse Barnesb24e7172011-01-04 15:09:30 -08001386static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1387 enum pipe pipe)
1388{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001389 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001390 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001391
Ville Syrjälä653e1022013-06-04 13:49:05 +03001392 /* Primary planes are fixed to pipes on gen4+ */
1393 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001394 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001395 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001396 "plane %c assertion failure, should be disabled but not\n",
1397 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001398 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001399 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001400
Jesse Barnesb24e7172011-01-04 15:09:30 -08001401 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001402 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001403 u32 val = I915_READ(DSPCNTR(i));
1404 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001405 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001406 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001407 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1408 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001409 }
1410}
1411
Jesse Barnes19332d72013-03-28 09:55:38 -07001412static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1413 enum pipe pipe)
1414{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001415 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001416 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001417
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001418 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001419 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001420 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001421 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001422 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1423 sprite, pipe_name(pipe));
1424 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001425 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001426 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001427 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001428 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001429 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001430 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001431 }
1432 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001433 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001434 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001435 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001436 plane_name(pipe), pipe_name(pipe));
1437 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001438 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001439 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001440 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1441 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001442 }
1443}
1444
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001445static void assert_vblank_disabled(struct drm_crtc *crtc)
1446{
Rob Clarke2c719b2014-12-15 13:56:32 -05001447 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001448 drm_crtc_vblank_put(crtc);
1449}
1450
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001451static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001452{
1453 u32 val;
1454 bool enabled;
1455
Rob Clarke2c719b2014-12-15 13:56:32 -05001456 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001457
Jesse Barnes92f25842011-01-04 15:09:34 -08001458 val = I915_READ(PCH_DREF_CONTROL);
1459 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1460 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001461 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001462}
1463
Daniel Vetterab9412b2013-05-03 11:49:46 +02001464static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1465 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001466{
Jesse Barnes92f25842011-01-04 15:09:34 -08001467 u32 val;
1468 bool enabled;
1469
Ville Syrjälä649636e2015-09-22 19:50:01 +03001470 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001471 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001472 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001473 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1474 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001475}
1476
Keith Packard4e634382011-08-06 10:39:45 -07001477static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1478 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001479{
1480 if ((val & DP_PORT_EN) == 0)
1481 return false;
1482
1483 if (HAS_PCH_CPT(dev_priv->dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001484 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001485 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1486 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001487 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1488 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1489 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001490 } else {
1491 if ((val & DP_PIPE_MASK) != (pipe << 30))
1492 return false;
1493 }
1494 return true;
1495}
1496
Keith Packard1519b992011-08-06 10:35:34 -07001497static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1498 enum pipe pipe, u32 val)
1499{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001500 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001501 return false;
1502
1503 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001504 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001505 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001506 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1507 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1508 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001509 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001510 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001511 return false;
1512 }
1513 return true;
1514}
1515
1516static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1517 enum pipe pipe, u32 val)
1518{
1519 if ((val & LVDS_PORT_EN) == 0)
1520 return false;
1521
1522 if (HAS_PCH_CPT(dev_priv->dev)) {
1523 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1524 return false;
1525 } else {
1526 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1527 return false;
1528 }
1529 return true;
1530}
1531
1532static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1533 enum pipe pipe, u32 val)
1534{
1535 if ((val & ADPA_DAC_ENABLE) == 0)
1536 return false;
1537 if (HAS_PCH_CPT(dev_priv->dev)) {
1538 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1539 return false;
1540 } else {
1541 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1542 return false;
1543 }
1544 return true;
1545}
1546
Jesse Barnes291906f2011-02-02 12:28:03 -08001547static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001548 enum pipe pipe, i915_reg_t reg,
1549 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001550{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001551 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001552 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001553 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001554 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001555
Rob Clarke2c719b2014-12-15 13:56:32 -05001556 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001557 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001558 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001559}
1560
1561static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001562 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001563{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001564 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001565 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001566 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001567 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001568
Rob Clarke2c719b2014-12-15 13:56:32 -05001569 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001570 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001571 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001572}
1573
1574static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1575 enum pipe pipe)
1576{
Jesse Barnes291906f2011-02-02 12:28:03 -08001577 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001578
Keith Packardf0575e92011-07-25 22:12:43 -07001579 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1580 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1581 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001582
Ville Syrjälä649636e2015-09-22 19:50:01 +03001583 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001584 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001585 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001586 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001587
Ville Syrjälä649636e2015-09-22 19:50:01 +03001588 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001589 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001590 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001591 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001592
Paulo Zanonie2debe92013-02-18 19:00:27 -03001593 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1594 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1595 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001596}
1597
Ville Syrjäläd288f652014-10-28 13:20:22 +02001598static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001599 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001600{
Daniel Vetter426115c2013-07-11 22:13:42 +02001601 struct drm_device *dev = crtc->base.dev;
1602 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001603 i915_reg_t reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001604 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001605
Daniel Vetter426115c2013-07-11 22:13:42 +02001606 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001607
Daniel Vetter87442f72013-06-06 00:52:17 +02001608 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001609 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001610 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001611
Daniel Vetter426115c2013-07-11 22:13:42 +02001612 I915_WRITE(reg, dpll);
1613 POSTING_READ(reg);
1614 udelay(150);
1615
1616 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1617 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1618
Ville Syrjäläd288f652014-10-28 13:20:22 +02001619 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001620 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001621
1622 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001623 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001624 POSTING_READ(reg);
1625 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001626 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001627 POSTING_READ(reg);
1628 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001629 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001630 POSTING_READ(reg);
1631 udelay(150); /* wait for warmup */
1632}
1633
Ville Syrjäläd288f652014-10-28 13:20:22 +02001634static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001635 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001636{
1637 struct drm_device *dev = crtc->base.dev;
1638 struct drm_i915_private *dev_priv = dev->dev_private;
1639 int pipe = crtc->pipe;
1640 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001641 u32 tmp;
1642
1643 assert_pipe_disabled(dev_priv, crtc->pipe);
1644
Ville Syrjäläa5805162015-05-26 20:42:30 +03001645 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001646
1647 /* Enable back the 10bit clock to display controller */
1648 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1649 tmp |= DPIO_DCLKP_EN;
1650 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1651
Ville Syrjälä54433e92015-05-26 20:42:31 +03001652 mutex_unlock(&dev_priv->sb_lock);
1653
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001654 /*
1655 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1656 */
1657 udelay(1);
1658
1659 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001660 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001661
1662 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001663 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001664 DRM_ERROR("PLL %d failed to lock\n", pipe);
1665
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001666 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001667 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001668 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001669}
1670
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001671static int intel_num_dvo_pipes(struct drm_device *dev)
1672{
1673 struct intel_crtc *crtc;
1674 int count = 0;
1675
1676 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001677 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001678 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001679
1680 return count;
1681}
1682
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001683static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001684{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001685 struct drm_device *dev = crtc->base.dev;
1686 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001687 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001688 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001689
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001690 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001691
1692 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001693 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001694
1695 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001696 if (IS_MOBILE(dev) && !IS_I830(dev))
1697 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001698
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001699 /* Enable DVO 2x clock on both PLLs if necessary */
1700 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1701 /*
1702 * It appears to be important that we don't enable this
1703 * for the current pipe before otherwise configuring the
1704 * PLL. No idea how this should be handled if multiple
1705 * DVO outputs are enabled simultaneosly.
1706 */
1707 dpll |= DPLL_DVO_2X_MODE;
1708 I915_WRITE(DPLL(!crtc->pipe),
1709 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1710 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001711
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001712 /*
1713 * Apparently we need to have VGA mode enabled prior to changing
1714 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1715 * dividers, even though the register value does change.
1716 */
1717 I915_WRITE(reg, 0);
1718
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001719 I915_WRITE(reg, dpll);
1720
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001721 /* Wait for the clocks to stabilize. */
1722 POSTING_READ(reg);
1723 udelay(150);
1724
1725 if (INTEL_INFO(dev)->gen >= 4) {
1726 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001727 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001728 } else {
1729 /* The pixel multiplier can only be updated once the
1730 * DPLL is enabled and the clocks are stable.
1731 *
1732 * So write it again.
1733 */
1734 I915_WRITE(reg, dpll);
1735 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001736
1737 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001738 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001739 POSTING_READ(reg);
1740 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001741 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001742 POSTING_READ(reg);
1743 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001744 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001745 POSTING_READ(reg);
1746 udelay(150); /* wait for warmup */
1747}
1748
1749/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001750 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001751 * @dev_priv: i915 private structure
1752 * @pipe: pipe PLL to disable
1753 *
1754 * Disable the PLL for @pipe, making sure the pipe is off first.
1755 *
1756 * Note! This is for pre-ILK only.
1757 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001758static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001759{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001760 struct drm_device *dev = crtc->base.dev;
1761 struct drm_i915_private *dev_priv = dev->dev_private;
1762 enum pipe pipe = crtc->pipe;
1763
1764 /* Disable DVO 2x clock on both PLLs if necessary */
1765 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001766 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001767 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001768 I915_WRITE(DPLL(PIPE_B),
1769 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1770 I915_WRITE(DPLL(PIPE_A),
1771 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1772 }
1773
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001774 /* Don't disable pipe or pipe PLLs if needed */
1775 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1776 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001777 return;
1778
1779 /* Make sure the pipe isn't still relying on us */
1780 assert_pipe_disabled(dev_priv, pipe);
1781
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001782 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001783 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001784}
1785
Jesse Barnesf6071162013-10-01 10:41:38 -07001786static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1787{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001788 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001789
1790 /* Make sure the pipe isn't still relying on us */
1791 assert_pipe_disabled(dev_priv, pipe);
1792
Imre Deake5cbfbf2014-01-09 17:08:16 +02001793 /*
1794 * Leave integrated clock source and reference clock enabled for pipe B.
1795 * The latter is needed for VGA hotplug / manual detection.
1796 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001797 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001798 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001799 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001800 I915_WRITE(DPLL(pipe), val);
1801 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001802
1803}
1804
1805static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1806{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001807 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001808 u32 val;
1809
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001810 /* Make sure the pipe isn't still relying on us */
1811 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001812
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001813 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001814 val = DPLL_SSC_REF_CLK_CHV |
1815 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001816 if (pipe != PIPE_A)
1817 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1818 I915_WRITE(DPLL(pipe), val);
1819 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001820
Ville Syrjäläa5805162015-05-26 20:42:30 +03001821 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001822
1823 /* Disable 10bit clock to display controller */
1824 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1825 val &= ~DPIO_DCLKP_EN;
1826 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1827
Ville Syrjäläa5805162015-05-26 20:42:30 +03001828 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001829}
1830
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001831void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001832 struct intel_digital_port *dport,
1833 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001834{
1835 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001836 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001837
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001838 switch (dport->port) {
1839 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001840 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001841 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001842 break;
1843 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001844 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001845 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001846 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001847 break;
1848 case PORT_D:
1849 port_mask = DPLL_PORTD_READY_MASK;
1850 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001851 break;
1852 default:
1853 BUG();
1854 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001855
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001856 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1857 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1858 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001859}
1860
Daniel Vetterb14b1052014-04-24 23:55:13 +02001861static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1862{
1863 struct drm_device *dev = crtc->base.dev;
1864 struct drm_i915_private *dev_priv = dev->dev_private;
1865 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1866
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001867 if (WARN_ON(pll == NULL))
1868 return;
1869
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001870 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001871 if (pll->active == 0) {
1872 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1873 WARN_ON(pll->on);
1874 assert_shared_dpll_disabled(dev_priv, pll);
1875
1876 pll->mode_set(dev_priv, pll);
1877 }
1878}
1879
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001880/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001881 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001882 * @dev_priv: i915 private structure
1883 * @pipe: pipe PLL to enable
1884 *
1885 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1886 * drives the transcoder clock.
1887 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001888static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001889{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001890 struct drm_device *dev = crtc->base.dev;
1891 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001892 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001893
Daniel Vetter87a875b2013-06-05 13:34:19 +02001894 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001895 return;
1896
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001897 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001898 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001899
Damien Lespiau74dd6922014-07-29 18:06:17 +01001900 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001901 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001902 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001903
Daniel Vettercdbd2312013-06-05 13:34:03 +02001904 if (pll->active++) {
1905 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001906 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001907 return;
1908 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001909 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001910
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001911 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1912
Daniel Vetter46edb022013-06-05 13:34:12 +02001913 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001914 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001915 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001916}
1917
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001918static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001919{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001920 struct drm_device *dev = crtc->base.dev;
1921 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001922 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001923
Jesse Barnes92f25842011-01-04 15:09:34 -08001924 /* PCH only available on ILK+ */
Jesse Barnes80aa9312015-08-03 13:09:11 -07001925 if (INTEL_INFO(dev)->gen < 5)
1926 return;
1927
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001928 if (pll == NULL)
1929 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001930
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001931 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
Chris Wilson48da64a2012-05-13 20:16:12 +01001932 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001933
Daniel Vetter46edb022013-06-05 13:34:12 +02001934 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1935 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001936 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001937
Chris Wilson48da64a2012-05-13 20:16:12 +01001938 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001939 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001940 return;
1941 }
1942
Daniel Vettere9d69442013-06-05 13:34:15 +02001943 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001944 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001945 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001946 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001947
Daniel Vetter46edb022013-06-05 13:34:12 +02001948 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001949 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001950 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001951
1952 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001953}
1954
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001955static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1956 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001957{
Daniel Vetter23670b322012-11-01 09:15:30 +01001958 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001959 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001960 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001961 i915_reg_t reg;
1962 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001963
1964 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001965 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001966
1967 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001968 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001969 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001970
1971 /* FDI must be feeding us bits for PCH ports */
1972 assert_fdi_tx_enabled(dev_priv, pipe);
1973 assert_fdi_rx_enabled(dev_priv, pipe);
1974
Daniel Vetter23670b322012-11-01 09:15:30 +01001975 if (HAS_PCH_CPT(dev)) {
1976 /* Workaround: Set the timing override bit before enabling the
1977 * pch transcoder. */
1978 reg = TRANS_CHICKEN2(pipe);
1979 val = I915_READ(reg);
1980 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1981 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001982 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001983
Daniel Vetterab9412b2013-05-03 11:49:46 +02001984 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001985 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001986 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001987
1988 if (HAS_PCH_IBX(dev_priv->dev)) {
1989 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001990 * Make the BPC in transcoder be consistent with
1991 * that in pipeconf reg. For HDMI we must use 8bpc
1992 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001993 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001994 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001995 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1996 val |= PIPECONF_8BPC;
1997 else
1998 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001999 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002000
2001 val &= ~TRANS_INTERLACE_MASK;
2002 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002003 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002004 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002005 val |= TRANS_LEGACY_INTERLACED_ILK;
2006 else
2007 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002008 else
2009 val |= TRANS_PROGRESSIVE;
2010
Jesse Barnes040484a2011-01-03 12:14:26 -08002011 I915_WRITE(reg, val | TRANS_ENABLE);
2012 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002013 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002014}
2015
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002016static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002017 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002018{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002019 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002020
2021 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002022 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002023
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002024 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002025 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002026 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002027
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002028 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002029 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01002030 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002031 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002032
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002033 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002034 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002035
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002036 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2037 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002038 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002039 else
2040 val |= TRANS_PROGRESSIVE;
2041
Daniel Vetterab9412b2013-05-03 11:49:46 +02002042 I915_WRITE(LPT_TRANSCONF, val);
2043 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002044 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002045}
2046
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002047static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2048 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002049{
Daniel Vetter23670b322012-11-01 09:15:30 +01002050 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002051 i915_reg_t reg;
2052 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002053
2054 /* FDI relies on the transcoder */
2055 assert_fdi_tx_disabled(dev_priv, pipe);
2056 assert_fdi_rx_disabled(dev_priv, pipe);
2057
Jesse Barnes291906f2011-02-02 12:28:03 -08002058 /* Ports must be off as well */
2059 assert_pch_ports_disabled(dev_priv, pipe);
2060
Daniel Vetterab9412b2013-05-03 11:49:46 +02002061 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002062 val = I915_READ(reg);
2063 val &= ~TRANS_ENABLE;
2064 I915_WRITE(reg, val);
2065 /* wait for PCH transcoder off, transcoder state */
2066 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002067 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002068
Ville Syrjäläc4656132015-10-29 21:25:56 +02002069 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01002070 /* Workaround: Clear the timing override chicken bit again. */
2071 reg = TRANS_CHICKEN2(pipe);
2072 val = I915_READ(reg);
2073 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2074 I915_WRITE(reg, val);
2075 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002076}
2077
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002078static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002079{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002080 u32 val;
2081
Daniel Vetterab9412b2013-05-03 11:49:46 +02002082 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002083 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002084 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002085 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002086 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002087 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002088
2089 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002090 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01002091 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002092 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002093}
2094
2095/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002096 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002097 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002098 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002099 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002100 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002101 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002102static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002103{
Paulo Zanoni03722642014-01-17 13:51:09 -02002104 struct drm_device *dev = crtc->base.dev;
2105 struct drm_i915_private *dev_priv = dev->dev_private;
2106 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02002107 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01002108 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002109 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002110 u32 val;
2111
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002112 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
2113
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002114 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002115 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002116 assert_sprites_disabled(dev_priv, pipe);
2117
Paulo Zanoni681e5812012-12-06 11:12:38 -02002118 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002119 pch_transcoder = TRANSCODER_A;
2120 else
2121 pch_transcoder = pipe;
2122
Jesse Barnesb24e7172011-01-04 15:09:30 -08002123 /*
2124 * A pipe without a PLL won't actually be able to drive bits from
2125 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2126 * need the check.
2127 */
Imre Deak50360402015-01-16 00:55:16 -08002128 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Jani Nikulaa65347b2015-11-27 12:21:46 +02002129 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03002130 assert_dsi_pll_enabled(dev_priv);
2131 else
2132 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002133 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002134 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002135 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002136 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002137 assert_fdi_tx_pll_enabled(dev_priv,
2138 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002139 }
2140 /* FIXME: assert CPU port conditions for SNB+ */
2141 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002142
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002143 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002144 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002145 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002146 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2147 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002148 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002149 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002150
2151 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002152 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002153
2154 /*
2155 * Until the pipe starts DSL will read as 0, which would cause
2156 * an apparent vblank timestamp jump, which messes up also the
2157 * frame count when it's derived from the timestamps. So let's
2158 * wait for the pipe to start properly before we call
2159 * drm_crtc_vblank_on()
2160 */
2161 if (dev->max_vblank_count == 0 &&
2162 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2163 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002164}
2165
2166/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002167 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002168 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002169 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002170 * Disable the pipe of @crtc, making sure that various hardware
2171 * specific requirements are met, if applicable, e.g. plane
2172 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002173 *
2174 * Will wait until the pipe has shut down before returning.
2175 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002176static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002177{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002178 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002179 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002180 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002181 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002182 u32 val;
2183
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002184 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2185
Jesse Barnesb24e7172011-01-04 15:09:30 -08002186 /*
2187 * Make sure planes won't keep trying to pump pixels to us,
2188 * or we might hang the display.
2189 */
2190 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002191 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002192 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002193
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002194 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002195 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002196 if ((val & PIPECONF_ENABLE) == 0)
2197 return;
2198
Ville Syrjälä67adc642014-08-15 01:21:57 +03002199 /*
2200 * Double wide has implications for planes
2201 * so best keep it disabled when not needed.
2202 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002203 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002204 val &= ~PIPECONF_DOUBLE_WIDE;
2205
2206 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002207 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2208 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002209 val &= ~PIPECONF_ENABLE;
2210
2211 I915_WRITE(reg, val);
2212 if ((val & PIPECONF_ENABLE) == 0)
2213 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002214}
2215
Chris Wilson693db182013-03-05 14:52:39 +00002216static bool need_vtd_wa(struct drm_device *dev)
2217{
2218#ifdef CONFIG_INTEL_IOMMU
2219 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2220 return true;
2221#endif
2222 return false;
2223}
2224
Ville Syrjälä832be822016-01-12 21:08:33 +02002225static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2226{
2227 return IS_GEN2(dev_priv) ? 2048 : 4096;
2228}
2229
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002230static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2231 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002232{
2233 switch (fb_modifier) {
2234 case DRM_FORMAT_MOD_NONE:
2235 return cpp;
2236 case I915_FORMAT_MOD_X_TILED:
2237 if (IS_GEN2(dev_priv))
2238 return 128;
2239 else
2240 return 512;
2241 case I915_FORMAT_MOD_Y_TILED:
2242 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2243 return 128;
2244 else
2245 return 512;
2246 case I915_FORMAT_MOD_Yf_TILED:
2247 switch (cpp) {
2248 case 1:
2249 return 64;
2250 case 2:
2251 case 4:
2252 return 128;
2253 case 8:
2254 case 16:
2255 return 256;
2256 default:
2257 MISSING_CASE(cpp);
2258 return cpp;
2259 }
2260 break;
2261 default:
2262 MISSING_CASE(fb_modifier);
2263 return cpp;
2264 }
2265}
2266
Ville Syrjälä832be822016-01-12 21:08:33 +02002267unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2268 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002269{
Ville Syrjälä832be822016-01-12 21:08:33 +02002270 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2271 return 1;
2272 else
2273 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002274 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002275}
2276
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002277/* Return the tile dimensions in pixel units */
2278static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2279 unsigned int *tile_width,
2280 unsigned int *tile_height,
2281 uint64_t fb_modifier,
2282 unsigned int cpp)
2283{
2284 unsigned int tile_width_bytes =
2285 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2286
2287 *tile_width = tile_width_bytes / cpp;
2288 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2289}
2290
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002291unsigned int
2292intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002293 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002294{
Ville Syrjälä832be822016-01-12 21:08:33 +02002295 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2296 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2297
2298 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002299}
2300
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002301unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2302{
2303 unsigned int size = 0;
2304 int i;
2305
2306 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2307 size += rot_info->plane[i].width * rot_info->plane[i].height;
2308
2309 return size;
2310}
2311
Daniel Vetter75c82a52015-10-14 16:51:04 +02002312static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002313intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2314 const struct drm_framebuffer *fb,
2315 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002316{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002317 if (intel_rotation_90_or_270(rotation)) {
2318 *view = i915_ggtt_view_rotated;
2319 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2320 } else {
2321 *view = i915_ggtt_view_normal;
2322 }
2323}
2324
2325static void
2326intel_fill_fb_info(struct drm_i915_private *dev_priv,
2327 struct drm_framebuffer *fb)
2328{
2329 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002330 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002331
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002332 tile_size = intel_tile_size(dev_priv);
2333
2334 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002335 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2336 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002337
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002338 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2339 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002340
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002341 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002342 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002343 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2344 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002345
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002346 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002347 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2348 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002349 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002350}
2351
Ville Syrjälä603525d2016-01-12 21:08:37 +02002352static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002353{
2354 if (INTEL_INFO(dev_priv)->gen >= 9)
2355 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002356 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002357 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002358 return 128 * 1024;
2359 else if (INTEL_INFO(dev_priv)->gen >= 4)
2360 return 4 * 1024;
2361 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002362 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002363}
2364
Ville Syrjälä603525d2016-01-12 21:08:37 +02002365static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2366 uint64_t fb_modifier)
2367{
2368 switch (fb_modifier) {
2369 case DRM_FORMAT_MOD_NONE:
2370 return intel_linear_alignment(dev_priv);
2371 case I915_FORMAT_MOD_X_TILED:
2372 if (INTEL_INFO(dev_priv)->gen >= 9)
2373 return 256 * 1024;
2374 return 0;
2375 case I915_FORMAT_MOD_Y_TILED:
2376 case I915_FORMAT_MOD_Yf_TILED:
2377 return 1 * 1024 * 1024;
2378 default:
2379 MISSING_CASE(fb_modifier);
2380 return 0;
2381 }
2382}
2383
Chris Wilson127bd2a2010-07-23 23:32:05 +01002384int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002385intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2386 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002387{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002388 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002389 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002390 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002391 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002392 u32 alignment;
2393 int ret;
2394
Matt Roperebcdd392014-07-09 16:22:11 -07002395 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2396
Ville Syrjälä603525d2016-01-12 21:08:37 +02002397 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002398
Ville Syrjälä3465c582016-02-15 22:54:43 +02002399 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002400
Chris Wilson693db182013-03-05 14:52:39 +00002401 /* Note that the w/a also requires 64 PTE of padding following the
2402 * bo. We currently fill all unused PTE with the shadow page and so
2403 * we should always have valid PTE following the scanout preventing
2404 * the VT-d warning.
2405 */
2406 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2407 alignment = 256 * 1024;
2408
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002409 /*
2410 * Global gtt pte registers are special registers which actually forward
2411 * writes to a chunk of system memory. Which means that there is no risk
2412 * that the register values disappear as soon as we call
2413 * intel_runtime_pm_put(), so it is correct to wrap only the
2414 * pin/unpin/fence and not more.
2415 */
2416 intel_runtime_pm_get(dev_priv);
2417
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002418 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2419 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002420 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002421 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002422
2423 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2424 * fence, whereas 965+ only requires a fence if using
2425 * framebuffer compression. For simplicity, we always install
2426 * a fence as the cost is not that onerous.
2427 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002428 if (view.type == I915_GGTT_VIEW_NORMAL) {
2429 ret = i915_gem_object_get_fence(obj);
2430 if (ret == -EDEADLK) {
2431 /*
2432 * -EDEADLK means there are no free fences
2433 * no pending flips.
2434 *
2435 * This is propagated to atomic, but it uses
2436 * -EDEADLK to force a locking recovery, so
2437 * change the returned error to -EBUSY.
2438 */
2439 ret = -EBUSY;
2440 goto err_unpin;
2441 } else if (ret)
2442 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002443
Vivek Kasireddy98072162015-10-29 18:54:38 -07002444 i915_gem_object_pin_fence(obj);
2445 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002446
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002447 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002448 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002449
2450err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002451 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002452err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002453 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002454 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002455}
2456
Ville Syrjälä3465c582016-02-15 22:54:43 +02002457static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002458{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002459 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002460 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002461
Matt Roperebcdd392014-07-09 16:22:11 -07002462 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2463
Ville Syrjälä3465c582016-02-15 22:54:43 +02002464 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002465
Vivek Kasireddy98072162015-10-29 18:54:38 -07002466 if (view.type == I915_GGTT_VIEW_NORMAL)
2467 i915_gem_object_unpin_fence(obj);
2468
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002469 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002470}
2471
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002472/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002473 * Adjust the tile offset by moving the difference into
2474 * the x/y offsets.
2475 *
2476 * Input tile dimensions and pitch must already be
2477 * rotated to match x and y, and in pixel units.
2478 */
2479static u32 intel_adjust_tile_offset(int *x, int *y,
2480 unsigned int tile_width,
2481 unsigned int tile_height,
2482 unsigned int tile_size,
2483 unsigned int pitch_tiles,
2484 u32 old_offset,
2485 u32 new_offset)
2486{
2487 unsigned int tiles;
2488
2489 WARN_ON(old_offset & (tile_size - 1));
2490 WARN_ON(new_offset & (tile_size - 1));
2491 WARN_ON(new_offset > old_offset);
2492
2493 tiles = (old_offset - new_offset) / tile_size;
2494
2495 *y += tiles / pitch_tiles * tile_height;
2496 *x += tiles % pitch_tiles * tile_width;
2497
2498 return new_offset;
2499}
2500
2501/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002502 * Computes the linear offset to the base tile and adjusts
2503 * x, y. bytes per pixel is assumed to be a power-of-two.
2504 *
2505 * In the 90/270 rotated case, x and y are assumed
2506 * to be already rotated to match the rotated GTT view, and
2507 * pitch is the tile_height aligned framebuffer height.
2508 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002509u32 intel_compute_tile_offset(int *x, int *y,
2510 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002511 unsigned int pitch,
2512 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002513{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002514 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2515 uint64_t fb_modifier = fb->modifier[plane];
2516 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002517 u32 offset, offset_aligned, alignment;
2518
2519 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2520 if (alignment)
2521 alignment--;
2522
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002523 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002524 unsigned int tile_size, tile_width, tile_height;
2525 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002526
Ville Syrjäläd8433102016-01-12 21:08:35 +02002527 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002528 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2529 fb_modifier, cpp);
2530
2531 if (intel_rotation_90_or_270(rotation)) {
2532 pitch_tiles = pitch / tile_height;
2533 swap(tile_width, tile_height);
2534 } else {
2535 pitch_tiles = pitch / (tile_width * cpp);
2536 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002537
Ville Syrjäläd8433102016-01-12 21:08:35 +02002538 tile_rows = *y / tile_height;
2539 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002540
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002541 tiles = *x / tile_width;
2542 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002543
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002544 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2545 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002546
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002547 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2548 tile_size, pitch_tiles,
2549 offset, offset_aligned);
2550 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002551 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002552 offset_aligned = offset & ~alignment;
2553
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002554 *y = (offset & alignment) / pitch;
2555 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002556 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002557
2558 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002559}
2560
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002561static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002562{
2563 switch (format) {
2564 case DISPPLANE_8BPP:
2565 return DRM_FORMAT_C8;
2566 case DISPPLANE_BGRX555:
2567 return DRM_FORMAT_XRGB1555;
2568 case DISPPLANE_BGRX565:
2569 return DRM_FORMAT_RGB565;
2570 default:
2571 case DISPPLANE_BGRX888:
2572 return DRM_FORMAT_XRGB8888;
2573 case DISPPLANE_RGBX888:
2574 return DRM_FORMAT_XBGR8888;
2575 case DISPPLANE_BGRX101010:
2576 return DRM_FORMAT_XRGB2101010;
2577 case DISPPLANE_RGBX101010:
2578 return DRM_FORMAT_XBGR2101010;
2579 }
2580}
2581
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002582static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2583{
2584 switch (format) {
2585 case PLANE_CTL_FORMAT_RGB_565:
2586 return DRM_FORMAT_RGB565;
2587 default:
2588 case PLANE_CTL_FORMAT_XRGB_8888:
2589 if (rgb_order) {
2590 if (alpha)
2591 return DRM_FORMAT_ABGR8888;
2592 else
2593 return DRM_FORMAT_XBGR8888;
2594 } else {
2595 if (alpha)
2596 return DRM_FORMAT_ARGB8888;
2597 else
2598 return DRM_FORMAT_XRGB8888;
2599 }
2600 case PLANE_CTL_FORMAT_XRGB_2101010:
2601 if (rgb_order)
2602 return DRM_FORMAT_XBGR2101010;
2603 else
2604 return DRM_FORMAT_XRGB2101010;
2605 }
2606}
2607
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002608static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002609intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2610 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002611{
2612 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002613 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002614 struct drm_i915_gem_object *obj = NULL;
2615 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002616 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002617 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2618 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2619 PAGE_SIZE);
2620
2621 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002622
Chris Wilsonff2652e2014-03-10 08:07:02 +00002623 if (plane_config->size == 0)
2624 return false;
2625
Paulo Zanoni3badb492015-09-23 12:52:23 -03002626 /* If the FB is too big, just don't use it since fbdev is not very
2627 * important and we should probably use that space with FBC or other
2628 * features. */
2629 if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
2630 return false;
2631
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002632 mutex_lock(&dev->struct_mutex);
2633
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002634 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2635 base_aligned,
2636 base_aligned,
2637 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002638 if (!obj) {
2639 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002640 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002641 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002642
Damien Lespiau49af4492015-01-20 12:51:44 +00002643 obj->tiling_mode = plane_config->tiling;
2644 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002645 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002646
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002647 mode_cmd.pixel_format = fb->pixel_format;
2648 mode_cmd.width = fb->width;
2649 mode_cmd.height = fb->height;
2650 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002651 mode_cmd.modifier[0] = fb->modifier[0];
2652 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002653
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002654 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002655 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002656 DRM_DEBUG_KMS("intel fb init failed\n");
2657 goto out_unref_obj;
2658 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002659
Jesse Barnes46f297f2014-03-07 08:57:48 -08002660 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002661
Daniel Vetterf6936e22015-03-26 12:17:05 +01002662 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002663 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002664
2665out_unref_obj:
2666 drm_gem_object_unreference(&obj->base);
2667 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002668 return false;
2669}
2670
Matt Roperafd65eb2015-02-03 13:10:04 -08002671/* Update plane->state->fb to match plane->fb after driver-internal updates */
2672static void
2673update_state_fb(struct drm_plane *plane)
2674{
2675 if (plane->fb == plane->state->fb)
2676 return;
2677
2678 if (plane->state->fb)
2679 drm_framebuffer_unreference(plane->state->fb);
2680 plane->state->fb = plane->fb;
2681 if (plane->state->fb)
2682 drm_framebuffer_reference(plane->state->fb);
2683}
2684
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002685static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002686intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2687 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002688{
2689 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002690 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002691 struct drm_crtc *c;
2692 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002693 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002694 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002695 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002696 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2697 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002698 struct intel_plane_state *intel_state =
2699 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002700 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002701
Damien Lespiau2d140302015-02-05 17:22:18 +00002702 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002703 return;
2704
Daniel Vetterf6936e22015-03-26 12:17:05 +01002705 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002706 fb = &plane_config->fb->base;
2707 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002708 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002709
Damien Lespiau2d140302015-02-05 17:22:18 +00002710 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002711
2712 /*
2713 * Failed to alloc the obj, check to see if we should share
2714 * an fb with another CRTC instead
2715 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002716 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002717 i = to_intel_crtc(c);
2718
2719 if (c == &intel_crtc->base)
2720 continue;
2721
Matt Roper2ff8fde2014-07-08 07:50:07 -07002722 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002723 continue;
2724
Daniel Vetter88595ac2015-03-26 12:42:24 +01002725 fb = c->primary->fb;
2726 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002727 continue;
2728
Daniel Vetter88595ac2015-03-26 12:42:24 +01002729 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002730 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002731 drm_framebuffer_reference(fb);
2732 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002733 }
2734 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002735
Matt Roper200757f2015-12-03 11:37:36 -08002736 /*
2737 * We've failed to reconstruct the BIOS FB. Current display state
2738 * indicates that the primary plane is visible, but has a NULL FB,
2739 * which will lead to problems later if we don't fix it up. The
2740 * simplest solution is to just disable the primary plane now and
2741 * pretend the BIOS never had it enabled.
2742 */
2743 to_intel_plane_state(plane_state)->visible = false;
2744 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2745 intel_pre_disable_primary(&intel_crtc->base);
2746 intel_plane->disable_plane(primary, &intel_crtc->base);
2747
Daniel Vetter88595ac2015-03-26 12:42:24 +01002748 return;
2749
2750valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002751 plane_state->src_x = 0;
2752 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002753 plane_state->src_w = fb->width << 16;
2754 plane_state->src_h = fb->height << 16;
2755
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002756 plane_state->crtc_x = 0;
2757 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002758 plane_state->crtc_w = fb->width;
2759 plane_state->crtc_h = fb->height;
2760
Matt Roper0a8d8a82015-12-03 11:37:38 -08002761 intel_state->src.x1 = plane_state->src_x;
2762 intel_state->src.y1 = plane_state->src_y;
2763 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2764 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2765 intel_state->dst.x1 = plane_state->crtc_x;
2766 intel_state->dst.y1 = plane_state->crtc_y;
2767 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2768 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2769
Daniel Vetter88595ac2015-03-26 12:42:24 +01002770 obj = intel_fb_obj(fb);
2771 if (obj->tiling_mode != I915_TILING_NONE)
2772 dev_priv->preserve_bios_swizzle = true;
2773
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002774 drm_framebuffer_reference(fb);
2775 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002776 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002777 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002778 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002779}
2780
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002781static void i9xx_update_primary_plane(struct drm_plane *primary,
2782 const struct intel_crtc_state *crtc_state,
2783 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002784{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002785 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002786 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002787 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2788 struct drm_framebuffer *fb = plane_state->base.fb;
2789 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002790 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002791 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002792 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002793 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002794 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002795 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002796 int x = plane_state->src.x1 >> 16;
2797 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002798
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002799 dspcntr = DISPPLANE_GAMMA_ENABLE;
2800
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002801 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002802
2803 if (INTEL_INFO(dev)->gen < 4) {
2804 if (intel_crtc->pipe == PIPE_B)
2805 dspcntr |= DISPPLANE_SEL_PIPE_B;
2806
2807 /* pipesrc and dspsize control the size that is scaled from,
2808 * which should always be the user's requested size.
2809 */
2810 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002811 ((crtc_state->pipe_src_h - 1) << 16) |
2812 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002813 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002814 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2815 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002816 ((crtc_state->pipe_src_h - 1) << 16) |
2817 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002818 I915_WRITE(PRIMPOS(plane), 0);
2819 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002820 }
2821
Ville Syrjälä57779d02012-10-31 17:50:14 +02002822 switch (fb->pixel_format) {
2823 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002824 dspcntr |= DISPPLANE_8BPP;
2825 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002826 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002827 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002828 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002829 case DRM_FORMAT_RGB565:
2830 dspcntr |= DISPPLANE_BGRX565;
2831 break;
2832 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002833 dspcntr |= DISPPLANE_BGRX888;
2834 break;
2835 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002836 dspcntr |= DISPPLANE_RGBX888;
2837 break;
2838 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002839 dspcntr |= DISPPLANE_BGRX101010;
2840 break;
2841 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002842 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002843 break;
2844 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002845 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002846 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002847
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002848 if (INTEL_INFO(dev)->gen >= 4 &&
2849 obj->tiling_mode != I915_TILING_NONE)
2850 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002851
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002852 if (IS_G4X(dev))
2853 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2854
Ville Syrjäläac484962016-01-20 21:05:26 +02002855 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002856
Daniel Vetterc2c75132012-07-05 12:17:30 +02002857 if (INTEL_INFO(dev)->gen >= 4) {
2858 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002859 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002860 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002861 linear_offset -= intel_crtc->dspaddr_offset;
2862 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002863 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002864 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002865
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002866 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302867 dspcntr |= DISPPLANE_ROTATE_180;
2868
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002869 x += (crtc_state->pipe_src_w - 1);
2870 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302871
2872 /* Finding the last pixel of the last line of the display
2873 data and adding to linear_offset*/
2874 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002875 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002876 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302877 }
2878
Paulo Zanoni2db33662015-09-14 15:20:03 -03002879 intel_crtc->adjusted_x = x;
2880 intel_crtc->adjusted_y = y;
2881
Sonika Jindal48404c12014-08-22 14:06:04 +05302882 I915_WRITE(reg, dspcntr);
2883
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002884 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002885 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002886 I915_WRITE(DSPSURF(plane),
2887 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002888 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002889 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002890 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002891 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002892 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002893}
2894
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002895static void i9xx_disable_primary_plane(struct drm_plane *primary,
2896 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002897{
2898 struct drm_device *dev = crtc->dev;
2899 struct drm_i915_private *dev_priv = dev->dev_private;
2900 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002901 int plane = intel_crtc->plane;
2902
2903 I915_WRITE(DSPCNTR(plane), 0);
2904 if (INTEL_INFO(dev_priv)->gen >= 4)
2905 I915_WRITE(DSPSURF(plane), 0);
2906 else
2907 I915_WRITE(DSPADDR(plane), 0);
2908 POSTING_READ(DSPCNTR(plane));
2909}
2910
2911static void ironlake_update_primary_plane(struct drm_plane *primary,
2912 const struct intel_crtc_state *crtc_state,
2913 const struct intel_plane_state *plane_state)
2914{
2915 struct drm_device *dev = primary->dev;
2916 struct drm_i915_private *dev_priv = dev->dev_private;
2917 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2918 struct drm_framebuffer *fb = plane_state->base.fb;
2919 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002920 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002921 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002922 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002923 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002924 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002925 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002926 int x = plane_state->src.x1 >> 16;
2927 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002928
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002929 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002930 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002931
2932 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2933 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2934
Ville Syrjälä57779d02012-10-31 17:50:14 +02002935 switch (fb->pixel_format) {
2936 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002937 dspcntr |= DISPPLANE_8BPP;
2938 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002939 case DRM_FORMAT_RGB565:
2940 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002941 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002942 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002943 dspcntr |= DISPPLANE_BGRX888;
2944 break;
2945 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002946 dspcntr |= DISPPLANE_RGBX888;
2947 break;
2948 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002949 dspcntr |= DISPPLANE_BGRX101010;
2950 break;
2951 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002952 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002953 break;
2954 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002955 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002956 }
2957
2958 if (obj->tiling_mode != I915_TILING_NONE)
2959 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002960
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002961 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002962 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002963
Ville Syrjäläac484962016-01-20 21:05:26 +02002964 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002965 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002966 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002967 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002968 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002969 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302970 dspcntr |= DISPPLANE_ROTATE_180;
2971
2972 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002973 x += (crtc_state->pipe_src_w - 1);
2974 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302975
2976 /* Finding the last pixel of the last line of the display
2977 data and adding to linear_offset*/
2978 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002979 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002980 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302981 }
2982 }
2983
Paulo Zanoni2db33662015-09-14 15:20:03 -03002984 intel_crtc->adjusted_x = x;
2985 intel_crtc->adjusted_y = y;
2986
Sonika Jindal48404c12014-08-22 14:06:04 +05302987 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002988
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002989 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002990 I915_WRITE(DSPSURF(plane),
2991 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002992 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002993 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2994 } else {
2995 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2996 I915_WRITE(DSPLINOFF(plane), linear_offset);
2997 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002998 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002999}
3000
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003001u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
3002 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00003003{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003004 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
3005 return 64;
3006 } else {
3007 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00003008
Ville Syrjälä27ba3912016-02-15 22:54:40 +02003009 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00003010 }
3011}
3012
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003013u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
3014 struct drm_i915_gem_object *obj,
3015 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003016{
Daniel Vetterce7f1722015-10-14 16:51:06 +02003017 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003018 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003019 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003020
Ville Syrjäläe7941292016-01-19 18:23:17 +02003021 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02003022 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003023
Daniel Vetterce7f1722015-10-14 16:51:06 +02003024 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003025 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02003026 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003027 return -1;
3028
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003029 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003030
3031 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02003032 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003033 PAGE_SIZE;
3034 }
3035
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003036 WARN_ON(upper_32_bits(offset));
3037
3038 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003039}
3040
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003041static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3042{
3043 struct drm_device *dev = intel_crtc->base.dev;
3044 struct drm_i915_private *dev_priv = dev->dev_private;
3045
3046 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3047 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3048 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003049}
3050
Chandra Kondurua1b22782015-04-07 15:28:45 -07003051/*
3052 * This function detaches (aka. unbinds) unused scalers in hardware
3053 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02003054static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07003055{
Chandra Kondurua1b22782015-04-07 15:28:45 -07003056 struct intel_crtc_scaler_state *scaler_state;
3057 int i;
3058
Chandra Kondurua1b22782015-04-07 15:28:45 -07003059 scaler_state = &intel_crtc->config->scaler_state;
3060
3061 /* loop through and disable scalers that aren't in use */
3062 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003063 if (!scaler_state->scalers[i].in_use)
3064 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07003065 }
3066}
3067
Chandra Konduru6156a452015-04-27 13:48:39 -07003068u32 skl_plane_ctl_format(uint32_t pixel_format)
3069{
Chandra Konduru6156a452015-04-27 13:48:39 -07003070 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01003071 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003072 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07003073 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003074 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07003075 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003076 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003077 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003078 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003079 /*
3080 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3081 * to be already pre-multiplied. We need to add a knob (or a different
3082 * DRM_FORMAT) for user-space to configure that.
3083 */
3084 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003085 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07003086 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003087 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003088 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07003089 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003090 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003091 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003092 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003093 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003094 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003095 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003096 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003097 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003098 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003099 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003100 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003101 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003102 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003103 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003104 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003105
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003106 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003107}
3108
3109u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3110{
Chandra Konduru6156a452015-04-27 13:48:39 -07003111 switch (fb_modifier) {
3112 case DRM_FORMAT_MOD_NONE:
3113 break;
3114 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003115 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003116 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003117 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003118 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003119 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003120 default:
3121 MISSING_CASE(fb_modifier);
3122 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003123
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003124 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003125}
3126
3127u32 skl_plane_ctl_rotation(unsigned int rotation)
3128{
Chandra Konduru6156a452015-04-27 13:48:39 -07003129 switch (rotation) {
3130 case BIT(DRM_ROTATE_0):
3131 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303132 /*
3133 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3134 * while i915 HW rotation is clockwise, thats why this swapping.
3135 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003136 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303137 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003138 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003139 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003140 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303141 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003142 default:
3143 MISSING_CASE(rotation);
3144 }
3145
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003146 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003147}
3148
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003149static void skylake_update_primary_plane(struct drm_plane *plane,
3150 const struct intel_crtc_state *crtc_state,
3151 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003152{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003153 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003154 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3156 struct drm_framebuffer *fb = plane_state->base.fb;
3157 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003158 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303159 u32 plane_ctl, stride_div, stride;
3160 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003161 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303162 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003163 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003164 int scaler_id = plane_state->scaler_id;
3165 int src_x = plane_state->src.x1 >> 16;
3166 int src_y = plane_state->src.y1 >> 16;
3167 int src_w = drm_rect_width(&plane_state->src) >> 16;
3168 int src_h = drm_rect_height(&plane_state->src) >> 16;
3169 int dst_x = plane_state->dst.x1;
3170 int dst_y = plane_state->dst.y1;
3171 int dst_w = drm_rect_width(&plane_state->dst);
3172 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003173
3174 plane_ctl = PLANE_CTL_ENABLE |
3175 PLANE_CTL_PIPE_GAMMA_ENABLE |
3176 PLANE_CTL_PIPE_CSC_ENABLE;
3177
Chandra Konduru6156a452015-04-27 13:48:39 -07003178 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3179 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003180 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003181 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003182
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003183 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003184 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003185 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303186
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003187 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003188
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303189 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003190 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3191
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303192 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003193 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303194 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003195 x_offset = stride * tile_height - src_y - src_h;
3196 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003197 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303198 } else {
3199 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003200 x_offset = src_x;
3201 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003202 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303203 }
3204 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003205
Paulo Zanoni2db33662015-09-14 15:20:03 -03003206 intel_crtc->adjusted_x = x_offset;
3207 intel_crtc->adjusted_y = y_offset;
3208
Damien Lespiau70d21f02013-07-03 21:06:04 +01003209 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303210 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3211 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3212 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003213
3214 if (scaler_id >= 0) {
3215 uint32_t ps_ctrl = 0;
3216
3217 WARN_ON(!dst_w || !dst_h);
3218 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3219 crtc_state->scaler_state.scalers[scaler_id].mode;
3220 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3221 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3222 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3223 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3224 I915_WRITE(PLANE_POS(pipe, 0), 0);
3225 } else {
3226 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3227 }
3228
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003229 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003230
3231 POSTING_READ(PLANE_SURF(pipe, 0));
3232}
3233
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003234static void skylake_disable_primary_plane(struct drm_plane *primary,
3235 struct drm_crtc *crtc)
3236{
3237 struct drm_device *dev = crtc->dev;
3238 struct drm_i915_private *dev_priv = dev->dev_private;
3239 int pipe = to_intel_crtc(crtc)->pipe;
3240
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003241 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3242 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3243 POSTING_READ(PLANE_SURF(pipe, 0));
3244}
3245
Jesse Barnes17638cd2011-06-24 12:19:23 -07003246/* Assume fb object is pinned & idle & fenced and just update base pointers */
3247static int
3248intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3249 int x, int y, enum mode_set_atomic state)
3250{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003251 /* Support for kgdboc is disabled, this needs a major rework. */
3252 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003253
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003254 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003255}
3256
Ville Syrjälä75147472014-11-24 18:28:11 +02003257static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003258{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003259 struct drm_crtc *crtc;
3260
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003261 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003262 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3263 enum plane plane = intel_crtc->plane;
3264
3265 intel_prepare_page_flip(dev, plane);
3266 intel_finish_page_flip_plane(dev, plane);
3267 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003268}
3269
3270static void intel_update_primary_planes(struct drm_device *dev)
3271{
Ville Syrjälä75147472014-11-24 18:28:11 +02003272 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003273
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003274 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003275 struct intel_plane *plane = to_intel_plane(crtc->primary);
3276 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003277
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003278 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003279 plane_state = to_intel_plane_state(plane->base.state);
3280
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003281 if (plane_state->visible)
3282 plane->update_plane(&plane->base,
3283 to_intel_crtc_state(crtc->state),
3284 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003285
3286 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003287 }
3288}
3289
Ville Syrjälä75147472014-11-24 18:28:11 +02003290void intel_prepare_reset(struct drm_device *dev)
3291{
3292 /* no reset support for gen2 */
3293 if (IS_GEN2(dev))
3294 return;
3295
3296 /* reset doesn't touch the display */
3297 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3298 return;
3299
3300 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003301 /*
3302 * Disabling the crtcs gracefully seems nicer. Also the
3303 * g33 docs say we should at least disable all the planes.
3304 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003305 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003306}
3307
3308void intel_finish_reset(struct drm_device *dev)
3309{
3310 struct drm_i915_private *dev_priv = to_i915(dev);
3311
3312 /*
3313 * Flips in the rings will be nuked by the reset,
3314 * so complete all pending flips so that user space
3315 * will get its events and not get stuck.
3316 */
3317 intel_complete_page_flips(dev);
3318
3319 /* no reset support for gen2 */
3320 if (IS_GEN2(dev))
3321 return;
3322
3323 /* reset doesn't touch the display */
3324 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3325 /*
3326 * Flips in the rings have been nuked by the reset,
3327 * so update the base address of all primary
3328 * planes to the the last fb to make sure we're
3329 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003330 *
3331 * FIXME: Atomic will make this obsolete since we won't schedule
3332 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003333 */
3334 intel_update_primary_planes(dev);
3335 return;
3336 }
3337
3338 /*
3339 * The display has been reset as well,
3340 * so need a full re-initialization.
3341 */
3342 intel_runtime_pm_disable_interrupts(dev_priv);
3343 intel_runtime_pm_enable_interrupts(dev_priv);
3344
3345 intel_modeset_init_hw(dev);
3346
3347 spin_lock_irq(&dev_priv->irq_lock);
3348 if (dev_priv->display.hpd_irq_setup)
3349 dev_priv->display.hpd_irq_setup(dev);
3350 spin_unlock_irq(&dev_priv->irq_lock);
3351
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003352 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003353
3354 intel_hpd_init(dev_priv);
3355
3356 drm_modeset_unlock_all(dev);
3357}
3358
Chris Wilson7d5e3792014-03-04 13:15:08 +00003359static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3360{
3361 struct drm_device *dev = crtc->dev;
3362 struct drm_i915_private *dev_priv = dev->dev_private;
3363 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003364 bool pending;
3365
3366 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3367 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3368 return false;
3369
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003370 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003371 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003372 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003373
3374 return pending;
3375}
3376
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003377static void intel_update_pipe_config(struct intel_crtc *crtc,
3378 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003379{
3380 struct drm_device *dev = crtc->base.dev;
3381 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003382 struct intel_crtc_state *pipe_config =
3383 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003384
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003385 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3386 crtc->base.mode = crtc->base.state->mode;
3387
3388 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3389 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3390 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003391
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003392 if (HAS_DDI(dev))
3393 intel_set_pipe_csc(&crtc->base);
3394
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003395 /*
3396 * Update pipe size and adjust fitter if needed: the reason for this is
3397 * that in compute_mode_changes we check the native mode (not the pfit
3398 * mode) to see if we can flip rather than do a full mode set. In the
3399 * fastboot case, we'll flip, but if we don't update the pipesrc and
3400 * pfit state, we'll end up with a big fb scanned out into the wrong
3401 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003402 */
3403
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003404 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003405 ((pipe_config->pipe_src_w - 1) << 16) |
3406 (pipe_config->pipe_src_h - 1));
3407
3408 /* on skylake this is done by detaching scalers */
3409 if (INTEL_INFO(dev)->gen >= 9) {
3410 skl_detach_scalers(crtc);
3411
3412 if (pipe_config->pch_pfit.enabled)
3413 skylake_pfit_enable(crtc);
3414 } else if (HAS_PCH_SPLIT(dev)) {
3415 if (pipe_config->pch_pfit.enabled)
3416 ironlake_pfit_enable(crtc);
3417 else if (old_crtc_state->pch_pfit.enabled)
3418 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003419 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003420}
3421
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003422static void intel_fdi_normal_train(struct drm_crtc *crtc)
3423{
3424 struct drm_device *dev = crtc->dev;
3425 struct drm_i915_private *dev_priv = dev->dev_private;
3426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3427 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003428 i915_reg_t reg;
3429 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003430
3431 /* enable normal train */
3432 reg = FDI_TX_CTL(pipe);
3433 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003434 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003435 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3436 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003437 } else {
3438 temp &= ~FDI_LINK_TRAIN_NONE;
3439 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003440 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003441 I915_WRITE(reg, temp);
3442
3443 reg = FDI_RX_CTL(pipe);
3444 temp = I915_READ(reg);
3445 if (HAS_PCH_CPT(dev)) {
3446 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3447 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3448 } else {
3449 temp &= ~FDI_LINK_TRAIN_NONE;
3450 temp |= FDI_LINK_TRAIN_NONE;
3451 }
3452 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3453
3454 /* wait one idle pattern time */
3455 POSTING_READ(reg);
3456 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003457
3458 /* IVB wants error correction enabled */
3459 if (IS_IVYBRIDGE(dev))
3460 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3461 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003462}
3463
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003464/* The FDI link training functions for ILK/Ibexpeak. */
3465static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3466{
3467 struct drm_device *dev = crtc->dev;
3468 struct drm_i915_private *dev_priv = dev->dev_private;
3469 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3470 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003471 i915_reg_t reg;
3472 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003474 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003475 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003476
Adam Jacksone1a44742010-06-25 15:32:14 -04003477 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3478 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003479 reg = FDI_RX_IMR(pipe);
3480 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003481 temp &= ~FDI_RX_SYMBOL_LOCK;
3482 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003483 I915_WRITE(reg, temp);
3484 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003485 udelay(150);
3486
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003487 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003488 reg = FDI_TX_CTL(pipe);
3489 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003490 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003491 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003492 temp &= ~FDI_LINK_TRAIN_NONE;
3493 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003494 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003495
Chris Wilson5eddb702010-09-11 13:48:45 +01003496 reg = FDI_RX_CTL(pipe);
3497 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003498 temp &= ~FDI_LINK_TRAIN_NONE;
3499 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003500 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3501
3502 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003503 udelay(150);
3504
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003505 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003506 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3507 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3508 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003509
Chris Wilson5eddb702010-09-11 13:48:45 +01003510 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003511 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003512 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003513 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3514
3515 if ((temp & FDI_RX_BIT_LOCK)) {
3516 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003517 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003518 break;
3519 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003520 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003521 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003522 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003523
3524 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003525 reg = FDI_TX_CTL(pipe);
3526 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003527 temp &= ~FDI_LINK_TRAIN_NONE;
3528 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003529 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003530
Chris Wilson5eddb702010-09-11 13:48:45 +01003531 reg = FDI_RX_CTL(pipe);
3532 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003533 temp &= ~FDI_LINK_TRAIN_NONE;
3534 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003535 I915_WRITE(reg, temp);
3536
3537 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003538 udelay(150);
3539
Chris Wilson5eddb702010-09-11 13:48:45 +01003540 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003541 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003542 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003543 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3544
3545 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003546 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003547 DRM_DEBUG_KMS("FDI train 2 done.\n");
3548 break;
3549 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003550 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003551 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003552 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003553
3554 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003555
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003556}
3557
Akshay Joshi0206e352011-08-16 15:34:10 -04003558static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003559 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3560 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3561 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3562 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3563};
3564
3565/* The FDI link training functions for SNB/Cougarpoint. */
3566static void gen6_fdi_link_train(struct drm_crtc *crtc)
3567{
3568 struct drm_device *dev = crtc->dev;
3569 struct drm_i915_private *dev_priv = dev->dev_private;
3570 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3571 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003572 i915_reg_t reg;
3573 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574
Adam Jacksone1a44742010-06-25 15:32:14 -04003575 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3576 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003577 reg = FDI_RX_IMR(pipe);
3578 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003579 temp &= ~FDI_RX_SYMBOL_LOCK;
3580 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003581 I915_WRITE(reg, temp);
3582
3583 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003584 udelay(150);
3585
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003586 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003587 reg = FDI_TX_CTL(pipe);
3588 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003589 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003590 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003591 temp &= ~FDI_LINK_TRAIN_NONE;
3592 temp |= FDI_LINK_TRAIN_PATTERN_1;
3593 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3594 /* SNB-B */
3595 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003596 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003597
Daniel Vetterd74cf322012-10-26 10:58:13 +02003598 I915_WRITE(FDI_RX_MISC(pipe),
3599 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3600
Chris Wilson5eddb702010-09-11 13:48:45 +01003601 reg = FDI_RX_CTL(pipe);
3602 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003603 if (HAS_PCH_CPT(dev)) {
3604 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3605 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3606 } else {
3607 temp &= ~FDI_LINK_TRAIN_NONE;
3608 temp |= FDI_LINK_TRAIN_PATTERN_1;
3609 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003610 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3611
3612 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003613 udelay(150);
3614
Akshay Joshi0206e352011-08-16 15:34:10 -04003615 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003616 reg = FDI_TX_CTL(pipe);
3617 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003618 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3619 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003620 I915_WRITE(reg, temp);
3621
3622 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003623 udelay(500);
3624
Sean Paulfa37d392012-03-02 12:53:39 -05003625 for (retry = 0; retry < 5; retry++) {
3626 reg = FDI_RX_IIR(pipe);
3627 temp = I915_READ(reg);
3628 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3629 if (temp & FDI_RX_BIT_LOCK) {
3630 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3631 DRM_DEBUG_KMS("FDI train 1 done.\n");
3632 break;
3633 }
3634 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003635 }
Sean Paulfa37d392012-03-02 12:53:39 -05003636 if (retry < 5)
3637 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003638 }
3639 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003640 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003641
3642 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003643 reg = FDI_TX_CTL(pipe);
3644 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003645 temp &= ~FDI_LINK_TRAIN_NONE;
3646 temp |= FDI_LINK_TRAIN_PATTERN_2;
3647 if (IS_GEN6(dev)) {
3648 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3649 /* SNB-B */
3650 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3651 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003652 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003653
Chris Wilson5eddb702010-09-11 13:48:45 +01003654 reg = FDI_RX_CTL(pipe);
3655 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003656 if (HAS_PCH_CPT(dev)) {
3657 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3658 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3659 } else {
3660 temp &= ~FDI_LINK_TRAIN_NONE;
3661 temp |= FDI_LINK_TRAIN_PATTERN_2;
3662 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003663 I915_WRITE(reg, temp);
3664
3665 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003666 udelay(150);
3667
Akshay Joshi0206e352011-08-16 15:34:10 -04003668 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003669 reg = FDI_TX_CTL(pipe);
3670 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003671 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3672 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003673 I915_WRITE(reg, temp);
3674
3675 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003676 udelay(500);
3677
Sean Paulfa37d392012-03-02 12:53:39 -05003678 for (retry = 0; retry < 5; retry++) {
3679 reg = FDI_RX_IIR(pipe);
3680 temp = I915_READ(reg);
3681 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3682 if (temp & FDI_RX_SYMBOL_LOCK) {
3683 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3684 DRM_DEBUG_KMS("FDI train 2 done.\n");
3685 break;
3686 }
3687 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003688 }
Sean Paulfa37d392012-03-02 12:53:39 -05003689 if (retry < 5)
3690 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003691 }
3692 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003693 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003694
3695 DRM_DEBUG_KMS("FDI train done.\n");
3696}
3697
Jesse Barnes357555c2011-04-28 15:09:55 -07003698/* Manual link training for Ivy Bridge A0 parts */
3699static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3700{
3701 struct drm_device *dev = crtc->dev;
3702 struct drm_i915_private *dev_priv = dev->dev_private;
3703 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3704 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003705 i915_reg_t reg;
3706 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003707
3708 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3709 for train result */
3710 reg = FDI_RX_IMR(pipe);
3711 temp = I915_READ(reg);
3712 temp &= ~FDI_RX_SYMBOL_LOCK;
3713 temp &= ~FDI_RX_BIT_LOCK;
3714 I915_WRITE(reg, temp);
3715
3716 POSTING_READ(reg);
3717 udelay(150);
3718
Daniel Vetter01a415f2012-10-27 15:58:40 +02003719 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3720 I915_READ(FDI_RX_IIR(pipe)));
3721
Jesse Barnes139ccd32013-08-19 11:04:55 -07003722 /* Try each vswing and preemphasis setting twice before moving on */
3723 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3724 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003725 reg = FDI_TX_CTL(pipe);
3726 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003727 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3728 temp &= ~FDI_TX_ENABLE;
3729 I915_WRITE(reg, temp);
3730
3731 reg = FDI_RX_CTL(pipe);
3732 temp = I915_READ(reg);
3733 temp &= ~FDI_LINK_TRAIN_AUTO;
3734 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3735 temp &= ~FDI_RX_ENABLE;
3736 I915_WRITE(reg, temp);
3737
3738 /* enable CPU FDI TX and PCH FDI RX */
3739 reg = FDI_TX_CTL(pipe);
3740 temp = I915_READ(reg);
3741 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003742 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003743 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003744 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003745 temp |= snb_b_fdi_train_param[j/2];
3746 temp |= FDI_COMPOSITE_SYNC;
3747 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3748
3749 I915_WRITE(FDI_RX_MISC(pipe),
3750 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3751
3752 reg = FDI_RX_CTL(pipe);
3753 temp = I915_READ(reg);
3754 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3755 temp |= FDI_COMPOSITE_SYNC;
3756 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3757
3758 POSTING_READ(reg);
3759 udelay(1); /* should be 0.5us */
3760
3761 for (i = 0; i < 4; i++) {
3762 reg = FDI_RX_IIR(pipe);
3763 temp = I915_READ(reg);
3764 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3765
3766 if (temp & FDI_RX_BIT_LOCK ||
3767 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3768 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3769 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3770 i);
3771 break;
3772 }
3773 udelay(1); /* should be 0.5us */
3774 }
3775 if (i == 4) {
3776 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3777 continue;
3778 }
3779
3780 /* Train 2 */
3781 reg = FDI_TX_CTL(pipe);
3782 temp = I915_READ(reg);
3783 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3784 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3785 I915_WRITE(reg, temp);
3786
3787 reg = FDI_RX_CTL(pipe);
3788 temp = I915_READ(reg);
3789 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3790 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003791 I915_WRITE(reg, temp);
3792
3793 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003794 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003795
Jesse Barnes139ccd32013-08-19 11:04:55 -07003796 for (i = 0; i < 4; i++) {
3797 reg = FDI_RX_IIR(pipe);
3798 temp = I915_READ(reg);
3799 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003800
Jesse Barnes139ccd32013-08-19 11:04:55 -07003801 if (temp & FDI_RX_SYMBOL_LOCK ||
3802 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3803 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3804 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3805 i);
3806 goto train_done;
3807 }
3808 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003809 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003810 if (i == 4)
3811 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003812 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003813
Jesse Barnes139ccd32013-08-19 11:04:55 -07003814train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003815 DRM_DEBUG_KMS("FDI train done.\n");
3816}
3817
Daniel Vetter88cefb62012-08-12 19:27:14 +02003818static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003819{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003820 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003821 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003822 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003823 i915_reg_t reg;
3824 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003825
Jesse Barnes0e23b992010-09-10 11:10:00 -07003826 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003827 reg = FDI_RX_CTL(pipe);
3828 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003829 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003830 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003831 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003832 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3833
3834 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003835 udelay(200);
3836
3837 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003838 temp = I915_READ(reg);
3839 I915_WRITE(reg, temp | FDI_PCDCLK);
3840
3841 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003842 udelay(200);
3843
Paulo Zanoni20749732012-11-23 15:30:38 -02003844 /* Enable CPU FDI TX PLL, always on for Ironlake */
3845 reg = FDI_TX_CTL(pipe);
3846 temp = I915_READ(reg);
3847 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3848 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003849
Paulo Zanoni20749732012-11-23 15:30:38 -02003850 POSTING_READ(reg);
3851 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003852 }
3853}
3854
Daniel Vetter88cefb62012-08-12 19:27:14 +02003855static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3856{
3857 struct drm_device *dev = intel_crtc->base.dev;
3858 struct drm_i915_private *dev_priv = dev->dev_private;
3859 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003860 i915_reg_t reg;
3861 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003862
3863 /* Switch from PCDclk to Rawclk */
3864 reg = FDI_RX_CTL(pipe);
3865 temp = I915_READ(reg);
3866 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3867
3868 /* Disable CPU FDI TX PLL */
3869 reg = FDI_TX_CTL(pipe);
3870 temp = I915_READ(reg);
3871 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3872
3873 POSTING_READ(reg);
3874 udelay(100);
3875
3876 reg = FDI_RX_CTL(pipe);
3877 temp = I915_READ(reg);
3878 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3879
3880 /* Wait for the clocks to turn off. */
3881 POSTING_READ(reg);
3882 udelay(100);
3883}
3884
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003885static void ironlake_fdi_disable(struct drm_crtc *crtc)
3886{
3887 struct drm_device *dev = crtc->dev;
3888 struct drm_i915_private *dev_priv = dev->dev_private;
3889 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3890 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003891 i915_reg_t reg;
3892 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003893
3894 /* disable CPU FDI tx and PCH FDI rx */
3895 reg = FDI_TX_CTL(pipe);
3896 temp = I915_READ(reg);
3897 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3898 POSTING_READ(reg);
3899
3900 reg = FDI_RX_CTL(pipe);
3901 temp = I915_READ(reg);
3902 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003903 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003904 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3905
3906 POSTING_READ(reg);
3907 udelay(100);
3908
3909 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003910 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003911 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003912
3913 /* still set train pattern 1 */
3914 reg = FDI_TX_CTL(pipe);
3915 temp = I915_READ(reg);
3916 temp &= ~FDI_LINK_TRAIN_NONE;
3917 temp |= FDI_LINK_TRAIN_PATTERN_1;
3918 I915_WRITE(reg, temp);
3919
3920 reg = FDI_RX_CTL(pipe);
3921 temp = I915_READ(reg);
3922 if (HAS_PCH_CPT(dev)) {
3923 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3924 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3925 } else {
3926 temp &= ~FDI_LINK_TRAIN_NONE;
3927 temp |= FDI_LINK_TRAIN_PATTERN_1;
3928 }
3929 /* BPC in FDI rx is consistent with that in PIPECONF */
3930 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003931 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003932 I915_WRITE(reg, temp);
3933
3934 POSTING_READ(reg);
3935 udelay(100);
3936}
3937
Chris Wilson5dce5b932014-01-20 10:17:36 +00003938bool intel_has_pending_fb_unpin(struct drm_device *dev)
3939{
3940 struct intel_crtc *crtc;
3941
3942 /* Note that we don't need to be called with mode_config.lock here
3943 * as our list of CRTC objects is static for the lifetime of the
3944 * device and so cannot disappear as we iterate. Similarly, we can
3945 * happily treat the predicates as racy, atomic checks as userspace
3946 * cannot claim and pin a new fb without at least acquring the
3947 * struct_mutex and so serialising with us.
3948 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003949 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003950 if (atomic_read(&crtc->unpin_work_count) == 0)
3951 continue;
3952
3953 if (crtc->unpin_work)
3954 intel_wait_for_vblank(dev, crtc->pipe);
3955
3956 return true;
3957 }
3958
3959 return false;
3960}
3961
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003962static void page_flip_completed(struct intel_crtc *intel_crtc)
3963{
3964 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3965 struct intel_unpin_work *work = intel_crtc->unpin_work;
3966
3967 /* ensure that the unpin work is consistent wrt ->pending. */
3968 smp_rmb();
3969 intel_crtc->unpin_work = NULL;
3970
3971 if (work->event)
3972 drm_send_vblank_event(intel_crtc->base.dev,
3973 intel_crtc->pipe,
3974 work->event);
3975
3976 drm_crtc_vblank_put(&intel_crtc->base);
3977
3978 wake_up_all(&dev_priv->pending_flip_queue);
3979 queue_work(dev_priv->wq, &work->work);
3980
3981 trace_i915_flip_complete(intel_crtc->plane,
3982 work->pending_flip_obj);
3983}
3984
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003985static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003986{
Chris Wilson0f911282012-04-17 10:05:38 +01003987 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003988 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003989 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003990
Daniel Vetter2c10d572012-12-20 21:24:07 +01003991 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003992
3993 ret = wait_event_interruptible_timeout(
3994 dev_priv->pending_flip_queue,
3995 !intel_crtc_has_pending_flip(crtc),
3996 60*HZ);
3997
3998 if (ret < 0)
3999 return ret;
4000
4001 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01004002 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01004003
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02004004 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01004005 if (intel_crtc->unpin_work) {
4006 WARN_ONCE(1, "Removing stuck page flip\n");
4007 page_flip_completed(intel_crtc);
4008 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02004009 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01004010 }
Chris Wilson5bb61642012-09-27 21:25:58 +01004011
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004012 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004013}
4014
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004015static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
4016{
4017 u32 temp;
4018
4019 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4020
4021 mutex_lock(&dev_priv->sb_lock);
4022
4023 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4024 temp |= SBI_SSCCTL_DISABLE;
4025 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4026
4027 mutex_unlock(&dev_priv->sb_lock);
4028}
4029
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004030/* Program iCLKIP clock to the desired frequency */
4031static void lpt_program_iclkip(struct drm_crtc *crtc)
4032{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004033 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004034 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004035 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4036 u32 temp;
4037
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004038 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004039
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004040 /* The iCLK virtual clock root frequency is in MHz,
4041 * but the adjusted_mode->crtc_clock in in KHz. To get the
4042 * divisors, it is necessary to divide one by another, so we
4043 * convert the virtual clock precision to KHz here for higher
4044 * precision.
4045 */
4046 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004047 u32 iclk_virtual_root_freq = 172800 * 1000;
4048 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004049 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004050
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004051 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4052 clock << auxdiv);
4053 divsel = (desired_divisor / iclk_pi_range) - 2;
4054 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004055
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004056 /*
4057 * Near 20MHz is a corner case which is
4058 * out of range for the 7-bit divisor
4059 */
4060 if (divsel <= 0x7f)
4061 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004062 }
4063
4064 /* This should not happen with any sane values */
4065 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4066 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4067 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4068 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4069
4070 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 +03004071 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004072 auxdiv,
4073 divsel,
4074 phasedir,
4075 phaseinc);
4076
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004077 mutex_lock(&dev_priv->sb_lock);
4078
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004079 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004080 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004081 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4082 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4083 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4084 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4085 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4086 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004087 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004088
4089 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004090 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004091 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4092 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004093 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004094
4095 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004096 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004097 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004098 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004099
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004100 mutex_unlock(&dev_priv->sb_lock);
4101
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004102 /* Wait for initialization time */
4103 udelay(24);
4104
4105 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4106}
4107
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02004108int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4109{
4110 u32 divsel, phaseinc, auxdiv;
4111 u32 iclk_virtual_root_freq = 172800 * 1000;
4112 u32 iclk_pi_range = 64;
4113 u32 desired_divisor;
4114 u32 temp;
4115
4116 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4117 return 0;
4118
4119 mutex_lock(&dev_priv->sb_lock);
4120
4121 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4122 if (temp & SBI_SSCCTL_DISABLE) {
4123 mutex_unlock(&dev_priv->sb_lock);
4124 return 0;
4125 }
4126
4127 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4128 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4129 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4130 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4131 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4132
4133 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4134 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4135 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4136
4137 mutex_unlock(&dev_priv->sb_lock);
4138
4139 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4140
4141 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4142 desired_divisor << auxdiv);
4143}
4144
Daniel Vetter275f01b22013-05-03 11:49:47 +02004145static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4146 enum pipe pch_transcoder)
4147{
4148 struct drm_device *dev = crtc->base.dev;
4149 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004150 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004151
4152 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4153 I915_READ(HTOTAL(cpu_transcoder)));
4154 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4155 I915_READ(HBLANK(cpu_transcoder)));
4156 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4157 I915_READ(HSYNC(cpu_transcoder)));
4158
4159 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4160 I915_READ(VTOTAL(cpu_transcoder)));
4161 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4162 I915_READ(VBLANK(cpu_transcoder)));
4163 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4164 I915_READ(VSYNC(cpu_transcoder)));
4165 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4166 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4167}
4168
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004169static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004170{
4171 struct drm_i915_private *dev_priv = dev->dev_private;
4172 uint32_t temp;
4173
4174 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004175 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004176 return;
4177
4178 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4179 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4180
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004181 temp &= ~FDI_BC_BIFURCATION_SELECT;
4182 if (enable)
4183 temp |= FDI_BC_BIFURCATION_SELECT;
4184
4185 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004186 I915_WRITE(SOUTH_CHICKEN1, temp);
4187 POSTING_READ(SOUTH_CHICKEN1);
4188}
4189
4190static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4191{
4192 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004193
4194 switch (intel_crtc->pipe) {
4195 case PIPE_A:
4196 break;
4197 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004198 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004199 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004200 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004201 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004202
4203 break;
4204 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004205 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004206
4207 break;
4208 default:
4209 BUG();
4210 }
4211}
4212
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004213/* Return which DP Port should be selected for Transcoder DP control */
4214static enum port
4215intel_trans_dp_port_sel(struct drm_crtc *crtc)
4216{
4217 struct drm_device *dev = crtc->dev;
4218 struct intel_encoder *encoder;
4219
4220 for_each_encoder_on_crtc(dev, crtc, encoder) {
4221 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4222 encoder->type == INTEL_OUTPUT_EDP)
4223 return enc_to_dig_port(&encoder->base)->port;
4224 }
4225
4226 return -1;
4227}
4228
Jesse Barnesf67a5592011-01-05 10:31:48 -08004229/*
4230 * Enable PCH resources required for PCH ports:
4231 * - PCH PLLs
4232 * - FDI training & RX/TX
4233 * - update transcoder timings
4234 * - DP transcoding bits
4235 * - transcoder
4236 */
4237static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004238{
4239 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004240 struct drm_i915_private *dev_priv = dev->dev_private;
4241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4242 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004243 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004244
Daniel Vetterab9412b2013-05-03 11:49:46 +02004245 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004246
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004247 if (IS_IVYBRIDGE(dev))
4248 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4249
Daniel Vettercd986ab2012-10-26 10:58:12 +02004250 /* Write the TU size bits before fdi link training, so that error
4251 * detection works. */
4252 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4253 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4254
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004255 /*
4256 * Sometimes spurious CPU pipe underruns happen during FDI
4257 * training, at least with VGA+HDMI cloning. Suppress them.
4258 */
4259 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4260
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004261 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004262 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004263
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004264 /* We need to program the right clock selection before writing the pixel
4265 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004266 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004267 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004268
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004269 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004270 temp |= TRANS_DPLL_ENABLE(pipe);
4271 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004272 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004273 temp |= sel;
4274 else
4275 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004276 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004277 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004278
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004279 /* XXX: pch pll's can be enabled any time before we enable the PCH
4280 * transcoder, and we actually should do this to not upset any PCH
4281 * transcoder that already use the clock when we share it.
4282 *
4283 * Note that enable_shared_dpll tries to do the right thing, but
4284 * get_shared_dpll unconditionally resets the pll - we need that to have
4285 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004286 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004287
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004288 /* set transcoder timing, panel must allow it */
4289 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004290 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004291
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004292 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004293
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004294 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4295
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004296 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004297 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004298 const struct drm_display_mode *adjusted_mode =
4299 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004300 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004301 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004302 temp = I915_READ(reg);
4303 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004304 TRANS_DP_SYNC_MASK |
4305 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004306 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004307 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004308
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004309 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004310 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004311 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004312 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004313
4314 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004315 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004316 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004317 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004318 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004319 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004320 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004321 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004322 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004323 break;
4324 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004325 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004326 }
4327
Chris Wilson5eddb702010-09-11 13:48:45 +01004328 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004329 }
4330
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004331 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004332}
4333
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004334static void lpt_pch_enable(struct drm_crtc *crtc)
4335{
4336 struct drm_device *dev = crtc->dev;
4337 struct drm_i915_private *dev_priv = dev->dev_private;
4338 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004339 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004340
Daniel Vetterab9412b2013-05-03 11:49:46 +02004341 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004342
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004343 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004344
Paulo Zanoni0540e482012-10-31 18:12:40 -02004345 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004346 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004347
Paulo Zanoni937bb612012-10-31 18:12:47 -02004348 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004349}
4350
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004351struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4352 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004353{
Daniel Vettere2b78262013-06-07 23:10:03 +02004354 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004355 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004356 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004357 enum intel_dpll_id i;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01004358 int max = dev_priv->num_shared_dpll;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004359
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004360 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4361
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004362 if (HAS_PCH_IBX(dev_priv->dev)) {
4363 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004364 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004365 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004366
Daniel Vetter46edb022013-06-05 13:34:12 +02004367 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4368 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004369
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004370 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004371
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004372 goto found;
4373 }
4374
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304375 if (IS_BROXTON(dev_priv->dev)) {
4376 /* PLL is attached to port in bxt */
4377 struct intel_encoder *encoder;
4378 struct intel_digital_port *intel_dig_port;
4379
4380 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4381 if (WARN_ON(!encoder))
4382 return NULL;
4383
4384 intel_dig_port = enc_to_dig_port(&encoder->base);
4385 /* 1:1 mapping between ports and PLLs */
4386 i = (enum intel_dpll_id)intel_dig_port->port;
4387 pll = &dev_priv->shared_dplls[i];
4388 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4389 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004390 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304391
4392 goto found;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01004393 } else if (INTEL_INFO(dev_priv)->gen < 9 && HAS_DDI(dev_priv))
4394 /* Do not consider SPLL */
4395 max = 2;
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304396
Maarten Lankhorst00490c22015-11-16 14:42:12 +01004397 for (i = 0; i < max; i++) {
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004398 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004399
4400 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004401 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004402 continue;
4403
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004404 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004405 &shared_dpll[i].hw_state,
4406 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004407 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004408 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004409 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004410 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004411 goto found;
4412 }
4413 }
4414
4415 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004416 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4417 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004418 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004419 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4420 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004421 goto found;
4422 }
4423 }
4424
4425 return NULL;
4426
4427found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004428 if (shared_dpll[i].crtc_mask == 0)
4429 shared_dpll[i].hw_state =
4430 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004431
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004432 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004433 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4434 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004435
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004436 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004437
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004438 return pll;
4439}
4440
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004441static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004442{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004443 struct drm_i915_private *dev_priv = to_i915(state->dev);
4444 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004445 struct intel_shared_dpll *pll;
4446 enum intel_dpll_id i;
4447
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004448 if (!to_intel_atomic_state(state)->dpll_set)
4449 return;
4450
4451 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004452 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4453 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004454 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004455 }
4456}
4457
Daniel Vettera1520312013-05-03 11:49:50 +02004458static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004459{
4460 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004461 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004462 u32 temp;
4463
4464 temp = I915_READ(dslreg);
4465 udelay(500);
4466 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004467 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004468 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004469 }
4470}
4471
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004472static int
4473skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4474 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4475 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004476{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004477 struct intel_crtc_scaler_state *scaler_state =
4478 &crtc_state->scaler_state;
4479 struct intel_crtc *intel_crtc =
4480 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004481 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004482
4483 need_scaling = intel_rotation_90_or_270(rotation) ?
4484 (src_h != dst_w || src_w != dst_h):
4485 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004486
4487 /*
4488 * if plane is being disabled or scaler is no more required or force detach
4489 * - free scaler binded to this plane/crtc
4490 * - in order to do this, update crtc->scaler_usage
4491 *
4492 * Here scaler state in crtc_state is set free so that
4493 * scaler can be assigned to other user. Actual register
4494 * update to free the scaler is done in plane/panel-fit programming.
4495 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4496 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004497 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004498 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004499 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004500 scaler_state->scalers[*scaler_id].in_use = 0;
4501
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004502 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4503 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4504 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004505 scaler_state->scaler_users);
4506 *scaler_id = -1;
4507 }
4508 return 0;
4509 }
4510
4511 /* range checks */
4512 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4513 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4514
4515 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4516 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004517 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004518 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004519 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004520 return -EINVAL;
4521 }
4522
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004523 /* mark this plane as a scaler user in crtc_state */
4524 scaler_state->scaler_users |= (1 << scaler_user);
4525 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4526 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4527 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4528 scaler_state->scaler_users);
4529
4530 return 0;
4531}
4532
4533/**
4534 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4535 *
4536 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004537 *
4538 * Return
4539 * 0 - scaler_usage updated successfully
4540 * error - requested scaling cannot be supported or other error condition
4541 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004542int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004543{
4544 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004545 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004546
4547 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4548 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4549
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004550 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004551 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004552 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004553 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004554}
4555
4556/**
4557 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4558 *
4559 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004560 * @plane_state: atomic plane state to update
4561 *
4562 * Return
4563 * 0 - scaler_usage updated successfully
4564 * error - requested scaling cannot be supported or other error condition
4565 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004566static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4567 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004568{
4569
4570 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004571 struct intel_plane *intel_plane =
4572 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004573 struct drm_framebuffer *fb = plane_state->base.fb;
4574 int ret;
4575
4576 bool force_detach = !fb || !plane_state->visible;
4577
4578 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4579 intel_plane->base.base.id, intel_crtc->pipe,
4580 drm_plane_index(&intel_plane->base));
4581
4582 ret = skl_update_scaler(crtc_state, force_detach,
4583 drm_plane_index(&intel_plane->base),
4584 &plane_state->scaler_id,
4585 plane_state->base.rotation,
4586 drm_rect_width(&plane_state->src) >> 16,
4587 drm_rect_height(&plane_state->src) >> 16,
4588 drm_rect_width(&plane_state->dst),
4589 drm_rect_height(&plane_state->dst));
4590
4591 if (ret || plane_state->scaler_id < 0)
4592 return ret;
4593
Chandra Kondurua1b22782015-04-07 15:28:45 -07004594 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004595 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004596 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004597 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004598 return -EINVAL;
4599 }
4600
4601 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004602 switch (fb->pixel_format) {
4603 case DRM_FORMAT_RGB565:
4604 case DRM_FORMAT_XBGR8888:
4605 case DRM_FORMAT_XRGB8888:
4606 case DRM_FORMAT_ABGR8888:
4607 case DRM_FORMAT_ARGB8888:
4608 case DRM_FORMAT_XRGB2101010:
4609 case DRM_FORMAT_XBGR2101010:
4610 case DRM_FORMAT_YUYV:
4611 case DRM_FORMAT_YVYU:
4612 case DRM_FORMAT_UYVY:
4613 case DRM_FORMAT_VYUY:
4614 break;
4615 default:
4616 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4617 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4618 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004619 }
4620
Chandra Kondurua1b22782015-04-07 15:28:45 -07004621 return 0;
4622}
4623
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004624static void skylake_scaler_disable(struct intel_crtc *crtc)
4625{
4626 int i;
4627
4628 for (i = 0; i < crtc->num_scalers; i++)
4629 skl_detach_scaler(crtc, i);
4630}
4631
4632static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004633{
4634 struct drm_device *dev = crtc->base.dev;
4635 struct drm_i915_private *dev_priv = dev->dev_private;
4636 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004637 struct intel_crtc_scaler_state *scaler_state =
4638 &crtc->config->scaler_state;
4639
4640 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4641
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004642 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004643 int id;
4644
4645 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4646 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4647 return;
4648 }
4649
4650 id = scaler_state->scaler_id;
4651 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4652 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4653 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4654 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4655
4656 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004657 }
4658}
4659
Jesse Barnesb074cec2013-04-25 12:55:02 -07004660static void ironlake_pfit_enable(struct intel_crtc *crtc)
4661{
4662 struct drm_device *dev = crtc->base.dev;
4663 struct drm_i915_private *dev_priv = dev->dev_private;
4664 int pipe = crtc->pipe;
4665
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004666 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004667 /* Force use of hard-coded filter coefficients
4668 * as some pre-programmed values are broken,
4669 * e.g. x201.
4670 */
4671 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4672 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4673 PF_PIPE_SEL_IVB(pipe));
4674 else
4675 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004676 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4677 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004678 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004679}
4680
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004681void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004682{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004683 struct drm_device *dev = crtc->base.dev;
4684 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004685
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004686 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004687 return;
4688
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004689 /* We can only enable IPS after we enable a plane and wait for a vblank */
4690 intel_wait_for_vblank(dev, crtc->pipe);
4691
Paulo Zanonid77e4532013-09-24 13:52:55 -03004692 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004693 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004694 mutex_lock(&dev_priv->rps.hw_lock);
4695 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4696 mutex_unlock(&dev_priv->rps.hw_lock);
4697 /* Quoting Art Runyan: "its not safe to expect any particular
4698 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004699 * mailbox." Moreover, the mailbox may return a bogus state,
4700 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004701 */
4702 } else {
4703 I915_WRITE(IPS_CTL, IPS_ENABLE);
4704 /* The bit only becomes 1 in the next vblank, so this wait here
4705 * is essentially intel_wait_for_vblank. If we don't have this
4706 * and don't wait for vblanks until the end of crtc_enable, then
4707 * the HW state readout code will complain that the expected
4708 * IPS_CTL value is not the one we read. */
4709 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4710 DRM_ERROR("Timed out waiting for IPS enable\n");
4711 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004712}
4713
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004714void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004715{
4716 struct drm_device *dev = crtc->base.dev;
4717 struct drm_i915_private *dev_priv = dev->dev_private;
4718
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004719 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004720 return;
4721
4722 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004723 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004724 mutex_lock(&dev_priv->rps.hw_lock);
4725 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4726 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004727 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4728 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4729 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004730 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004731 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004732 POSTING_READ(IPS_CTL);
4733 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004734
4735 /* We need to wait for a vblank before we can disable the plane. */
4736 intel_wait_for_vblank(dev, crtc->pipe);
4737}
4738
4739/** Loads the palette/gamma unit for the CRTC with the prepared values */
4740static void intel_crtc_load_lut(struct drm_crtc *crtc)
4741{
4742 struct drm_device *dev = crtc->dev;
4743 struct drm_i915_private *dev_priv = dev->dev_private;
4744 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4745 enum pipe pipe = intel_crtc->pipe;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004746 int i;
4747 bool reenable_ips = false;
4748
4749 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004750 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004751 return;
4752
Imre Deak50360402015-01-16 00:55:16 -08004753 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Jani Nikulaa65347b2015-11-27 12:21:46 +02004754 if (intel_crtc->config->has_dsi_encoder)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004755 assert_dsi_pll_enabled(dev_priv);
4756 else
4757 assert_pll_enabled(dev_priv, pipe);
4758 }
4759
Paulo Zanonid77e4532013-09-24 13:52:55 -03004760 /* Workaround : Do not read or write the pipe palette/gamma data while
4761 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4762 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004763 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004764 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4765 GAMMA_MODE_MODE_SPLIT)) {
4766 hsw_disable_ips(intel_crtc);
4767 reenable_ips = true;
4768 }
4769
4770 for (i = 0; i < 256; i++) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004771 i915_reg_t palreg;
Ville Syrjäläf65a9c52015-09-18 20:03:28 +03004772
4773 if (HAS_GMCH_DISPLAY(dev))
4774 palreg = PALETTE(pipe, i);
4775 else
4776 palreg = LGC_PALETTE(pipe, i);
4777
4778 I915_WRITE(palreg,
Paulo Zanonid77e4532013-09-24 13:52:55 -03004779 (intel_crtc->lut_r[i] << 16) |
4780 (intel_crtc->lut_g[i] << 8) |
4781 intel_crtc->lut_b[i]);
4782 }
4783
4784 if (reenable_ips)
4785 hsw_enable_ips(intel_crtc);
4786}
4787
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004788static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004789{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004790 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004791 struct drm_device *dev = intel_crtc->base.dev;
4792 struct drm_i915_private *dev_priv = dev->dev_private;
4793
4794 mutex_lock(&dev->struct_mutex);
4795 dev_priv->mm.interruptible = false;
4796 (void) intel_overlay_switch_off(intel_crtc->overlay);
4797 dev_priv->mm.interruptible = true;
4798 mutex_unlock(&dev->struct_mutex);
4799 }
4800
4801 /* Let userspace switch the overlay on again. In most cases userspace
4802 * has to recompute where to put it anyway.
4803 */
4804}
4805
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004806/**
4807 * intel_post_enable_primary - Perform operations after enabling primary plane
4808 * @crtc: the CRTC whose primary plane was just enabled
4809 *
4810 * Performs potentially sleeping operations that must be done after the primary
4811 * plane is enabled, such as updating FBC and IPS. Note that this may be
4812 * called due to an explicit primary plane update, or due to an implicit
4813 * re-enable that is caused when a sprite plane is updated to no longer
4814 * completely hide the primary plane.
4815 */
4816static void
4817intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004818{
4819 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004820 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004821 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4822 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004823
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004824 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004825 * FIXME IPS should be fine as long as one plane is
4826 * enabled, but in practice it seems to have problems
4827 * when going from primary only to sprite only and vice
4828 * versa.
4829 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004830 hsw_enable_ips(intel_crtc);
4831
Daniel Vetterf99d7062014-06-19 16:01:59 +02004832 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004833 * Gen2 reports pipe underruns whenever all planes are disabled.
4834 * So don't enable underrun reporting before at least some planes
4835 * are enabled.
4836 * FIXME: Need to fix the logic to work when we turn off all planes
4837 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004838 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004839 if (IS_GEN2(dev))
4840 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4841
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004842 /* Underruns don't always raise interrupts, so check manually. */
4843 intel_check_cpu_fifo_underruns(dev_priv);
4844 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004845}
4846
4847/**
4848 * intel_pre_disable_primary - Perform operations before disabling primary plane
4849 * @crtc: the CRTC whose primary plane is to be disabled
4850 *
4851 * Performs potentially sleeping operations that must be done before the
4852 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4853 * be called due to an explicit primary plane update, or due to an implicit
4854 * disable that is caused when a sprite plane completely hides the primary
4855 * plane.
4856 */
4857static void
4858intel_pre_disable_primary(struct drm_crtc *crtc)
4859{
4860 struct drm_device *dev = crtc->dev;
4861 struct drm_i915_private *dev_priv = dev->dev_private;
4862 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4863 int pipe = intel_crtc->pipe;
4864
4865 /*
4866 * Gen2 reports pipe underruns whenever all planes are disabled.
4867 * So diasble underrun reporting before all the planes get disabled.
4868 * FIXME: Need to fix the logic to work when we turn off all planes
4869 * but leave the pipe running.
4870 */
4871 if (IS_GEN2(dev))
4872 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4873
4874 /*
4875 * Vblank time updates from the shadow to live plane control register
4876 * are blocked if the memory self-refresh mode is active at that
4877 * moment. So to make sure the plane gets truly disabled, disable
4878 * first the self-refresh mode. The self-refresh enable bit in turn
4879 * will be checked/applied by the HW only at the next frame start
4880 * event which is after the vblank start event, so we need to have a
4881 * wait-for-vblank between disabling the plane and the pipe.
4882 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004883 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004884 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004885 dev_priv->wm.vlv.cxsr = false;
4886 intel_wait_for_vblank(dev, pipe);
4887 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004888
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004889 /*
4890 * FIXME IPS should be fine as long as one plane is
4891 * enabled, but in practice it seems to have problems
4892 * when going from primary only to sprite only and vice
4893 * versa.
4894 */
4895 hsw_disable_ips(intel_crtc);
4896}
4897
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004898static void intel_post_plane_update(struct intel_crtc *crtc)
4899{
4900 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004901 struct intel_crtc_state *pipe_config =
4902 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004903 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004904
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004905 intel_frontbuffer_flip(dev, atomic->fb_bits);
4906
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004907 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004908
Maarten Lankhorstb9001112015-11-19 16:07:16 +01004909 if (pipe_config->wm_changed && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004910 intel_update_watermarks(&crtc->base);
4911
Paulo Zanonic80ac852015-07-02 19:25:13 -03004912 if (atomic->update_fbc)
Paulo Zanoni1eb52232016-01-19 11:35:44 -02004913 intel_fbc_post_update(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004914
4915 if (atomic->post_enable_primary)
4916 intel_post_enable_primary(&crtc->base);
4917
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004918 memset(atomic, 0, sizeof(*atomic));
4919}
4920
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004921static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004922{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004923 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004924 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004925 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004926 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004927 struct intel_crtc_state *pipe_config =
4928 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004929 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4930 struct drm_plane *primary = crtc->base.primary;
4931 struct drm_plane_state *old_pri_state =
4932 drm_atomic_get_existing_plane_state(old_state, primary);
4933 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004934
Paulo Zanoni1eb52232016-01-19 11:35:44 -02004935 if (atomic->update_fbc)
4936 intel_fbc_pre_update(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004937
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004938 if (old_pri_state) {
4939 struct intel_plane_state *primary_state =
4940 to_intel_plane_state(primary->state);
4941 struct intel_plane_state *old_primary_state =
4942 to_intel_plane_state(old_pri_state);
4943
4944 if (old_primary_state->visible &&
4945 (modeset || !primary_state->visible))
4946 intel_pre_disable_primary(&crtc->base);
4947 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004948
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004949 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004950 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004951
4952 if (old_crtc_state->base.active)
4953 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä852eb002015-06-24 22:00:07 +03004954 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004955
Matt Ropered4a6a72016-02-23 17:20:13 -08004956 /*
4957 * IVB workaround: must disable low power watermarks for at least
4958 * one frame before enabling scaling. LP watermarks can be re-enabled
4959 * when scaling is disabled.
4960 *
4961 * WaCxSRDisabledForSpriteScaling:ivb
4962 */
4963 if (pipe_config->disable_lp_wm) {
4964 ilk_disable_lp_wm(dev);
4965 intel_wait_for_vblank(dev, crtc->pipe);
4966 }
4967
4968 /*
4969 * If we're doing a modeset, we're done. No need to do any pre-vblank
4970 * watermark programming here.
4971 */
4972 if (needs_modeset(&pipe_config->base))
4973 return;
4974
4975 /*
4976 * For platforms that support atomic watermarks, program the
4977 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4978 * will be the intermediate values that are safe for both pre- and
4979 * post- vblank; when vblank happens, the 'active' values will be set
4980 * to the final 'target' values and we'll do this again to get the
4981 * optimal watermarks. For gen9+ platforms, the values we program here
4982 * will be the final target values which will get automatically latched
4983 * at vblank time; no further programming will be necessary.
4984 *
4985 * If a platform hasn't been transitioned to atomic watermarks yet,
4986 * we'll continue to update watermarks the old way, if flags tell
4987 * us to.
4988 */
4989 if (dev_priv->display.initial_watermarks != NULL)
4990 dev_priv->display.initial_watermarks(pipe_config);
4991 else if (pipe_config->wm_changed)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004992 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004993}
4994
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004995static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004996{
4997 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004998 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004999 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005000 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005001
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005002 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03005003
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005004 drm_for_each_plane_mask(p, dev, plane_mask)
5005 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03005006
Daniel Vetterf99d7062014-06-19 16:01:59 +02005007 /*
5008 * FIXME: Once we grow proper nuclear flip support out of this we need
5009 * to compute the mask of flip planes precisely. For the time being
5010 * consider this a flip to a NULL plane.
5011 */
5012 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005013}
5014
Jesse Barnesf67a5592011-01-05 10:31:48 -08005015static void ironlake_crtc_enable(struct drm_crtc *crtc)
5016{
5017 struct drm_device *dev = crtc->dev;
5018 struct drm_i915_private *dev_priv = dev->dev_private;
5019 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005020 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08005021 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08005022
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005023 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08005024 return;
5025
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005026 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005027 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5028
5029 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02005030 intel_prepare_shared_dpll(intel_crtc);
5031
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005032 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305033 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005034
5035 intel_set_pipe_timings(intel_crtc);
5036
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005037 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02005038 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005039 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005040 }
5041
5042 ironlake_set_pipeconf(crtc);
5043
Jesse Barnesf67a5592011-01-05 10:31:48 -08005044 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005045
Daniel Vettera72e4c92014-09-30 10:56:47 +02005046 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03005047
Daniel Vetterf6736a12013-06-05 13:34:30 +02005048 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02005049 if (encoder->pre_enable)
5050 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005051
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005052 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02005053 /* Note: FDI PLL enabling _must_ be done before we enable the
5054 * cpu pipes, hence this is separate from all the other fdi/pch
5055 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02005056 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02005057 } else {
5058 assert_fdi_tx_disabled(dev_priv, pipe);
5059 assert_fdi_rx_disabled(dev_priv, pipe);
5060 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005061
Jesse Barnesb074cec2013-04-25 12:55:02 -07005062 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005063
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005064 /*
5065 * On ILK+ LUT must be loaded before the pipe is running but with
5066 * clocks enabled
5067 */
5068 intel_crtc_load_lut(crtc);
5069
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005070 if (dev_priv->display.initial_watermarks != NULL)
5071 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005072 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005073
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005074 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005075 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005076
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005077 assert_vblank_disabled(crtc);
5078 drm_crtc_vblank_on(crtc);
5079
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02005080 for_each_encoder_on_crtc(dev, crtc, encoder)
5081 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02005082
5083 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02005084 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005085
5086 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5087 if (intel_crtc->config->has_pch_encoder)
5088 intel_wait_for_vblank(dev, pipe);
5089 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005090}
5091
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005092/* IPS only exists on ULT machines and is tied to pipe A. */
5093static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5094{
Damien Lespiauf5adf942013-06-24 18:29:34 +01005095 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005096}
5097
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005098static void haswell_crtc_enable(struct drm_crtc *crtc)
5099{
5100 struct drm_device *dev = crtc->dev;
5101 struct drm_i915_private *dev_priv = dev->dev_private;
5102 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5103 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005104 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
5105 struct intel_crtc_state *pipe_config =
5106 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005107
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005108 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005109 return;
5110
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005111 if (intel_crtc->config->has_pch_encoder)
5112 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5113 false);
5114
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005115 if (intel_crtc_to_shared_dpll(intel_crtc))
5116 intel_enable_shared_dpll(intel_crtc);
5117
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005118 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305119 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005120
5121 intel_set_pipe_timings(intel_crtc);
5122
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005123 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5124 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5125 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005126 }
5127
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005128 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005129 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005130 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005131 }
5132
5133 haswell_set_pipeconf(crtc);
5134
5135 intel_set_pipe_csc(crtc);
5136
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005137 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005138
Daniel Vetter6b698512015-11-28 11:05:39 +01005139 if (intel_crtc->config->has_pch_encoder)
5140 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5141 else
5142 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5143
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305144 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005145 if (encoder->pre_enable)
5146 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305147 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005148
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005149 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03005150 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03005151
Jani Nikulaa65347b2015-11-27 12:21:46 +02005152 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305153 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005154
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005155 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005156 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005157 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005158 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005159
5160 /*
5161 * On ILK+ LUT must be loaded before the pipe is running but with
5162 * clocks enabled
5163 */
5164 intel_crtc_load_lut(crtc);
5165
Paulo Zanoni1f544382012-10-24 11:32:00 -02005166 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02005167 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305168 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005169
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005170 if (dev_priv->display.initial_watermarks != NULL)
5171 dev_priv->display.initial_watermarks(pipe_config);
5172 else
5173 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005174 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005175
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005176 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005177 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005178
Jani Nikulaa65347b2015-11-27 12:21:46 +02005179 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005180 intel_ddi_set_vc_payload_alloc(crtc, true);
5181
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005182 assert_vblank_disabled(crtc);
5183 drm_crtc_vblank_on(crtc);
5184
Jani Nikula8807e552013-08-30 19:40:32 +03005185 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005186 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005187 intel_opregion_notify_encoder(encoder, true);
5188 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005189
Daniel Vetter6b698512015-11-28 11:05:39 +01005190 if (intel_crtc->config->has_pch_encoder) {
5191 intel_wait_for_vblank(dev, pipe);
5192 intel_wait_for_vblank(dev, pipe);
5193 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005194 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5195 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01005196 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005197
Paulo Zanonie4916942013-09-20 16:21:19 -03005198 /* If we change the relative order between pipe/planes enabling, we need
5199 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005200 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5201 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5202 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5203 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5204 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005205}
5206
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005207static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005208{
5209 struct drm_device *dev = crtc->base.dev;
5210 struct drm_i915_private *dev_priv = dev->dev_private;
5211 int pipe = crtc->pipe;
5212
5213 /* To avoid upsetting the power well on haswell only disable the pfit if
5214 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005215 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005216 I915_WRITE(PF_CTL(pipe), 0);
5217 I915_WRITE(PF_WIN_POS(pipe), 0);
5218 I915_WRITE(PF_WIN_SZ(pipe), 0);
5219 }
5220}
5221
Jesse Barnes6be4a602010-09-10 10:26:01 -07005222static void ironlake_crtc_disable(struct drm_crtc *crtc)
5223{
5224 struct drm_device *dev = crtc->dev;
5225 struct drm_i915_private *dev_priv = dev->dev_private;
5226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005227 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005228 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005229
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005230 if (intel_crtc->config->has_pch_encoder)
5231 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5232
Daniel Vetterea9d7582012-07-10 10:42:52 +02005233 for_each_encoder_on_crtc(dev, crtc, encoder)
5234 encoder->disable(encoder);
5235
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005236 drm_crtc_vblank_off(crtc);
5237 assert_vblank_disabled(crtc);
5238
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005239 /*
5240 * Sometimes spurious CPU pipe underruns happen when the
5241 * pipe is already disabled, but FDI RX/TX is still enabled.
5242 * Happens at least with VGA+HDMI cloning. Suppress them.
5243 */
5244 if (intel_crtc->config->has_pch_encoder)
5245 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5246
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005247 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005248
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005249 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005250
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005251 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005252 ironlake_fdi_disable(crtc);
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02005253 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5254 }
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005255
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005256 for_each_encoder_on_crtc(dev, crtc, encoder)
5257 if (encoder->post_disable)
5258 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005259
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005260 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005261 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005262
Daniel Vetterd925c592013-06-05 13:34:04 +02005263 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005264 i915_reg_t reg;
5265 u32 temp;
5266
Daniel Vetterd925c592013-06-05 13:34:04 +02005267 /* disable TRANS_DP_CTL */
5268 reg = TRANS_DP_CTL(pipe);
5269 temp = I915_READ(reg);
5270 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5271 TRANS_DP_PORT_SEL_MASK);
5272 temp |= TRANS_DP_PORT_SEL_NONE;
5273 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005274
Daniel Vetterd925c592013-06-05 13:34:04 +02005275 /* disable DPLL_SEL */
5276 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005277 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005278 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005279 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005280
Daniel Vetterd925c592013-06-05 13:34:04 +02005281 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005282 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005283
5284 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005285}
5286
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005287static void haswell_crtc_disable(struct drm_crtc *crtc)
5288{
5289 struct drm_device *dev = crtc->dev;
5290 struct drm_i915_private *dev_priv = dev->dev_private;
5291 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5292 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005293 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005294
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005295 if (intel_crtc->config->has_pch_encoder)
5296 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5297 false);
5298
Jani Nikula8807e552013-08-30 19:40:32 +03005299 for_each_encoder_on_crtc(dev, crtc, encoder) {
5300 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005301 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005302 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005303
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005304 drm_crtc_vblank_off(crtc);
5305 assert_vblank_disabled(crtc);
5306
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005307 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005308
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005309 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005310 intel_ddi_set_vc_payload_alloc(crtc, false);
5311
Jani Nikulaa65347b2015-11-27 12:21:46 +02005312 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305313 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005314
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005315 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005316 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005317 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005318 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005319
Jani Nikulaa65347b2015-11-27 12:21:46 +02005320 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305321 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005322
Imre Deak97b040a2014-06-25 22:01:50 +03005323 for_each_encoder_on_crtc(dev, crtc, encoder)
5324 if (encoder->post_disable)
5325 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005326
Ville Syrjälä92966a32015-12-08 16:05:48 +02005327 if (intel_crtc->config->has_pch_encoder) {
5328 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005329 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005330 intel_ddi_fdi_disable(crtc);
5331
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005332 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5333 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005334 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005335}
5336
Jesse Barnes2dd24552013-04-25 12:55:01 -07005337static void i9xx_pfit_enable(struct intel_crtc *crtc)
5338{
5339 struct drm_device *dev = crtc->base.dev;
5340 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005341 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005342
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005343 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005344 return;
5345
Daniel Vetterc0b03412013-05-28 12:05:54 +02005346 /*
5347 * The panel fitter should only be adjusted whilst the pipe is disabled,
5348 * according to register description and PRM.
5349 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005350 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5351 assert_pipe_disabled(dev_priv, crtc->pipe);
5352
Jesse Barnesb074cec2013-04-25 12:55:02 -07005353 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5354 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005355
5356 /* Border color in case we don't scale up to the full screen. Black by
5357 * default, change to something else for debugging. */
5358 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005359}
5360
Dave Airlied05410f2014-06-05 13:22:59 +10005361static enum intel_display_power_domain port_to_power_domain(enum port port)
5362{
5363 switch (port) {
5364 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005365 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005366 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005367 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005368 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005369 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005370 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005371 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005372 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005373 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005374 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005375 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005376 return POWER_DOMAIN_PORT_OTHER;
5377 }
5378}
5379
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005380static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5381{
5382 switch (port) {
5383 case PORT_A:
5384 return POWER_DOMAIN_AUX_A;
5385 case PORT_B:
5386 return POWER_DOMAIN_AUX_B;
5387 case PORT_C:
5388 return POWER_DOMAIN_AUX_C;
5389 case PORT_D:
5390 return POWER_DOMAIN_AUX_D;
5391 case PORT_E:
5392 /* FIXME: Check VBT for actual wiring of PORT E */
5393 return POWER_DOMAIN_AUX_D;
5394 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005395 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005396 return POWER_DOMAIN_AUX_A;
5397 }
5398}
5399
Imre Deak319be8a2014-03-04 19:22:57 +02005400enum intel_display_power_domain
5401intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005402{
Imre Deak319be8a2014-03-04 19:22:57 +02005403 struct drm_device *dev = intel_encoder->base.dev;
5404 struct intel_digital_port *intel_dig_port;
5405
5406 switch (intel_encoder->type) {
5407 case INTEL_OUTPUT_UNKNOWN:
5408 /* Only DDI platforms should ever use this output type */
5409 WARN_ON_ONCE(!HAS_DDI(dev));
5410 case INTEL_OUTPUT_DISPLAYPORT:
5411 case INTEL_OUTPUT_HDMI:
5412 case INTEL_OUTPUT_EDP:
5413 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005414 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005415 case INTEL_OUTPUT_DP_MST:
5416 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5417 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005418 case INTEL_OUTPUT_ANALOG:
5419 return POWER_DOMAIN_PORT_CRT;
5420 case INTEL_OUTPUT_DSI:
5421 return POWER_DOMAIN_PORT_DSI;
5422 default:
5423 return POWER_DOMAIN_PORT_OTHER;
5424 }
5425}
5426
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005427enum intel_display_power_domain
5428intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5429{
5430 struct drm_device *dev = intel_encoder->base.dev;
5431 struct intel_digital_port *intel_dig_port;
5432
5433 switch (intel_encoder->type) {
5434 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005435 case INTEL_OUTPUT_HDMI:
5436 /*
5437 * Only DDI platforms should ever use these output types.
5438 * We can get here after the HDMI detect code has already set
5439 * the type of the shared encoder. Since we can't be sure
5440 * what's the status of the given connectors, play safe and
5441 * run the DP detection too.
5442 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005443 WARN_ON_ONCE(!HAS_DDI(dev));
5444 case INTEL_OUTPUT_DISPLAYPORT:
5445 case INTEL_OUTPUT_EDP:
5446 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5447 return port_to_aux_power_domain(intel_dig_port->port);
5448 case INTEL_OUTPUT_DP_MST:
5449 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5450 return port_to_aux_power_domain(intel_dig_port->port);
5451 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005452 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005453 return POWER_DOMAIN_AUX_A;
5454 }
5455}
5456
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005457static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5458 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005459{
5460 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005461 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005462 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5463 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005464 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005465 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005466
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005467 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005468 return 0;
5469
Imre Deak77d22dc2014-03-05 16:20:52 +02005470 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5471 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005472 if (crtc_state->pch_pfit.enabled ||
5473 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005474 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5475
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005476 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5477 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5478
Imre Deak319be8a2014-03-04 19:22:57 +02005479 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005480 }
Imre Deak319be8a2014-03-04 19:22:57 +02005481
Imre Deak77d22dc2014-03-05 16:20:52 +02005482 return mask;
5483}
5484
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005485static unsigned long
5486modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5487 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005488{
5489 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5490 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5491 enum intel_display_power_domain domain;
5492 unsigned long domains, new_domains, old_domains;
5493
5494 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005495 intel_crtc->enabled_power_domains = new_domains =
5496 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005497
5498 domains = new_domains & ~old_domains;
5499
5500 for_each_power_domain(domain, domains)
5501 intel_display_power_get(dev_priv, domain);
5502
5503 return old_domains & ~new_domains;
5504}
5505
5506static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5507 unsigned long domains)
5508{
5509 enum intel_display_power_domain domain;
5510
5511 for_each_power_domain(domain, domains)
5512 intel_display_power_put(dev_priv, domain);
5513}
5514
Mika Kaholaadafdc62015-08-18 14:36:59 +03005515static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5516{
5517 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5518
5519 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5520 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5521 return max_cdclk_freq;
5522 else if (IS_CHERRYVIEW(dev_priv))
5523 return max_cdclk_freq*95/100;
5524 else if (INTEL_INFO(dev_priv)->gen < 4)
5525 return 2*max_cdclk_freq*90/100;
5526 else
5527 return max_cdclk_freq*90/100;
5528}
5529
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005530static void intel_update_max_cdclk(struct drm_device *dev)
5531{
5532 struct drm_i915_private *dev_priv = dev->dev_private;
5533
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005534 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005535 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5536
5537 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5538 dev_priv->max_cdclk_freq = 675000;
5539 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5540 dev_priv->max_cdclk_freq = 540000;
5541 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5542 dev_priv->max_cdclk_freq = 450000;
5543 else
5544 dev_priv->max_cdclk_freq = 337500;
5545 } else if (IS_BROADWELL(dev)) {
5546 /*
5547 * FIXME with extra cooling we can allow
5548 * 540 MHz for ULX and 675 Mhz for ULT.
5549 * How can we know if extra cooling is
5550 * available? PCI ID, VTB, something else?
5551 */
5552 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5553 dev_priv->max_cdclk_freq = 450000;
5554 else if (IS_BDW_ULX(dev))
5555 dev_priv->max_cdclk_freq = 450000;
5556 else if (IS_BDW_ULT(dev))
5557 dev_priv->max_cdclk_freq = 540000;
5558 else
5559 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005560 } else if (IS_CHERRYVIEW(dev)) {
5561 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005562 } else if (IS_VALLEYVIEW(dev)) {
5563 dev_priv->max_cdclk_freq = 400000;
5564 } else {
5565 /* otherwise assume cdclk is fixed */
5566 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5567 }
5568
Mika Kaholaadafdc62015-08-18 14:36:59 +03005569 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5570
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005571 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5572 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005573
5574 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5575 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005576}
5577
5578static void intel_update_cdclk(struct drm_device *dev)
5579{
5580 struct drm_i915_private *dev_priv = dev->dev_private;
5581
5582 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5583 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5584 dev_priv->cdclk_freq);
5585
5586 /*
5587 * Program the gmbus_freq based on the cdclk frequency.
5588 * BSpec erroneously claims we should aim for 4MHz, but
5589 * in fact 1MHz is the correct frequency.
5590 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005591 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005592 /*
5593 * Program the gmbus_freq based on the cdclk frequency.
5594 * BSpec erroneously claims we should aim for 4MHz, but
5595 * in fact 1MHz is the correct frequency.
5596 */
5597 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5598 }
5599
5600 if (dev_priv->max_cdclk_freq == 0)
5601 intel_update_max_cdclk(dev);
5602}
5603
Damien Lespiau70d0c572015-06-04 18:21:29 +01005604static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305605{
5606 struct drm_i915_private *dev_priv = dev->dev_private;
5607 uint32_t divider;
5608 uint32_t ratio;
5609 uint32_t current_freq;
5610 int ret;
5611
5612 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5613 switch (frequency) {
5614 case 144000:
5615 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5616 ratio = BXT_DE_PLL_RATIO(60);
5617 break;
5618 case 288000:
5619 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5620 ratio = BXT_DE_PLL_RATIO(60);
5621 break;
5622 case 384000:
5623 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5624 ratio = BXT_DE_PLL_RATIO(60);
5625 break;
5626 case 576000:
5627 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5628 ratio = BXT_DE_PLL_RATIO(60);
5629 break;
5630 case 624000:
5631 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5632 ratio = BXT_DE_PLL_RATIO(65);
5633 break;
5634 case 19200:
5635 /*
5636 * Bypass frequency with DE PLL disabled. Init ratio, divider
5637 * to suppress GCC warning.
5638 */
5639 ratio = 0;
5640 divider = 0;
5641 break;
5642 default:
5643 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5644
5645 return;
5646 }
5647
5648 mutex_lock(&dev_priv->rps.hw_lock);
5649 /* Inform power controller of upcoming frequency change */
5650 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5651 0x80000000);
5652 mutex_unlock(&dev_priv->rps.hw_lock);
5653
5654 if (ret) {
5655 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5656 ret, frequency);
5657 return;
5658 }
5659
5660 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5661 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5662 current_freq = current_freq * 500 + 1000;
5663
5664 /*
5665 * DE PLL has to be disabled when
5666 * - setting to 19.2MHz (bypass, PLL isn't used)
5667 * - before setting to 624MHz (PLL needs toggling)
5668 * - before setting to any frequency from 624MHz (PLL needs toggling)
5669 */
5670 if (frequency == 19200 || frequency == 624000 ||
5671 current_freq == 624000) {
5672 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5673 /* Timeout 200us */
5674 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5675 1))
5676 DRM_ERROR("timout waiting for DE PLL unlock\n");
5677 }
5678
5679 if (frequency != 19200) {
5680 uint32_t val;
5681
5682 val = I915_READ(BXT_DE_PLL_CTL);
5683 val &= ~BXT_DE_PLL_RATIO_MASK;
5684 val |= ratio;
5685 I915_WRITE(BXT_DE_PLL_CTL, val);
5686
5687 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5688 /* Timeout 200us */
5689 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5690 DRM_ERROR("timeout waiting for DE PLL lock\n");
5691
5692 val = I915_READ(CDCLK_CTL);
5693 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5694 val |= divider;
5695 /*
5696 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5697 * enable otherwise.
5698 */
5699 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5700 if (frequency >= 500000)
5701 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5702
5703 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5704 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5705 val |= (frequency - 1000) / 500;
5706 I915_WRITE(CDCLK_CTL, val);
5707 }
5708
5709 mutex_lock(&dev_priv->rps.hw_lock);
5710 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5711 DIV_ROUND_UP(frequency, 25000));
5712 mutex_unlock(&dev_priv->rps.hw_lock);
5713
5714 if (ret) {
5715 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5716 ret, frequency);
5717 return;
5718 }
5719
Damien Lespiaua47871b2015-06-04 18:21:34 +01005720 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305721}
5722
5723void broxton_init_cdclk(struct drm_device *dev)
5724{
5725 struct drm_i915_private *dev_priv = dev->dev_private;
5726 uint32_t val;
5727
5728 /*
5729 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5730 * or else the reset will hang because there is no PCH to respond.
5731 * Move the handshake programming to initialization sequence.
5732 * Previously was left up to BIOS.
5733 */
5734 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5735 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5736 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5737
5738 /* Enable PG1 for cdclk */
5739 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5740
5741 /* check if cd clock is enabled */
5742 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5743 DRM_DEBUG_KMS("Display already initialized\n");
5744 return;
5745 }
5746
5747 /*
5748 * FIXME:
5749 * - The initial CDCLK needs to be read from VBT.
5750 * Need to make this change after VBT has changes for BXT.
5751 * - check if setting the max (or any) cdclk freq is really necessary
5752 * here, it belongs to modeset time
5753 */
5754 broxton_set_cdclk(dev, 624000);
5755
5756 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005757 POSTING_READ(DBUF_CTL);
5758
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305759 udelay(10);
5760
5761 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5762 DRM_ERROR("DBuf power enable timeout!\n");
5763}
5764
5765void broxton_uninit_cdclk(struct drm_device *dev)
5766{
5767 struct drm_i915_private *dev_priv = dev->dev_private;
5768
5769 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005770 POSTING_READ(DBUF_CTL);
5771
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305772 udelay(10);
5773
5774 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5775 DRM_ERROR("DBuf power disable timeout!\n");
5776
5777 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5778 broxton_set_cdclk(dev, 19200);
5779
5780 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5781}
5782
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005783static const struct skl_cdclk_entry {
5784 unsigned int freq;
5785 unsigned int vco;
5786} skl_cdclk_frequencies[] = {
5787 { .freq = 308570, .vco = 8640 },
5788 { .freq = 337500, .vco = 8100 },
5789 { .freq = 432000, .vco = 8640 },
5790 { .freq = 450000, .vco = 8100 },
5791 { .freq = 540000, .vco = 8100 },
5792 { .freq = 617140, .vco = 8640 },
5793 { .freq = 675000, .vco = 8100 },
5794};
5795
5796static unsigned int skl_cdclk_decimal(unsigned int freq)
5797{
5798 return (freq - 1000) / 500;
5799}
5800
5801static unsigned int skl_cdclk_get_vco(unsigned int freq)
5802{
5803 unsigned int i;
5804
5805 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5806 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5807
5808 if (e->freq == freq)
5809 return e->vco;
5810 }
5811
5812 return 8100;
5813}
5814
5815static void
5816skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5817{
5818 unsigned int min_freq;
5819 u32 val;
5820
5821 /* select the minimum CDCLK before enabling DPLL 0 */
5822 val = I915_READ(CDCLK_CTL);
5823 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5824 val |= CDCLK_FREQ_337_308;
5825
5826 if (required_vco == 8640)
5827 min_freq = 308570;
5828 else
5829 min_freq = 337500;
5830
5831 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5832
5833 I915_WRITE(CDCLK_CTL, val);
5834 POSTING_READ(CDCLK_CTL);
5835
5836 /*
5837 * We always enable DPLL0 with the lowest link rate possible, but still
5838 * taking into account the VCO required to operate the eDP panel at the
5839 * desired frequency. The usual DP link rates operate with a VCO of
5840 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5841 * The modeset code is responsible for the selection of the exact link
5842 * rate later on, with the constraint of choosing a frequency that
5843 * works with required_vco.
5844 */
5845 val = I915_READ(DPLL_CTRL1);
5846
5847 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5848 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5849 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5850 if (required_vco == 8640)
5851 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5852 SKL_DPLL0);
5853 else
5854 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5855 SKL_DPLL0);
5856
5857 I915_WRITE(DPLL_CTRL1, val);
5858 POSTING_READ(DPLL_CTRL1);
5859
5860 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5861
5862 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5863 DRM_ERROR("DPLL0 not locked\n");
5864}
5865
5866static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5867{
5868 int ret;
5869 u32 val;
5870
5871 /* inform PCU we want to change CDCLK */
5872 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5873 mutex_lock(&dev_priv->rps.hw_lock);
5874 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5875 mutex_unlock(&dev_priv->rps.hw_lock);
5876
5877 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5878}
5879
5880static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5881{
5882 unsigned int i;
5883
5884 for (i = 0; i < 15; i++) {
5885 if (skl_cdclk_pcu_ready(dev_priv))
5886 return true;
5887 udelay(10);
5888 }
5889
5890 return false;
5891}
5892
5893static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5894{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005895 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005896 u32 freq_select, pcu_ack;
5897
5898 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5899
5900 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5901 DRM_ERROR("failed to inform PCU about cdclk change\n");
5902 return;
5903 }
5904
5905 /* set CDCLK_CTL */
5906 switch(freq) {
5907 case 450000:
5908 case 432000:
5909 freq_select = CDCLK_FREQ_450_432;
5910 pcu_ack = 1;
5911 break;
5912 case 540000:
5913 freq_select = CDCLK_FREQ_540;
5914 pcu_ack = 2;
5915 break;
5916 case 308570:
5917 case 337500:
5918 default:
5919 freq_select = CDCLK_FREQ_337_308;
5920 pcu_ack = 0;
5921 break;
5922 case 617140:
5923 case 675000:
5924 freq_select = CDCLK_FREQ_675_617;
5925 pcu_ack = 3;
5926 break;
5927 }
5928
5929 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5930 POSTING_READ(CDCLK_CTL);
5931
5932 /* inform PCU of the change */
5933 mutex_lock(&dev_priv->rps.hw_lock);
5934 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5935 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005936
5937 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005938}
5939
5940void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5941{
5942 /* disable DBUF power */
5943 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5944 POSTING_READ(DBUF_CTL);
5945
5946 udelay(10);
5947
5948 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5949 DRM_ERROR("DBuf power disable timeout\n");
5950
Imre Deakab96c1ee2015-11-04 19:24:18 +02005951 /* disable DPLL0 */
5952 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5953 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5954 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005955}
5956
5957void skl_init_cdclk(struct drm_i915_private *dev_priv)
5958{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005959 unsigned int required_vco;
5960
Gary Wang39d9b852015-08-28 16:40:34 +08005961 /* DPLL0 not enabled (happens on early BIOS versions) */
5962 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5963 /* enable DPLL0 */
5964 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5965 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005966 }
5967
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005968 /* set CDCLK to the frequency the BIOS chose */
5969 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5970
5971 /* enable DBUF power */
5972 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5973 POSTING_READ(DBUF_CTL);
5974
5975 udelay(10);
5976
5977 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5978 DRM_ERROR("DBuf power enable timeout\n");
5979}
5980
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305981int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5982{
5983 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5984 uint32_t cdctl = I915_READ(CDCLK_CTL);
5985 int freq = dev_priv->skl_boot_cdclk;
5986
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305987 /*
5988 * check if the pre-os intialized the display
5989 * There is SWF18 scratchpad register defined which is set by the
5990 * pre-os which can be used by the OS drivers to check the status
5991 */
5992 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5993 goto sanitize;
5994
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305995 /* Is PLL enabled and locked ? */
5996 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5997 goto sanitize;
5998
5999 /* DPLL okay; verify the cdclock
6000 *
6001 * Noticed in some instances that the freq selection is correct but
6002 * decimal part is programmed wrong from BIOS where pre-os does not
6003 * enable display. Verify the same as well.
6004 */
6005 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
6006 /* All well; nothing to sanitize */
6007 return false;
6008sanitize:
6009 /*
6010 * As of now initialize with max cdclk till
6011 * we get dynamic cdclk support
6012 * */
6013 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
6014 skl_init_cdclk(dev_priv);
6015
6016 /* we did have to sanitize */
6017 return true;
6018}
6019
Jesse Barnes30a970c2013-11-04 13:48:12 -08006020/* Adjust CDclk dividers to allow high res or save power if possible */
6021static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
6022{
6023 struct drm_i915_private *dev_priv = dev->dev_private;
6024 u32 val, cmd;
6025
Vandana Kannan164dfd22014-11-24 13:37:41 +05306026 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6027 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02006028
Ville Syrjälädfcab172014-06-13 13:37:47 +03006029 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08006030 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03006031 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006032 cmd = 1;
6033 else
6034 cmd = 0;
6035
6036 mutex_lock(&dev_priv->rps.hw_lock);
6037 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6038 val &= ~DSPFREQGUAR_MASK;
6039 val |= (cmd << DSPFREQGUAR_SHIFT);
6040 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6041 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6042 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
6043 50)) {
6044 DRM_ERROR("timed out waiting for CDclk change\n");
6045 }
6046 mutex_unlock(&dev_priv->rps.hw_lock);
6047
Ville Syrjälä54433e92015-05-26 20:42:31 +03006048 mutex_lock(&dev_priv->sb_lock);
6049
Ville Syrjälädfcab172014-06-13 13:37:47 +03006050 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006051 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006052
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006053 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006054
Jesse Barnes30a970c2013-11-04 13:48:12 -08006055 /* adjust cdclk divider */
6056 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03006057 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006058 val |= divider;
6059 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03006060
6061 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03006062 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03006063 50))
6064 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08006065 }
6066
Jesse Barnes30a970c2013-11-04 13:48:12 -08006067 /* adjust self-refresh exit latency value */
6068 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
6069 val &= ~0x7f;
6070
6071 /*
6072 * For high bandwidth configs, we set a higher latency in the bunit
6073 * so that the core display fetch happens in time to avoid underruns.
6074 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03006075 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006076 val |= 4500 / 250; /* 4.5 usec */
6077 else
6078 val |= 3000 / 250; /* 3.0 usec */
6079 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03006080
Ville Syrjäläa5805162015-05-26 20:42:30 +03006081 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006082
Ville Syrjäläb6283052015-06-03 15:45:07 +03006083 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006084}
6085
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006086static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6087{
6088 struct drm_i915_private *dev_priv = dev->dev_private;
6089 u32 val, cmd;
6090
Vandana Kannan164dfd22014-11-24 13:37:41 +05306091 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6092 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006093
6094 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006095 case 333333:
6096 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006097 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006098 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006099 break;
6100 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01006101 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006102 return;
6103 }
6104
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02006105 /*
6106 * Specs are full of misinformation, but testing on actual
6107 * hardware has shown that we just need to write the desired
6108 * CCK divider into the Punit register.
6109 */
6110 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6111
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006112 mutex_lock(&dev_priv->rps.hw_lock);
6113 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6114 val &= ~DSPFREQGUAR_MASK_CHV;
6115 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6116 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6117 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6118 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6119 50)) {
6120 DRM_ERROR("timed out waiting for CDclk change\n");
6121 }
6122 mutex_unlock(&dev_priv->rps.hw_lock);
6123
Ville Syrjäläb6283052015-06-03 15:45:07 +03006124 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006125}
6126
Jesse Barnes30a970c2013-11-04 13:48:12 -08006127static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6128 int max_pixclk)
6129{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006130 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006131 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006132
Jesse Barnes30a970c2013-11-04 13:48:12 -08006133 /*
6134 * Really only a few cases to deal with, as only 4 CDclks are supported:
6135 * 200MHz
6136 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006137 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006138 * 400MHz (VLV only)
6139 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6140 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006141 *
6142 * We seem to get an unstable or solid color picture at 200MHz.
6143 * Not sure what's wrong. For now use 200MHz only when all pipes
6144 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08006145 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006146 if (!IS_CHERRYVIEW(dev_priv) &&
6147 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006148 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006149 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006150 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006151 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006152 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006153 else
6154 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006155}
6156
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306157static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
6158 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006159{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306160 /*
6161 * FIXME:
6162 * - remove the guardband, it's not needed on BXT
6163 * - set 19.2MHz bypass frequency if there are no active pipes
6164 */
6165 if (max_pixclk > 576000*9/10)
6166 return 624000;
6167 else if (max_pixclk > 384000*9/10)
6168 return 576000;
6169 else if (max_pixclk > 288000*9/10)
6170 return 384000;
6171 else if (max_pixclk > 144000*9/10)
6172 return 288000;
6173 else
6174 return 144000;
6175}
6176
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01006177/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006178static int intel_mode_max_pixclk(struct drm_device *dev,
6179 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006180{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006181 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
6182 struct drm_i915_private *dev_priv = dev->dev_private;
6183 struct drm_crtc *crtc;
6184 struct drm_crtc_state *crtc_state;
6185 unsigned max_pixclk = 0, i;
6186 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006187
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006188 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6189 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006190
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006191 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6192 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006193
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006194 if (crtc_state->enable)
6195 pixclk = crtc_state->adjusted_mode.crtc_clock;
6196
6197 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006198 }
6199
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006200 for_each_pipe(dev_priv, pipe)
6201 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6202
Jesse Barnes30a970c2013-11-04 13:48:12 -08006203 return max_pixclk;
6204}
6205
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006206static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006207{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006208 struct drm_device *dev = state->dev;
6209 struct drm_i915_private *dev_priv = dev->dev_private;
6210 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006211 struct intel_atomic_state *intel_state =
6212 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006213
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006214 if (max_pixclk < 0)
6215 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006216
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006217 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006218 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306219
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006220 if (!intel_state->active_crtcs)
6221 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6222
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006223 return 0;
6224}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006225
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006226static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
6227{
6228 struct drm_device *dev = state->dev;
6229 struct drm_i915_private *dev_priv = dev->dev_private;
6230 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006231 struct intel_atomic_state *intel_state =
6232 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006233
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006234 if (max_pixclk < 0)
6235 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006236
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006237 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006238 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006239
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006240 if (!intel_state->active_crtcs)
6241 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
6242
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006243 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006244}
6245
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006246static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6247{
6248 unsigned int credits, default_credits;
6249
6250 if (IS_CHERRYVIEW(dev_priv))
6251 default_credits = PFI_CREDIT(12);
6252 else
6253 default_credits = PFI_CREDIT(8);
6254
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006255 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006256 /* CHV suggested value is 31 or 63 */
6257 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006258 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006259 else
6260 credits = PFI_CREDIT(15);
6261 } else {
6262 credits = default_credits;
6263 }
6264
6265 /*
6266 * WA - write default credits before re-programming
6267 * FIXME: should we also set the resend bit here?
6268 */
6269 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6270 default_credits);
6271
6272 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6273 credits | PFI_CREDIT_RESEND);
6274
6275 /*
6276 * FIXME is this guaranteed to clear
6277 * immediately or should we poll for it?
6278 */
6279 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6280}
6281
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006282static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006283{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006284 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006285 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006286 struct intel_atomic_state *old_intel_state =
6287 to_intel_atomic_state(old_state);
6288 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006289
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006290 /*
6291 * FIXME: We can end up here with all power domains off, yet
6292 * with a CDCLK frequency other than the minimum. To account
6293 * for this take the PIPE-A power domain, which covers the HW
6294 * blocks needed for the following programming. This can be
6295 * removed once it's guaranteed that we get here either with
6296 * the minimum CDCLK set, or the required power domains
6297 * enabled.
6298 */
6299 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006300
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006301 if (IS_CHERRYVIEW(dev))
6302 cherryview_set_cdclk(dev, req_cdclk);
6303 else
6304 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006305
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006306 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006307
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006308 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006309}
6310
Jesse Barnes89b667f2013-04-18 14:51:36 -07006311static void valleyview_crtc_enable(struct drm_crtc *crtc)
6312{
6313 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006314 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006315 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6316 struct intel_encoder *encoder;
6317 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006318
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006319 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006320 return;
6321
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006322 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306323 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006324
6325 intel_set_pipe_timings(intel_crtc);
6326
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006327 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6328 struct drm_i915_private *dev_priv = dev->dev_private;
6329
6330 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6331 I915_WRITE(CHV_CANVAS(pipe), 0);
6332 }
6333
Daniel Vetter5b18e572014-04-24 23:55:06 +02006334 i9xx_set_pipeconf(intel_crtc);
6335
Jesse Barnes89b667f2013-04-18 14:51:36 -07006336 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006337
Daniel Vettera72e4c92014-09-30 10:56:47 +02006338 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006339
Jesse Barnes89b667f2013-04-18 14:51:36 -07006340 for_each_encoder_on_crtc(dev, crtc, encoder)
6341 if (encoder->pre_pll_enable)
6342 encoder->pre_pll_enable(encoder);
6343
Jani Nikulaa65347b2015-11-27 12:21:46 +02006344 if (!intel_crtc->config->has_dsi_encoder) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006345 if (IS_CHERRYVIEW(dev)) {
6346 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006347 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006348 } else {
6349 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006350 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006351 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006352 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006353
6354 for_each_encoder_on_crtc(dev, crtc, encoder)
6355 if (encoder->pre_enable)
6356 encoder->pre_enable(encoder);
6357
Jesse Barnes2dd24552013-04-25 12:55:01 -07006358 i9xx_pfit_enable(intel_crtc);
6359
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006360 intel_crtc_load_lut(crtc);
6361
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006362 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006363
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006364 assert_vblank_disabled(crtc);
6365 drm_crtc_vblank_on(crtc);
6366
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006367 for_each_encoder_on_crtc(dev, crtc, encoder)
6368 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006369}
6370
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006371static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6372{
6373 struct drm_device *dev = crtc->base.dev;
6374 struct drm_i915_private *dev_priv = dev->dev_private;
6375
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006376 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6377 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006378}
6379
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006380static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006381{
6382 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006383 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006384 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006385 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006386 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006387
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006388 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006389 return;
6390
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006391 i9xx_set_pll_dividers(intel_crtc);
6392
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006393 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306394 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006395
6396 intel_set_pipe_timings(intel_crtc);
6397
Daniel Vetter5b18e572014-04-24 23:55:06 +02006398 i9xx_set_pipeconf(intel_crtc);
6399
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006400 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006401
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006402 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006403 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006404
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006405 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006406 if (encoder->pre_enable)
6407 encoder->pre_enable(encoder);
6408
Daniel Vetterf6736a12013-06-05 13:34:30 +02006409 i9xx_enable_pll(intel_crtc);
6410
Jesse Barnes2dd24552013-04-25 12:55:01 -07006411 i9xx_pfit_enable(intel_crtc);
6412
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006413 intel_crtc_load_lut(crtc);
6414
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006415 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006416 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006417
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006418 assert_vblank_disabled(crtc);
6419 drm_crtc_vblank_on(crtc);
6420
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006421 for_each_encoder_on_crtc(dev, crtc, encoder)
6422 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006423}
6424
Daniel Vetter87476d62013-04-11 16:29:06 +02006425static void i9xx_pfit_disable(struct intel_crtc *crtc)
6426{
6427 struct drm_device *dev = crtc->base.dev;
6428 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006429
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006430 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006431 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006432
6433 assert_pipe_disabled(dev_priv, crtc->pipe);
6434
Daniel Vetter328d8e82013-05-08 10:36:31 +02006435 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6436 I915_READ(PFIT_CONTROL));
6437 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006438}
6439
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006440static void i9xx_crtc_disable(struct drm_crtc *crtc)
6441{
6442 struct drm_device *dev = crtc->dev;
6443 struct drm_i915_private *dev_priv = dev->dev_private;
6444 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006445 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006446 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006447
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006448 /*
6449 * On gen2 planes are double buffered but the pipe isn't, so we must
6450 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006451 * We also need to wait on all gmch platforms because of the
6452 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006453 */
Imre Deak564ed192014-06-13 14:54:21 +03006454 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006455
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006456 for_each_encoder_on_crtc(dev, crtc, encoder)
6457 encoder->disable(encoder);
6458
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006459 drm_crtc_vblank_off(crtc);
6460 assert_vblank_disabled(crtc);
6461
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006462 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006463
Daniel Vetter87476d62013-04-11 16:29:06 +02006464 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006465
Jesse Barnes89b667f2013-04-18 14:51:36 -07006466 for_each_encoder_on_crtc(dev, crtc, encoder)
6467 if (encoder->post_disable)
6468 encoder->post_disable(encoder);
6469
Jani Nikulaa65347b2015-11-27 12:21:46 +02006470 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006471 if (IS_CHERRYVIEW(dev))
6472 chv_disable_pll(dev_priv, pipe);
6473 else if (IS_VALLEYVIEW(dev))
6474 vlv_disable_pll(dev_priv, pipe);
6475 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006476 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006477 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006478
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006479 for_each_encoder_on_crtc(dev, crtc, encoder)
6480 if (encoder->post_pll_disable)
6481 encoder->post_pll_disable(encoder);
6482
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006483 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006484 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006485}
6486
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006487static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006488{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006489 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006490 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006491 enum intel_display_power_domain domain;
6492 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006493
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006494 if (!intel_crtc->active)
6495 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006496
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006497 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006498 WARN_ON(intel_crtc->unpin_work);
6499
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006500 intel_pre_disable_primary(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006501
6502 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6503 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006504 }
6505
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006506 dev_priv->display.crtc_disable(crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006507 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006508 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006509 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006510 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006511
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006512 domains = intel_crtc->enabled_power_domains;
6513 for_each_power_domain(domain, domains)
6514 intel_display_power_put(dev_priv, domain);
6515 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006516
6517 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6518 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006519}
6520
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006521/*
6522 * turn all crtc's off, but do not adjust state
6523 * This has to be paired with a call to intel_modeset_setup_hw_state.
6524 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006525int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006526{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006527 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006528 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006529 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006530
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006531 state = drm_atomic_helper_suspend(dev);
6532 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006533 if (ret)
6534 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006535 else
6536 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006537 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006538}
6539
Chris Wilsonea5b2132010-08-04 13:50:23 +01006540void intel_encoder_destroy(struct drm_encoder *encoder)
6541{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006542 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006543
Chris Wilsonea5b2132010-08-04 13:50:23 +01006544 drm_encoder_cleanup(encoder);
6545 kfree(intel_encoder);
6546}
6547
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006548/* Cross check the actual hw state with our own modeset state tracking (and it's
6549 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006550static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006551{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006552 struct drm_crtc *crtc = connector->base.state->crtc;
6553
6554 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6555 connector->base.base.id,
6556 connector->base.name);
6557
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006558 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006559 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006560 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006561
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006562 I915_STATE_WARN(!crtc,
6563 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006564
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006565 if (!crtc)
6566 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006567
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006568 I915_STATE_WARN(!crtc->state->active,
6569 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006570
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006571 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006572 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006573
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006574 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006575 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006576
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006577 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006578 "attached encoder crtc differs from connector crtc\n");
6579 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006580 I915_STATE_WARN(crtc && crtc->state->active,
6581 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006582 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6583 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006584 }
6585}
6586
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006587int intel_connector_init(struct intel_connector *connector)
6588{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006589 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006590
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006591 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006592 return -ENOMEM;
6593
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006594 return 0;
6595}
6596
6597struct intel_connector *intel_connector_alloc(void)
6598{
6599 struct intel_connector *connector;
6600
6601 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6602 if (!connector)
6603 return NULL;
6604
6605 if (intel_connector_init(connector) < 0) {
6606 kfree(connector);
6607 return NULL;
6608 }
6609
6610 return connector;
6611}
6612
Daniel Vetterf0947c32012-07-02 13:10:34 +02006613/* Simple connector->get_hw_state implementation for encoders that support only
6614 * one connector and no cloning and hence the encoder state determines the state
6615 * of the connector. */
6616bool intel_connector_get_hw_state(struct intel_connector *connector)
6617{
Daniel Vetter24929352012-07-02 20:28:59 +02006618 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006619 struct intel_encoder *encoder = connector->encoder;
6620
6621 return encoder->get_hw_state(encoder, &pipe);
6622}
6623
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006624static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006625{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006626 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6627 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006628
6629 return 0;
6630}
6631
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006632static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006633 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006634{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006635 struct drm_atomic_state *state = pipe_config->base.state;
6636 struct intel_crtc *other_crtc;
6637 struct intel_crtc_state *other_crtc_state;
6638
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006639 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6640 pipe_name(pipe), pipe_config->fdi_lanes);
6641 if (pipe_config->fdi_lanes > 4) {
6642 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6643 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006644 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006645 }
6646
Paulo Zanonibafb6552013-11-02 21:07:44 -07006647 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006648 if (pipe_config->fdi_lanes > 2) {
6649 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6650 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006651 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006652 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006653 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006654 }
6655 }
6656
6657 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006658 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006659
6660 /* Ivybridge 3 pipe is really complicated */
6661 switch (pipe) {
6662 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006663 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006664 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006665 if (pipe_config->fdi_lanes <= 2)
6666 return 0;
6667
6668 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6669 other_crtc_state =
6670 intel_atomic_get_crtc_state(state, other_crtc);
6671 if (IS_ERR(other_crtc_state))
6672 return PTR_ERR(other_crtc_state);
6673
6674 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006675 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6676 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006677 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006678 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006679 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006680 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006681 if (pipe_config->fdi_lanes > 2) {
6682 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6683 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006684 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006685 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006686
6687 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6688 other_crtc_state =
6689 intel_atomic_get_crtc_state(state, other_crtc);
6690 if (IS_ERR(other_crtc_state))
6691 return PTR_ERR(other_crtc_state);
6692
6693 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006694 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006695 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006696 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006697 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006698 default:
6699 BUG();
6700 }
6701}
6702
Daniel Vettere29c22c2013-02-21 00:00:16 +01006703#define RETRY 1
6704static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006705 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006706{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006707 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006708 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006709 int lane, link_bw, fdi_dotclock, ret;
6710 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006711
Daniel Vettere29c22c2013-02-21 00:00:16 +01006712retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006713 /* FDI is a binary signal running at ~2.7GHz, encoding
6714 * each output octet as 10 bits. The actual frequency
6715 * is stored as a divider into a 100MHz clock, and the
6716 * mode pixel clock is stored in units of 1KHz.
6717 * Hence the bw of each lane in terms of the mode signal
6718 * is:
6719 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006720 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006721
Damien Lespiau241bfc32013-09-25 16:45:37 +01006722 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006723
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006724 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006725 pipe_config->pipe_bpp);
6726
6727 pipe_config->fdi_lanes = lane;
6728
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006729 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006730 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006731
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006732 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006733 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006734 pipe_config->pipe_bpp -= 2*3;
6735 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6736 pipe_config->pipe_bpp);
6737 needs_recompute = true;
6738 pipe_config->bw_constrained = true;
6739
6740 goto retry;
6741 }
6742
6743 if (needs_recompute)
6744 return RETRY;
6745
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006746 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006747}
6748
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006749static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6750 struct intel_crtc_state *pipe_config)
6751{
6752 if (pipe_config->pipe_bpp > 24)
6753 return false;
6754
6755 /* HSW can handle pixel rate up to cdclk? */
6756 if (IS_HASWELL(dev_priv->dev))
6757 return true;
6758
6759 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006760 * We compare against max which means we must take
6761 * the increased cdclk requirement into account when
6762 * calculating the new cdclk.
6763 *
6764 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006765 */
6766 return ilk_pipe_pixel_rate(pipe_config) <=
6767 dev_priv->max_cdclk_freq * 95 / 100;
6768}
6769
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006770static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006771 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006772{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006773 struct drm_device *dev = crtc->base.dev;
6774 struct drm_i915_private *dev_priv = dev->dev_private;
6775
Jani Nikulad330a952014-01-21 11:24:25 +02006776 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006777 hsw_crtc_supports_ips(crtc) &&
6778 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006779}
6780
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006781static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6782{
6783 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6784
6785 /* GDG double wide on either pipe, otherwise pipe A only */
6786 return INTEL_INFO(dev_priv)->gen < 4 &&
6787 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6788}
6789
Daniel Vettera43f6e02013-06-07 23:10:32 +02006790static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006791 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006792{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006793 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006794 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006795 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006796
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006797 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006798 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006799 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006800
6801 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006802 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006803 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006804 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006805 if (intel_crtc_supports_double_wide(crtc) &&
6806 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006807 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006808 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006809 }
6810
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006811 if (adjusted_mode->crtc_clock > clock_limit) {
6812 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6813 adjusted_mode->crtc_clock, clock_limit,
6814 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006815 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006816 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006817 }
Chris Wilson89749352010-09-12 18:25:19 +01006818
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006819 /*
6820 * Pipe horizontal size must be even in:
6821 * - DVO ganged mode
6822 * - LVDS dual channel mode
6823 * - Double wide pipe
6824 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006825 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006826 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6827 pipe_config->pipe_src_w &= ~1;
6828
Damien Lespiau8693a822013-05-03 18:48:11 +01006829 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6830 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006831 */
6832 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006833 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006834 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006835
Damien Lespiauf5adf942013-06-24 18:29:34 +01006836 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006837 hsw_compute_ips_config(crtc, pipe_config);
6838
Daniel Vetter877d48d2013-04-19 11:24:43 +02006839 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006840 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006841
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006842 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006843}
6844
Ville Syrjälä1652d192015-03-31 14:12:01 +03006845static int skylake_get_display_clock_speed(struct drm_device *dev)
6846{
6847 struct drm_i915_private *dev_priv = to_i915(dev);
6848 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6849 uint32_t cdctl = I915_READ(CDCLK_CTL);
6850 uint32_t linkrate;
6851
Damien Lespiau414355a2015-06-04 18:21:31 +01006852 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006853 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006854
6855 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6856 return 540000;
6857
6858 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006859 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006860
Damien Lespiau71cd8422015-04-30 16:39:17 +01006861 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6862 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006863 /* vco 8640 */
6864 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6865 case CDCLK_FREQ_450_432:
6866 return 432000;
6867 case CDCLK_FREQ_337_308:
6868 return 308570;
6869 case CDCLK_FREQ_675_617:
6870 return 617140;
6871 default:
6872 WARN(1, "Unknown cd freq selection\n");
6873 }
6874 } else {
6875 /* vco 8100 */
6876 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6877 case CDCLK_FREQ_450_432:
6878 return 450000;
6879 case CDCLK_FREQ_337_308:
6880 return 337500;
6881 case CDCLK_FREQ_675_617:
6882 return 675000;
6883 default:
6884 WARN(1, "Unknown cd freq selection\n");
6885 }
6886 }
6887
6888 /* error case, do as if DPLL0 isn't enabled */
6889 return 24000;
6890}
6891
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006892static int broxton_get_display_clock_speed(struct drm_device *dev)
6893{
6894 struct drm_i915_private *dev_priv = to_i915(dev);
6895 uint32_t cdctl = I915_READ(CDCLK_CTL);
6896 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6897 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6898 int cdclk;
6899
6900 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6901 return 19200;
6902
6903 cdclk = 19200 * pll_ratio / 2;
6904
6905 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6906 case BXT_CDCLK_CD2X_DIV_SEL_1:
6907 return cdclk; /* 576MHz or 624MHz */
6908 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6909 return cdclk * 2 / 3; /* 384MHz */
6910 case BXT_CDCLK_CD2X_DIV_SEL_2:
6911 return cdclk / 2; /* 288MHz */
6912 case BXT_CDCLK_CD2X_DIV_SEL_4:
6913 return cdclk / 4; /* 144MHz */
6914 }
6915
6916 /* error case, do as if DE PLL isn't enabled */
6917 return 19200;
6918}
6919
Ville Syrjälä1652d192015-03-31 14:12:01 +03006920static int broadwell_get_display_clock_speed(struct drm_device *dev)
6921{
6922 struct drm_i915_private *dev_priv = dev->dev_private;
6923 uint32_t lcpll = I915_READ(LCPLL_CTL);
6924 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6925
6926 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6927 return 800000;
6928 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6929 return 450000;
6930 else if (freq == LCPLL_CLK_FREQ_450)
6931 return 450000;
6932 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6933 return 540000;
6934 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6935 return 337500;
6936 else
6937 return 675000;
6938}
6939
6940static int haswell_get_display_clock_speed(struct drm_device *dev)
6941{
6942 struct drm_i915_private *dev_priv = dev->dev_private;
6943 uint32_t lcpll = I915_READ(LCPLL_CTL);
6944 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6945
6946 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6947 return 800000;
6948 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6949 return 450000;
6950 else if (freq == LCPLL_CLK_FREQ_450)
6951 return 450000;
6952 else if (IS_HSW_ULT(dev))
6953 return 337500;
6954 else
6955 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006956}
6957
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006958static int valleyview_get_display_clock_speed(struct drm_device *dev)
6959{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006960 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6961 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006962}
6963
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006964static int ilk_get_display_clock_speed(struct drm_device *dev)
6965{
6966 return 450000;
6967}
6968
Jesse Barnese70236a2009-09-21 10:42:27 -07006969static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006970{
Jesse Barnese70236a2009-09-21 10:42:27 -07006971 return 400000;
6972}
Jesse Barnes79e53942008-11-07 14:24:08 -08006973
Jesse Barnese70236a2009-09-21 10:42:27 -07006974static int i915_get_display_clock_speed(struct drm_device *dev)
6975{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006976 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006977}
Jesse Barnes79e53942008-11-07 14:24:08 -08006978
Jesse Barnese70236a2009-09-21 10:42:27 -07006979static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6980{
6981 return 200000;
6982}
Jesse Barnes79e53942008-11-07 14:24:08 -08006983
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006984static int pnv_get_display_clock_speed(struct drm_device *dev)
6985{
6986 u16 gcfgc = 0;
6987
6988 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6989
6990 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6991 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006992 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006993 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006994 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006995 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006996 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006997 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6998 return 200000;
6999 default:
7000 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
7001 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007002 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007003 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007004 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007005 }
7006}
7007
Jesse Barnese70236a2009-09-21 10:42:27 -07007008static int i915gm_get_display_clock_speed(struct drm_device *dev)
7009{
7010 u16 gcfgc = 0;
7011
7012 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
7013
7014 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03007015 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007016 else {
7017 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7018 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007019 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007020 default:
7021 case GC_DISPLAY_CLOCK_190_200_MHZ:
7022 return 190000;
7023 }
7024 }
7025}
Jesse Barnes79e53942008-11-07 14:24:08 -08007026
Jesse Barnese70236a2009-09-21 10:42:27 -07007027static int i865_get_display_clock_speed(struct drm_device *dev)
7028{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007029 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007030}
7031
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007032static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07007033{
7034 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007035
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03007036 /*
7037 * 852GM/852GMV only supports 133 MHz and the HPLLCC
7038 * encoding is different :(
7039 * FIXME is this the right way to detect 852GM/852GMV?
7040 */
7041 if (dev->pdev->revision == 0x1)
7042 return 133333;
7043
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007044 pci_bus_read_config_word(dev->pdev->bus,
7045 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7046
Jesse Barnese70236a2009-09-21 10:42:27 -07007047 /* Assume that the hardware is in the high speed state. This
7048 * should be the default.
7049 */
7050 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7051 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007052 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07007053 case GC_CLOCK_100_200:
7054 return 200000;
7055 case GC_CLOCK_166_250:
7056 return 250000;
7057 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007058 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007059 case GC_CLOCK_133_266:
7060 case GC_CLOCK_133_266_2:
7061 case GC_CLOCK_166_266:
7062 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007063 }
7064
7065 /* Shouldn't happen */
7066 return 0;
7067}
7068
7069static int i830_get_display_clock_speed(struct drm_device *dev)
7070{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007071 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08007072}
7073
Ville Syrjälä34edce22015-05-22 11:22:33 +03007074static unsigned int intel_hpll_vco(struct drm_device *dev)
7075{
7076 struct drm_i915_private *dev_priv = dev->dev_private;
7077 static const unsigned int blb_vco[8] = {
7078 [0] = 3200000,
7079 [1] = 4000000,
7080 [2] = 5333333,
7081 [3] = 4800000,
7082 [4] = 6400000,
7083 };
7084 static const unsigned int pnv_vco[8] = {
7085 [0] = 3200000,
7086 [1] = 4000000,
7087 [2] = 5333333,
7088 [3] = 4800000,
7089 [4] = 2666667,
7090 };
7091 static const unsigned int cl_vco[8] = {
7092 [0] = 3200000,
7093 [1] = 4000000,
7094 [2] = 5333333,
7095 [3] = 6400000,
7096 [4] = 3333333,
7097 [5] = 3566667,
7098 [6] = 4266667,
7099 };
7100 static const unsigned int elk_vco[8] = {
7101 [0] = 3200000,
7102 [1] = 4000000,
7103 [2] = 5333333,
7104 [3] = 4800000,
7105 };
7106 static const unsigned int ctg_vco[8] = {
7107 [0] = 3200000,
7108 [1] = 4000000,
7109 [2] = 5333333,
7110 [3] = 6400000,
7111 [4] = 2666667,
7112 [5] = 4266667,
7113 };
7114 const unsigned int *vco_table;
7115 unsigned int vco;
7116 uint8_t tmp = 0;
7117
7118 /* FIXME other chipsets? */
7119 if (IS_GM45(dev))
7120 vco_table = ctg_vco;
7121 else if (IS_G4X(dev))
7122 vco_table = elk_vco;
7123 else if (IS_CRESTLINE(dev))
7124 vco_table = cl_vco;
7125 else if (IS_PINEVIEW(dev))
7126 vco_table = pnv_vco;
7127 else if (IS_G33(dev))
7128 vco_table = blb_vco;
7129 else
7130 return 0;
7131
7132 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7133
7134 vco = vco_table[tmp & 0x7];
7135 if (vco == 0)
7136 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7137 else
7138 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7139
7140 return vco;
7141}
7142
7143static int gm45_get_display_clock_speed(struct drm_device *dev)
7144{
7145 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7146 uint16_t tmp = 0;
7147
7148 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7149
7150 cdclk_sel = (tmp >> 12) & 0x1;
7151
7152 switch (vco) {
7153 case 2666667:
7154 case 4000000:
7155 case 5333333:
7156 return cdclk_sel ? 333333 : 222222;
7157 case 3200000:
7158 return cdclk_sel ? 320000 : 228571;
7159 default:
7160 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7161 return 222222;
7162 }
7163}
7164
7165static int i965gm_get_display_clock_speed(struct drm_device *dev)
7166{
7167 static const uint8_t div_3200[] = { 16, 10, 8 };
7168 static const uint8_t div_4000[] = { 20, 12, 10 };
7169 static const uint8_t div_5333[] = { 24, 16, 14 };
7170 const uint8_t *div_table;
7171 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7172 uint16_t tmp = 0;
7173
7174 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7175
7176 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7177
7178 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7179 goto fail;
7180
7181 switch (vco) {
7182 case 3200000:
7183 div_table = div_3200;
7184 break;
7185 case 4000000:
7186 div_table = div_4000;
7187 break;
7188 case 5333333:
7189 div_table = div_5333;
7190 break;
7191 default:
7192 goto fail;
7193 }
7194
7195 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7196
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007197fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007198 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7199 return 200000;
7200}
7201
7202static int g33_get_display_clock_speed(struct drm_device *dev)
7203{
7204 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7205 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7206 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7207 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7208 const uint8_t *div_table;
7209 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7210 uint16_t tmp = 0;
7211
7212 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7213
7214 cdclk_sel = (tmp >> 4) & 0x7;
7215
7216 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7217 goto fail;
7218
7219 switch (vco) {
7220 case 3200000:
7221 div_table = div_3200;
7222 break;
7223 case 4000000:
7224 div_table = div_4000;
7225 break;
7226 case 4800000:
7227 div_table = div_4800;
7228 break;
7229 case 5333333:
7230 div_table = div_5333;
7231 break;
7232 default:
7233 goto fail;
7234 }
7235
7236 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7237
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007238fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007239 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7240 return 190476;
7241}
7242
Zhenyu Wang2c072452009-06-05 15:38:42 +08007243static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007244intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007245{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007246 while (*num > DATA_LINK_M_N_MASK ||
7247 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007248 *num >>= 1;
7249 *den >>= 1;
7250 }
7251}
7252
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007253static void compute_m_n(unsigned int m, unsigned int n,
7254 uint32_t *ret_m, uint32_t *ret_n)
7255{
7256 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7257 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7258 intel_reduce_m_n_ratio(ret_m, ret_n);
7259}
7260
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007261void
7262intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7263 int pixel_clock, int link_clock,
7264 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007265{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007266 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007267
7268 compute_m_n(bits_per_pixel * pixel_clock,
7269 link_clock * nlanes * 8,
7270 &m_n->gmch_m, &m_n->gmch_n);
7271
7272 compute_m_n(pixel_clock, link_clock,
7273 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007274}
7275
Chris Wilsona7615032011-01-12 17:04:08 +00007276static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7277{
Jani Nikulad330a952014-01-21 11:24:25 +02007278 if (i915.panel_use_ssc >= 0)
7279 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007280 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007281 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007282}
7283
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007284static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7285 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007286{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007287 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007288 struct drm_i915_private *dev_priv = dev->dev_private;
7289 int refclk;
7290
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007291 WARN_ON(!crtc_state->base.state);
7292
Wayne Boyer666a4532015-12-09 12:29:35 -08007293 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007294 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007295 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007296 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007297 refclk = dev_priv->vbt.lvds_ssc_freq;
7298 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007299 } else if (!IS_GEN2(dev)) {
7300 refclk = 96000;
7301 } else {
7302 refclk = 48000;
7303 }
7304
7305 return refclk;
7306}
7307
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007308static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007309{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007310 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007311}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007312
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007313static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7314{
7315 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007316}
7317
Daniel Vetterf47709a2013-03-28 10:42:02 +01007318static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007319 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007320 intel_clock_t *reduced_clock)
7321{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007322 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007323 u32 fp, fp2 = 0;
7324
7325 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007326 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007327 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007328 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007329 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007330 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007331 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007332 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007333 }
7334
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007335 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007336
Daniel Vetterf47709a2013-03-28 10:42:02 +01007337 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007338 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007339 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007340 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007341 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007342 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007343 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007344 }
7345}
7346
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007347static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7348 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007349{
7350 u32 reg_val;
7351
7352 /*
7353 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7354 * and set it to a reasonable value instead.
7355 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007356 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007357 reg_val &= 0xffffff00;
7358 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007359 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007360
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007361 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007362 reg_val &= 0x8cffffff;
7363 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007364 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007365
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007366 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007367 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007368 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007369
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007370 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007371 reg_val &= 0x00ffffff;
7372 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007373 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007374}
7375
Daniel Vetterb5518422013-05-03 11:49:48 +02007376static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7377 struct intel_link_m_n *m_n)
7378{
7379 struct drm_device *dev = crtc->base.dev;
7380 struct drm_i915_private *dev_priv = dev->dev_private;
7381 int pipe = crtc->pipe;
7382
Daniel Vettere3b95f12013-05-03 11:49:49 +02007383 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7384 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7385 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7386 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007387}
7388
7389static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007390 struct intel_link_m_n *m_n,
7391 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007392{
7393 struct drm_device *dev = crtc->base.dev;
7394 struct drm_i915_private *dev_priv = dev->dev_private;
7395 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007396 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007397
7398 if (INTEL_INFO(dev)->gen >= 5) {
7399 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7400 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7401 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7402 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007403 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7404 * for gen < 8) and if DRRS is supported (to make sure the
7405 * registers are not unnecessarily accessed).
7406 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307407 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007408 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007409 I915_WRITE(PIPE_DATA_M2(transcoder),
7410 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7411 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7412 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7413 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7414 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007415 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007416 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7417 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7418 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7419 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007420 }
7421}
7422
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307423void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007424{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307425 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7426
7427 if (m_n == M1_N1) {
7428 dp_m_n = &crtc->config->dp_m_n;
7429 dp_m2_n2 = &crtc->config->dp_m2_n2;
7430 } else if (m_n == M2_N2) {
7431
7432 /*
7433 * M2_N2 registers are not supported. Hence m2_n2 divider value
7434 * needs to be programmed into M1_N1.
7435 */
7436 dp_m_n = &crtc->config->dp_m2_n2;
7437 } else {
7438 DRM_ERROR("Unsupported divider value\n");
7439 return;
7440 }
7441
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007442 if (crtc->config->has_pch_encoder)
7443 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007444 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307445 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007446}
7447
Daniel Vetter251ac862015-06-18 10:30:24 +02007448static void vlv_compute_dpll(struct intel_crtc *crtc,
7449 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007450{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007451 u32 dpll, dpll_md;
7452
7453 /*
7454 * Enable DPIO clock input. We should never disable the reference
7455 * clock for pipe B, since VGA hotplug / manual detection depends
7456 * on it.
7457 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007458 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7459 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007460 /* We should never disable this, set it here for state tracking */
7461 if (crtc->pipe == PIPE_B)
7462 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7463 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007464 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007465
Ville Syrjäläd288f652014-10-28 13:20:22 +02007466 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007467 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007468 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007469}
7470
Ville Syrjäläd288f652014-10-28 13:20:22 +02007471static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007472 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007473{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007474 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007475 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007476 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007477 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007478 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007479 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007480
Ville Syrjäläa5805162015-05-26 20:42:30 +03007481 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007482
Ville Syrjäläd288f652014-10-28 13:20:22 +02007483 bestn = pipe_config->dpll.n;
7484 bestm1 = pipe_config->dpll.m1;
7485 bestm2 = pipe_config->dpll.m2;
7486 bestp1 = pipe_config->dpll.p1;
7487 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007488
Jesse Barnes89b667f2013-04-18 14:51:36 -07007489 /* See eDP HDMI DPIO driver vbios notes doc */
7490
7491 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007492 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007493 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007494
7495 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007496 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007497
7498 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007499 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007500 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007501 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007502
7503 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007504 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007505
7506 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007507 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7508 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7509 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007510 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007511
7512 /*
7513 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7514 * but we don't support that).
7515 * Note: don't use the DAC post divider as it seems unstable.
7516 */
7517 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007518 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007519
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007520 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007521 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007522
Jesse Barnes89b667f2013-04-18 14:51:36 -07007523 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007524 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007525 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7526 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007527 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007528 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007529 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007530 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007531 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007532
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007533 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007534 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007535 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007536 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007537 0x0df40000);
7538 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007539 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007540 0x0df70000);
7541 } else { /* HDMI or VGA */
7542 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007543 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007544 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007545 0x0df70000);
7546 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007547 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007548 0x0df40000);
7549 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007550
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007551 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007552 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007553 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7554 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007555 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007556 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007557
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007558 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007559 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007560}
7561
Daniel Vetter251ac862015-06-18 10:30:24 +02007562static void chv_compute_dpll(struct intel_crtc *crtc,
7563 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007564{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007565 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7566 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007567 DPLL_VCO_ENABLE;
7568 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007569 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007570
Ville Syrjäläd288f652014-10-28 13:20:22 +02007571 pipe_config->dpll_hw_state.dpll_md =
7572 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007573}
7574
Ville Syrjäläd288f652014-10-28 13:20:22 +02007575static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007576 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007577{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007578 struct drm_device *dev = crtc->base.dev;
7579 struct drm_i915_private *dev_priv = dev->dev_private;
7580 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007581 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007582 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307583 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007584 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307585 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307586 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007587
Ville Syrjäläd288f652014-10-28 13:20:22 +02007588 bestn = pipe_config->dpll.n;
7589 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7590 bestm1 = pipe_config->dpll.m1;
7591 bestm2 = pipe_config->dpll.m2 >> 22;
7592 bestp1 = pipe_config->dpll.p1;
7593 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307594 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307595 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307596 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007597
7598 /*
7599 * Enable Refclk and SSC
7600 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007601 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007602 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007603
Ville Syrjäläa5805162015-05-26 20:42:30 +03007604 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007605
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007606 /* p1 and p2 divider */
7607 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7608 5 << DPIO_CHV_S1_DIV_SHIFT |
7609 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7610 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7611 1 << DPIO_CHV_K_DIV_SHIFT);
7612
7613 /* Feedback post-divider - m2 */
7614 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7615
7616 /* Feedback refclk divider - n and m1 */
7617 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7618 DPIO_CHV_M1_DIV_BY_2 |
7619 1 << DPIO_CHV_N_DIV_SHIFT);
7620
7621 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007622 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007623
7624 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307625 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7626 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7627 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7628 if (bestm2_frac)
7629 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7630 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007631
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307632 /* Program digital lock detect threshold */
7633 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7634 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7635 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7636 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7637 if (!bestm2_frac)
7638 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7639 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7640
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007641 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307642 if (vco == 5400000) {
7643 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7644 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7645 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7646 tribuf_calcntr = 0x9;
7647 } else if (vco <= 6200000) {
7648 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7649 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7650 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7651 tribuf_calcntr = 0x9;
7652 } else if (vco <= 6480000) {
7653 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7654 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7655 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7656 tribuf_calcntr = 0x8;
7657 } else {
7658 /* Not supported. Apply the same limits as in the max case */
7659 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7660 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7661 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7662 tribuf_calcntr = 0;
7663 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007664 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7665
Ville Syrjälä968040b2015-03-11 22:52:08 +02007666 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307667 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7668 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7669 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7670
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007671 /* AFC Recal */
7672 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7673 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7674 DPIO_AFC_RECAL);
7675
Ville Syrjäläa5805162015-05-26 20:42:30 +03007676 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007677}
7678
Ville Syrjäläd288f652014-10-28 13:20:22 +02007679/**
7680 * vlv_force_pll_on - forcibly enable just the PLL
7681 * @dev_priv: i915 private structure
7682 * @pipe: pipe PLL to enable
7683 * @dpll: PLL configuration
7684 *
7685 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7686 * in cases where we need the PLL enabled even when @pipe is not going to
7687 * be enabled.
7688 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007689int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7690 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007691{
7692 struct intel_crtc *crtc =
7693 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007694 struct intel_crtc_state *pipe_config;
7695
7696 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7697 if (!pipe_config)
7698 return -ENOMEM;
7699
7700 pipe_config->base.crtc = &crtc->base;
7701 pipe_config->pixel_multiplier = 1;
7702 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007703
7704 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007705 chv_compute_dpll(crtc, pipe_config);
7706 chv_prepare_pll(crtc, pipe_config);
7707 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007708 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007709 vlv_compute_dpll(crtc, pipe_config);
7710 vlv_prepare_pll(crtc, pipe_config);
7711 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007712 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007713
7714 kfree(pipe_config);
7715
7716 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007717}
7718
7719/**
7720 * vlv_force_pll_off - forcibly disable just the PLL
7721 * @dev_priv: i915 private structure
7722 * @pipe: pipe PLL to disable
7723 *
7724 * Disable the PLL for @pipe. To be used in cases where we need
7725 * the PLL enabled even when @pipe is not going to be enabled.
7726 */
7727void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7728{
7729 if (IS_CHERRYVIEW(dev))
7730 chv_disable_pll(to_i915(dev), pipe);
7731 else
7732 vlv_disable_pll(to_i915(dev), pipe);
7733}
7734
Daniel Vetter251ac862015-06-18 10:30:24 +02007735static void i9xx_compute_dpll(struct intel_crtc *crtc,
7736 struct intel_crtc_state *crtc_state,
7737 intel_clock_t *reduced_clock,
7738 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007739{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007740 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007741 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007742 u32 dpll;
7743 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007744 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007745
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007746 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307747
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007748 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7749 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007750
7751 dpll = DPLL_VGA_MODE_DIS;
7752
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007753 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007754 dpll |= DPLLB_MODE_LVDS;
7755 else
7756 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007757
Daniel Vetteref1b4602013-06-01 17:17:04 +02007758 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007759 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007760 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007761 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007762
7763 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007764 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007765
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007766 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007767 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007768
7769 /* compute bitmask from p1 value */
7770 if (IS_PINEVIEW(dev))
7771 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7772 else {
7773 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7774 if (IS_G4X(dev) && reduced_clock)
7775 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7776 }
7777 switch (clock->p2) {
7778 case 5:
7779 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7780 break;
7781 case 7:
7782 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7783 break;
7784 case 10:
7785 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7786 break;
7787 case 14:
7788 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7789 break;
7790 }
7791 if (INTEL_INFO(dev)->gen >= 4)
7792 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7793
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007794 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007795 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007796 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007797 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7798 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7799 else
7800 dpll |= PLL_REF_INPUT_DREFCLK;
7801
7802 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007803 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007804
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007805 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007806 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007807 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007808 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007809 }
7810}
7811
Daniel Vetter251ac862015-06-18 10:30:24 +02007812static void i8xx_compute_dpll(struct intel_crtc *crtc,
7813 struct intel_crtc_state *crtc_state,
7814 intel_clock_t *reduced_clock,
7815 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007816{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007817 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007818 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007819 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007820 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007821
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007822 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307823
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007824 dpll = DPLL_VGA_MODE_DIS;
7825
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007826 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007827 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7828 } else {
7829 if (clock->p1 == 2)
7830 dpll |= PLL_P1_DIVIDE_BY_TWO;
7831 else
7832 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7833 if (clock->p2 == 4)
7834 dpll |= PLL_P2_DIVIDE_BY_4;
7835 }
7836
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007837 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007838 dpll |= DPLL_DVO_2X_MODE;
7839
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007840 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007841 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7842 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7843 else
7844 dpll |= PLL_REF_INPUT_DREFCLK;
7845
7846 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007847 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007848}
7849
Daniel Vetter8a654f32013-06-01 17:16:22 +02007850static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007851{
7852 struct drm_device *dev = intel_crtc->base.dev;
7853 struct drm_i915_private *dev_priv = dev->dev_private;
7854 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007855 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007856 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007857 uint32_t crtc_vtotal, crtc_vblank_end;
7858 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007859
7860 /* We need to be careful not to changed the adjusted mode, for otherwise
7861 * the hw state checker will get angry at the mismatch. */
7862 crtc_vtotal = adjusted_mode->crtc_vtotal;
7863 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007864
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007865 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007866 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007867 crtc_vtotal -= 1;
7868 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007869
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007870 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007871 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7872 else
7873 vsyncshift = adjusted_mode->crtc_hsync_start -
7874 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007875 if (vsyncshift < 0)
7876 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007877 }
7878
7879 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007880 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007881
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007882 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007883 (adjusted_mode->crtc_hdisplay - 1) |
7884 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007885 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007886 (adjusted_mode->crtc_hblank_start - 1) |
7887 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007888 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007889 (adjusted_mode->crtc_hsync_start - 1) |
7890 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7891
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007892 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007893 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007894 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007895 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007896 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007897 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007898 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007899 (adjusted_mode->crtc_vsync_start - 1) |
7900 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7901
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007902 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7903 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7904 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7905 * bits. */
7906 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7907 (pipe == PIPE_B || pipe == PIPE_C))
7908 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7909
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007910 /* pipesrc controls the size that is scaled from, which should
7911 * always be the user's requested size.
7912 */
7913 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007914 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7915 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007916}
7917
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007918static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007919 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007920{
7921 struct drm_device *dev = crtc->base.dev;
7922 struct drm_i915_private *dev_priv = dev->dev_private;
7923 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7924 uint32_t tmp;
7925
7926 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007927 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7928 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007929 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007930 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7931 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007932 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007933 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7934 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007935
7936 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007937 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7938 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007939 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007940 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7941 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007942 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007943 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7944 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007945
7946 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007947 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7948 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7949 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007950 }
7951
7952 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007953 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7954 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7955
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007956 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7957 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007958}
7959
Daniel Vetterf6a83282014-02-11 15:28:57 -08007960void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007961 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007962{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007963 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7964 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7965 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7966 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007967
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007968 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7969 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7970 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7971 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007972
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007973 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007974 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007975
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007976 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7977 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007978
7979 mode->hsync = drm_mode_hsync(mode);
7980 mode->vrefresh = drm_mode_vrefresh(mode);
7981 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007982}
7983
Daniel Vetter84b046f2013-02-19 18:48:54 +01007984static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7985{
7986 struct drm_device *dev = intel_crtc->base.dev;
7987 struct drm_i915_private *dev_priv = dev->dev_private;
7988 uint32_t pipeconf;
7989
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007990 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007991
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007992 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7993 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7994 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007995
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007996 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007997 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007998
Daniel Vetterff9ce462013-04-24 14:57:17 +02007999 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08008000 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02008001 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008002 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02008003 pipeconf |= PIPECONF_DITHER_EN |
8004 PIPECONF_DITHER_TYPE_SP;
8005
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008006 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02008007 case 18:
8008 pipeconf |= PIPECONF_6BPC;
8009 break;
8010 case 24:
8011 pipeconf |= PIPECONF_8BPC;
8012 break;
8013 case 30:
8014 pipeconf |= PIPECONF_10BPC;
8015 break;
8016 default:
8017 /* Case prevented by intel_choose_pipe_bpp_dither. */
8018 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01008019 }
8020 }
8021
8022 if (HAS_PIPE_CXSR(dev)) {
8023 if (intel_crtc->lowfreq_avail) {
8024 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
8025 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
8026 } else {
8027 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01008028 }
8029 }
8030
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008031 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02008032 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008033 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02008034 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8035 else
8036 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8037 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01008038 pipeconf |= PIPECONF_PROGRESSIVE;
8039
Wayne Boyer666a4532015-12-09 12:29:35 -08008040 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8041 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02008042 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03008043
Daniel Vetter84b046f2013-02-19 18:48:54 +01008044 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8045 POSTING_READ(PIPECONF(intel_crtc->pipe));
8046}
8047
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008048static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8049 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008050{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008051 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008052 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07008053 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008054 intel_clock_t clock;
8055 bool ok;
Ma Lingd4906092009-03-18 20:13:27 +08008056 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008057 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008058 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008059 struct drm_connector_state *connector_state;
8060 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008061
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008062 memset(&crtc_state->dpll_hw_state, 0,
8063 sizeof(crtc_state->dpll_hw_state));
8064
Jani Nikulaa65347b2015-11-27 12:21:46 +02008065 if (crtc_state->has_dsi_encoder)
Daniel Vetter5b18e572014-04-24 23:55:06 +02008066 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008067
Jani Nikulaa65347b2015-11-27 12:21:46 +02008068 for_each_connector_in_state(state, connector, connector_state, i) {
8069 if (connector_state->crtc == &crtc->base)
8070 num_connectors++;
8071 }
8072
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008073 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008074 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03008075
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008076 /*
8077 * Returns a set of divisors for the desired target clock with
8078 * the given refclk, or FALSE. The returned values represent
8079 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
8080 * 2) / p1 / p2.
8081 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008082 limit = intel_limit(crtc_state, refclk);
8083 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008084 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008085 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03008086 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008087 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8088 return -EINVAL;
8089 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008090
Jani Nikulaf2335332013-09-13 11:03:09 +03008091 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008092 crtc_state->dpll.n = clock.n;
8093 crtc_state->dpll.m1 = clock.m1;
8094 crtc_state->dpll.m2 = clock.m2;
8095 crtc_state->dpll.p1 = clock.p1;
8096 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008097 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008098
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008099 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008100 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02008101 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008102 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02008103 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008104 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02008105 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008106 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008107 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02008108 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008109 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008110
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008111 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008112}
8113
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008114static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008115 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008116{
8117 struct drm_device *dev = crtc->base.dev;
8118 struct drm_i915_private *dev_priv = dev->dev_private;
8119 uint32_t tmp;
8120
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008121 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8122 return;
8123
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008124 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008125 if (!(tmp & PFIT_ENABLE))
8126 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008127
Daniel Vetter06922822013-07-11 13:35:40 +02008128 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008129 if (INTEL_INFO(dev)->gen < 4) {
8130 if (crtc->pipe != PIPE_B)
8131 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008132 } else {
8133 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8134 return;
8135 }
8136
Daniel Vetter06922822013-07-11 13:35:40 +02008137 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008138 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
8139 if (INTEL_INFO(dev)->gen < 5)
8140 pipe_config->gmch_pfit.lvds_border_bits =
8141 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
8142}
8143
Jesse Barnesacbec812013-09-20 11:29:32 -07008144static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008145 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008146{
8147 struct drm_device *dev = crtc->base.dev;
8148 struct drm_i915_private *dev_priv = dev->dev_private;
8149 int pipe = pipe_config->cpu_transcoder;
8150 intel_clock_t clock;
8151 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008152 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008153
Shobhit Kumarf573de52014-07-30 20:32:37 +05308154 /* In case of MIPI DPLL will not even be used */
8155 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8156 return;
8157
Ville Syrjäläa5805162015-05-26 20:42:30 +03008158 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008159 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008160 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008161
8162 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8163 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8164 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8165 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8166 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8167
Imre Deakdccbea32015-06-22 23:35:51 +03008168 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008169}
8170
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008171static void
8172i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8173 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008174{
8175 struct drm_device *dev = crtc->base.dev;
8176 struct drm_i915_private *dev_priv = dev->dev_private;
8177 u32 val, base, offset;
8178 int pipe = crtc->pipe, plane = crtc->plane;
8179 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008180 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008181 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008182 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008183
Damien Lespiau42a7b082015-02-05 19:35:13 +00008184 val = I915_READ(DSPCNTR(plane));
8185 if (!(val & DISPLAY_PLANE_ENABLE))
8186 return;
8187
Damien Lespiaud9806c92015-01-21 14:07:19 +00008188 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008189 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008190 DRM_DEBUG_KMS("failed to alloc fb\n");
8191 return;
8192 }
8193
Damien Lespiau1b842c82015-01-21 13:50:54 +00008194 fb = &intel_fb->base;
8195
Daniel Vetter18c52472015-02-10 17:16:09 +00008196 if (INTEL_INFO(dev)->gen >= 4) {
8197 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008198 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008199 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8200 }
8201 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008202
8203 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008204 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008205 fb->pixel_format = fourcc;
8206 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008207
8208 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008209 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008210 offset = I915_READ(DSPTILEOFF(plane));
8211 else
8212 offset = I915_READ(DSPLINOFF(plane));
8213 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8214 } else {
8215 base = I915_READ(DSPADDR(plane));
8216 }
8217 plane_config->base = base;
8218
8219 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008220 fb->width = ((val >> 16) & 0xfff) + 1;
8221 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008222
8223 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008224 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008225
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008226 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008227 fb->pixel_format,
8228 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008229
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008230 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008231
Damien Lespiau2844a922015-01-20 12:51:48 +00008232 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8233 pipe_name(pipe), plane, fb->width, fb->height,
8234 fb->bits_per_pixel, base, fb->pitches[0],
8235 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008236
Damien Lespiau2d140302015-02-05 17:22:18 +00008237 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008238}
8239
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008240static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008241 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008242{
8243 struct drm_device *dev = crtc->base.dev;
8244 struct drm_i915_private *dev_priv = dev->dev_private;
8245 int pipe = pipe_config->cpu_transcoder;
8246 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8247 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008248 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008249 int refclk = 100000;
8250
Ville Syrjäläa5805162015-05-26 20:42:30 +03008251 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008252 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8253 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8254 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8255 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008256 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008257 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008258
8259 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008260 clock.m2 = (pll_dw0 & 0xff) << 22;
8261 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8262 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008263 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8264 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8265 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8266
Imre Deakdccbea32015-06-22 23:35:51 +03008267 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008268}
8269
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008270static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008271 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008272{
8273 struct drm_device *dev = crtc->base.dev;
8274 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008275 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008276 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008277 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008278
Imre Deak17290502016-02-12 18:55:11 +02008279 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8280 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008281 return false;
8282
Daniel Vettere143a212013-07-04 12:01:15 +02008283 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008284 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008285
Imre Deak17290502016-02-12 18:55:11 +02008286 ret = false;
8287
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008288 tmp = I915_READ(PIPECONF(crtc->pipe));
8289 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008290 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008291
Wayne Boyer666a4532015-12-09 12:29:35 -08008292 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008293 switch (tmp & PIPECONF_BPC_MASK) {
8294 case PIPECONF_6BPC:
8295 pipe_config->pipe_bpp = 18;
8296 break;
8297 case PIPECONF_8BPC:
8298 pipe_config->pipe_bpp = 24;
8299 break;
8300 case PIPECONF_10BPC:
8301 pipe_config->pipe_bpp = 30;
8302 break;
8303 default:
8304 break;
8305 }
8306 }
8307
Wayne Boyer666a4532015-12-09 12:29:35 -08008308 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8309 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008310 pipe_config->limited_color_range = true;
8311
Ville Syrjälä282740f2013-09-04 18:30:03 +03008312 if (INTEL_INFO(dev)->gen < 4)
8313 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8314
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008315 intel_get_pipe_timings(crtc, pipe_config);
8316
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008317 i9xx_get_pfit_config(crtc, pipe_config);
8318
Daniel Vetter6c49f242013-06-06 12:45:25 +02008319 if (INTEL_INFO(dev)->gen >= 4) {
8320 tmp = I915_READ(DPLL_MD(crtc->pipe));
8321 pipe_config->pixel_multiplier =
8322 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8323 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008324 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008325 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8326 tmp = I915_READ(DPLL(crtc->pipe));
8327 pipe_config->pixel_multiplier =
8328 ((tmp & SDVO_MULTIPLIER_MASK)
8329 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8330 } else {
8331 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8332 * port and will be fixed up in the encoder->get_config
8333 * function. */
8334 pipe_config->pixel_multiplier = 1;
8335 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008336 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008337 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008338 /*
8339 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8340 * on 830. Filter it out here so that we don't
8341 * report errors due to that.
8342 */
8343 if (IS_I830(dev))
8344 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8345
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008346 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8347 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008348 } else {
8349 /* Mask out read-only status bits. */
8350 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8351 DPLL_PORTC_READY_MASK |
8352 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008353 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008354
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008355 if (IS_CHERRYVIEW(dev))
8356 chv_crtc_clock_get(crtc, pipe_config);
8357 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008358 vlv_crtc_clock_get(crtc, pipe_config);
8359 else
8360 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008361
Ville Syrjälä0f646142015-08-26 19:39:18 +03008362 /*
8363 * Normally the dotclock is filled in by the encoder .get_config()
8364 * but in case the pipe is enabled w/o any ports we need a sane
8365 * default.
8366 */
8367 pipe_config->base.adjusted_mode.crtc_clock =
8368 pipe_config->port_clock / pipe_config->pixel_multiplier;
8369
Imre Deak17290502016-02-12 18:55:11 +02008370 ret = true;
8371
8372out:
8373 intel_display_power_put(dev_priv, power_domain);
8374
8375 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008376}
8377
Paulo Zanonidde86e22012-12-01 12:04:25 -02008378static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008379{
8380 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008381 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008382 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008383 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008384 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008385 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008386 bool has_ck505 = false;
8387 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008388
8389 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008390 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008391 switch (encoder->type) {
8392 case INTEL_OUTPUT_LVDS:
8393 has_panel = true;
8394 has_lvds = true;
8395 break;
8396 case INTEL_OUTPUT_EDP:
8397 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008398 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008399 has_cpu_edp = true;
8400 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008401 default:
8402 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008403 }
8404 }
8405
Keith Packard99eb6a02011-09-26 14:29:12 -07008406 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008407 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008408 can_ssc = has_ck505;
8409 } else {
8410 has_ck505 = false;
8411 can_ssc = true;
8412 }
8413
Imre Deak2de69052013-05-08 13:14:04 +03008414 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8415 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008416
8417 /* Ironlake: try to setup display ref clock before DPLL
8418 * enabling. This is only under driver's control after
8419 * PCH B stepping, previous chipset stepping should be
8420 * ignoring this setting.
8421 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008422 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008423
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008424 /* As we must carefully and slowly disable/enable each source in turn,
8425 * compute the final state we want first and check if we need to
8426 * make any changes at all.
8427 */
8428 final = val;
8429 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008430 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008431 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008432 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008433 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8434
8435 final &= ~DREF_SSC_SOURCE_MASK;
8436 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8437 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008438
Keith Packard199e5d72011-09-22 12:01:57 -07008439 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008440 final |= DREF_SSC_SOURCE_ENABLE;
8441
8442 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8443 final |= DREF_SSC1_ENABLE;
8444
8445 if (has_cpu_edp) {
8446 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8447 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8448 else
8449 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8450 } else
8451 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8452 } else {
8453 final |= DREF_SSC_SOURCE_DISABLE;
8454 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8455 }
8456
8457 if (final == val)
8458 return;
8459
8460 /* Always enable nonspread source */
8461 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8462
8463 if (has_ck505)
8464 val |= DREF_NONSPREAD_CK505_ENABLE;
8465 else
8466 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8467
8468 if (has_panel) {
8469 val &= ~DREF_SSC_SOURCE_MASK;
8470 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008471
Keith Packard199e5d72011-09-22 12:01:57 -07008472 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008473 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008474 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008475 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008476 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008477 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008478
8479 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008480 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008481 POSTING_READ(PCH_DREF_CONTROL);
8482 udelay(200);
8483
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008484 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008485
8486 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008487 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008488 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008489 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008490 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008491 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008492 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008493 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008494 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008495
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008496 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008497 POSTING_READ(PCH_DREF_CONTROL);
8498 udelay(200);
8499 } else {
8500 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8501
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008502 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008503
8504 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008505 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008506
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008507 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008508 POSTING_READ(PCH_DREF_CONTROL);
8509 udelay(200);
8510
8511 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008512 val &= ~DREF_SSC_SOURCE_MASK;
8513 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008514
8515 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008516 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008517
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008518 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008519 POSTING_READ(PCH_DREF_CONTROL);
8520 udelay(200);
8521 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008522
8523 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008524}
8525
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008526static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008527{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008528 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008529
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008530 tmp = I915_READ(SOUTH_CHICKEN2);
8531 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8532 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008533
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008534 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8535 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8536 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008537
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008538 tmp = I915_READ(SOUTH_CHICKEN2);
8539 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8540 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008541
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008542 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8543 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8544 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008545}
8546
8547/* WaMPhyProgramming:hsw */
8548static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8549{
8550 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008551
8552 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8553 tmp &= ~(0xFF << 24);
8554 tmp |= (0x12 << 24);
8555 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8556
Paulo Zanonidde86e22012-12-01 12:04:25 -02008557 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8558 tmp |= (1 << 11);
8559 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8560
8561 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8562 tmp |= (1 << 11);
8563 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8564
Paulo Zanonidde86e22012-12-01 12:04:25 -02008565 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8566 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8567 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8568
8569 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8570 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8571 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8572
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008573 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8574 tmp &= ~(7 << 13);
8575 tmp |= (5 << 13);
8576 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008577
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008578 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8579 tmp &= ~(7 << 13);
8580 tmp |= (5 << 13);
8581 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008582
8583 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8584 tmp &= ~0xFF;
8585 tmp |= 0x1C;
8586 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8587
8588 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8589 tmp &= ~0xFF;
8590 tmp |= 0x1C;
8591 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8592
8593 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8594 tmp &= ~(0xFF << 16);
8595 tmp |= (0x1C << 16);
8596 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8597
8598 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8599 tmp &= ~(0xFF << 16);
8600 tmp |= (0x1C << 16);
8601 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8602
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008603 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8604 tmp |= (1 << 27);
8605 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008606
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008607 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8608 tmp |= (1 << 27);
8609 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008610
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008611 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8612 tmp &= ~(0xF << 28);
8613 tmp |= (4 << 28);
8614 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008615
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008616 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8617 tmp &= ~(0xF << 28);
8618 tmp |= (4 << 28);
8619 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008620}
8621
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008622/* Implements 3 different sequences from BSpec chapter "Display iCLK
8623 * Programming" based on the parameters passed:
8624 * - Sequence to enable CLKOUT_DP
8625 * - Sequence to enable CLKOUT_DP without spread
8626 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8627 */
8628static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8629 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008630{
8631 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008632 uint32_t reg, tmp;
8633
8634 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8635 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008636 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008637 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008638
Ville Syrjäläa5805162015-05-26 20:42:30 +03008639 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008640
8641 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8642 tmp &= ~SBI_SSCCTL_DISABLE;
8643 tmp |= SBI_SSCCTL_PATHALT;
8644 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8645
8646 udelay(24);
8647
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008648 if (with_spread) {
8649 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8650 tmp &= ~SBI_SSCCTL_PATHALT;
8651 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008652
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008653 if (with_fdi) {
8654 lpt_reset_fdi_mphy(dev_priv);
8655 lpt_program_fdi_mphy(dev_priv);
8656 }
8657 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008658
Ville Syrjäläc2699522015-08-27 23:55:59 +03008659 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008660 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8661 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8662 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008663
Ville Syrjäläa5805162015-05-26 20:42:30 +03008664 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008665}
8666
Paulo Zanoni47701c32013-07-23 11:19:25 -03008667/* Sequence to disable CLKOUT_DP */
8668static void lpt_disable_clkout_dp(struct drm_device *dev)
8669{
8670 struct drm_i915_private *dev_priv = dev->dev_private;
8671 uint32_t reg, tmp;
8672
Ville Syrjäläa5805162015-05-26 20:42:30 +03008673 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008674
Ville Syrjäläc2699522015-08-27 23:55:59 +03008675 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008676 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8677 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8678 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8679
8680 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8681 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8682 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8683 tmp |= SBI_SSCCTL_PATHALT;
8684 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8685 udelay(32);
8686 }
8687 tmp |= SBI_SSCCTL_DISABLE;
8688 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8689 }
8690
Ville Syrjäläa5805162015-05-26 20:42:30 +03008691 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008692}
8693
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008694#define BEND_IDX(steps) ((50 + (steps)) / 5)
8695
8696static const uint16_t sscdivintphase[] = {
8697 [BEND_IDX( 50)] = 0x3B23,
8698 [BEND_IDX( 45)] = 0x3B23,
8699 [BEND_IDX( 40)] = 0x3C23,
8700 [BEND_IDX( 35)] = 0x3C23,
8701 [BEND_IDX( 30)] = 0x3D23,
8702 [BEND_IDX( 25)] = 0x3D23,
8703 [BEND_IDX( 20)] = 0x3E23,
8704 [BEND_IDX( 15)] = 0x3E23,
8705 [BEND_IDX( 10)] = 0x3F23,
8706 [BEND_IDX( 5)] = 0x3F23,
8707 [BEND_IDX( 0)] = 0x0025,
8708 [BEND_IDX( -5)] = 0x0025,
8709 [BEND_IDX(-10)] = 0x0125,
8710 [BEND_IDX(-15)] = 0x0125,
8711 [BEND_IDX(-20)] = 0x0225,
8712 [BEND_IDX(-25)] = 0x0225,
8713 [BEND_IDX(-30)] = 0x0325,
8714 [BEND_IDX(-35)] = 0x0325,
8715 [BEND_IDX(-40)] = 0x0425,
8716 [BEND_IDX(-45)] = 0x0425,
8717 [BEND_IDX(-50)] = 0x0525,
8718};
8719
8720/*
8721 * Bend CLKOUT_DP
8722 * steps -50 to 50 inclusive, in steps of 5
8723 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8724 * change in clock period = -(steps / 10) * 5.787 ps
8725 */
8726static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8727{
8728 uint32_t tmp;
8729 int idx = BEND_IDX(steps);
8730
8731 if (WARN_ON(steps % 5 != 0))
8732 return;
8733
8734 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8735 return;
8736
8737 mutex_lock(&dev_priv->sb_lock);
8738
8739 if (steps % 10 != 0)
8740 tmp = 0xAAAAAAAB;
8741 else
8742 tmp = 0x00000000;
8743 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8744
8745 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8746 tmp &= 0xffff0000;
8747 tmp |= sscdivintphase[idx];
8748 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8749
8750 mutex_unlock(&dev_priv->sb_lock);
8751}
8752
8753#undef BEND_IDX
8754
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008755static void lpt_init_pch_refclk(struct drm_device *dev)
8756{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008757 struct intel_encoder *encoder;
8758 bool has_vga = false;
8759
Damien Lespiaub2784e12014-08-05 11:29:37 +01008760 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008761 switch (encoder->type) {
8762 case INTEL_OUTPUT_ANALOG:
8763 has_vga = true;
8764 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008765 default:
8766 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008767 }
8768 }
8769
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008770 if (has_vga) {
8771 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008772 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008773 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008774 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008775 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008776}
8777
Paulo Zanonidde86e22012-12-01 12:04:25 -02008778/*
8779 * Initialize reference clocks when the driver loads
8780 */
8781void intel_init_pch_refclk(struct drm_device *dev)
8782{
8783 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8784 ironlake_init_pch_refclk(dev);
8785 else if (HAS_PCH_LPT(dev))
8786 lpt_init_pch_refclk(dev);
8787}
8788
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008789static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008790{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008791 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008792 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008793 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008794 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008795 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008796 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008797 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008798 bool is_lvds = false;
8799
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008800 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008801 if (connector_state->crtc != crtc_state->base.crtc)
8802 continue;
8803
8804 encoder = to_intel_encoder(connector_state->best_encoder);
8805
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008806 switch (encoder->type) {
8807 case INTEL_OUTPUT_LVDS:
8808 is_lvds = true;
8809 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008810 default:
8811 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008812 }
8813 num_connectors++;
8814 }
8815
8816 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008817 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008818 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008819 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008820 }
8821
8822 return 120000;
8823}
8824
Daniel Vetter6ff93602013-04-19 11:24:36 +02008825static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008826{
8827 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8829 int pipe = intel_crtc->pipe;
8830 uint32_t val;
8831
Daniel Vetter78114072013-06-13 00:54:57 +02008832 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008833
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008834 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008835 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008836 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008837 break;
8838 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008839 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008840 break;
8841 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008842 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008843 break;
8844 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008845 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008846 break;
8847 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008848 /* Case prevented by intel_choose_pipe_bpp_dither. */
8849 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008850 }
8851
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008852 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008853 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8854
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008855 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008856 val |= PIPECONF_INTERLACED_ILK;
8857 else
8858 val |= PIPECONF_PROGRESSIVE;
8859
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008860 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008861 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008862
Paulo Zanonic8203562012-09-12 10:06:29 -03008863 I915_WRITE(PIPECONF(pipe), val);
8864 POSTING_READ(PIPECONF(pipe));
8865}
8866
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008867/*
8868 * Set up the pipe CSC unit.
8869 *
8870 * Currently only full range RGB to limited range RGB conversion
8871 * is supported, but eventually this should handle various
8872 * RGB<->YCbCr scenarios as well.
8873 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008874static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008875{
8876 struct drm_device *dev = crtc->dev;
8877 struct drm_i915_private *dev_priv = dev->dev_private;
8878 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8879 int pipe = intel_crtc->pipe;
8880 uint16_t coeff = 0x7800; /* 1.0 */
8881
8882 /*
8883 * TODO: Check what kind of values actually come out of the pipe
8884 * with these coeff/postoff values and adjust to get the best
8885 * accuracy. Perhaps we even need to take the bpc value into
8886 * consideration.
8887 */
8888
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008889 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008890 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8891
8892 /*
8893 * GY/GU and RY/RU should be the other way around according
8894 * to BSpec, but reality doesn't agree. Just set them up in
8895 * a way that results in the correct picture.
8896 */
8897 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8898 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8899
8900 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8901 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8902
8903 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8904 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8905
8906 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8907 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8908 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8909
8910 if (INTEL_INFO(dev)->gen > 6) {
8911 uint16_t postoff = 0;
8912
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008913 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008914 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008915
8916 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8917 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8918 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8919
8920 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8921 } else {
8922 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8923
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008924 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008925 mode |= CSC_BLACK_SCREEN_OFFSET;
8926
8927 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8928 }
8929}
8930
Daniel Vetter6ff93602013-04-19 11:24:36 +02008931static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008932{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008933 struct drm_device *dev = crtc->dev;
8934 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008935 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008936 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008937 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008938 uint32_t val;
8939
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008940 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008941
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008942 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008943 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8944
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008945 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008946 val |= PIPECONF_INTERLACED_ILK;
8947 else
8948 val |= PIPECONF_PROGRESSIVE;
8949
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008950 I915_WRITE(PIPECONF(cpu_transcoder), val);
8951 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008952
8953 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8954 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008955
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308956 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008957 val = 0;
8958
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008959 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008960 case 18:
8961 val |= PIPEMISC_DITHER_6_BPC;
8962 break;
8963 case 24:
8964 val |= PIPEMISC_DITHER_8_BPC;
8965 break;
8966 case 30:
8967 val |= PIPEMISC_DITHER_10_BPC;
8968 break;
8969 case 36:
8970 val |= PIPEMISC_DITHER_12_BPC;
8971 break;
8972 default:
8973 /* Case prevented by pipe_config_set_bpp. */
8974 BUG();
8975 }
8976
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008977 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008978 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8979
8980 I915_WRITE(PIPEMISC(pipe), val);
8981 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008982}
8983
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008984static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008985 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008986 intel_clock_t *clock,
8987 bool *has_reduced_clock,
8988 intel_clock_t *reduced_clock)
8989{
8990 struct drm_device *dev = crtc->dev;
8991 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008992 int refclk;
8993 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008994 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008995
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008996 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008997
8998 /*
8999 * Returns a set of divisors for the desired target clock with the given
9000 * refclk, or FALSE. The returned values represent the clock equation:
9001 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
9002 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02009003 limit = intel_limit(crtc_state, refclk);
9004 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009005 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02009006 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03009007 if (!ret)
9008 return false;
9009
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03009010 return true;
9011}
9012
Paulo Zanonid4b19312012-11-29 11:29:32 -02009013int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
9014{
9015 /*
9016 * Account for spread spectrum to avoid
9017 * oversubscribing the link. Max center spread
9018 * is 2.5%; use 5% for safety's sake.
9019 */
9020 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02009021 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02009022}
9023
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009024static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02009025{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009026 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03009027}
9028
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009029static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009030 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009031 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02009032 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009033{
9034 struct drm_crtc *crtc = &intel_crtc->base;
9035 struct drm_device *dev = crtc->dev;
9036 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02009037 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03009038 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02009039 struct drm_connector_state *connector_state;
9040 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009041 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02009042 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02009043 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009044
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03009045 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02009046 if (connector_state->crtc != crtc_state->base.crtc)
9047 continue;
9048
9049 encoder = to_intel_encoder(connector_state->best_encoder);
9050
9051 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009052 case INTEL_OUTPUT_LVDS:
9053 is_lvds = true;
9054 break;
9055 case INTEL_OUTPUT_SDVO:
9056 case INTEL_OUTPUT_HDMI:
9057 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009058 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02009059 default:
9060 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009061 }
9062
9063 num_connectors++;
9064 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009065
Chris Wilsonc1858122010-12-03 21:35:48 +00009066 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07009067 factor = 21;
9068 if (is_lvds) {
9069 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009070 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02009071 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07009072 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009073 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07009074 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00009075
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009076 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02009077 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00009078
Daniel Vetter9a7c7892013-04-04 22:20:34 +02009079 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
9080 *fp2 |= FP_CB_TUNE;
9081
Chris Wilson5eddb702010-09-11 13:48:45 +01009082 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08009083
Eric Anholta07d6782011-03-30 13:01:08 -07009084 if (is_lvds)
9085 dpll |= DPLLB_MODE_LVDS;
9086 else
9087 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009088
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009089 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02009090 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009091
9092 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02009093 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009094 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02009095 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08009096
Eric Anholta07d6782011-03-30 13:01:08 -07009097 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009098 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009099 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009100 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009101
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009102 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07009103 case 5:
9104 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9105 break;
9106 case 7:
9107 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9108 break;
9109 case 10:
9110 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9111 break;
9112 case 14:
9113 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9114 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009115 }
9116
Daniel Vetterb4c09f32013-04-30 14:01:42 +02009117 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05009118 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08009119 else
9120 dpll |= PLL_REF_INPUT_DREFCLK;
9121
Daniel Vetter959e16d2013-06-05 13:34:21 +02009122 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009123}
9124
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009125static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9126 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08009127{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009128 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009129 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009130 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03009131 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01009132 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009133 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009134
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03009135 memset(&crtc_state->dpll_hw_state, 0,
9136 sizeof(crtc_state->dpll_hw_state));
9137
Ville Syrjälä7905df22015-11-25 16:35:30 +02009138 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08009139
Paulo Zanoni5dc52982012-10-05 12:05:56 -03009140 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
9141 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
9142
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009143 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03009144 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009145 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009146 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9147 return -EINVAL;
9148 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01009149 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009150 if (!crtc_state->clock_set) {
9151 crtc_state->dpll.n = clock.n;
9152 crtc_state->dpll.m1 = clock.m1;
9153 crtc_state->dpll.m2 = clock.m2;
9154 crtc_state->dpll.p1 = clock.p1;
9155 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009156 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009157
Paulo Zanoni5dc52982012-10-05 12:05:56 -03009158 /* 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 +02009159 if (crtc_state->has_pch_encoder) {
9160 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009161 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009162 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009163
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009164 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009165 &fp, &reduced_clock,
9166 has_reduced_clock ? &fp2 : NULL);
9167
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009168 crtc_state->dpll_hw_state.dpll = dpll;
9169 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009170 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009171 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009172 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009173 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009174
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009175 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009176 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03009177 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009178 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07009179 return -EINVAL;
9180 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009181 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009182
Rodrigo Viviab585de2015-03-24 12:40:09 -07009183 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009184 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02009185 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009186 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009187
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009188 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009189}
9190
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009191static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9192 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009193{
9194 struct drm_device *dev = crtc->base.dev;
9195 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009196 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009197
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009198 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9199 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9200 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9201 & ~TU_SIZE_MASK;
9202 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9203 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9204 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9205}
9206
9207static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9208 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009209 struct intel_link_m_n *m_n,
9210 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009211{
9212 struct drm_device *dev = crtc->base.dev;
9213 struct drm_i915_private *dev_priv = dev->dev_private;
9214 enum pipe pipe = crtc->pipe;
9215
9216 if (INTEL_INFO(dev)->gen >= 5) {
9217 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9218 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9219 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9220 & ~TU_SIZE_MASK;
9221 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9222 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9223 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009224 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9225 * gen < 8) and if DRRS is supported (to make sure the
9226 * registers are not unnecessarily read).
9227 */
9228 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009229 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009230 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9231 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9232 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9233 & ~TU_SIZE_MASK;
9234 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9235 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9236 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9237 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009238 } else {
9239 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9240 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9241 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9242 & ~TU_SIZE_MASK;
9243 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9244 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9245 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9246 }
9247}
9248
9249void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009250 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009251{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009252 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009253 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9254 else
9255 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009256 &pipe_config->dp_m_n,
9257 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009258}
9259
Daniel Vetter72419202013-04-04 13:28:53 +02009260static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009261 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009262{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009263 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009264 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009265}
9266
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009267static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009268 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009269{
9270 struct drm_device *dev = crtc->base.dev;
9271 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009272 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9273 uint32_t ps_ctrl = 0;
9274 int id = -1;
9275 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009276
Chandra Kondurua1b22782015-04-07 15:28:45 -07009277 /* find scaler attached to this pipe */
9278 for (i = 0; i < crtc->num_scalers; i++) {
9279 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9280 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9281 id = i;
9282 pipe_config->pch_pfit.enabled = true;
9283 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9284 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9285 break;
9286 }
9287 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009288
Chandra Kondurua1b22782015-04-07 15:28:45 -07009289 scaler_state->scaler_id = id;
9290 if (id >= 0) {
9291 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9292 } else {
9293 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009294 }
9295}
9296
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009297static void
9298skylake_get_initial_plane_config(struct intel_crtc *crtc,
9299 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009300{
9301 struct drm_device *dev = crtc->base.dev;
9302 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009303 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009304 int pipe = crtc->pipe;
9305 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009306 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009307 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009308 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009309
Damien Lespiaud9806c92015-01-21 14:07:19 +00009310 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009311 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009312 DRM_DEBUG_KMS("failed to alloc fb\n");
9313 return;
9314 }
9315
Damien Lespiau1b842c82015-01-21 13:50:54 +00009316 fb = &intel_fb->base;
9317
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009318 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009319 if (!(val & PLANE_CTL_ENABLE))
9320 goto error;
9321
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009322 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9323 fourcc = skl_format_to_fourcc(pixel_format,
9324 val & PLANE_CTL_ORDER_RGBX,
9325 val & PLANE_CTL_ALPHA_MASK);
9326 fb->pixel_format = fourcc;
9327 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9328
Damien Lespiau40f46282015-02-27 11:15:21 +00009329 tiling = val & PLANE_CTL_TILED_MASK;
9330 switch (tiling) {
9331 case PLANE_CTL_TILED_LINEAR:
9332 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9333 break;
9334 case PLANE_CTL_TILED_X:
9335 plane_config->tiling = I915_TILING_X;
9336 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9337 break;
9338 case PLANE_CTL_TILED_Y:
9339 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9340 break;
9341 case PLANE_CTL_TILED_YF:
9342 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9343 break;
9344 default:
9345 MISSING_CASE(tiling);
9346 goto error;
9347 }
9348
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009349 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9350 plane_config->base = base;
9351
9352 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9353
9354 val = I915_READ(PLANE_SIZE(pipe, 0));
9355 fb->height = ((val >> 16) & 0xfff) + 1;
9356 fb->width = ((val >> 0) & 0x1fff) + 1;
9357
9358 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009359 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009360 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009361 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9362
9363 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009364 fb->pixel_format,
9365 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009366
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009367 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009368
9369 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9370 pipe_name(pipe), fb->width, fb->height,
9371 fb->bits_per_pixel, base, fb->pitches[0],
9372 plane_config->size);
9373
Damien Lespiau2d140302015-02-05 17:22:18 +00009374 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009375 return;
9376
9377error:
9378 kfree(fb);
9379}
9380
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009381static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009382 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009383{
9384 struct drm_device *dev = crtc->base.dev;
9385 struct drm_i915_private *dev_priv = dev->dev_private;
9386 uint32_t tmp;
9387
9388 tmp = I915_READ(PF_CTL(crtc->pipe));
9389
9390 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009391 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009392 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9393 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009394
9395 /* We currently do not free assignements of panel fitters on
9396 * ivb/hsw (since we don't use the higher upscaling modes which
9397 * differentiates them) so just WARN about this case for now. */
9398 if (IS_GEN7(dev)) {
9399 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9400 PF_PIPE_SEL_IVB(crtc->pipe));
9401 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009402 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009403}
9404
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009405static void
9406ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9407 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009408{
9409 struct drm_device *dev = crtc->base.dev;
9410 struct drm_i915_private *dev_priv = dev->dev_private;
9411 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009412 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009413 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009414 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009415 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009416 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009417
Damien Lespiau42a7b082015-02-05 19:35:13 +00009418 val = I915_READ(DSPCNTR(pipe));
9419 if (!(val & DISPLAY_PLANE_ENABLE))
9420 return;
9421
Damien Lespiaud9806c92015-01-21 14:07:19 +00009422 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009423 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009424 DRM_DEBUG_KMS("failed to alloc fb\n");
9425 return;
9426 }
9427
Damien Lespiau1b842c82015-01-21 13:50:54 +00009428 fb = &intel_fb->base;
9429
Daniel Vetter18c52472015-02-10 17:16:09 +00009430 if (INTEL_INFO(dev)->gen >= 4) {
9431 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009432 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009433 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9434 }
9435 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009436
9437 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009438 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009439 fb->pixel_format = fourcc;
9440 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009441
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009442 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009443 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009444 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009445 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009446 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009447 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009448 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009449 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009450 }
9451 plane_config->base = base;
9452
9453 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009454 fb->width = ((val >> 16) & 0xfff) + 1;
9455 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009456
9457 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009458 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009459
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009460 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009461 fb->pixel_format,
9462 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009463
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009464 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009465
Damien Lespiau2844a922015-01-20 12:51:48 +00009466 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9467 pipe_name(pipe), fb->width, fb->height,
9468 fb->bits_per_pixel, base, fb->pitches[0],
9469 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009470
Damien Lespiau2d140302015-02-05 17:22:18 +00009471 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009472}
9473
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009474static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009475 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009476{
9477 struct drm_device *dev = crtc->base.dev;
9478 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009479 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009480 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009481 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009482
Imre Deak17290502016-02-12 18:55:11 +02009483 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9484 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009485 return false;
9486
Daniel Vettere143a212013-07-04 12:01:15 +02009487 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009488 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009489
Imre Deak17290502016-02-12 18:55:11 +02009490 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009491 tmp = I915_READ(PIPECONF(crtc->pipe));
9492 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009493 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009494
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009495 switch (tmp & PIPECONF_BPC_MASK) {
9496 case PIPECONF_6BPC:
9497 pipe_config->pipe_bpp = 18;
9498 break;
9499 case PIPECONF_8BPC:
9500 pipe_config->pipe_bpp = 24;
9501 break;
9502 case PIPECONF_10BPC:
9503 pipe_config->pipe_bpp = 30;
9504 break;
9505 case PIPECONF_12BPC:
9506 pipe_config->pipe_bpp = 36;
9507 break;
9508 default:
9509 break;
9510 }
9511
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009512 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9513 pipe_config->limited_color_range = true;
9514
Daniel Vetterab9412b2013-05-03 11:49:46 +02009515 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009516 struct intel_shared_dpll *pll;
9517
Daniel Vetter88adfff2013-03-28 10:42:01 +01009518 pipe_config->has_pch_encoder = true;
9519
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009520 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9521 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9522 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009523
9524 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009525
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009526 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009527 pipe_config->shared_dpll =
9528 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009529 } else {
9530 tmp = I915_READ(PCH_DPLL_SEL);
9531 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9532 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9533 else
9534 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9535 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009536
9537 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9538
9539 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9540 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009541
9542 tmp = pipe_config->dpll_hw_state.dpll;
9543 pipe_config->pixel_multiplier =
9544 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9545 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009546
9547 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009548 } else {
9549 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009550 }
9551
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009552 intel_get_pipe_timings(crtc, pipe_config);
9553
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009554 ironlake_get_pfit_config(crtc, pipe_config);
9555
Imre Deak17290502016-02-12 18:55:11 +02009556 ret = true;
9557
9558out:
9559 intel_display_power_put(dev_priv, power_domain);
9560
9561 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009562}
9563
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009564static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9565{
9566 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009567 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009568
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009569 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009570 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009571 pipe_name(crtc->pipe));
9572
Rob Clarke2c719b2014-12-15 13:56:32 -05009573 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9574 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009575 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9576 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009577 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9578 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009579 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009580 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009581 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009582 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009583 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009584 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009585 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009586 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009587 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009588
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009589 /*
9590 * In theory we can still leave IRQs enabled, as long as only the HPD
9591 * interrupts remain enabled. We used to check for that, but since it's
9592 * gen-specific and since we only disable LCPLL after we fully disable
9593 * the interrupts, the check below should be enough.
9594 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009595 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009596}
9597
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009598static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9599{
9600 struct drm_device *dev = dev_priv->dev;
9601
9602 if (IS_HASWELL(dev))
9603 return I915_READ(D_COMP_HSW);
9604 else
9605 return I915_READ(D_COMP_BDW);
9606}
9607
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009608static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9609{
9610 struct drm_device *dev = dev_priv->dev;
9611
9612 if (IS_HASWELL(dev)) {
9613 mutex_lock(&dev_priv->rps.hw_lock);
9614 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9615 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009616 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009617 mutex_unlock(&dev_priv->rps.hw_lock);
9618 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009619 I915_WRITE(D_COMP_BDW, val);
9620 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009621 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009622}
9623
9624/*
9625 * This function implements pieces of two sequences from BSpec:
9626 * - Sequence for display software to disable LCPLL
9627 * - Sequence for display software to allow package C8+
9628 * The steps implemented here are just the steps that actually touch the LCPLL
9629 * register. Callers should take care of disabling all the display engine
9630 * functions, doing the mode unset, fixing interrupts, etc.
9631 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009632static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9633 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009634{
9635 uint32_t val;
9636
9637 assert_can_disable_lcpll(dev_priv);
9638
9639 val = I915_READ(LCPLL_CTL);
9640
9641 if (switch_to_fclk) {
9642 val |= LCPLL_CD_SOURCE_FCLK;
9643 I915_WRITE(LCPLL_CTL, val);
9644
9645 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9646 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9647 DRM_ERROR("Switching to FCLK failed\n");
9648
9649 val = I915_READ(LCPLL_CTL);
9650 }
9651
9652 val |= LCPLL_PLL_DISABLE;
9653 I915_WRITE(LCPLL_CTL, val);
9654 POSTING_READ(LCPLL_CTL);
9655
9656 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9657 DRM_ERROR("LCPLL still locked\n");
9658
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009659 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009660 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009661 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009662 ndelay(100);
9663
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009664 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9665 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009666 DRM_ERROR("D_COMP RCOMP still in progress\n");
9667
9668 if (allow_power_down) {
9669 val = I915_READ(LCPLL_CTL);
9670 val |= LCPLL_POWER_DOWN_ALLOW;
9671 I915_WRITE(LCPLL_CTL, val);
9672 POSTING_READ(LCPLL_CTL);
9673 }
9674}
9675
9676/*
9677 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9678 * source.
9679 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009680static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009681{
9682 uint32_t val;
9683
9684 val = I915_READ(LCPLL_CTL);
9685
9686 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9687 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9688 return;
9689
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009690 /*
9691 * Make sure we're not on PC8 state before disabling PC8, otherwise
9692 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009693 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009694 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009695
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009696 if (val & LCPLL_POWER_DOWN_ALLOW) {
9697 val &= ~LCPLL_POWER_DOWN_ALLOW;
9698 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009699 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009700 }
9701
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009702 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009703 val |= D_COMP_COMP_FORCE;
9704 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009705 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009706
9707 val = I915_READ(LCPLL_CTL);
9708 val &= ~LCPLL_PLL_DISABLE;
9709 I915_WRITE(LCPLL_CTL, val);
9710
9711 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9712 DRM_ERROR("LCPLL not locked yet\n");
9713
9714 if (val & LCPLL_CD_SOURCE_FCLK) {
9715 val = I915_READ(LCPLL_CTL);
9716 val &= ~LCPLL_CD_SOURCE_FCLK;
9717 I915_WRITE(LCPLL_CTL, val);
9718
9719 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9720 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9721 DRM_ERROR("Switching back to LCPLL failed\n");
9722 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009723
Mika Kuoppala59bad942015-01-16 11:34:40 +02009724 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009725 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009726}
9727
Paulo Zanoni765dab672014-03-07 20:08:18 -03009728/*
9729 * Package states C8 and deeper are really deep PC states that can only be
9730 * reached when all the devices on the system allow it, so even if the graphics
9731 * device allows PC8+, it doesn't mean the system will actually get to these
9732 * states. Our driver only allows PC8+ when going into runtime PM.
9733 *
9734 * The requirements for PC8+ are that all the outputs are disabled, the power
9735 * well is disabled and most interrupts are disabled, and these are also
9736 * requirements for runtime PM. When these conditions are met, we manually do
9737 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9738 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9739 * hang the machine.
9740 *
9741 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9742 * the state of some registers, so when we come back from PC8+ we need to
9743 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9744 * need to take care of the registers kept by RC6. Notice that this happens even
9745 * if we don't put the device in PCI D3 state (which is what currently happens
9746 * because of the runtime PM support).
9747 *
9748 * For more, read "Display Sequences for Package C8" on the hardware
9749 * documentation.
9750 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009751void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009752{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009753 struct drm_device *dev = dev_priv->dev;
9754 uint32_t val;
9755
Paulo Zanonic67a4702013-08-19 13:18:09 -03009756 DRM_DEBUG_KMS("Enabling package C8+\n");
9757
Ville Syrjäläc2699522015-08-27 23:55:59 +03009758 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009759 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9760 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9761 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9762 }
9763
9764 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009765 hsw_disable_lcpll(dev_priv, true, true);
9766}
9767
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009768void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009769{
9770 struct drm_device *dev = dev_priv->dev;
9771 uint32_t val;
9772
Paulo Zanonic67a4702013-08-19 13:18:09 -03009773 DRM_DEBUG_KMS("Disabling package C8+\n");
9774
9775 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009776 lpt_init_pch_refclk(dev);
9777
Ville Syrjäläc2699522015-08-27 23:55:59 +03009778 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009779 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9780 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9781 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9782 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009783}
9784
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009785static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309786{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009787 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009788 struct intel_atomic_state *old_intel_state =
9789 to_intel_atomic_state(old_state);
9790 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309791
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009792 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309793}
9794
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009795/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009796static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009797{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009798 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9799 struct drm_i915_private *dev_priv = state->dev->dev_private;
9800 struct drm_crtc *crtc;
9801 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009802 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009803 unsigned max_pixel_rate = 0, i;
9804 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009805
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009806 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9807 sizeof(intel_state->min_pixclk));
9808
9809 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009810 int pixel_rate;
9811
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009812 crtc_state = to_intel_crtc_state(cstate);
9813 if (!crtc_state->base.enable) {
9814 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009815 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009816 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009817
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009818 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009819
9820 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009821 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009822 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9823
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009824 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009825 }
9826
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009827 for_each_pipe(dev_priv, pipe)
9828 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9829
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009830 return max_pixel_rate;
9831}
9832
9833static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9834{
9835 struct drm_i915_private *dev_priv = dev->dev_private;
9836 uint32_t val, data;
9837 int ret;
9838
9839 if (WARN((I915_READ(LCPLL_CTL) &
9840 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9841 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9842 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9843 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9844 "trying to change cdclk frequency with cdclk not enabled\n"))
9845 return;
9846
9847 mutex_lock(&dev_priv->rps.hw_lock);
9848 ret = sandybridge_pcode_write(dev_priv,
9849 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9850 mutex_unlock(&dev_priv->rps.hw_lock);
9851 if (ret) {
9852 DRM_ERROR("failed to inform pcode about cdclk change\n");
9853 return;
9854 }
9855
9856 val = I915_READ(LCPLL_CTL);
9857 val |= LCPLL_CD_SOURCE_FCLK;
9858 I915_WRITE(LCPLL_CTL, val);
9859
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009860 if (wait_for_us(I915_READ(LCPLL_CTL) &
9861 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009862 DRM_ERROR("Switching to FCLK failed\n");
9863
9864 val = I915_READ(LCPLL_CTL);
9865 val &= ~LCPLL_CLK_FREQ_MASK;
9866
9867 switch (cdclk) {
9868 case 450000:
9869 val |= LCPLL_CLK_FREQ_450;
9870 data = 0;
9871 break;
9872 case 540000:
9873 val |= LCPLL_CLK_FREQ_54O_BDW;
9874 data = 1;
9875 break;
9876 case 337500:
9877 val |= LCPLL_CLK_FREQ_337_5_BDW;
9878 data = 2;
9879 break;
9880 case 675000:
9881 val |= LCPLL_CLK_FREQ_675_BDW;
9882 data = 3;
9883 break;
9884 default:
9885 WARN(1, "invalid cdclk frequency\n");
9886 return;
9887 }
9888
9889 I915_WRITE(LCPLL_CTL, val);
9890
9891 val = I915_READ(LCPLL_CTL);
9892 val &= ~LCPLL_CD_SOURCE_FCLK;
9893 I915_WRITE(LCPLL_CTL, val);
9894
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009895 if (wait_for_us((I915_READ(LCPLL_CTL) &
9896 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009897 DRM_ERROR("Switching back to LCPLL failed\n");
9898
9899 mutex_lock(&dev_priv->rps.hw_lock);
9900 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9901 mutex_unlock(&dev_priv->rps.hw_lock);
9902
9903 intel_update_cdclk(dev);
9904
9905 WARN(cdclk != dev_priv->cdclk_freq,
9906 "cdclk requested %d kHz but got %d kHz\n",
9907 cdclk, dev_priv->cdclk_freq);
9908}
9909
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009910static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009911{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009912 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009913 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009914 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009915 int cdclk;
9916
9917 /*
9918 * FIXME should also account for plane ratio
9919 * once 64bpp pixel formats are supported.
9920 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009921 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009922 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009923 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009924 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009925 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009926 cdclk = 450000;
9927 else
9928 cdclk = 337500;
9929
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009930 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009931 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9932 cdclk, dev_priv->max_cdclk_freq);
9933 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009934 }
9935
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009936 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9937 if (!intel_state->active_crtcs)
9938 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009939
9940 return 0;
9941}
9942
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009943static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009944{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009945 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009946 struct intel_atomic_state *old_intel_state =
9947 to_intel_atomic_state(old_state);
9948 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009949
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009950 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009951}
9952
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009953static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9954 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009955{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009956 struct intel_encoder *intel_encoder =
9957 intel_ddi_get_crtc_new_encoder(crtc_state);
9958
9959 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9960 if (!intel_ddi_pll_select(crtc, crtc_state))
9961 return -EINVAL;
9962 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009963
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009964 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009965
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009966 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009967}
9968
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309969static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9970 enum port port,
9971 struct intel_crtc_state *pipe_config)
9972{
9973 switch (port) {
9974 case PORT_A:
9975 pipe_config->ddi_pll_sel = SKL_DPLL0;
9976 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9977 break;
9978 case PORT_B:
9979 pipe_config->ddi_pll_sel = SKL_DPLL1;
9980 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9981 break;
9982 case PORT_C:
9983 pipe_config->ddi_pll_sel = SKL_DPLL2;
9984 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9985 break;
9986 default:
9987 DRM_ERROR("Incorrect port type\n");
9988 }
9989}
9990
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009991static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9992 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009993 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009994{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009995 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009996
9997 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9998 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9999
10000 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +000010001 case SKL_DPLL0:
10002 /*
10003 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
10004 * of the shared DPLL framework and thus needs to be read out
10005 * separately
10006 */
10007 dpll_ctl1 = I915_READ(DPLL_CTRL1);
10008 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
10009 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010010 case SKL_DPLL1:
10011 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
10012 break;
10013 case SKL_DPLL2:
10014 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
10015 break;
10016 case SKL_DPLL3:
10017 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
10018 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010019 }
10020}
10021
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010022static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10023 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010024 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010025{
10026 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
10027
10028 switch (pipe_config->ddi_pll_sel) {
10029 case PORT_CLK_SEL_WRPLL1:
10030 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
10031 break;
10032 case PORT_CLK_SEL_WRPLL2:
10033 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
10034 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +010010035 case PORT_CLK_SEL_SPLL:
10036 pipe_config->shared_dpll = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +020010037 break;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010038 }
10039}
10040
Daniel Vetter26804af2014-06-25 22:01:55 +030010041static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010042 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010043{
10044 struct drm_device *dev = crtc->base.dev;
10045 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010046 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010047 enum port port;
10048 uint32_t tmp;
10049
10050 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10051
10052 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10053
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070010054 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010055 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010056 else if (IS_BROXTON(dev))
10057 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010058 else
10059 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010060
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010061 if (pipe_config->shared_dpll >= 0) {
10062 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
10063
10064 WARN_ON(!pll->get_hw_state(dev_priv, pll,
10065 &pipe_config->dpll_hw_state));
10066 }
10067
Daniel Vetter26804af2014-06-25 22:01:55 +030010068 /*
10069 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10070 * DDI E. So just check whether this pipe is wired to DDI E and whether
10071 * the PCH transcoder is on.
10072 */
Damien Lespiauca370452013-12-03 13:56:24 +000010073 if (INTEL_INFO(dev)->gen < 9 &&
10074 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010075 pipe_config->has_pch_encoder = true;
10076
10077 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10078 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10079 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10080
10081 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10082 }
10083}
10084
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010085static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010086 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010087{
10088 struct drm_device *dev = crtc->base.dev;
10089 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +020010090 enum intel_display_power_domain power_domain;
10091 unsigned long power_domain_mask;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010092 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +020010093 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010094
Imre Deak17290502016-02-12 18:55:11 +020010095 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10096 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010097 return false;
Imre Deak17290502016-02-12 18:55:11 +020010098 power_domain_mask = BIT(power_domain);
10099
10100 ret = false;
Imre Deakb5482bd2014-03-05 16:20:55 +020010101
Daniel Vettere143a212013-07-04 12:01:15 +020010102 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010103 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
10104
Daniel Vettereccb1402013-05-22 00:50:22 +020010105 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10106 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10107 enum pipe trans_edp_pipe;
10108 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10109 default:
10110 WARN(1, "unknown pipe linked to edp transcoder\n");
10111 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10112 case TRANS_DDI_EDP_INPUT_A_ON:
10113 trans_edp_pipe = PIPE_A;
10114 break;
10115 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10116 trans_edp_pipe = PIPE_B;
10117 break;
10118 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10119 trans_edp_pipe = PIPE_C;
10120 break;
10121 }
10122
10123 if (trans_edp_pipe == crtc->pipe)
10124 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10125 }
10126
Imre Deak17290502016-02-12 18:55:11 +020010127 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10128 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10129 goto out;
10130 power_domain_mask |= BIT(power_domain);
Paulo Zanoni2bfce952013-04-18 16:35:40 -030010131
Daniel Vettereccb1402013-05-22 00:50:22 +020010132 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010133 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +020010134 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010135
Daniel Vetter26804af2014-06-25 22:01:55 +030010136 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010137
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010138 intel_get_pipe_timings(crtc, pipe_config);
10139
Chandra Kondurua1b22782015-04-07 15:28:45 -070010140 if (INTEL_INFO(dev)->gen >= 9) {
10141 skl_init_scalers(dev, crtc, pipe_config);
10142 }
10143
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010144 if (INTEL_INFO(dev)->gen >= 9) {
10145 pipe_config->scaler_state.scaler_id = -1;
10146 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10147 }
10148
Imre Deak17290502016-02-12 18:55:11 +020010149 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10150 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10151 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010152 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010153 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010154 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010155 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010156 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010157
Jesse Barnese59150d2014-01-07 13:30:45 -080010158 if (IS_HASWELL(dev))
10159 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10160 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010161
Clint Taylorebb69c92014-09-30 10:30:22 -070010162 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
10163 pipe_config->pixel_multiplier =
10164 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10165 } else {
10166 pipe_config->pixel_multiplier = 1;
10167 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010168
Imre Deak17290502016-02-12 18:55:11 +020010169 ret = true;
10170
10171out:
10172 for_each_power_domain(power_domain, power_domain_mask)
10173 intel_display_power_put(dev_priv, power_domain);
10174
10175 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010176}
10177
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010178static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10179 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010180{
10181 struct drm_device *dev = crtc->dev;
10182 struct drm_i915_private *dev_priv = dev->dev_private;
10183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010184 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010185
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010186 if (plane_state && plane_state->visible) {
10187 unsigned int width = plane_state->base.crtc_w;
10188 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010189 unsigned int stride = roundup_pow_of_two(width) * 4;
10190
10191 switch (stride) {
10192 default:
10193 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10194 width, stride);
10195 stride = 256;
10196 /* fallthrough */
10197 case 256:
10198 case 512:
10199 case 1024:
10200 case 2048:
10201 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010202 }
10203
Ville Syrjälädc41c152014-08-13 11:57:05 +030010204 cntl |= CURSOR_ENABLE |
10205 CURSOR_GAMMA_ENABLE |
10206 CURSOR_FORMAT_ARGB |
10207 CURSOR_STRIDE(stride);
10208
10209 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010210 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010211
Ville Syrjälädc41c152014-08-13 11:57:05 +030010212 if (intel_crtc->cursor_cntl != 0 &&
10213 (intel_crtc->cursor_base != base ||
10214 intel_crtc->cursor_size != size ||
10215 intel_crtc->cursor_cntl != cntl)) {
10216 /* On these chipsets we can only modify the base/size/stride
10217 * whilst the cursor is disabled.
10218 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010219 I915_WRITE(CURCNTR(PIPE_A), 0);
10220 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010221 intel_crtc->cursor_cntl = 0;
10222 }
10223
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010224 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010225 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010226 intel_crtc->cursor_base = base;
10227 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010228
10229 if (intel_crtc->cursor_size != size) {
10230 I915_WRITE(CURSIZE, size);
10231 intel_crtc->cursor_size = size;
10232 }
10233
Chris Wilson4b0e3332014-05-30 16:35:26 +030010234 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010235 I915_WRITE(CURCNTR(PIPE_A), cntl);
10236 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010237 intel_crtc->cursor_cntl = cntl;
10238 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010239}
10240
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010241static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10242 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010243{
10244 struct drm_device *dev = crtc->dev;
10245 struct drm_i915_private *dev_priv = dev->dev_private;
10246 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10247 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010248 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010249
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010250 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010251 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010252 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010253 case 64:
10254 cntl |= CURSOR_MODE_64_ARGB_AX;
10255 break;
10256 case 128:
10257 cntl |= CURSOR_MODE_128_ARGB_AX;
10258 break;
10259 case 256:
10260 cntl |= CURSOR_MODE_256_ARGB_AX;
10261 break;
10262 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010263 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010264 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010265 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010266 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010267
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010268 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010269 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010270
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010271 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10272 cntl |= CURSOR_ROTATE_180;
10273 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010274
Chris Wilson4b0e3332014-05-30 16:35:26 +030010275 if (intel_crtc->cursor_cntl != cntl) {
10276 I915_WRITE(CURCNTR(pipe), cntl);
10277 POSTING_READ(CURCNTR(pipe));
10278 intel_crtc->cursor_cntl = cntl;
10279 }
10280
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010281 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010282 I915_WRITE(CURBASE(pipe), base);
10283 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010284
10285 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010286}
10287
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010288/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010289static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010290 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010291{
10292 struct drm_device *dev = crtc->dev;
10293 struct drm_i915_private *dev_priv = dev->dev_private;
10294 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10295 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010296 u32 base = intel_crtc->cursor_addr;
10297 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010298
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010299 if (plane_state) {
10300 int x = plane_state->base.crtc_x;
10301 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010302
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010303 if (x < 0) {
10304 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10305 x = -x;
10306 }
10307 pos |= x << CURSOR_X_SHIFT;
10308
10309 if (y < 0) {
10310 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10311 y = -y;
10312 }
10313 pos |= y << CURSOR_Y_SHIFT;
10314
10315 /* ILK+ do this automagically */
10316 if (HAS_GMCH_DISPLAY(dev) &&
10317 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10318 base += (plane_state->base.crtc_h *
10319 plane_state->base.crtc_w - 1) * 4;
10320 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010321 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010322
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010323 I915_WRITE(CURPOS(pipe), pos);
10324
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010325 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010326 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010327 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010328 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010329}
10330
Ville Syrjälädc41c152014-08-13 11:57:05 +030010331static bool cursor_size_ok(struct drm_device *dev,
10332 uint32_t width, uint32_t height)
10333{
10334 if (width == 0 || height == 0)
10335 return false;
10336
10337 /*
10338 * 845g/865g are special in that they are only limited by
10339 * the width of their cursors, the height is arbitrary up to
10340 * the precision of the register. Everything else requires
10341 * square cursors, limited to a few power-of-two sizes.
10342 */
10343 if (IS_845G(dev) || IS_I865G(dev)) {
10344 if ((width & 63) != 0)
10345 return false;
10346
10347 if (width > (IS_845G(dev) ? 64 : 512))
10348 return false;
10349
10350 if (height > 1023)
10351 return false;
10352 } else {
10353 switch (width | height) {
10354 case 256:
10355 case 128:
10356 if (IS_GEN2(dev))
10357 return false;
10358 case 64:
10359 break;
10360 default:
10361 return false;
10362 }
10363 }
10364
10365 return true;
10366}
10367
Jesse Barnes79e53942008-11-07 14:24:08 -080010368static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010369 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010370{
James Simmons72034252010-08-03 01:33:19 +010010371 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010372 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010373
James Simmons72034252010-08-03 01:33:19 +010010374 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010375 intel_crtc->lut_r[i] = red[i] >> 8;
10376 intel_crtc->lut_g[i] = green[i] >> 8;
10377 intel_crtc->lut_b[i] = blue[i] >> 8;
10378 }
10379
10380 intel_crtc_load_lut(crtc);
10381}
10382
Jesse Barnes79e53942008-11-07 14:24:08 -080010383/* VESA 640x480x72Hz mode to set on the pipe */
10384static struct drm_display_mode load_detect_mode = {
10385 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10386 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10387};
10388
Daniel Vettera8bb6812014-02-10 18:00:39 +010010389struct drm_framebuffer *
10390__intel_framebuffer_create(struct drm_device *dev,
10391 struct drm_mode_fb_cmd2 *mode_cmd,
10392 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010393{
10394 struct intel_framebuffer *intel_fb;
10395 int ret;
10396
10397 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010398 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010399 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010400
10401 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010402 if (ret)
10403 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010404
10405 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010406
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010407err:
10408 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010409 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010410}
10411
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010412static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010413intel_framebuffer_create(struct drm_device *dev,
10414 struct drm_mode_fb_cmd2 *mode_cmd,
10415 struct drm_i915_gem_object *obj)
10416{
10417 struct drm_framebuffer *fb;
10418 int ret;
10419
10420 ret = i915_mutex_lock_interruptible(dev);
10421 if (ret)
10422 return ERR_PTR(ret);
10423 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10424 mutex_unlock(&dev->struct_mutex);
10425
10426 return fb;
10427}
10428
Chris Wilsond2dff872011-04-19 08:36:26 +010010429static u32
10430intel_framebuffer_pitch_for_width(int width, int bpp)
10431{
10432 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10433 return ALIGN(pitch, 64);
10434}
10435
10436static u32
10437intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10438{
10439 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010440 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010441}
10442
10443static struct drm_framebuffer *
10444intel_framebuffer_create_for_mode(struct drm_device *dev,
10445 struct drm_display_mode *mode,
10446 int depth, int bpp)
10447{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010448 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010449 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010450 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010451
10452 obj = i915_gem_alloc_object(dev,
10453 intel_framebuffer_size_for_mode(mode, bpp));
10454 if (obj == NULL)
10455 return ERR_PTR(-ENOMEM);
10456
10457 mode_cmd.width = mode->hdisplay;
10458 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010459 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10460 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010461 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010462
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010463 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10464 if (IS_ERR(fb))
10465 drm_gem_object_unreference_unlocked(&obj->base);
10466
10467 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010468}
10469
10470static struct drm_framebuffer *
10471mode_fits_in_fbdev(struct drm_device *dev,
10472 struct drm_display_mode *mode)
10473{
Daniel Vetter06957262015-08-10 13:34:08 +020010474#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010475 struct drm_i915_private *dev_priv = dev->dev_private;
10476 struct drm_i915_gem_object *obj;
10477 struct drm_framebuffer *fb;
10478
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010479 if (!dev_priv->fbdev)
10480 return NULL;
10481
10482 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010483 return NULL;
10484
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010485 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010486 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010487
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010488 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010489 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10490 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010491 return NULL;
10492
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010493 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010494 return NULL;
10495
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010496 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010497 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010498#else
10499 return NULL;
10500#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010501}
10502
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010503static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10504 struct drm_crtc *crtc,
10505 struct drm_display_mode *mode,
10506 struct drm_framebuffer *fb,
10507 int x, int y)
10508{
10509 struct drm_plane_state *plane_state;
10510 int hdisplay, vdisplay;
10511 int ret;
10512
10513 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10514 if (IS_ERR(plane_state))
10515 return PTR_ERR(plane_state);
10516
10517 if (mode)
10518 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10519 else
10520 hdisplay = vdisplay = 0;
10521
10522 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10523 if (ret)
10524 return ret;
10525 drm_atomic_set_fb_for_plane(plane_state, fb);
10526 plane_state->crtc_x = 0;
10527 plane_state->crtc_y = 0;
10528 plane_state->crtc_w = hdisplay;
10529 plane_state->crtc_h = vdisplay;
10530 plane_state->src_x = x << 16;
10531 plane_state->src_y = y << 16;
10532 plane_state->src_w = hdisplay << 16;
10533 plane_state->src_h = vdisplay << 16;
10534
10535 return 0;
10536}
10537
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010538bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010539 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010540 struct intel_load_detect_pipe *old,
10541 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010542{
10543 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010544 struct intel_encoder *intel_encoder =
10545 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010546 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010547 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010548 struct drm_crtc *crtc = NULL;
10549 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010550 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010551 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010552 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010553 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010554 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010555 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010556
Chris Wilsond2dff872011-04-19 08:36:26 +010010557 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010558 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010559 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010560
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010561 old->restore_state = NULL;
10562
Rob Clark51fd3712013-11-19 12:10:12 -050010563retry:
10564 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10565 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010566 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010567
Jesse Barnes79e53942008-11-07 14:24:08 -080010568 /*
10569 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010570 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010571 * - if the connector already has an assigned crtc, use it (but make
10572 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010573 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010574 * - try to find the first unused crtc that can drive this connector,
10575 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010576 */
10577
10578 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010579 if (connector->state->crtc) {
10580 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010581
Rob Clark51fd3712013-11-19 12:10:12 -050010582 ret = drm_modeset_lock(&crtc->mutex, ctx);
10583 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010584 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010585
10586 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010587 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010588 }
10589
10590 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010591 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010592 i++;
10593 if (!(encoder->possible_crtcs & (1 << i)))
10594 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010595
10596 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10597 if (ret)
10598 goto fail;
10599
10600 if (possible_crtc->state->enable) {
10601 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010602 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010603 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010604
10605 crtc = possible_crtc;
10606 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010607 }
10608
10609 /*
10610 * If we didn't find an unused CRTC, don't use any.
10611 */
10612 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010613 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010614 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010615 }
10616
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010617found:
10618 intel_crtc = to_intel_crtc(crtc);
10619
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010620 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10621 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010622 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010623
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010624 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010625 restore_state = drm_atomic_state_alloc(dev);
10626 if (!state || !restore_state) {
10627 ret = -ENOMEM;
10628 goto fail;
10629 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010630
10631 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010632 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010633
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010634 connector_state = drm_atomic_get_connector_state(state, connector);
10635 if (IS_ERR(connector_state)) {
10636 ret = PTR_ERR(connector_state);
10637 goto fail;
10638 }
10639
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010640 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10641 if (ret)
10642 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010643
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010644 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10645 if (IS_ERR(crtc_state)) {
10646 ret = PTR_ERR(crtc_state);
10647 goto fail;
10648 }
10649
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010650 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010651
Chris Wilson64927112011-04-20 07:25:26 +010010652 if (!mode)
10653 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010654
Chris Wilsond2dff872011-04-19 08:36:26 +010010655 /* We need a framebuffer large enough to accommodate all accesses
10656 * that the plane may generate whilst we perform load detection.
10657 * We can not rely on the fbcon either being present (we get called
10658 * during its initialisation to detect all boot displays, or it may
10659 * not even exist) or that it is large enough to satisfy the
10660 * requested mode.
10661 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010662 fb = mode_fits_in_fbdev(dev, mode);
10663 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010664 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010665 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010666 } else
10667 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010668 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010669 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010670 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010671 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010672
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010673 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10674 if (ret)
10675 goto fail;
10676
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010677 drm_framebuffer_unreference(fb);
10678
10679 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10680 if (ret)
10681 goto fail;
10682
10683 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10684 if (!ret)
10685 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10686 if (!ret)
10687 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10688 if (ret) {
10689 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10690 goto fail;
10691 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010692
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010693 ret = drm_atomic_commit(state);
10694 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010695 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010696 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010697 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010698
10699 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010700
Jesse Barnes79e53942008-11-07 14:24:08 -080010701 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010702 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010703 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010704
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010705fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010706 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010707 drm_atomic_state_free(restore_state);
10708 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010709
Rob Clark51fd3712013-11-19 12:10:12 -050010710 if (ret == -EDEADLK) {
10711 drm_modeset_backoff(ctx);
10712 goto retry;
10713 }
10714
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010715 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010716}
10717
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010718void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010719 struct intel_load_detect_pipe *old,
10720 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010721{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010722 struct intel_encoder *intel_encoder =
10723 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010724 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010725 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010726 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010727
Chris Wilsond2dff872011-04-19 08:36:26 +010010728 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010729 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010730 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010731
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010732 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010733 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010734
10735 ret = drm_atomic_commit(state);
10736 if (ret) {
10737 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10738 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010739 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010740}
10741
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010742static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010743 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010744{
10745 struct drm_i915_private *dev_priv = dev->dev_private;
10746 u32 dpll = pipe_config->dpll_hw_state.dpll;
10747
10748 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010749 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010750 else if (HAS_PCH_SPLIT(dev))
10751 return 120000;
10752 else if (!IS_GEN2(dev))
10753 return 96000;
10754 else
10755 return 48000;
10756}
10757
Jesse Barnes79e53942008-11-07 14:24:08 -080010758/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010759static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010760 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010761{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010762 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010763 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010764 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010765 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010766 u32 fp;
10767 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010768 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010769 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010770
10771 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010772 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010773 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010774 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010775
10776 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010777 if (IS_PINEVIEW(dev)) {
10778 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10779 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010780 } else {
10781 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10782 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10783 }
10784
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010785 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010786 if (IS_PINEVIEW(dev))
10787 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10788 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010789 else
10790 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010791 DPLL_FPA01_P1_POST_DIV_SHIFT);
10792
10793 switch (dpll & DPLL_MODE_MASK) {
10794 case DPLLB_MODE_DAC_SERIAL:
10795 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10796 5 : 10;
10797 break;
10798 case DPLLB_MODE_LVDS:
10799 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10800 7 : 14;
10801 break;
10802 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010803 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010804 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010805 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010806 }
10807
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010808 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010809 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010810 else
Imre Deakdccbea32015-06-22 23:35:51 +030010811 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010812 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010813 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010814 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010815
10816 if (is_lvds) {
10817 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10818 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010819
10820 if (lvds & LVDS_CLKB_POWER_UP)
10821 clock.p2 = 7;
10822 else
10823 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010824 } else {
10825 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10826 clock.p1 = 2;
10827 else {
10828 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10829 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10830 }
10831 if (dpll & PLL_P2_DIVIDE_BY_4)
10832 clock.p2 = 4;
10833 else
10834 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010835 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010836
Imre Deakdccbea32015-06-22 23:35:51 +030010837 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010838 }
10839
Ville Syrjälä18442d02013-09-13 16:00:08 +030010840 /*
10841 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010842 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010843 * encoder's get_config() function.
10844 */
Imre Deakdccbea32015-06-22 23:35:51 +030010845 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010846}
10847
Ville Syrjälä6878da02013-09-13 15:59:11 +030010848int intel_dotclock_calculate(int link_freq,
10849 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010850{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010851 /*
10852 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010853 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010854 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010855 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010856 *
10857 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010858 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010859 */
10860
Ville Syrjälä6878da02013-09-13 15:59:11 +030010861 if (!m_n->link_n)
10862 return 0;
10863
10864 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10865}
10866
Ville Syrjälä18442d02013-09-13 16:00:08 +030010867static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010868 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010869{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010870 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010871
10872 /* read out port_clock from the DPLL */
10873 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010874
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010875 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010876 * In case there is an active pipe without active ports,
10877 * we may need some idea for the dotclock anyway.
10878 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010879 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010880 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010881 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010882 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010883}
10884
10885/** Returns the currently programmed mode of the given pipe. */
10886struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10887 struct drm_crtc *crtc)
10888{
Jesse Barnes548f2452011-02-17 10:40:53 -080010889 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010890 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010891 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010892 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010893 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010894 int htot = I915_READ(HTOTAL(cpu_transcoder));
10895 int hsync = I915_READ(HSYNC(cpu_transcoder));
10896 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10897 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010898 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010899
10900 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10901 if (!mode)
10902 return NULL;
10903
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010904 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10905 if (!pipe_config) {
10906 kfree(mode);
10907 return NULL;
10908 }
10909
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010910 /*
10911 * Construct a pipe_config sufficient for getting the clock info
10912 * back out of crtc_clock_get.
10913 *
10914 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10915 * to use a real value here instead.
10916 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010917 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10918 pipe_config->pixel_multiplier = 1;
10919 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10920 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10921 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10922 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010923
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010924 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010925 mode->hdisplay = (htot & 0xffff) + 1;
10926 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10927 mode->hsync_start = (hsync & 0xffff) + 1;
10928 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10929 mode->vdisplay = (vtot & 0xffff) + 1;
10930 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10931 mode->vsync_start = (vsync & 0xffff) + 1;
10932 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10933
10934 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010935
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010936 kfree(pipe_config);
10937
Jesse Barnes79e53942008-11-07 14:24:08 -080010938 return mode;
10939}
10940
Chris Wilsonf047e392012-07-21 12:31:41 +010010941void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010942{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010943 struct drm_i915_private *dev_priv = dev->dev_private;
10944
Chris Wilsonf62a0072014-02-21 17:55:39 +000010945 if (dev_priv->mm.busy)
10946 return;
10947
Paulo Zanoni43694d62014-03-07 20:08:08 -030010948 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010949 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010950 if (INTEL_INFO(dev)->gen >= 6)
10951 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010952 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010953}
10954
10955void intel_mark_idle(struct drm_device *dev)
10956{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010957 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010958
Chris Wilsonf62a0072014-02-21 17:55:39 +000010959 if (!dev_priv->mm.busy)
10960 return;
10961
10962 dev_priv->mm.busy = false;
10963
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010964 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010965 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010966
Paulo Zanoni43694d62014-03-07 20:08:08 -030010967 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010968}
10969
Jesse Barnes79e53942008-11-07 14:24:08 -080010970static void intel_crtc_destroy(struct drm_crtc *crtc)
10971{
10972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010973 struct drm_device *dev = crtc->dev;
10974 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010975
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010976 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010977 work = intel_crtc->unpin_work;
10978 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010979 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010980
10981 if (work) {
10982 cancel_work_sync(&work->work);
10983 kfree(work);
10984 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010985
10986 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010987
Jesse Barnes79e53942008-11-07 14:24:08 -080010988 kfree(intel_crtc);
10989}
10990
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010991static void intel_unpin_work_fn(struct work_struct *__work)
10992{
10993 struct intel_unpin_work *work =
10994 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010995 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10996 struct drm_device *dev = crtc->base.dev;
10997 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010998
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010999 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020011000 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000011001 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000011002
John Harrisonf06cc1b2014-11-24 18:49:37 +000011003 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000011004 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011005 mutex_unlock(&dev->struct_mutex);
11006
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011007 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020011008 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011009 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011010
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011011 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11012 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011013
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011014 kfree(work);
11015}
11016
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011017static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010011018 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011019{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011020 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11021 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011022 unsigned long flags;
11023
11024 /* Ignore early vblank irqs */
11025 if (intel_crtc == NULL)
11026 return;
11027
Daniel Vetterf3260382014-09-15 14:55:23 +020011028 /*
11029 * This is called both by irq handlers and the reset code (to complete
11030 * lost pageflips) so needs the full irqsave spinlocks.
11031 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011032 spin_lock_irqsave(&dev->event_lock, flags);
11033 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000011034
11035 /* Ensure we don't miss a work->pending update ... */
11036 smp_rmb();
11037
11038 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011039 spin_unlock_irqrestore(&dev->event_lock, flags);
11040 return;
11041 }
11042
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011043 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010011044
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011045 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011046}
11047
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011048void intel_finish_page_flip(struct drm_device *dev, int pipe)
11049{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011050 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011051 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11052
Mario Kleiner49b14a52010-12-09 07:00:07 +010011053 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011054}
11055
11056void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
11057{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011058 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011059 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
11060
Mario Kleiner49b14a52010-12-09 07:00:07 +010011061 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070011062}
11063
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011064/* Is 'a' after or equal to 'b'? */
11065static bool g4x_flip_count_after_eq(u32 a, u32 b)
11066{
11067 return !((a - b) & 0x80000000);
11068}
11069
11070static bool page_flip_finished(struct intel_crtc *crtc)
11071{
11072 struct drm_device *dev = crtc->base.dev;
11073 struct drm_i915_private *dev_priv = dev->dev_private;
11074
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030011075 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
11076 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
11077 return true;
11078
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011079 /*
11080 * The relevant registers doen't exist on pre-ctg.
11081 * As the flip done interrupt doesn't trigger for mmio
11082 * flips on gmch platforms, a flip count check isn't
11083 * really needed there. But since ctg has the registers,
11084 * include it in the check anyway.
11085 */
11086 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11087 return true;
11088
11089 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010011090 * BDW signals flip done immediately if the plane
11091 * is disabled, even if the plane enable is already
11092 * armed to occur at the next vblank :(
11093 */
11094
11095 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011096 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11097 * used the same base address. In that case the mmio flip might
11098 * have completed, but the CS hasn't even executed the flip yet.
11099 *
11100 * A flip count check isn't enough as the CS might have updated
11101 * the base address just after start of vblank, but before we
11102 * managed to process the interrupt. This means we'd complete the
11103 * CS flip too soon.
11104 *
11105 * Combining both checks should get us a good enough result. It may
11106 * still happen that the CS flip has been executed, but has not
11107 * yet actually completed. But in case the base address is the same
11108 * anyway, we don't really care.
11109 */
11110 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11111 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030011112 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011113 crtc->unpin_work->flip_count);
11114}
11115
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011116void intel_prepare_page_flip(struct drm_device *dev, int plane)
11117{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011118 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011119 struct intel_crtc *intel_crtc =
11120 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
11121 unsigned long flags;
11122
Daniel Vetterf3260382014-09-15 14:55:23 +020011123
11124 /*
11125 * This is called both by irq handlers and the reset code (to complete
11126 * lost pageflips) so needs the full irqsave spinlocks.
11127 *
11128 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000011129 * generate a page-flip completion irq, i.e. every modeset
11130 * is also accompanied by a spurious intel_prepare_page_flip().
11131 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011132 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011133 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000011134 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011135 spin_unlock_irqrestore(&dev->event_lock, flags);
11136}
11137
Chris Wilson60426392015-10-10 10:44:32 +010011138static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011139{
11140 /* Ensure that the work item is consistent when activating it ... */
11141 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010011142 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011143 /* and that it is marked active as soon as the irq could fire. */
11144 smp_wmb();
11145}
11146
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011147static int intel_gen2_queue_flip(struct drm_device *dev,
11148 struct drm_crtc *crtc,
11149 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011150 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011151 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011152 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011153{
John Harrison6258fbe2015-05-29 17:43:48 +010011154 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011156 u32 flip_mask;
11157 int ret;
11158
John Harrison5fb9de12015-05-29 17:44:07 +010011159 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011160 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011161 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011162
11163 /* Can't queue multiple flips, so wait for the previous
11164 * one to finish before executing the next.
11165 */
11166 if (intel_crtc->plane)
11167 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11168 else
11169 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011170 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11171 intel_ring_emit(ring, MI_NOOP);
11172 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11173 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11174 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011175 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011176 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011177
Chris Wilson60426392015-10-10 10:44:32 +010011178 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011179 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011180}
11181
11182static int intel_gen3_queue_flip(struct drm_device *dev,
11183 struct drm_crtc *crtc,
11184 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011185 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011186 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011187 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011188{
John Harrison6258fbe2015-05-29 17:43:48 +010011189 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011190 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011191 u32 flip_mask;
11192 int ret;
11193
John Harrison5fb9de12015-05-29 17:44:07 +010011194 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011195 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011196 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011197
11198 if (intel_crtc->plane)
11199 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11200 else
11201 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011202 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11203 intel_ring_emit(ring, MI_NOOP);
11204 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11205 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11206 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011207 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011208 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011209
Chris Wilson60426392015-10-10 10:44:32 +010011210 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011211 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011212}
11213
11214static int intel_gen4_queue_flip(struct drm_device *dev,
11215 struct drm_crtc *crtc,
11216 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011217 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011218 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011219 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011220{
John Harrison6258fbe2015-05-29 17:43:48 +010011221 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011222 struct drm_i915_private *dev_priv = dev->dev_private;
11223 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11224 uint32_t pf, pipesrc;
11225 int ret;
11226
John Harrison5fb9de12015-05-29 17:44:07 +010011227 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011228 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011229 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011230
11231 /* i965+ uses the linear or tiled offsets from the
11232 * Display Registers (which do not change across a page-flip)
11233 * so we need only reprogram the base address.
11234 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020011235 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11236 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11237 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011238 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011239 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011240
11241 /* XXX Enabling the panel-fitter across page-flip is so far
11242 * untested on non-native modes, so ignore it for now.
11243 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11244 */
11245 pf = 0;
11246 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011247 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011248
Chris Wilson60426392015-10-10 10:44:32 +010011249 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011250 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011251}
11252
11253static int intel_gen6_queue_flip(struct drm_device *dev,
11254 struct drm_crtc *crtc,
11255 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011256 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011257 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011258 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011259{
John Harrison6258fbe2015-05-29 17:43:48 +010011260 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011261 struct drm_i915_private *dev_priv = dev->dev_private;
11262 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11263 uint32_t pf, pipesrc;
11264 int ret;
11265
John Harrison5fb9de12015-05-29 17:44:07 +010011266 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011267 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011268 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011269
Daniel Vetter6d90c952012-04-26 23:28:05 +020011270 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11271 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11272 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011273 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011274
Chris Wilson99d9acd2012-04-17 20:37:00 +010011275 /* Contrary to the suggestions in the documentation,
11276 * "Enable Panel Fitter" does not seem to be required when page
11277 * flipping with a non-native mode, and worse causes a normal
11278 * modeset to fail.
11279 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11280 */
11281 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011282 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011283 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011284
Chris Wilson60426392015-10-10 10:44:32 +010011285 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011286 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011287}
11288
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011289static int intel_gen7_queue_flip(struct drm_device *dev,
11290 struct drm_crtc *crtc,
11291 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011292 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011293 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011294 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011295{
John Harrison6258fbe2015-05-29 17:43:48 +010011296 struct intel_engine_cs *ring = req->ring;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011298 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011299 int len, ret;
11300
Robin Schroereba905b2014-05-18 02:24:50 +020011301 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011302 case PLANE_A:
11303 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11304 break;
11305 case PLANE_B:
11306 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11307 break;
11308 case PLANE_C:
11309 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11310 break;
11311 default:
11312 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011313 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011314 }
11315
Chris Wilsonffe74d72013-08-26 20:58:12 +010011316 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011317 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011318 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011319 /*
11320 * On Gen 8, SRM is now taking an extra dword to accommodate
11321 * 48bits addresses, and we need a NOOP for the batch size to
11322 * stay even.
11323 */
11324 if (IS_GEN8(dev))
11325 len += 2;
11326 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011327
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011328 /*
11329 * BSpec MI_DISPLAY_FLIP for IVB:
11330 * "The full packet must be contained within the same cache line."
11331 *
11332 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11333 * cacheline, if we ever start emitting more commands before
11334 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11335 * then do the cacheline alignment, and finally emit the
11336 * MI_DISPLAY_FLIP.
11337 */
John Harrisonbba09b12015-05-29 17:44:06 +010011338 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011339 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011340 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011341
John Harrison5fb9de12015-05-29 17:44:07 +010011342 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011343 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011344 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011345
Chris Wilsonffe74d72013-08-26 20:58:12 +010011346 /* Unmask the flip-done completion message. Note that the bspec says that
11347 * we should do this for both the BCS and RCS, and that we must not unmask
11348 * more than one flip event at any time (or ensure that one flip message
11349 * can be sent by waiting for flip-done prior to queueing new flips).
11350 * Experimentation says that BCS works despite DERRMR masking all
11351 * flip-done completion events and that unmasking all planes at once
11352 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11353 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11354 */
11355 if (ring->id == RCS) {
11356 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
Ville Syrjäläf92a9162015-11-04 23:20:07 +020011357 intel_ring_emit_reg(ring, DERRMR);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011358 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11359 DERRMR_PIPEB_PRI_FLIP_DONE |
11360 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011361 if (IS_GEN8(dev))
Arun Siluveryf1afe242015-08-04 16:22:20 +010011362 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011363 MI_SRM_LRM_GLOBAL_GTT);
11364 else
Arun Siluveryf1afe242015-08-04 16:22:20 +010011365 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011366 MI_SRM_LRM_GLOBAL_GTT);
Ville Syrjäläf92a9162015-11-04 23:20:07 +020011367 intel_ring_emit_reg(ring, DERRMR);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011368 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011369 if (IS_GEN8(dev)) {
11370 intel_ring_emit(ring, 0);
11371 intel_ring_emit(ring, MI_NOOP);
11372 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011373 }
11374
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011375 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011376 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011377 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011378 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011379
Chris Wilson60426392015-10-10 10:44:32 +010011380 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011381 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011382}
11383
Sourab Gupta84c33a62014-06-02 16:47:17 +053011384static bool use_mmio_flip(struct intel_engine_cs *ring,
11385 struct drm_i915_gem_object *obj)
11386{
11387 /*
11388 * This is not being used for older platforms, because
11389 * non-availability of flip done interrupt forces us to use
11390 * CS flips. Older platforms derive flip done using some clever
11391 * tricks involving the flip_pending status bits and vblank irqs.
11392 * So using MMIO flips there would disrupt this mechanism.
11393 */
11394
Chris Wilson8e09bf82014-07-08 10:40:30 +010011395 if (ring == NULL)
11396 return true;
11397
Sourab Gupta84c33a62014-06-02 16:47:17 +053011398 if (INTEL_INFO(ring->dev)->gen < 5)
11399 return false;
11400
11401 if (i915.use_mmio_flip < 0)
11402 return false;
11403 else if (i915.use_mmio_flip > 0)
11404 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011405 else if (i915.enable_execlists)
11406 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011407 else if (obj->base.dma_buf &&
11408 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11409 false))
11410 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011411 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011412 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011413}
11414
Chris Wilson60426392015-10-10 10:44:32 +010011415static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011416 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011417 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011418{
11419 struct drm_device *dev = intel_crtc->base.dev;
11420 struct drm_i915_private *dev_priv = dev->dev_private;
11421 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011422 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011423 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011424
11425 ctl = I915_READ(PLANE_CTL(pipe, 0));
11426 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011427 switch (fb->modifier[0]) {
11428 case DRM_FORMAT_MOD_NONE:
11429 break;
11430 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011431 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011432 break;
11433 case I915_FORMAT_MOD_Y_TILED:
11434 ctl |= PLANE_CTL_TILED_Y;
11435 break;
11436 case I915_FORMAT_MOD_Yf_TILED:
11437 ctl |= PLANE_CTL_TILED_YF;
11438 break;
11439 default:
11440 MISSING_CASE(fb->modifier[0]);
11441 }
Damien Lespiauff944562014-11-20 14:58:16 +000011442
11443 /*
11444 * The stride is either expressed as a multiple of 64 bytes chunks for
11445 * linear buffers or in number of tiles for tiled buffers.
11446 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011447 if (intel_rotation_90_or_270(rotation)) {
11448 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011449 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011450 stride = DIV_ROUND_UP(fb->height, tile_height);
11451 } else {
11452 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011453 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11454 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011455 }
Damien Lespiauff944562014-11-20 14:58:16 +000011456
11457 /*
11458 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11459 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11460 */
11461 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11462 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11463
Chris Wilson60426392015-10-10 10:44:32 +010011464 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011465 POSTING_READ(PLANE_SURF(pipe, 0));
11466}
11467
Chris Wilson60426392015-10-10 10:44:32 +010011468static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11469 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011470{
11471 struct drm_device *dev = intel_crtc->base.dev;
11472 struct drm_i915_private *dev_priv = dev->dev_private;
11473 struct intel_framebuffer *intel_fb =
11474 to_intel_framebuffer(intel_crtc->base.primary->fb);
11475 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011476 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011477 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011478
Sourab Gupta84c33a62014-06-02 16:47:17 +053011479 dspcntr = I915_READ(reg);
11480
Damien Lespiauc5d97472014-10-25 00:11:11 +010011481 if (obj->tiling_mode != I915_TILING_NONE)
11482 dspcntr |= DISPPLANE_TILED;
11483 else
11484 dspcntr &= ~DISPPLANE_TILED;
11485
Sourab Gupta84c33a62014-06-02 16:47:17 +053011486 I915_WRITE(reg, dspcntr);
11487
Chris Wilson60426392015-10-10 10:44:32 +010011488 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011489 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011490}
11491
11492/*
11493 * XXX: This is the temporary way to update the plane registers until we get
11494 * around to using the usual plane update functions for MMIO flips
11495 */
Chris Wilson60426392015-10-10 10:44:32 +010011496static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011497{
Chris Wilson60426392015-10-10 10:44:32 +010011498 struct intel_crtc *crtc = mmio_flip->crtc;
11499 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011500
Chris Wilson60426392015-10-10 10:44:32 +010011501 spin_lock_irq(&crtc->base.dev->event_lock);
11502 work = crtc->unpin_work;
11503 spin_unlock_irq(&crtc->base.dev->event_lock);
11504 if (work == NULL)
11505 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011506
Chris Wilson60426392015-10-10 10:44:32 +010011507 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011508
Chris Wilson60426392015-10-10 10:44:32 +010011509 intel_pipe_update_start(crtc);
11510
11511 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011512 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011513 else
11514 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011515 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011516
Chris Wilson60426392015-10-10 10:44:32 +010011517 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011518}
11519
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011520static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011521{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011522 struct intel_mmio_flip *mmio_flip =
11523 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011524 struct intel_framebuffer *intel_fb =
11525 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11526 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011527
Chris Wilson60426392015-10-10 10:44:32 +010011528 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011529 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011530 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011531 false, NULL,
11532 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011533 i915_gem_request_unreference__unlocked(mmio_flip->req);
11534 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011535
Alex Goinsfd8e0582015-11-25 18:43:38 -080011536 /* For framebuffer backed by dmabuf, wait for fence */
11537 if (obj->base.dma_buf)
11538 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11539 false, false,
11540 MAX_SCHEDULE_TIMEOUT) < 0);
11541
Chris Wilson60426392015-10-10 10:44:32 +010011542 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011543 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011544}
11545
11546static int intel_queue_mmio_flip(struct drm_device *dev,
11547 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011548 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011549{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011550 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011551
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011552 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11553 if (mmio_flip == NULL)
11554 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011555
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011556 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011557 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011558 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011559 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011560
11561 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11562 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011563
Sourab Gupta84c33a62014-06-02 16:47:17 +053011564 return 0;
11565}
11566
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011567static int intel_default_queue_flip(struct drm_device *dev,
11568 struct drm_crtc *crtc,
11569 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011570 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011571 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011572 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011573{
11574 return -ENODEV;
11575}
11576
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011577static bool __intel_pageflip_stall_check(struct drm_device *dev,
11578 struct drm_crtc *crtc)
11579{
11580 struct drm_i915_private *dev_priv = dev->dev_private;
11581 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11582 struct intel_unpin_work *work = intel_crtc->unpin_work;
11583 u32 addr;
11584
11585 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11586 return true;
11587
Chris Wilson908565c2015-08-12 13:08:22 +010011588 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11589 return false;
11590
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011591 if (!work->enable_stall_check)
11592 return false;
11593
11594 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011595 if (work->flip_queued_req &&
11596 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011597 return false;
11598
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011599 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011600 }
11601
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011602 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011603 return false;
11604
11605 /* Potential stall - if we see that the flip has happened,
11606 * assume a missed interrupt. */
11607 if (INTEL_INFO(dev)->gen >= 4)
11608 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11609 else
11610 addr = I915_READ(DSPADDR(intel_crtc->plane));
11611
11612 /* There is a potential issue here with a false positive after a flip
11613 * to the same address. We could address this by checking for a
11614 * non-incrementing frame counter.
11615 */
11616 return addr == work->gtt_offset;
11617}
11618
11619void intel_check_page_flip(struct drm_device *dev, int pipe)
11620{
11621 struct drm_i915_private *dev_priv = dev->dev_private;
11622 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011624 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011625
Dave Gordon6c51d462015-03-06 15:34:26 +000011626 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011627
11628 if (crtc == NULL)
11629 return;
11630
Daniel Vetterf3260382014-09-15 14:55:23 +020011631 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011632 work = intel_crtc->unpin_work;
11633 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011634 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011635 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011636 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011637 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011638 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011639 if (work != NULL &&
11640 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11641 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011642 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011643}
11644
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011645static int intel_crtc_page_flip(struct drm_crtc *crtc,
11646 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011647 struct drm_pending_vblank_event *event,
11648 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011649{
11650 struct drm_device *dev = crtc->dev;
11651 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011652 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011653 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011655 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011656 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011657 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011658 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011659 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011660 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011661 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011662
Matt Roper2ff8fde2014-07-08 07:50:07 -070011663 /*
11664 * drm_mode_page_flip_ioctl() should already catch this, but double
11665 * check to be safe. In the future we may enable pageflipping from
11666 * a disabled primary plane.
11667 */
11668 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11669 return -EBUSY;
11670
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011671 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011672 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011673 return -EINVAL;
11674
11675 /*
11676 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11677 * Note that pitch changes could also affect these register.
11678 */
11679 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011680 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11681 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011682 return -EINVAL;
11683
Chris Wilsonf900db42014-02-20 09:26:13 +000011684 if (i915_terminally_wedged(&dev_priv->gpu_error))
11685 goto out_hang;
11686
Daniel Vetterb14c5672013-09-19 12:18:32 +020011687 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011688 if (work == NULL)
11689 return -ENOMEM;
11690
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011691 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011692 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011693 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011694 INIT_WORK(&work->work, intel_unpin_work_fn);
11695
Daniel Vetter87b6b102014-05-15 15:33:46 +020011696 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011697 if (ret)
11698 goto free_work;
11699
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011700 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011701 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011702 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011703 /* Before declaring the flip queue wedged, check if
11704 * the hardware completed the operation behind our backs.
11705 */
11706 if (__intel_pageflip_stall_check(dev, crtc)) {
11707 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11708 page_flip_completed(intel_crtc);
11709 } else {
11710 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011711 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011712
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011713 drm_crtc_vblank_put(crtc);
11714 kfree(work);
11715 return -EBUSY;
11716 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011717 }
11718 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011719 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011720
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011721 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11722 flush_workqueue(dev_priv->wq);
11723
Jesse Barnes75dfca82010-02-10 15:09:44 -080011724 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011725 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011726 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011727
Matt Roperf4510a22014-04-01 15:22:40 -070011728 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011729 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011730 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011731
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011732 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011733
Chris Wilson89ed88b2015-02-16 14:31:49 +000011734 ret = i915_mutex_lock_interruptible(dev);
11735 if (ret)
11736 goto cleanup;
11737
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011738 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011739 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011740
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011741 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030011742 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011743
Wayne Boyer666a4532015-12-09 12:29:35 -080011744 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011745 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011746 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011747 /* vlv: DISPLAY_FLIP fails to change tiling */
11748 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011749 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011750 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011751 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011752 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011753 if (ring == NULL || ring->id != RCS)
11754 ring = &dev_priv->ring[BCS];
11755 } else {
11756 ring = &dev_priv->ring[RCS];
11757 }
11758
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011759 mmio_flip = use_mmio_flip(ring, obj);
11760
11761 /* When using CS flips, we want to emit semaphores between rings.
11762 * However, when using mmio flips we will create a task to do the
11763 * synchronisation, so all we want here is to pin the framebuffer
11764 * into the display plane and skip any waits.
11765 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011766 if (!mmio_flip) {
11767 ret = i915_gem_object_sync(obj, ring, &request);
11768 if (ret)
11769 goto cleanup_pending;
11770 }
11771
Ville Syrjälä3465c582016-02-15 22:54:43 +020011772 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011773 if (ret)
11774 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011775
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011776 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11777 obj, 0);
11778 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011779
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011780 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011781 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011782 if (ret)
11783 goto cleanup_unpin;
11784
John Harrisonf06cc1b2014-11-24 18:49:37 +000011785 i915_gem_request_assign(&work->flip_queued_req,
11786 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011787 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011788 if (!request) {
Dave Gordon26827082016-01-19 19:02:53 +000011789 request = i915_gem_request_alloc(ring, NULL);
11790 if (IS_ERR(request)) {
11791 ret = PTR_ERR(request);
John Harrison6258fbe2015-05-29 17:43:48 +010011792 goto cleanup_unpin;
Dave Gordon26827082016-01-19 19:02:53 +000011793 }
John Harrison6258fbe2015-05-29 17:43:48 +010011794 }
11795
11796 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011797 page_flip_flags);
11798 if (ret)
11799 goto cleanup_unpin;
11800
John Harrison6258fbe2015-05-29 17:43:48 +010011801 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011802 }
11803
John Harrison91af1272015-06-18 13:14:56 +010011804 if (request)
John Harrison75289872015-05-29 17:43:49 +010011805 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011806
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011807 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011808 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011809
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011810 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011811 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011812 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011813
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011814 intel_frontbuffer_flip_prepare(dev,
11815 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011816
Jesse Barnese5510fa2010-07-01 16:48:37 -070011817 trace_i915_flip_request(intel_crtc->plane, obj);
11818
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011819 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011820
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011821cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011822 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011823cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011824 if (!IS_ERR_OR_NULL(request))
John Harrison91af1272015-06-18 13:14:56 +010011825 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011826 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011827 mutex_unlock(&dev->struct_mutex);
11828cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011829 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011830 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011831
Chris Wilson89ed88b2015-02-16 14:31:49 +000011832 drm_gem_object_unreference_unlocked(&obj->base);
11833 drm_framebuffer_unreference(work->old_fb);
11834
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011835 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011836 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011837 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011838
Daniel Vetter87b6b102014-05-15 15:33:46 +020011839 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011840free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011841 kfree(work);
11842
Chris Wilsonf900db42014-02-20 09:26:13 +000011843 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011844 struct drm_atomic_state *state;
11845 struct drm_plane_state *plane_state;
11846
Chris Wilsonf900db42014-02-20 09:26:13 +000011847out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011848 state = drm_atomic_state_alloc(dev);
11849 if (!state)
11850 return -ENOMEM;
11851 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11852
11853retry:
11854 plane_state = drm_atomic_get_plane_state(state, primary);
11855 ret = PTR_ERR_OR_ZERO(plane_state);
11856 if (!ret) {
11857 drm_atomic_set_fb_for_plane(plane_state, fb);
11858
11859 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11860 if (!ret)
11861 ret = drm_atomic_commit(state);
11862 }
11863
11864 if (ret == -EDEADLK) {
11865 drm_modeset_backoff(state->acquire_ctx);
11866 drm_atomic_state_clear(state);
11867 goto retry;
11868 }
11869
11870 if (ret)
11871 drm_atomic_state_free(state);
11872
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011873 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011874 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011875 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011876 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011877 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011878 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011879 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011880}
11881
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011882
11883/**
11884 * intel_wm_need_update - Check whether watermarks need updating
11885 * @plane: drm plane
11886 * @state: new plane state
11887 *
11888 * Check current plane state versus the new one to determine whether
11889 * watermarks need to be recalculated.
11890 *
11891 * Returns true or false.
11892 */
11893static bool intel_wm_need_update(struct drm_plane *plane,
11894 struct drm_plane_state *state)
11895{
Matt Roperd21fbe82015-09-24 15:53:12 -070011896 struct intel_plane_state *new = to_intel_plane_state(state);
11897 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11898
11899 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011900 if (new->visible != cur->visible)
11901 return true;
11902
11903 if (!cur->base.fb || !new->base.fb)
11904 return false;
11905
11906 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11907 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011908 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11909 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11910 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11911 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011912 return true;
11913
11914 return false;
11915}
11916
Matt Roperd21fbe82015-09-24 15:53:12 -070011917static bool needs_scaling(struct intel_plane_state *state)
11918{
11919 int src_w = drm_rect_width(&state->src) >> 16;
11920 int src_h = drm_rect_height(&state->src) >> 16;
11921 int dst_w = drm_rect_width(&state->dst);
11922 int dst_h = drm_rect_height(&state->dst);
11923
11924 return (src_w != dst_w || src_h != dst_h);
11925}
11926
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011927int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11928 struct drm_plane_state *plane_state)
11929{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011930 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011931 struct drm_crtc *crtc = crtc_state->crtc;
11932 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11933 struct drm_plane *plane = plane_state->plane;
11934 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011935 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011936 struct intel_plane_state *old_plane_state =
11937 to_intel_plane_state(plane->state);
11938 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011939 bool mode_changed = needs_modeset(crtc_state);
11940 bool was_crtc_enabled = crtc->state->active;
11941 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011942 bool turn_off, turn_on, visible, was_visible;
11943 struct drm_framebuffer *fb = plane_state->fb;
11944
11945 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11946 plane->type != DRM_PLANE_TYPE_CURSOR) {
11947 ret = skl_update_scaler_plane(
11948 to_intel_crtc_state(crtc_state),
11949 to_intel_plane_state(plane_state));
11950 if (ret)
11951 return ret;
11952 }
11953
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011954 was_visible = old_plane_state->visible;
11955 visible = to_intel_plane_state(plane_state)->visible;
11956
11957 if (!was_crtc_enabled && WARN_ON(was_visible))
11958 was_visible = false;
11959
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011960 /*
11961 * Visibility is calculated as if the crtc was on, but
11962 * after scaler setup everything depends on it being off
11963 * when the crtc isn't active.
11964 */
11965 if (!is_crtc_enabled)
11966 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011967
11968 if (!was_visible && !visible)
11969 return 0;
11970
Maarten Lankhorste8861672016-02-24 11:24:26 +010011971 if (fb != old_plane_state->base.fb)
11972 pipe_config->fb_changed = true;
11973
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011974 turn_off = was_visible && (!visible || mode_changed);
11975 turn_on = visible && (!was_visible || mode_changed);
11976
11977 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11978 plane->base.id, fb ? fb->base.id : -1);
11979
11980 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11981 plane->base.id, was_visible, visible,
11982 turn_off, turn_on, mode_changed);
11983
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011984 if (turn_on || turn_off) {
11985 pipe_config->wm_changed = true;
11986
Ville Syrjälä852eb002015-06-24 22:00:07 +030011987 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011988 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011989 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011990 } else if (intel_wm_need_update(plane, plane_state)) {
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011991 pipe_config->wm_changed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011992 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011993
Matt Ropered4a6a72016-02-23 17:20:13 -080011994 /* Pre-gen9 platforms need two-step watermark updates */
11995 if (pipe_config->wm_changed && INTEL_INFO(dev)->gen < 9 &&
11996 dev_priv->display.optimize_watermarks)
11997 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11998
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011999 if (visible || was_visible)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030012000 intel_crtc->atomic.fb_bits |=
12001 to_intel_plane(plane)->frontbuffer_bit;
12002
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012003 switch (plane->type) {
12004 case DRM_PLANE_TYPE_PRIMARY:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012005 intel_crtc->atomic.post_enable_primary = turn_on;
Paulo Zanonifcf38d12016-01-21 18:07:17 -020012006 intel_crtc->atomic.update_fbc = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012007
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012008 break;
12009 case DRM_PLANE_TYPE_CURSOR:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012010 break;
12011 case DRM_PLANE_TYPE_OVERLAY:
Matt Roperd21fbe82015-09-24 15:53:12 -070012012 /*
12013 * WaCxSRDisabledForSpriteScaling:ivb
12014 *
12015 * cstate->update_wm was already set above, so this flag will
12016 * take effect when we commit and program watermarks.
12017 */
12018 if (IS_IVYBRIDGE(dev) &&
12019 needs_scaling(to_intel_plane_state(plane_state)) &&
Maarten Lankhorste8861672016-02-24 11:24:26 +010012020 !needs_scaling(old_plane_state))
12021 pipe_config->disable_lp_wm = true;
Matt Roperd21fbe82015-09-24 15:53:12 -070012022
12023 break;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012024 }
12025 return 0;
12026}
12027
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012028static bool encoders_cloneable(const struct intel_encoder *a,
12029 const struct intel_encoder *b)
12030{
12031 /* masks could be asymmetric, so check both ways */
12032 return a == b || (a->cloneable & (1 << b->type) &&
12033 b->cloneable & (1 << a->type));
12034}
12035
12036static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12037 struct intel_crtc *crtc,
12038 struct intel_encoder *encoder)
12039{
12040 struct intel_encoder *source_encoder;
12041 struct drm_connector *connector;
12042 struct drm_connector_state *connector_state;
12043 int i;
12044
12045 for_each_connector_in_state(state, connector, connector_state, i) {
12046 if (connector_state->crtc != &crtc->base)
12047 continue;
12048
12049 source_encoder =
12050 to_intel_encoder(connector_state->best_encoder);
12051 if (!encoders_cloneable(encoder, source_encoder))
12052 return false;
12053 }
12054
12055 return true;
12056}
12057
12058static bool check_encoder_cloning(struct drm_atomic_state *state,
12059 struct intel_crtc *crtc)
12060{
12061 struct intel_encoder *encoder;
12062 struct drm_connector *connector;
12063 struct drm_connector_state *connector_state;
12064 int i;
12065
12066 for_each_connector_in_state(state, connector, connector_state, i) {
12067 if (connector_state->crtc != &crtc->base)
12068 continue;
12069
12070 encoder = to_intel_encoder(connector_state->best_encoder);
12071 if (!check_single_encoder_cloning(state, crtc, encoder))
12072 return false;
12073 }
12074
12075 return true;
12076}
12077
12078static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12079 struct drm_crtc_state *crtc_state)
12080{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012081 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012082 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012083 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012084 struct intel_crtc_state *pipe_config =
12085 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012086 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012087 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012088 bool mode_changed = needs_modeset(crtc_state);
12089
12090 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12091 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12092 return -EINVAL;
12093 }
12094
Ville Syrjälä852eb002015-06-24 22:00:07 +030012095 if (mode_changed && !crtc_state->active)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012096 pipe_config->wm_changed = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012097
Maarten Lankhorstad421372015-06-15 12:33:42 +020012098 if (mode_changed && crtc_state->enable &&
12099 dev_priv->display.crtc_compute_clock &&
12100 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
12101 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12102 pipe_config);
12103 if (ret)
12104 return ret;
12105 }
12106
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012107 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012108 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012109 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012110 if (ret) {
12111 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012112 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012113 }
12114 }
12115
12116 if (dev_priv->display.compute_intermediate_wm &&
12117 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12118 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12119 return 0;
12120
12121 /*
12122 * Calculate 'intermediate' watermarks that satisfy both the
12123 * old state and the new state. We can program these
12124 * immediately.
12125 */
12126 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12127 intel_crtc,
12128 pipe_config);
12129 if (ret) {
12130 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12131 return ret;
12132 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070012133 }
12134
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012135 if (INTEL_INFO(dev)->gen >= 9) {
12136 if (mode_changed)
12137 ret = skl_update_scaler_crtc(pipe_config);
12138
12139 if (!ret)
12140 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12141 pipe_config);
12142 }
12143
12144 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012145}
12146
Jani Nikula65b38e02015-04-13 11:26:56 +030012147static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012148 .mode_set_base_atomic = intel_pipe_set_base_atomic,
12149 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080012150 .atomic_begin = intel_begin_crtc_commit,
12151 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012152 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012153};
12154
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012155static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12156{
12157 struct intel_connector *connector;
12158
12159 for_each_intel_connector(dev, connector) {
12160 if (connector->base.encoder) {
12161 connector->base.state->best_encoder =
12162 connector->base.encoder;
12163 connector->base.state->crtc =
12164 connector->base.encoder->crtc;
12165 } else {
12166 connector->base.state->best_encoder = NULL;
12167 connector->base.state->crtc = NULL;
12168 }
12169 }
12170}
12171
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012172static void
Robin Schroereba905b2014-05-18 02:24:50 +020012173connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012174 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012175{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012176 int bpp = pipe_config->pipe_bpp;
12177
12178 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12179 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012180 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012181
12182 /* Don't use an invalid EDID bpc value */
12183 if (connector->base.display_info.bpc &&
12184 connector->base.display_info.bpc * 3 < bpp) {
12185 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12186 bpp, connector->base.display_info.bpc*3);
12187 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12188 }
12189
Jani Nikula013dd9e2016-01-13 16:35:20 +020012190 /* Clamp bpp to default limit on screens without EDID 1.4 */
12191 if (connector->base.display_info.bpc == 0) {
12192 int type = connector->base.connector_type;
12193 int clamp_bpp = 24;
12194
12195 /* Fall back to 18 bpp when DP sink capability is unknown. */
12196 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12197 type == DRM_MODE_CONNECTOR_eDP)
12198 clamp_bpp = 18;
12199
12200 if (bpp > clamp_bpp) {
12201 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12202 bpp, clamp_bpp);
12203 pipe_config->pipe_bpp = clamp_bpp;
12204 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012205 }
12206}
12207
12208static int
12209compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012210 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012211{
12212 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012213 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012214 struct drm_connector *connector;
12215 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012216 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012217
Wayne Boyer666a4532015-12-09 12:29:35 -080012218 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012219 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012220 else if (INTEL_INFO(dev)->gen >= 5)
12221 bpp = 12*3;
12222 else
12223 bpp = 8*3;
12224
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012225
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012226 pipe_config->pipe_bpp = bpp;
12227
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012228 state = pipe_config->base.state;
12229
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012230 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012231 for_each_connector_in_state(state, connector, connector_state, i) {
12232 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012233 continue;
12234
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012235 connected_sink_compute_bpp(to_intel_connector(connector),
12236 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012237 }
12238
12239 return bpp;
12240}
12241
Daniel Vetter644db712013-09-19 14:53:58 +020012242static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12243{
12244 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12245 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012246 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012247 mode->crtc_hdisplay, mode->crtc_hsync_start,
12248 mode->crtc_hsync_end, mode->crtc_htotal,
12249 mode->crtc_vdisplay, mode->crtc_vsync_start,
12250 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12251}
12252
Daniel Vetterc0b03412013-05-28 12:05:54 +020012253static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012254 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012255 const char *context)
12256{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012257 struct drm_device *dev = crtc->base.dev;
12258 struct drm_plane *plane;
12259 struct intel_plane *intel_plane;
12260 struct intel_plane_state *state;
12261 struct drm_framebuffer *fb;
12262
12263 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12264 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012265
12266 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
12267 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12268 pipe_config->pipe_bpp, pipe_config->dither);
12269 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12270 pipe_config->has_pch_encoder,
12271 pipe_config->fdi_lanes,
12272 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12273 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12274 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012275 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 +030012276 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012277 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012278 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12279 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12280 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012281
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012282 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 -070012283 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012284 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012285 pipe_config->dp_m2_n2.gmch_m,
12286 pipe_config->dp_m2_n2.gmch_n,
12287 pipe_config->dp_m2_n2.link_m,
12288 pipe_config->dp_m2_n2.link_n,
12289 pipe_config->dp_m2_n2.tu);
12290
Daniel Vetter55072d12014-11-20 16:10:28 +010012291 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12292 pipe_config->has_audio,
12293 pipe_config->has_infoframe);
12294
Daniel Vetterc0b03412013-05-28 12:05:54 +020012295 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012296 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012297 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012298 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12299 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012300 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012301 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12302 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012303 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12304 crtc->num_scalers,
12305 pipe_config->scaler_state.scaler_users,
12306 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012307 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12308 pipe_config->gmch_pfit.control,
12309 pipe_config->gmch_pfit.pgm_ratios,
12310 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012311 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012312 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012313 pipe_config->pch_pfit.size,
12314 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012315 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012316 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012317
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012318 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012319 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012320 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012321 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012322 pipe_config->ddi_pll_sel,
12323 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012324 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012325 pipe_config->dpll_hw_state.pll0,
12326 pipe_config->dpll_hw_state.pll1,
12327 pipe_config->dpll_hw_state.pll2,
12328 pipe_config->dpll_hw_state.pll3,
12329 pipe_config->dpll_hw_state.pll6,
12330 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012331 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012332 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012333 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012334 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012335 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12336 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12337 pipe_config->ddi_pll_sel,
12338 pipe_config->dpll_hw_state.ctrl1,
12339 pipe_config->dpll_hw_state.cfgcr1,
12340 pipe_config->dpll_hw_state.cfgcr2);
12341 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012342 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012343 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012344 pipe_config->dpll_hw_state.wrpll,
12345 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012346 } else {
12347 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12348 "fp0: 0x%x, fp1: 0x%x\n",
12349 pipe_config->dpll_hw_state.dpll,
12350 pipe_config->dpll_hw_state.dpll_md,
12351 pipe_config->dpll_hw_state.fp0,
12352 pipe_config->dpll_hw_state.fp1);
12353 }
12354
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012355 DRM_DEBUG_KMS("planes on this crtc\n");
12356 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12357 intel_plane = to_intel_plane(plane);
12358 if (intel_plane->pipe != crtc->pipe)
12359 continue;
12360
12361 state = to_intel_plane_state(plane->state);
12362 fb = state->base.fb;
12363 if (!fb) {
12364 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12365 "disabled, scaler_id = %d\n",
12366 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12367 plane->base.id, intel_plane->pipe,
12368 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12369 drm_plane_index(plane), state->scaler_id);
12370 continue;
12371 }
12372
12373 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12374 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12375 plane->base.id, intel_plane->pipe,
12376 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12377 drm_plane_index(plane));
12378 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12379 fb->base.id, fb->width, fb->height, fb->pixel_format);
12380 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12381 state->scaler_id,
12382 state->src.x1 >> 16, state->src.y1 >> 16,
12383 drm_rect_width(&state->src) >> 16,
12384 drm_rect_height(&state->src) >> 16,
12385 state->dst.x1, state->dst.y1,
12386 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12387 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012388}
12389
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012390static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012391{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012392 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012393 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012394 unsigned int used_ports = 0;
12395
12396 /*
12397 * Walk the connector list instead of the encoder
12398 * list to detect the problem on ddi platforms
12399 * where there's just one encoder per digital port.
12400 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012401 drm_for_each_connector(connector, dev) {
12402 struct drm_connector_state *connector_state;
12403 struct intel_encoder *encoder;
12404
12405 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12406 if (!connector_state)
12407 connector_state = connector->state;
12408
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012409 if (!connector_state->best_encoder)
12410 continue;
12411
12412 encoder = to_intel_encoder(connector_state->best_encoder);
12413
12414 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012415
12416 switch (encoder->type) {
12417 unsigned int port_mask;
12418 case INTEL_OUTPUT_UNKNOWN:
12419 if (WARN_ON(!HAS_DDI(dev)))
12420 break;
12421 case INTEL_OUTPUT_DISPLAYPORT:
12422 case INTEL_OUTPUT_HDMI:
12423 case INTEL_OUTPUT_EDP:
12424 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12425
12426 /* the same port mustn't appear more than once */
12427 if (used_ports & port_mask)
12428 return false;
12429
12430 used_ports |= port_mask;
12431 default:
12432 break;
12433 }
12434 }
12435
12436 return true;
12437}
12438
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012439static void
12440clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12441{
12442 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012443 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012444 struct intel_dpll_hw_state dpll_hw_state;
12445 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012446 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012447 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012448
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012449 /* FIXME: before the switch to atomic started, a new pipe_config was
12450 * kzalloc'd. Code that depends on any field being zero should be
12451 * fixed, so that the crtc_state can be safely duplicated. For now,
12452 * only fields that are know to not cause problems are preserved. */
12453
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012454 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012455 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012456 shared_dpll = crtc_state->shared_dpll;
12457 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012458 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012459 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012460
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012461 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012462
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012463 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012464 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012465 crtc_state->shared_dpll = shared_dpll;
12466 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012467 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012468 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012469}
12470
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012471static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012472intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012473 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012474{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012475 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012476 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012477 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012478 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012479 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012480 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012481 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012482
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012483 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012484
Daniel Vettere143a212013-07-04 12:01:15 +020012485 pipe_config->cpu_transcoder =
12486 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012487
Imre Deak2960bc92013-07-30 13:36:32 +030012488 /*
12489 * Sanitize sync polarity flags based on requested ones. If neither
12490 * positive or negative polarity is requested, treat this as meaning
12491 * negative polarity.
12492 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012493 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012494 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012495 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012496
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012497 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012498 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012499 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012500
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012501 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12502 pipe_config);
12503 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012504 goto fail;
12505
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012506 /*
12507 * Determine the real pipe dimensions. Note that stereo modes can
12508 * increase the actual pipe size due to the frame doubling and
12509 * insertion of additional space for blanks between the frame. This
12510 * is stored in the crtc timings. We use the requested mode to do this
12511 * computation to clearly distinguish it from the adjusted mode, which
12512 * can be changed by the connectors in the below retry loop.
12513 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012514 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012515 &pipe_config->pipe_src_w,
12516 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012517
Daniel Vettere29c22c2013-02-21 00:00:16 +010012518encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012519 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012520 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012521 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012522
Daniel Vetter135c81b2013-07-21 21:37:09 +020012523 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012524 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12525 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012526
Daniel Vetter7758a112012-07-08 19:40:39 +020012527 /* Pass our mode to the connectors and the CRTC to give them a chance to
12528 * adjust it according to limitations or connector properties, and also
12529 * a chance to reject the mode entirely.
12530 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012531 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012532 if (connector_state->crtc != crtc)
12533 continue;
12534
12535 encoder = to_intel_encoder(connector_state->best_encoder);
12536
Daniel Vetterefea6e82013-07-21 21:36:59 +020012537 if (!(encoder->compute_config(encoder, pipe_config))) {
12538 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012539 goto fail;
12540 }
12541 }
12542
Daniel Vetterff9a6752013-06-01 17:16:21 +020012543 /* Set default port clock if not overwritten by the encoder. Needs to be
12544 * done afterwards in case the encoder adjusts the mode. */
12545 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012546 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012547 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012548
Daniel Vettera43f6e02013-06-07 23:10:32 +020012549 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012550 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012551 DRM_DEBUG_KMS("CRTC fixup failed\n");
12552 goto fail;
12553 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012554
12555 if (ret == RETRY) {
12556 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12557 ret = -EINVAL;
12558 goto fail;
12559 }
12560
12561 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12562 retry = false;
12563 goto encoder_retry;
12564 }
12565
Daniel Vettere8fa4272015-08-12 11:43:34 +020012566 /* Dithering seems to not pass-through bits correctly when it should, so
12567 * only enable it on 6bpc panels. */
12568 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012569 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012570 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012571
Daniel Vetter7758a112012-07-08 19:40:39 +020012572fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012573 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012574}
12575
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012576static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012577intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012578{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012579 struct drm_crtc *crtc;
12580 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012581 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012582
Ville Syrjälä76688512014-01-10 11:28:06 +020012583 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012584 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012585 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012586
12587 /* Update hwmode for vblank functions */
12588 if (crtc->state->active)
12589 crtc->hwmode = crtc->state->adjusted_mode;
12590 else
12591 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012592
12593 /*
12594 * Update legacy state to satisfy fbc code. This can
12595 * be removed when fbc uses the atomic state.
12596 */
12597 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12598 struct drm_plane_state *plane_state = crtc->primary->state;
12599
12600 crtc->primary->fb = plane_state->fb;
12601 crtc->x = plane_state->src_x >> 16;
12602 crtc->y = plane_state->src_y >> 16;
12603 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012604 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012605}
12606
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012607static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012608{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012609 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012610
12611 if (clock1 == clock2)
12612 return true;
12613
12614 if (!clock1 || !clock2)
12615 return false;
12616
12617 diff = abs(clock1 - clock2);
12618
12619 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12620 return true;
12621
12622 return false;
12623}
12624
Daniel Vetter25c5b262012-07-08 22:08:04 +020012625#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12626 list_for_each_entry((intel_crtc), \
12627 &(dev)->mode_config.crtc_list, \
12628 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012629 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012630
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012631static bool
12632intel_compare_m_n(unsigned int m, unsigned int n,
12633 unsigned int m2, unsigned int n2,
12634 bool exact)
12635{
12636 if (m == m2 && n == n2)
12637 return true;
12638
12639 if (exact || !m || !n || !m2 || !n2)
12640 return false;
12641
12642 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12643
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012644 if (n > n2) {
12645 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012646 m2 <<= 1;
12647 n2 <<= 1;
12648 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012649 } else if (n < n2) {
12650 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012651 m <<= 1;
12652 n <<= 1;
12653 }
12654 }
12655
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012656 if (n != n2)
12657 return false;
12658
12659 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012660}
12661
12662static bool
12663intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12664 struct intel_link_m_n *m2_n2,
12665 bool adjust)
12666{
12667 if (m_n->tu == m2_n2->tu &&
12668 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12669 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12670 intel_compare_m_n(m_n->link_m, m_n->link_n,
12671 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12672 if (adjust)
12673 *m2_n2 = *m_n;
12674
12675 return true;
12676 }
12677
12678 return false;
12679}
12680
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012681static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012682intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012683 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012684 struct intel_crtc_state *pipe_config,
12685 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012686{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012687 bool ret = true;
12688
12689#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12690 do { \
12691 if (!adjust) \
12692 DRM_ERROR(fmt, ##__VA_ARGS__); \
12693 else \
12694 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12695 } while (0)
12696
Daniel Vetter66e985c2013-06-05 13:34:20 +020012697#define PIPE_CONF_CHECK_X(name) \
12698 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012699 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012700 "(expected 0x%08x, found 0x%08x)\n", \
12701 current_config->name, \
12702 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012703 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012704 }
12705
Daniel Vetter08a24032013-04-19 11:25:34 +020012706#define PIPE_CONF_CHECK_I(name) \
12707 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012708 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012709 "(expected %i, found %i)\n", \
12710 current_config->name, \
12711 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012712 ret = false; \
12713 }
12714
12715#define PIPE_CONF_CHECK_M_N(name) \
12716 if (!intel_compare_link_m_n(&current_config->name, \
12717 &pipe_config->name,\
12718 adjust)) { \
12719 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12720 "(expected tu %i gmch %i/%i link %i/%i, " \
12721 "found tu %i, gmch %i/%i link %i/%i)\n", \
12722 current_config->name.tu, \
12723 current_config->name.gmch_m, \
12724 current_config->name.gmch_n, \
12725 current_config->name.link_m, \
12726 current_config->name.link_n, \
12727 pipe_config->name.tu, \
12728 pipe_config->name.gmch_m, \
12729 pipe_config->name.gmch_n, \
12730 pipe_config->name.link_m, \
12731 pipe_config->name.link_n); \
12732 ret = false; \
12733 }
12734
12735#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12736 if (!intel_compare_link_m_n(&current_config->name, \
12737 &pipe_config->name, adjust) && \
12738 !intel_compare_link_m_n(&current_config->alt_name, \
12739 &pipe_config->name, adjust)) { \
12740 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12741 "(expected tu %i gmch %i/%i link %i/%i, " \
12742 "or tu %i gmch %i/%i link %i/%i, " \
12743 "found tu %i, gmch %i/%i link %i/%i)\n", \
12744 current_config->name.tu, \
12745 current_config->name.gmch_m, \
12746 current_config->name.gmch_n, \
12747 current_config->name.link_m, \
12748 current_config->name.link_n, \
12749 current_config->alt_name.tu, \
12750 current_config->alt_name.gmch_m, \
12751 current_config->alt_name.gmch_n, \
12752 current_config->alt_name.link_m, \
12753 current_config->alt_name.link_n, \
12754 pipe_config->name.tu, \
12755 pipe_config->name.gmch_m, \
12756 pipe_config->name.gmch_n, \
12757 pipe_config->name.link_m, \
12758 pipe_config->name.link_n); \
12759 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012760 }
12761
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012762/* This is required for BDW+ where there is only one set of registers for
12763 * switching between high and low RR.
12764 * This macro can be used whenever a comparison has to be made between one
12765 * hw state and multiple sw state variables.
12766 */
12767#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12768 if ((current_config->name != pipe_config->name) && \
12769 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012770 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012771 "(expected %i or %i, found %i)\n", \
12772 current_config->name, \
12773 current_config->alt_name, \
12774 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012775 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012776 }
12777
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012778#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12779 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012780 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012781 "(expected %i, found %i)\n", \
12782 current_config->name & (mask), \
12783 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012784 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012785 }
12786
Ville Syrjälä5e550652013-09-06 23:29:07 +030012787#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12788 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012789 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012790 "(expected %i, found %i)\n", \
12791 current_config->name, \
12792 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012793 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012794 }
12795
Daniel Vetterbb760062013-06-06 14:55:52 +020012796#define PIPE_CONF_QUIRK(quirk) \
12797 ((current_config->quirks | pipe_config->quirks) & (quirk))
12798
Daniel Vettereccb1402013-05-22 00:50:22 +020012799 PIPE_CONF_CHECK_I(cpu_transcoder);
12800
Daniel Vetter08a24032013-04-19 11:25:34 +020012801 PIPE_CONF_CHECK_I(has_pch_encoder);
12802 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012803 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012804
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012805 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012806 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012807
12808 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012809 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012810
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012811 if (current_config->has_drrs)
12812 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12813 } else
12814 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012815
Jani Nikulaa65347b2015-11-27 12:21:46 +020012816 PIPE_CONF_CHECK_I(has_dsi_encoder);
12817
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012818 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12819 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12820 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12821 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12822 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12823 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012824
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012825 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12826 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12827 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12828 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12829 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12830 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012831
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012832 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012833 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012834 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012835 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012836 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012837 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012838
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012839 PIPE_CONF_CHECK_I(has_audio);
12840
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012841 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012842 DRM_MODE_FLAG_INTERLACE);
12843
Daniel Vetterbb760062013-06-06 14:55:52 +020012844 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012845 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012846 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012847 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012848 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012849 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012850 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012851 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012852 DRM_MODE_FLAG_NVSYNC);
12853 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012854
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012855 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012856 /* pfit ratios are autocomputed by the hw on gen4+ */
12857 if (INTEL_INFO(dev)->gen < 4)
12858 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012859 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012860
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012861 if (!adjust) {
12862 PIPE_CONF_CHECK_I(pipe_src_w);
12863 PIPE_CONF_CHECK_I(pipe_src_h);
12864
12865 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12866 if (current_config->pch_pfit.enabled) {
12867 PIPE_CONF_CHECK_X(pch_pfit.pos);
12868 PIPE_CONF_CHECK_X(pch_pfit.size);
12869 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012870
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012871 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12872 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012873
Jesse Barnese59150d2014-01-07 13:30:45 -080012874 /* BDW+ don't expose a synchronous way to read the state */
12875 if (IS_HASWELL(dev))
12876 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012877
Ville Syrjälä282740f2013-09-04 18:30:03 +030012878 PIPE_CONF_CHECK_I(double_wide);
12879
Daniel Vetter26804af2014-06-25 22:01:55 +030012880 PIPE_CONF_CHECK_X(ddi_pll_sel);
12881
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012882 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012883 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012884 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012885 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12886 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012887 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012888 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012889 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12890 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12891 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012892
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012893 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12894 PIPE_CONF_CHECK_I(pipe_bpp);
12895
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012896 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012897 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012898
Daniel Vetter66e985c2013-06-05 13:34:20 +020012899#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012900#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012901#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012902#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012903#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012904#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012905#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012906
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012907 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012908}
12909
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012910static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12911 const struct intel_crtc_state *pipe_config)
12912{
12913 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012914 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012915 &pipe_config->fdi_m_n);
12916 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12917
12918 /*
12919 * FDI already provided one idea for the dotclock.
12920 * Yell if the encoder disagrees.
12921 */
12922 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12923 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12924 fdi_dotclock, dotclock);
12925 }
12926}
12927
Damien Lespiau08db6652014-11-04 17:06:52 +000012928static void check_wm_state(struct drm_device *dev)
12929{
12930 struct drm_i915_private *dev_priv = dev->dev_private;
12931 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12932 struct intel_crtc *intel_crtc;
12933 int plane;
12934
12935 if (INTEL_INFO(dev)->gen < 9)
12936 return;
12937
12938 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12939 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12940
12941 for_each_intel_crtc(dev, intel_crtc) {
12942 struct skl_ddb_entry *hw_entry, *sw_entry;
12943 const enum pipe pipe = intel_crtc->pipe;
12944
12945 if (!intel_crtc->active)
12946 continue;
12947
12948 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012949 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012950 hw_entry = &hw_ddb.plane[pipe][plane];
12951 sw_entry = &sw_ddb->plane[pipe][plane];
12952
12953 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12954 continue;
12955
12956 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12957 "(expected (%u,%u), found (%u,%u))\n",
12958 pipe_name(pipe), plane + 1,
12959 sw_entry->start, sw_entry->end,
12960 hw_entry->start, hw_entry->end);
12961 }
12962
12963 /* cursor */
Matt Roper4969d332015-09-24 15:53:10 -070012964 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12965 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
Damien Lespiau08db6652014-11-04 17:06:52 +000012966
12967 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12968 continue;
12969
12970 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12971 "(expected (%u,%u), found (%u,%u))\n",
12972 pipe_name(pipe),
12973 sw_entry->start, sw_entry->end,
12974 hw_entry->start, hw_entry->end);
12975 }
12976}
12977
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012978static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012979check_connector_state(struct drm_device *dev,
12980 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012981{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012982 struct drm_connector_state *old_conn_state;
12983 struct drm_connector *connector;
12984 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012985
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012986 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12987 struct drm_encoder *encoder = connector->encoder;
12988 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012989
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012990 /* This also checks the encoder/connector hw state with the
12991 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012992 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012993
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012994 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012995 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012996 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012997}
12998
12999static void
13000check_encoder_state(struct drm_device *dev)
13001{
13002 struct intel_encoder *encoder;
13003 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013004
Damien Lespiaub2784e12014-08-05 11:29:37 +010013005 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013006 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013007 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013008
13009 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13010 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013011 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013012
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013013 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013014 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013015 continue;
13016 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013017
13018 I915_STATE_WARN(connector->base.state->crtc !=
13019 encoder->base.crtc,
13020 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013021 }
Dave Airlie0e32b392014-05-02 14:02:48 +100013022
Rob Clarke2c719b2014-12-15 13:56:32 -050013023 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013024 "encoder's enabled state mismatch "
13025 "(expected %i, found %i)\n",
13026 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013027
13028 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013029 bool active;
13030
13031 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013032 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013033 "encoder detached but still enabled on pipe %c.\n",
13034 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013035 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013036 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013037}
13038
13039static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013040check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013041{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013042 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013043 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013044 struct drm_crtc_state *old_crtc_state;
13045 struct drm_crtc *crtc;
13046 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013047
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013048 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
13049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13050 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020013051 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013052
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013053 if (!needs_modeset(crtc->state) &&
13054 !to_intel_crtc_state(crtc->state)->update_pipe)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013055 continue;
13056
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013057 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
13058 pipe_config = to_intel_crtc_state(old_crtc_state);
13059 memset(pipe_config, 0, sizeof(*pipe_config));
13060 pipe_config->base.crtc = crtc;
13061 pipe_config->base.state = old_state;
13062
13063 DRM_DEBUG_KMS("[CRTC:%d]\n",
13064 crtc->base.id);
13065
13066 active = dev_priv->display.get_pipe_config(intel_crtc,
13067 pipe_config);
13068
13069 /* hw state is inconsistent with the pipe quirk */
13070 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13071 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13072 active = crtc->state->active;
13073
13074 I915_STATE_WARN(crtc->state->active != active,
13075 "crtc active state doesn't match with hw state "
13076 "(expected %i, found %i)\n", crtc->state->active, active);
13077
13078 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
13079 "transitional active state does not match atomic hw state "
13080 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
13081
13082 for_each_encoder_on_crtc(dev, crtc, encoder) {
13083 enum pipe pipe;
13084
13085 active = encoder->get_hw_state(encoder, &pipe);
13086 I915_STATE_WARN(active != crtc->state->active,
13087 "[ENCODER:%i] active %i with crtc active %i\n",
13088 encoder->base.base.id, active, crtc->state->active);
13089
13090 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13091 "Encoder connected to wrong pipe %c\n",
13092 pipe_name(pipe));
13093
13094 if (active)
13095 encoder->get_config(encoder, pipe_config);
13096 }
13097
13098 if (!crtc->state->active)
13099 continue;
13100
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020013101 intel_pipe_config_sanity_check(dev_priv, pipe_config);
13102
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013103 sw_config = to_intel_crtc_state(crtc->state);
13104 if (!intel_pipe_config_compare(dev, sw_config,
13105 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050013106 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013107 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020013108 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013109 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020013110 "[sw state]");
13111 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013112 }
13113}
13114
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013115static void
13116check_shared_dpll_state(struct drm_device *dev)
13117{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013118 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013119 struct intel_crtc *crtc;
13120 struct intel_dpll_hw_state dpll_hw_state;
13121 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013122
13123 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13124 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13125 int enabled_crtcs = 0, active_crtcs = 0;
13126 bool active;
13127
13128 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13129
13130 DRM_DEBUG_KMS("%s\n", pll->name);
13131
13132 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
13133
Rob Clarke2c719b2014-12-15 13:56:32 -050013134 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020013135 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013136 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050013137 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020013138 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050013139 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020013140 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050013141 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020013142 "pll on state mismatch (expected %i, found %i)\n",
13143 pll->on, active);
13144
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013145 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080013146 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020013147 enabled_crtcs++;
13148 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13149 active_crtcs++;
13150 }
Rob Clarke2c719b2014-12-15 13:56:32 -050013151 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020013152 "pll active crtcs mismatch (expected %i, found %i)\n",
13153 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050013154 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020013155 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013156 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013157
Rob Clarke2c719b2014-12-15 13:56:32 -050013158 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020013159 sizeof(dpll_hw_state)),
13160 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020013161 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013162}
13163
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013164static void
13165intel_modeset_check_state(struct drm_device *dev,
13166 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013167{
Damien Lespiau08db6652014-11-04 17:06:52 +000013168 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013169 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013170 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013171 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013172 check_shared_dpll_state(dev);
13173}
13174
Ville Syrjälä80715b22014-05-15 20:23:23 +030013175static void update_scanline_offset(struct intel_crtc *crtc)
13176{
13177 struct drm_device *dev = crtc->base.dev;
13178
13179 /*
13180 * The scanline counter increments at the leading edge of hsync.
13181 *
13182 * On most platforms it starts counting from vtotal-1 on the
13183 * first active line. That means the scanline counter value is
13184 * always one less than what we would expect. Ie. just after
13185 * start of vblank, which also occurs at start of hsync (on the
13186 * last active line), the scanline counter will read vblank_start-1.
13187 *
13188 * On gen2 the scanline counter starts counting from 1 instead
13189 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13190 * to keep the value positive), instead of adding one.
13191 *
13192 * On HSW+ the behaviour of the scanline counter depends on the output
13193 * type. For DP ports it behaves like most other platforms, but on HDMI
13194 * there's an extra 1 line difference. So we need to add two instead of
13195 * one to the value.
13196 */
13197 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013198 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013199 int vtotal;
13200
Ville Syrjälä124abe02015-09-08 13:40:45 +030013201 vtotal = adjusted_mode->crtc_vtotal;
13202 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013203 vtotal /= 2;
13204
13205 crtc->scanline_offset = vtotal - 1;
13206 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013207 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013208 crtc->scanline_offset = 2;
13209 } else
13210 crtc->scanline_offset = 1;
13211}
13212
Maarten Lankhorstad421372015-06-15 12:33:42 +020013213static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013214{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013215 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013216 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013217 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013218 struct drm_crtc *crtc;
13219 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013220 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013221
13222 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013223 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013224
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013225 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13227 int old_dpll = to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013228
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013229 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013230 continue;
13231
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013232 to_intel_crtc_state(crtc_state)->shared_dpll = DPLL_ID_PRIVATE;
13233
13234 if (old_dpll == DPLL_ID_PRIVATE)
13235 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013236
Maarten Lankhorstad421372015-06-15 12:33:42 +020013237 if (!shared_dpll)
13238 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13239
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013240 shared_dpll[old_dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013241 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013242}
13243
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013244/*
13245 * This implements the workaround described in the "notes" section of the mode
13246 * set sequence documentation. When going from no pipes or single pipe to
13247 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13248 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13249 */
13250static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13251{
13252 struct drm_crtc_state *crtc_state;
13253 struct intel_crtc *intel_crtc;
13254 struct drm_crtc *crtc;
13255 struct intel_crtc_state *first_crtc_state = NULL;
13256 struct intel_crtc_state *other_crtc_state = NULL;
13257 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13258 int i;
13259
13260 /* look at all crtc's that are going to be enabled in during modeset */
13261 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13262 intel_crtc = to_intel_crtc(crtc);
13263
13264 if (!crtc_state->active || !needs_modeset(crtc_state))
13265 continue;
13266
13267 if (first_crtc_state) {
13268 other_crtc_state = to_intel_crtc_state(crtc_state);
13269 break;
13270 } else {
13271 first_crtc_state = to_intel_crtc_state(crtc_state);
13272 first_pipe = intel_crtc->pipe;
13273 }
13274 }
13275
13276 /* No workaround needed? */
13277 if (!first_crtc_state)
13278 return 0;
13279
13280 /* w/a possibly needed, check how many crtc's are already enabled. */
13281 for_each_intel_crtc(state->dev, intel_crtc) {
13282 struct intel_crtc_state *pipe_config;
13283
13284 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13285 if (IS_ERR(pipe_config))
13286 return PTR_ERR(pipe_config);
13287
13288 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13289
13290 if (!pipe_config->base.active ||
13291 needs_modeset(&pipe_config->base))
13292 continue;
13293
13294 /* 2 or more enabled crtcs means no need for w/a */
13295 if (enabled_pipe != INVALID_PIPE)
13296 return 0;
13297
13298 enabled_pipe = intel_crtc->pipe;
13299 }
13300
13301 if (enabled_pipe != INVALID_PIPE)
13302 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13303 else if (other_crtc_state)
13304 other_crtc_state->hsw_workaround_pipe = first_pipe;
13305
13306 return 0;
13307}
13308
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013309static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13310{
13311 struct drm_crtc *crtc;
13312 struct drm_crtc_state *crtc_state;
13313 int ret = 0;
13314
13315 /* add all active pipes to the state */
13316 for_each_crtc(state->dev, crtc) {
13317 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13318 if (IS_ERR(crtc_state))
13319 return PTR_ERR(crtc_state);
13320
13321 if (!crtc_state->active || needs_modeset(crtc_state))
13322 continue;
13323
13324 crtc_state->mode_changed = true;
13325
13326 ret = drm_atomic_add_affected_connectors(state, crtc);
13327 if (ret)
13328 break;
13329
13330 ret = drm_atomic_add_affected_planes(state, crtc);
13331 if (ret)
13332 break;
13333 }
13334
13335 return ret;
13336}
13337
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013338static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013339{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013340 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13341 struct drm_i915_private *dev_priv = state->dev->dev_private;
13342 struct drm_crtc *crtc;
13343 struct drm_crtc_state *crtc_state;
13344 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013345
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013346 if (!check_digital_port_conflicts(state)) {
13347 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13348 return -EINVAL;
13349 }
13350
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013351 intel_state->modeset = true;
13352 intel_state->active_crtcs = dev_priv->active_crtcs;
13353
13354 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13355 if (crtc_state->active)
13356 intel_state->active_crtcs |= 1 << i;
13357 else
13358 intel_state->active_crtcs &= ~(1 << i);
13359 }
13360
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013361 /*
13362 * See if the config requires any additional preparation, e.g.
13363 * to adjust global state with pipes off. We need to do this
13364 * here so we can get the modeset_pipe updated config for the new
13365 * mode set on this crtc. For other crtcs we need to use the
13366 * adjusted_mode bits in the crtc directly.
13367 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013368 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013369 ret = dev_priv->display.modeset_calc_cdclk(state);
13370
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013371 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013372 ret = intel_modeset_all_pipes(state);
13373
13374 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013375 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013376
13377 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13378 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013379 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013380 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013381
Maarten Lankhorstad421372015-06-15 12:33:42 +020013382 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013383
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013384 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013385 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013386
Maarten Lankhorstad421372015-06-15 12:33:42 +020013387 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013388}
13389
Matt Roperaa363132015-09-24 15:53:18 -070013390/*
13391 * Handle calculation of various watermark data at the end of the atomic check
13392 * phase. The code here should be run after the per-crtc and per-plane 'check'
13393 * handlers to ensure that all derived state has been updated.
13394 */
13395static void calc_watermark_data(struct drm_atomic_state *state)
13396{
13397 struct drm_device *dev = state->dev;
13398 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13399 struct drm_crtc *crtc;
13400 struct drm_crtc_state *cstate;
13401 struct drm_plane *plane;
13402 struct drm_plane_state *pstate;
13403
13404 /*
13405 * Calculate watermark configuration details now that derived
13406 * plane/crtc state is all properly updated.
13407 */
13408 drm_for_each_crtc(crtc, dev) {
13409 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13410 crtc->state;
13411
13412 if (cstate->active)
13413 intel_state->wm_config.num_pipes_active++;
13414 }
13415 drm_for_each_legacy_plane(plane, dev) {
13416 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13417 plane->state;
13418
13419 if (!to_intel_plane_state(pstate)->visible)
13420 continue;
13421
13422 intel_state->wm_config.sprites_enabled = true;
13423 if (pstate->crtc_w != pstate->src_w >> 16 ||
13424 pstate->crtc_h != pstate->src_h >> 16)
13425 intel_state->wm_config.sprites_scaled = true;
13426 }
13427}
13428
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013429/**
13430 * intel_atomic_check - validate state object
13431 * @dev: drm device
13432 * @state: state to validate
13433 */
13434static int intel_atomic_check(struct drm_device *dev,
13435 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013436{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013437 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013438 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013439 struct drm_crtc *crtc;
13440 struct drm_crtc_state *crtc_state;
13441 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013442 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013443
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013444 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013445 if (ret)
13446 return ret;
13447
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013448 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013449 struct intel_crtc_state *pipe_config =
13450 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013451
Maarten Lankhorstba8af3e2015-11-16 12:49:14 +010013452 memset(&to_intel_crtc(crtc)->atomic, 0,
13453 sizeof(struct intel_crtc_atomic_commit));
13454
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013455 /* Catch I915_MODE_FLAG_INHERITED */
13456 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13457 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013458
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013459 if (!crtc_state->enable) {
13460 if (needs_modeset(crtc_state))
13461 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013462 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013463 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013464
Daniel Vetter26495482015-07-15 14:15:52 +020013465 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013466 continue;
13467
Daniel Vetter26495482015-07-15 14:15:52 +020013468 /* FIXME: For only active_changed we shouldn't need to do any
13469 * state recomputation at all. */
13470
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013471 ret = drm_atomic_add_affected_connectors(state, crtc);
13472 if (ret)
13473 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013474
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013475 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013476 if (ret)
13477 return ret;
13478
Jani Nikula73831232015-11-19 10:26:30 +020013479 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013480 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013481 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013482 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013483 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013484 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013485 }
13486
13487 if (needs_modeset(crtc_state)) {
13488 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013489
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013490 ret = drm_atomic_add_affected_planes(state, crtc);
13491 if (ret)
13492 return ret;
13493 }
13494
Daniel Vetter26495482015-07-15 14:15:52 +020013495 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13496 needs_modeset(crtc_state) ?
13497 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013498 }
13499
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013500 if (any_ms) {
13501 ret = intel_modeset_checks(state);
13502
13503 if (ret)
13504 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013505 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013506 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013507
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013508 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013509 if (ret)
13510 return ret;
13511
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013512 intel_fbc_choose_crtc(dev_priv, state);
Matt Roperaa363132015-09-24 15:53:18 -070013513 calc_watermark_data(state);
13514
13515 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013516}
13517
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013518static int intel_atomic_prepare_commit(struct drm_device *dev,
13519 struct drm_atomic_state *state,
13520 bool async)
13521{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013522 struct drm_i915_private *dev_priv = dev->dev_private;
13523 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013524 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013525 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013526 struct drm_crtc *crtc;
13527 int i, ret;
13528
13529 if (async) {
13530 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13531 return -EINVAL;
13532 }
13533
13534 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13535 ret = intel_crtc_wait_for_pending_flips(crtc);
13536 if (ret)
13537 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013538
13539 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13540 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013541 }
13542
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013543 ret = mutex_lock_interruptible(&dev->struct_mutex);
13544 if (ret)
13545 return ret;
13546
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013547 ret = drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013548 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13549 u32 reset_counter;
13550
13551 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13552 mutex_unlock(&dev->struct_mutex);
13553
13554 for_each_plane_in_state(state, plane, plane_state, i) {
13555 struct intel_plane_state *intel_plane_state =
13556 to_intel_plane_state(plane_state);
13557
13558 if (!intel_plane_state->wait_req)
13559 continue;
13560
13561 ret = __i915_wait_request(intel_plane_state->wait_req,
13562 reset_counter, true,
13563 NULL, NULL);
13564
13565 /* Swallow -EIO errors to allow updates during hw lockup. */
13566 if (ret == -EIO)
13567 ret = 0;
13568
13569 if (ret)
13570 break;
13571 }
13572
13573 if (!ret)
13574 return 0;
13575
13576 mutex_lock(&dev->struct_mutex);
13577 drm_atomic_helper_cleanup_planes(dev, state);
13578 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013579
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013580 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013581 return ret;
13582}
13583
Maarten Lankhorste8861672016-02-24 11:24:26 +010013584static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13585 struct drm_i915_private *dev_priv,
13586 unsigned crtc_mask)
13587{
13588 unsigned last_vblank_count[I915_MAX_PIPES];
13589 enum pipe pipe;
13590 int ret;
13591
13592 if (!crtc_mask)
13593 return;
13594
13595 for_each_pipe(dev_priv, pipe) {
13596 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13597
13598 if (!((1 << pipe) & crtc_mask))
13599 continue;
13600
13601 ret = drm_crtc_vblank_get(crtc);
13602 if (WARN_ON(ret != 0)) {
13603 crtc_mask &= ~(1 << pipe);
13604 continue;
13605 }
13606
13607 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13608 }
13609
13610 for_each_pipe(dev_priv, pipe) {
13611 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13612 long lret;
13613
13614 if (!((1 << pipe) & crtc_mask))
13615 continue;
13616
13617 lret = wait_event_timeout(dev->vblank[pipe].queue,
13618 last_vblank_count[pipe] !=
13619 drm_crtc_vblank_count(crtc),
13620 msecs_to_jiffies(50));
13621
13622 WARN_ON(!lret);
13623
13624 drm_crtc_vblank_put(crtc);
13625 }
13626}
13627
13628static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13629{
13630 /* fb updated, need to unpin old fb */
13631 if (crtc_state->fb_changed)
13632 return true;
13633
13634 /* wm changes, need vblank before final wm's */
13635 if (crtc_state->wm_changed)
13636 return true;
13637
13638 /*
13639 * cxsr is re-enabled after vblank.
13640 * This is already handled by crtc_state->wm_changed,
13641 * but added for clarity.
13642 */
13643 if (crtc_state->disable_cxsr)
13644 return true;
13645
13646 return false;
13647}
13648
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013649/**
13650 * intel_atomic_commit - commit validated state object
13651 * @dev: DRM device
13652 * @state: the top-level driver state object
13653 * @async: asynchronous commit
13654 *
13655 * This function commits a top-level state object that has been validated
13656 * with drm_atomic_helper_check().
13657 *
13658 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13659 * we can only handle plane-related operations and do not yet support
13660 * asynchronous commit.
13661 *
13662 * RETURNS
13663 * Zero for success or -errno.
13664 */
13665static int intel_atomic_commit(struct drm_device *dev,
13666 struct drm_atomic_state *state,
13667 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013668{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013669 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013670 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013671 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013672 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013673 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013674 int ret = 0, i;
13675 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013676 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013677 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013678
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013679 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013680 if (ret) {
13681 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013682 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013683 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013684
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013685 drm_atomic_helper_swap_state(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013686 dev_priv->wm.config = to_intel_atomic_state(state)->wm_config;
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013687
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013688 if (intel_state->modeset) {
13689 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13690 sizeof(intel_state->min_pixclk));
13691 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013692 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013693
13694 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013695 }
13696
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013697 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013698 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13699
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013700 if (needs_modeset(crtc->state) ||
13701 to_intel_crtc_state(crtc->state)->update_pipe) {
13702 hw_check = true;
13703
13704 put_domains[to_intel_crtc(crtc)->pipe] =
13705 modeset_get_crtc_power_domains(crtc,
13706 to_intel_crtc_state(crtc->state));
13707 }
13708
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013709 if (!needs_modeset(crtc->state))
13710 continue;
13711
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +010013712 intel_pre_plane_update(to_intel_crtc_state(crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013713
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013714 if (crtc_state->active) {
13715 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13716 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013717 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013718 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013719 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013720
13721 /*
13722 * Underruns don't always raise
13723 * interrupts, so check manually.
13724 */
13725 intel_check_cpu_fifo_underruns(dev_priv);
13726 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013727
13728 if (!crtc->state->active)
13729 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013730 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013731 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013732
Daniel Vetterea9d7582012-07-10 10:42:52 +020013733 /* Only after disabling all output pipelines that will be changed can we
13734 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013735 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013736
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013737 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013738 intel_shared_dpll_commit(state);
13739
13740 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013741
13742 if (dev_priv->display.modeset_commit_cdclk &&
13743 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13744 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013745 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013746
Daniel Vettera6778b32012-07-02 09:56:42 +020013747 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013748 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013749 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13750 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013751 struct intel_crtc_state *pipe_config =
13752 to_intel_crtc_state(crtc->state);
13753 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013754
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013755 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013756 update_scanline_offset(to_intel_crtc(crtc));
13757 dev_priv->display.crtc_enable(crtc);
13758 }
13759
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013760 if (!modeset)
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +010013761 intel_pre_plane_update(to_intel_crtc_state(crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013762
Paulo Zanoni49227c42016-01-19 11:35:52 -020013763 if (crtc->state->active && intel_crtc->atomic.update_fbc)
13764 intel_fbc_enable(intel_crtc);
13765
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013766 if (crtc->state->active &&
13767 (crtc->state->planes_changed || update_pipe))
Maarten Lankhorst62852622015-09-23 16:29:38 +020013768 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013769
Maarten Lankhorste8861672016-02-24 11:24:26 +010013770 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13771 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013772 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013773
Daniel Vettera6778b32012-07-02 09:56:42 +020013774 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013775
Maarten Lankhorste8861672016-02-24 11:24:26 +010013776 if (!state->legacy_cursor_update)
13777 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013778
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013779 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorste8861672016-02-24 11:24:26 +010013780 intel_post_plane_update(to_intel_crtc(crtc));
13781
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013782 if (put_domains[i])
13783 modeset_put_power_domains(dev_priv, put_domains[i]);
13784 }
13785
13786 if (intel_state->modeset)
13787 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13788
Matt Ropered4a6a72016-02-23 17:20:13 -080013789 /*
13790 * Now that the vblank has passed, we can go ahead and program the
13791 * optimal watermarks on platforms that need two-step watermark
13792 * programming.
13793 *
13794 * TODO: Move this (and other cleanup) to an async worker eventually.
13795 */
13796 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13797 intel_cstate = to_intel_crtc_state(crtc->state);
13798
13799 if (dev_priv->display.optimize_watermarks)
13800 dev_priv->display.optimize_watermarks(intel_cstate);
13801 }
13802
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013803 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013804 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013805 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013806
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013807 if (hw_check)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013808 intel_modeset_check_state(dev, state);
13809
13810 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013811
Mika Kuoppala75714942015-12-16 09:26:48 +020013812 /* As one of the primary mmio accessors, KMS has a high likelihood
13813 * of triggering bugs in unclaimed access. After we finish
13814 * modesetting, see if an error has been flagged, and if so
13815 * enable debugging for the next modeset - and hope we catch
13816 * the culprit.
13817 *
13818 * XXX note that we assume display power is on at this point.
13819 * This might hold true now but we need to add pm helper to check
13820 * unclaimed only when the hardware is on, as atomic commits
13821 * can happen also when the device is completely off.
13822 */
13823 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13824
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013825 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013826}
13827
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013828void intel_crtc_restore_mode(struct drm_crtc *crtc)
13829{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013830 struct drm_device *dev = crtc->dev;
13831 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013832 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013833 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013834
13835 state = drm_atomic_state_alloc(dev);
13836 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013837 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013838 crtc->base.id);
13839 return;
13840 }
13841
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013842 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013843
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013844retry:
13845 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13846 ret = PTR_ERR_OR_ZERO(crtc_state);
13847 if (!ret) {
13848 if (!crtc_state->active)
13849 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013850
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013851 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013852 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013853 }
13854
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013855 if (ret == -EDEADLK) {
13856 drm_atomic_state_clear(state);
13857 drm_modeset_backoff(state->acquire_ctx);
13858 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013859 }
13860
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013861 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013862out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013863 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013864}
13865
Daniel Vetter25c5b262012-07-08 22:08:04 +020013866#undef for_each_intel_crtc_masked
13867
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013868static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013869 .gamma_set = intel_crtc_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013870 .set_config = drm_atomic_helper_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013871 .destroy = intel_crtc_destroy,
13872 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013873 .atomic_duplicate_state = intel_crtc_duplicate_state,
13874 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013875};
13876
Daniel Vetter53589012013-06-05 13:34:16 +020013877static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13878 struct intel_shared_dpll *pll,
13879 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013880{
Daniel Vetter53589012013-06-05 13:34:16 +020013881 uint32_t val;
13882
Imre Deak12fda382016-02-12 18:55:12 +020013883 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013884 return false;
13885
Daniel Vetter53589012013-06-05 13:34:16 +020013886 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013887 hw_state->dpll = val;
13888 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13889 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013890
Imre Deak12fda382016-02-12 18:55:12 +020013891 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
13892
Daniel Vetter53589012013-06-05 13:34:16 +020013893 return val & DPLL_VCO_ENABLE;
13894}
13895
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013896static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13897 struct intel_shared_dpll *pll)
13898{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013899 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13900 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013901}
13902
Daniel Vettere7b903d2013-06-05 13:34:14 +020013903static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13904 struct intel_shared_dpll *pll)
13905{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013906 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013907 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013908
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013909 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013910
13911 /* Wait for the clocks to stabilize. */
13912 POSTING_READ(PCH_DPLL(pll->id));
13913 udelay(150);
13914
13915 /* The pixel multiplier can only be updated once the
13916 * DPLL is enabled and the clocks are stable.
13917 *
13918 * So write it again.
13919 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013920 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013921 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013922 udelay(200);
13923}
13924
13925static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13926 struct intel_shared_dpll *pll)
13927{
13928 struct drm_device *dev = dev_priv->dev;
13929 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013930
13931 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013932 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013933 if (intel_crtc_to_shared_dpll(crtc) == pll)
13934 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13935 }
13936
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013937 I915_WRITE(PCH_DPLL(pll->id), 0);
13938 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013939 udelay(200);
13940}
13941
Daniel Vetter46edb022013-06-05 13:34:12 +020013942static char *ibx_pch_dpll_names[] = {
13943 "PCH DPLL A",
13944 "PCH DPLL B",
13945};
13946
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013947static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013948{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013949 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013950 int i;
13951
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013952 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013953
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013954 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013955 dev_priv->shared_dplls[i].id = i;
13956 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013957 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013958 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13959 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013960 dev_priv->shared_dplls[i].get_hw_state =
13961 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013962 }
13963}
13964
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013965static void intel_shared_dpll_init(struct drm_device *dev)
13966{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013967 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013968
Daniel Vetter9cd86932014-06-25 22:01:57 +030013969 if (HAS_DDI(dev))
13970 intel_ddi_pll_init(dev);
13971 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013972 ibx_pch_dpll_init(dev);
13973 else
13974 dev_priv->num_shared_dpll = 0;
13975
13976 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013977}
13978
Matt Roper6beb8c232014-12-01 15:40:14 -080013979/**
13980 * intel_prepare_plane_fb - Prepare fb for usage on plane
13981 * @plane: drm plane to prepare for
13982 * @fb: framebuffer to prepare for presentation
13983 *
13984 * Prepares a framebuffer for usage on a display plane. Generally this
13985 * involves pinning the underlying object and updating the frontbuffer tracking
13986 * bits. Some older platforms need special physical address handling for
13987 * cursor planes.
13988 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013989 * Must be called with struct_mutex held.
13990 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013991 * Returns 0 on success, negative error code on failure.
13992 */
13993int
13994intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013995 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013996{
13997 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013998 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013999 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080014000 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014001 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080014002 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070014003
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014004 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070014005 return 0;
14006
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014007 if (old_obj) {
14008 struct drm_crtc_state *crtc_state =
14009 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14010
14011 /* Big Hammer, we also need to ensure that any pending
14012 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14013 * current scanout is retired before unpinning the old
14014 * framebuffer. Note that we rely on userspace rendering
14015 * into the buffer attached to the pipe they are waiting
14016 * on. If not, userspace generates a GPU hang with IPEHR
14017 * point to the MI_WAIT_FOR_EVENT.
14018 *
14019 * This should only fail upon a hung GPU, in which case we
14020 * can safely continue.
14021 */
14022 if (needs_modeset(crtc_state))
14023 ret = i915_gem_object_wait_rendering(old_obj, true);
14024
14025 /* Swallow -EIO errors to allow updates during hw lockup. */
14026 if (ret && ret != -EIO)
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014027 return ret;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014028 }
14029
Alex Goins3c28ff22015-11-25 18:43:39 -080014030 /* For framebuffer backed by dmabuf, wait for fence */
14031 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010014032 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080014033
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010014034 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
14035 false, true,
14036 MAX_SCHEDULE_TIMEOUT);
14037 if (lret == -ERESTARTSYS)
14038 return lret;
14039
14040 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080014041 }
14042
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014043 if (!obj) {
14044 ret = 0;
14045 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080014046 INTEL_INFO(dev)->cursor_needs_physical) {
14047 int align = IS_I830(dev) ? 16 * 1024 : 256;
14048 ret = i915_gem_object_attach_phys(obj, align);
14049 if (ret)
14050 DRM_DEBUG_KMS("failed to attach phys object\n");
14051 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020014052 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080014053 }
14054
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014055 if (ret == 0) {
14056 if (obj) {
14057 struct intel_plane_state *plane_state =
14058 to_intel_plane_state(new_state);
14059
14060 i915_gem_request_assign(&plane_state->wait_req,
14061 obj->last_write_req);
14062 }
14063
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014064 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014065 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014066
Matt Roper6beb8c232014-12-01 15:40:14 -080014067 return ret;
14068}
14069
Matt Roper38f3ce32014-12-02 07:45:25 -080014070/**
14071 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14072 * @plane: drm plane to clean up for
14073 * @fb: old framebuffer that was on plane
14074 *
14075 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014076 *
14077 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014078 */
14079void
14080intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014081 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014082{
14083 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014084 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014085 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014086 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14087 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014088
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014089 old_intel_state = to_intel_plane_state(old_state);
14090
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014091 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014092 return;
14093
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014094 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14095 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014096 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014097
14098 /* prepare_fb aborted? */
14099 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
14100 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
14101 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014102
14103 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070014104}
14105
Chandra Konduru6156a452015-04-27 13:48:39 -070014106int
14107skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14108{
14109 int max_scale;
14110 struct drm_device *dev;
14111 struct drm_i915_private *dev_priv;
14112 int crtc_clock, cdclk;
14113
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014114 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014115 return DRM_PLANE_HELPER_NO_SCALING;
14116
14117 dev = intel_crtc->base.dev;
14118 dev_priv = dev->dev_private;
14119 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014120 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014121
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014122 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014123 return DRM_PLANE_HELPER_NO_SCALING;
14124
14125 /*
14126 * skl max scale is lower of:
14127 * close to 3 but not 3, -1 is for that purpose
14128 * or
14129 * cdclk/crtc_clock
14130 */
14131 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14132
14133 return max_scale;
14134}
14135
Matt Roper465c1202014-05-29 08:06:54 -070014136static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014137intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014138 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014139 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014140{
Matt Roper2b875c22014-12-01 15:40:13 -080014141 struct drm_crtc *crtc = state->base.crtc;
14142 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014143 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014144 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14145 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014146
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014147 if (INTEL_INFO(plane->dev)->gen >= 9) {
14148 /* use scaler when colorkey is not required */
14149 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14150 min_scale = 1;
14151 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14152 }
Sonika Jindald8106362015-04-10 14:37:28 +053014153 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014154 }
Sonika Jindald8106362015-04-10 14:37:28 +053014155
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014156 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14157 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014158 min_scale, max_scale,
14159 can_position, true,
14160 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014161}
14162
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020014163static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14164 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080014165{
14166 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080014167 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020014168 struct intel_crtc_state *old_intel_state =
14169 to_intel_crtc_state(old_crtc_state);
14170 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030014171
Matt Roperc34c9ee2014-12-23 10:41:50 -080014172 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020014173 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020014174
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020014175 if (modeset)
14176 return;
14177
14178 if (to_intel_crtc_state(crtc->state)->update_pipe)
14179 intel_update_pipe_config(intel_crtc, old_intel_state);
14180 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020014181 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080014182}
14183
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020014184static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14185 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080014186{
Matt Roper32b7eee2014-12-24 07:59:06 -080014187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080014188
Maarten Lankhorst62852622015-09-23 16:29:38 +020014189 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014190}
14191
Matt Ropercf4c7c12014-12-04 10:27:42 -080014192/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014193 * intel_plane_destroy - destroy a plane
14194 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014195 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014196 * Common destruction function for all types of planes (primary, cursor,
14197 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014198 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014199void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014200{
14201 struct intel_plane *intel_plane = to_intel_plane(plane);
14202 drm_plane_cleanup(plane);
14203 kfree(intel_plane);
14204}
14205
Matt Roper65a3fea2015-01-21 16:35:42 -080014206const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014207 .update_plane = drm_atomic_helper_update_plane,
14208 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014209 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014210 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014211 .atomic_get_property = intel_plane_atomic_get_property,
14212 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014213 .atomic_duplicate_state = intel_plane_duplicate_state,
14214 .atomic_destroy_state = intel_plane_destroy_state,
14215
Matt Roper465c1202014-05-29 08:06:54 -070014216};
14217
14218static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14219 int pipe)
14220{
14221 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080014222 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070014223 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014224 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070014225
14226 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
14227 if (primary == NULL)
14228 return NULL;
14229
Matt Roper8e7d6882015-01-21 16:35:41 -080014230 state = intel_create_plane_state(&primary->base);
14231 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014232 kfree(primary);
14233 return NULL;
14234 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014235 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014236
Matt Roper465c1202014-05-29 08:06:54 -070014237 primary->can_scale = false;
14238 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014239 if (INTEL_INFO(dev)->gen >= 9) {
14240 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014241 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014242 }
Matt Roper465c1202014-05-29 08:06:54 -070014243 primary->pipe = pipe;
14244 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014245 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014246 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014247 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14248 primary->plane = !pipe;
14249
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014250 if (INTEL_INFO(dev)->gen >= 9) {
14251 intel_primary_formats = skl_primary_formats;
14252 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014253
14254 primary->update_plane = skylake_update_primary_plane;
14255 primary->disable_plane = skylake_disable_primary_plane;
14256 } else if (HAS_PCH_SPLIT(dev)) {
14257 intel_primary_formats = i965_primary_formats;
14258 num_formats = ARRAY_SIZE(i965_primary_formats);
14259
14260 primary->update_plane = ironlake_update_primary_plane;
14261 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014262 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014263 intel_primary_formats = i965_primary_formats;
14264 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014265
14266 primary->update_plane = i9xx_update_primary_plane;
14267 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014268 } else {
14269 intel_primary_formats = i8xx_primary_formats;
14270 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014271
14272 primary->update_plane = i9xx_update_primary_plane;
14273 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014274 }
14275
14276 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014277 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070014278 intel_primary_formats, num_formats,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014279 DRM_PLANE_TYPE_PRIMARY, NULL);
Sonika Jindal48404c12014-08-22 14:06:04 +053014280
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014281 if (INTEL_INFO(dev)->gen >= 4)
14282 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014283
Matt Roperea2c67b2014-12-23 10:41:52 -080014284 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14285
Matt Roper465c1202014-05-29 08:06:54 -070014286 return &primary->base;
14287}
14288
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014289void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14290{
14291 if (!dev->mode_config.rotation_property) {
14292 unsigned long flags = BIT(DRM_ROTATE_0) |
14293 BIT(DRM_ROTATE_180);
14294
14295 if (INTEL_INFO(dev)->gen >= 9)
14296 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14297
14298 dev->mode_config.rotation_property =
14299 drm_mode_create_rotation_property(dev, flags);
14300 }
14301 if (dev->mode_config.rotation_property)
14302 drm_object_attach_property(&plane->base.base,
14303 dev->mode_config.rotation_property,
14304 plane->base.state->rotation);
14305}
14306
Matt Roper3d7d6512014-06-10 08:28:13 -070014307static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014308intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014309 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014310 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014311{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014312 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014313 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014314 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014315 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014316 unsigned stride;
14317 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014318
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014319 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14320 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014321 DRM_PLANE_HELPER_NO_SCALING,
14322 DRM_PLANE_HELPER_NO_SCALING,
14323 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014324 if (ret)
14325 return ret;
14326
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014327 /* if we want to turn off the cursor ignore width and height */
14328 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014329 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014330
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014331 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014332 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014333 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14334 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014335 return -EINVAL;
14336 }
14337
Matt Roperea2c67b2014-12-23 10:41:52 -080014338 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14339 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014340 DRM_DEBUG_KMS("buffer is too small\n");
14341 return -ENOMEM;
14342 }
14343
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014344 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014345 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014346 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014347 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014348
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014349 /*
14350 * There's something wrong with the cursor on CHV pipe C.
14351 * If it straddles the left edge of the screen then
14352 * moving it away from the edge or disabling it often
14353 * results in a pipe underrun, and often that can lead to
14354 * dead pipe (constant underrun reported, and it scans
14355 * out just a solid color). To recover from that, the
14356 * display power well must be turned off and on again.
14357 * Refuse the put the cursor into that compromised position.
14358 */
14359 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14360 state->visible && state->base.crtc_x < 0) {
14361 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14362 return -EINVAL;
14363 }
14364
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014365 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014366}
14367
Matt Roperf4a2cf22014-12-01 15:40:12 -080014368static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014369intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014370 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014371{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014372 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14373
14374 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014375 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014376}
14377
14378static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014379intel_update_cursor_plane(struct drm_plane *plane,
14380 const struct intel_crtc_state *crtc_state,
14381 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014382{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014383 struct drm_crtc *crtc = crtc_state->base.crtc;
14384 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014385 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014386 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014387 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014388
Matt Roperf4a2cf22014-12-01 15:40:12 -080014389 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014390 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014391 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014392 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014393 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014394 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014395
Gustavo Padovana912f122014-12-01 15:40:10 -080014396 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014397 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014398}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014399
Matt Roper3d7d6512014-06-10 08:28:13 -070014400static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14401 int pipe)
14402{
14403 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014404 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014405
14406 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14407 if (cursor == NULL)
14408 return NULL;
14409
Matt Roper8e7d6882015-01-21 16:35:41 -080014410 state = intel_create_plane_state(&cursor->base);
14411 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014412 kfree(cursor);
14413 return NULL;
14414 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014415 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014416
Matt Roper3d7d6512014-06-10 08:28:13 -070014417 cursor->can_scale = false;
14418 cursor->max_downscale = 1;
14419 cursor->pipe = pipe;
14420 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014421 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014422 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014423 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014424 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014425
14426 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014427 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014428 intel_cursor_formats,
14429 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014430 DRM_PLANE_TYPE_CURSOR, NULL);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014431
14432 if (INTEL_INFO(dev)->gen >= 4) {
14433 if (!dev->mode_config.rotation_property)
14434 dev->mode_config.rotation_property =
14435 drm_mode_create_rotation_property(dev,
14436 BIT(DRM_ROTATE_0) |
14437 BIT(DRM_ROTATE_180));
14438 if (dev->mode_config.rotation_property)
14439 drm_object_attach_property(&cursor->base.base,
14440 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014441 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014442 }
14443
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014444 if (INTEL_INFO(dev)->gen >=9)
14445 state->scaler_id = -1;
14446
Matt Roperea2c67b2014-12-23 10:41:52 -080014447 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14448
Matt Roper3d7d6512014-06-10 08:28:13 -070014449 return &cursor->base;
14450}
14451
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014452static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14453 struct intel_crtc_state *crtc_state)
14454{
14455 int i;
14456 struct intel_scaler *intel_scaler;
14457 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14458
14459 for (i = 0; i < intel_crtc->num_scalers; i++) {
14460 intel_scaler = &scaler_state->scalers[i];
14461 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014462 intel_scaler->mode = PS_SCALER_MODE_DYN;
14463 }
14464
14465 scaler_state->scaler_id = -1;
14466}
14467
Hannes Ederb358d0a2008-12-18 21:18:47 +010014468static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014469{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014470 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014471 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014472 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014473 struct drm_plane *primary = NULL;
14474 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014475 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014476
Daniel Vetter955382f2013-09-19 14:05:45 +020014477 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014478 if (intel_crtc == NULL)
14479 return;
14480
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014481 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14482 if (!crtc_state)
14483 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014484 intel_crtc->config = crtc_state;
14485 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014486 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014487
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014488 /* initialize shared scalers */
14489 if (INTEL_INFO(dev)->gen >= 9) {
14490 if (pipe == PIPE_C)
14491 intel_crtc->num_scalers = 1;
14492 else
14493 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14494
14495 skl_init_scalers(dev, intel_crtc, crtc_state);
14496 }
14497
Matt Roper465c1202014-05-29 08:06:54 -070014498 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014499 if (!primary)
14500 goto fail;
14501
14502 cursor = intel_cursor_plane_create(dev, pipe);
14503 if (!cursor)
14504 goto fail;
14505
Matt Roper465c1202014-05-29 08:06:54 -070014506 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014507 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014508 if (ret)
14509 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014510
14511 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014512 for (i = 0; i < 256; i++) {
14513 intel_crtc->lut_r[i] = i;
14514 intel_crtc->lut_g[i] = i;
14515 intel_crtc->lut_b[i] = i;
14516 }
14517
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014518 /*
14519 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014520 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014521 */
Jesse Barnes80824002009-09-10 15:28:06 -070014522 intel_crtc->pipe = pipe;
14523 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014524 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014525 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014526 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014527 }
14528
Chris Wilson4b0e3332014-05-30 16:35:26 +030014529 intel_crtc->cursor_base = ~0;
14530 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014531 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014532
Ville Syrjälä852eb002015-06-24 22:00:07 +030014533 intel_crtc->wm.cxsr_allowed = true;
14534
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014535 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14536 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14537 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14538 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14539
Jesse Barnes79e53942008-11-07 14:24:08 -080014540 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014541
14542 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014543 return;
14544
14545fail:
14546 if (primary)
14547 drm_plane_cleanup(primary);
14548 if (cursor)
14549 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014550 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014551 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014552}
14553
Jesse Barnes752aa882013-10-31 18:55:49 +020014554enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14555{
14556 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014557 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014558
Rob Clark51fd3712013-11-19 12:10:12 -050014559 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014560
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014561 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014562 return INVALID_PIPE;
14563
14564 return to_intel_crtc(encoder->crtc)->pipe;
14565}
14566
Carl Worth08d7b3d2009-04-29 14:43:54 -070014567int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014568 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014569{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014570 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014571 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014572 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014573
Rob Clark7707e652014-07-17 23:30:04 -040014574 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014575
Rob Clark7707e652014-07-17 23:30:04 -040014576 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014577 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014578 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014579 }
14580
Rob Clark7707e652014-07-17 23:30:04 -040014581 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014582 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014583
Daniel Vetterc05422d2009-08-11 16:05:30 +020014584 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014585}
14586
Daniel Vetter66a92782012-07-12 20:08:18 +020014587static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014588{
Daniel Vetter66a92782012-07-12 20:08:18 +020014589 struct drm_device *dev = encoder->base.dev;
14590 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014591 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014592 int entry = 0;
14593
Damien Lespiaub2784e12014-08-05 11:29:37 +010014594 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014595 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014596 index_mask |= (1 << entry);
14597
Jesse Barnes79e53942008-11-07 14:24:08 -080014598 entry++;
14599 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014600
Jesse Barnes79e53942008-11-07 14:24:08 -080014601 return index_mask;
14602}
14603
Chris Wilson4d302442010-12-14 19:21:29 +000014604static bool has_edp_a(struct drm_device *dev)
14605{
14606 struct drm_i915_private *dev_priv = dev->dev_private;
14607
14608 if (!IS_MOBILE(dev))
14609 return false;
14610
14611 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14612 return false;
14613
Damien Lespiaue3589902014-02-07 19:12:50 +000014614 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014615 return false;
14616
14617 return true;
14618}
14619
Jesse Barnes84b4e042014-06-25 08:24:29 -070014620static bool intel_crt_present(struct drm_device *dev)
14621{
14622 struct drm_i915_private *dev_priv = dev->dev_private;
14623
Damien Lespiau884497e2013-12-03 13:56:23 +000014624 if (INTEL_INFO(dev)->gen >= 9)
14625 return false;
14626
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014627 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014628 return false;
14629
14630 if (IS_CHERRYVIEW(dev))
14631 return false;
14632
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014633 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14634 return false;
14635
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014636 /* DDI E can't be used if DDI A requires 4 lanes */
14637 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14638 return false;
14639
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014640 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014641 return false;
14642
14643 return true;
14644}
14645
Jesse Barnes79e53942008-11-07 14:24:08 -080014646static void intel_setup_outputs(struct drm_device *dev)
14647{
Eric Anholt725e30a2009-01-22 13:01:02 -080014648 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014649 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014650 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014651
Daniel Vetterc9093352013-06-06 22:22:47 +020014652 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014653
Jesse Barnes84b4e042014-06-25 08:24:29 -070014654 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014655 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014656
Vandana Kannanc776eb22014-08-19 12:05:01 +053014657 if (IS_BROXTON(dev)) {
14658 /*
14659 * FIXME: Broxton doesn't support port detection via the
14660 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14661 * detect the ports.
14662 */
14663 intel_ddi_init(dev, PORT_A);
14664 intel_ddi_init(dev, PORT_B);
14665 intel_ddi_init(dev, PORT_C);
14666 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014667 int found;
14668
Jesse Barnesde31fac2015-03-06 15:53:32 -080014669 /*
14670 * Haswell uses DDI functions to detect digital outputs.
14671 * On SKL pre-D0 the strap isn't connected, so we assume
14672 * it's there.
14673 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014674 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014675 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014676 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014677 intel_ddi_init(dev, PORT_A);
14678
14679 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14680 * register */
14681 found = I915_READ(SFUSE_STRAP);
14682
14683 if (found & SFUSE_STRAP_DDIB_DETECTED)
14684 intel_ddi_init(dev, PORT_B);
14685 if (found & SFUSE_STRAP_DDIC_DETECTED)
14686 intel_ddi_init(dev, PORT_C);
14687 if (found & SFUSE_STRAP_DDID_DETECTED)
14688 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014689 /*
14690 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14691 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014692 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014693 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14694 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14695 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14696 intel_ddi_init(dev, PORT_E);
14697
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014698 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014699 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014700 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014701
14702 if (has_edp_a(dev))
14703 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014704
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014705 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014706 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014707 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014708 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014709 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014710 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014711 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014712 }
14713
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014714 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014715 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014716
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014717 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014718 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014719
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014720 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014721 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014722
Daniel Vetter270b3042012-10-27 15:52:05 +020014723 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014724 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014725 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014726 /*
14727 * The DP_DETECTED bit is the latched state of the DDC
14728 * SDA pin at boot. However since eDP doesn't require DDC
14729 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14730 * eDP ports may have been muxed to an alternate function.
14731 * Thus we can't rely on the DP_DETECTED bit alone to detect
14732 * eDP ports. Consult the VBT as well as DP_DETECTED to
14733 * detect eDP ports.
14734 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014735 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014736 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014737 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14738 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014739 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014740 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014741
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014742 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014743 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014744 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14745 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014746 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014747 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014748
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014749 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014750 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014751 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14752 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14753 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14754 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014755 }
14756
Jani Nikula3cfca972013-08-27 15:12:26 +030014757 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014758 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014759 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014760
Paulo Zanonie2debe92013-02-18 19:00:27 -030014761 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014762 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014763 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014764 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014765 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014766 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014767 }
Ma Ling27185ae2009-08-24 13:50:23 +080014768
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014769 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014770 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014771 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014772
14773 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014774
Paulo Zanonie2debe92013-02-18 19:00:27 -030014775 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014776 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014777 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014778 }
Ma Ling27185ae2009-08-24 13:50:23 +080014779
Paulo Zanonie2debe92013-02-18 19:00:27 -030014780 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014781
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014782 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014783 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014784 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014785 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014786 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014787 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014788 }
Ma Ling27185ae2009-08-24 13:50:23 +080014789
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014790 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014791 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014792 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014793 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014794 intel_dvo_init(dev);
14795
Zhenyu Wang103a1962009-11-27 11:44:36 +080014796 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014797 intel_tv_init(dev);
14798
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014799 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014800
Damien Lespiaub2784e12014-08-05 11:29:37 +010014801 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014802 encoder->base.possible_crtcs = encoder->crtc_mask;
14803 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014804 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014805 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014806
Paulo Zanonidde86e22012-12-01 12:04:25 -020014807 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014808
14809 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014810}
14811
14812static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14813{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014814 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014815 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014816
Daniel Vetteref2d6332014-02-10 18:00:38 +010014817 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014818 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014819 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014820 drm_gem_object_unreference(&intel_fb->obj->base);
14821 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014822 kfree(intel_fb);
14823}
14824
14825static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014826 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014827 unsigned int *handle)
14828{
14829 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014830 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014831
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014832 if (obj->userptr.mm) {
14833 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14834 return -EINVAL;
14835 }
14836
Chris Wilson05394f32010-11-08 19:18:58 +000014837 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014838}
14839
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014840static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14841 struct drm_file *file,
14842 unsigned flags, unsigned color,
14843 struct drm_clip_rect *clips,
14844 unsigned num_clips)
14845{
14846 struct drm_device *dev = fb->dev;
14847 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14848 struct drm_i915_gem_object *obj = intel_fb->obj;
14849
14850 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014851 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014852 mutex_unlock(&dev->struct_mutex);
14853
14854 return 0;
14855}
14856
Jesse Barnes79e53942008-11-07 14:24:08 -080014857static const struct drm_framebuffer_funcs intel_fb_funcs = {
14858 .destroy = intel_user_framebuffer_destroy,
14859 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014860 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014861};
14862
Damien Lespiaub3218032015-02-27 11:15:18 +000014863static
14864u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14865 uint32_t pixel_format)
14866{
14867 u32 gen = INTEL_INFO(dev)->gen;
14868
14869 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014870 int cpp = drm_format_plane_cpp(pixel_format, 0);
14871
Damien Lespiaub3218032015-02-27 11:15:18 +000014872 /* "The stride in bytes must not exceed the of the size of 8K
14873 * pixels and 32K bytes."
14874 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014875 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014876 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014877 return 32*1024;
14878 } else if (gen >= 4) {
14879 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14880 return 16*1024;
14881 else
14882 return 32*1024;
14883 } else if (gen >= 3) {
14884 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14885 return 8*1024;
14886 else
14887 return 16*1024;
14888 } else {
14889 /* XXX DSPC is limited to 4k tiled */
14890 return 8*1024;
14891 }
14892}
14893
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014894static int intel_framebuffer_init(struct drm_device *dev,
14895 struct intel_framebuffer *intel_fb,
14896 struct drm_mode_fb_cmd2 *mode_cmd,
14897 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014898{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014899 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014900 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014901 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014902 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014903
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014904 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14905
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014906 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14907 /* Enforce that fb modifier and tiling mode match, but only for
14908 * X-tiled. This is needed for FBC. */
14909 if (!!(obj->tiling_mode == I915_TILING_X) !=
14910 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14911 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14912 return -EINVAL;
14913 }
14914 } else {
14915 if (obj->tiling_mode == I915_TILING_X)
14916 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14917 else if (obj->tiling_mode == I915_TILING_Y) {
14918 DRM_DEBUG("No Y tiling for legacy addfb\n");
14919 return -EINVAL;
14920 }
14921 }
14922
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014923 /* Passed in modifier sanity checking. */
14924 switch (mode_cmd->modifier[0]) {
14925 case I915_FORMAT_MOD_Y_TILED:
14926 case I915_FORMAT_MOD_Yf_TILED:
14927 if (INTEL_INFO(dev)->gen < 9) {
14928 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14929 mode_cmd->modifier[0]);
14930 return -EINVAL;
14931 }
14932 case DRM_FORMAT_MOD_NONE:
14933 case I915_FORMAT_MOD_X_TILED:
14934 break;
14935 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014936 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14937 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014938 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014939 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014940
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014941 stride_alignment = intel_fb_stride_alignment(dev_priv,
14942 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014943 mode_cmd->pixel_format);
14944 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14945 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14946 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014947 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014948 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014949
Damien Lespiaub3218032015-02-27 11:15:18 +000014950 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14951 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014952 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014953 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14954 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014955 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014956 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014957 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014958 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014959
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014960 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014961 mode_cmd->pitches[0] != obj->stride) {
14962 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14963 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014964 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014965 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014966
Ville Syrjälä57779d02012-10-31 17:50:14 +020014967 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014968 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014969 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014970 case DRM_FORMAT_RGB565:
14971 case DRM_FORMAT_XRGB8888:
14972 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014973 break;
14974 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014975 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014976 DRM_DEBUG("unsupported pixel format: %s\n",
14977 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014978 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014979 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014980 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014981 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014982 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14983 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014984 DRM_DEBUG("unsupported pixel format: %s\n",
14985 drm_get_format_name(mode_cmd->pixel_format));
14986 return -EINVAL;
14987 }
14988 break;
14989 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014990 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014991 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014992 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014993 DRM_DEBUG("unsupported pixel format: %s\n",
14994 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014995 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014996 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014997 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014998 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014999 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010015000 DRM_DEBUG("unsupported pixel format: %s\n",
15001 drm_get_format_name(mode_cmd->pixel_format));
15002 return -EINVAL;
15003 }
15004 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020015005 case DRM_FORMAT_YUYV:
15006 case DRM_FORMAT_UYVY:
15007 case DRM_FORMAT_YVYU:
15008 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015009 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015010 DRM_DEBUG("unsupported pixel format: %s\n",
15011 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015012 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015013 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015014 break;
15015 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015016 DRM_DEBUG("unsupported pixel format: %s\n",
15017 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010015018 return -EINVAL;
15019 }
15020
Ville Syrjälä90f9a332012-10-31 17:50:19 +020015021 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15022 if (mode_cmd->offsets[0] != 0)
15023 return -EINVAL;
15024
Damien Lespiauec2c9812015-01-20 12:51:45 +000015025 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000015026 mode_cmd->pixel_format,
15027 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020015028 /* FIXME drm helper for size checks (especially planar formats)? */
15029 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15030 return -EINVAL;
15031
Daniel Vetterc7d73f62012-12-13 23:38:38 +010015032 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15033 intel_fb->obj = obj;
15034
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020015035 intel_fill_fb_info(dev_priv, &intel_fb->base);
15036
Jesse Barnes79e53942008-11-07 14:24:08 -080015037 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15038 if (ret) {
15039 DRM_ERROR("framebuffer init failed %d\n", ret);
15040 return ret;
15041 }
15042
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020015043 intel_fb->obj->framebuffer_references++;
15044
Jesse Barnes79e53942008-11-07 14:24:08 -080015045 return 0;
15046}
15047
Jesse Barnes79e53942008-11-07 14:24:08 -080015048static struct drm_framebuffer *
15049intel_user_framebuffer_create(struct drm_device *dev,
15050 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020015051 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015052{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015053 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015054 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015055 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015056
Jesse Barnes308e5bc2011-11-14 14:51:28 -080015057 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015058 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000015059 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015060 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015061
Daniel Vetter92907cb2015-11-23 09:04:05 +010015062 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015063 if (IS_ERR(fb))
15064 drm_gem_object_unreference_unlocked(&obj->base);
15065
15066 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080015067}
15068
Daniel Vetter06957262015-08-10 13:34:08 +020015069#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020015070static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020015071{
15072}
15073#endif
15074
Jesse Barnes79e53942008-11-07 14:24:08 -080015075static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080015076 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020015077 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080015078 .atomic_check = intel_atomic_check,
15079 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020015080 .atomic_state_alloc = intel_atomic_state_alloc,
15081 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080015082};
15083
Jesse Barnese70236a2009-09-21 10:42:27 -070015084/* Set up chip specific display functions */
15085static void intel_init_display(struct drm_device *dev)
15086{
15087 struct drm_i915_private *dev_priv = dev->dev_private;
15088
Daniel Vetteree9300b2013-06-03 22:40:22 +020015089 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
15090 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030015091 else if (IS_CHERRYVIEW(dev))
15092 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020015093 else if (IS_VALLEYVIEW(dev))
15094 dev_priv->display.find_dpll = vlv_find_best_dpll;
15095 else if (IS_PINEVIEW(dev))
15096 dev_priv->display.find_dpll = pnv_find_best_dpll;
15097 else
15098 dev_priv->display.find_dpll = i9xx_find_best_dpll;
15099
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015100 if (INTEL_INFO(dev)->gen >= 9) {
15101 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015102 dev_priv->display.get_initial_plane_config =
15103 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015104 dev_priv->display.crtc_compute_clock =
15105 haswell_crtc_compute_clock;
15106 dev_priv->display.crtc_enable = haswell_crtc_enable;
15107 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015108 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015109 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015110 dev_priv->display.get_initial_plane_config =
15111 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020015112 dev_priv->display.crtc_compute_clock =
15113 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020015114 dev_priv->display.crtc_enable = haswell_crtc_enable;
15115 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030015116 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015117 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015118 dev_priv->display.get_initial_plane_config =
15119 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015120 dev_priv->display.crtc_compute_clock =
15121 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015122 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15123 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Wayne Boyer666a4532015-12-09 12:29:35 -080015124 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015125 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015126 dev_priv->display.get_initial_plane_config =
15127 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015128 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015129 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15130 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015131 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015132 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015133 dev_priv->display.get_initial_plane_config =
15134 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015135 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015136 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15137 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015138 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015139
Jesse Barnese70236a2009-09-21 10:42:27 -070015140 /* Returns the core display clock speed */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070015141 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015142 dev_priv->display.get_display_clock_speed =
15143 skylake_get_display_clock_speed;
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015144 else if (IS_BROXTON(dev))
15145 dev_priv->display.get_display_clock_speed =
15146 broxton_get_display_clock_speed;
Ville Syrjälä1652d192015-03-31 14:12:01 +030015147 else if (IS_BROADWELL(dev))
15148 dev_priv->display.get_display_clock_speed =
15149 broadwell_get_display_clock_speed;
15150 else if (IS_HASWELL(dev))
15151 dev_priv->display.get_display_clock_speed =
15152 haswell_get_display_clock_speed;
Wayne Boyer666a4532015-12-09 12:29:35 -080015153 else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015154 dev_priv->display.get_display_clock_speed =
15155 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015156 else if (IS_GEN5(dev))
15157 dev_priv->display.get_display_clock_speed =
15158 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030015159 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030015160 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070015161 dev_priv->display.get_display_clock_speed =
15162 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030015163 else if (IS_GM45(dev))
15164 dev_priv->display.get_display_clock_speed =
15165 gm45_get_display_clock_speed;
15166 else if (IS_CRESTLINE(dev))
15167 dev_priv->display.get_display_clock_speed =
15168 i965gm_get_display_clock_speed;
15169 else if (IS_PINEVIEW(dev))
15170 dev_priv->display.get_display_clock_speed =
15171 pnv_get_display_clock_speed;
15172 else if (IS_G33(dev) || IS_G4X(dev))
15173 dev_priv->display.get_display_clock_speed =
15174 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070015175 else if (IS_I915G(dev))
15176 dev_priv->display.get_display_clock_speed =
15177 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020015178 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070015179 dev_priv->display.get_display_clock_speed =
15180 i9xx_misc_get_display_clock_speed;
15181 else if (IS_I915GM(dev))
15182 dev_priv->display.get_display_clock_speed =
15183 i915gm_get_display_clock_speed;
15184 else if (IS_I865G(dev))
15185 dev_priv->display.get_display_clock_speed =
15186 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020015187 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070015188 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015189 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015190 else { /* 830 */
15191 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015192 dev_priv->display.get_display_clock_speed =
15193 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015194 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015195
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015196 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015197 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015198 } else if (IS_GEN6(dev)) {
15199 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015200 } else if (IS_IVYBRIDGE(dev)) {
15201 /* FIXME: detect B0+ stepping and use auto training */
15202 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030015203 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015204 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015205 if (IS_BROADWELL(dev)) {
15206 dev_priv->display.modeset_commit_cdclk =
15207 broadwell_modeset_commit_cdclk;
15208 dev_priv->display.modeset_calc_cdclk =
15209 broadwell_modeset_calc_cdclk;
15210 }
Wayne Boyer666a4532015-12-09 12:29:35 -080015211 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015212 dev_priv->display.modeset_commit_cdclk =
15213 valleyview_modeset_commit_cdclk;
15214 dev_priv->display.modeset_calc_cdclk =
15215 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053015216 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015217 dev_priv->display.modeset_commit_cdclk =
15218 broxton_modeset_commit_cdclk;
15219 dev_priv->display.modeset_calc_cdclk =
15220 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015221 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015222
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015223 switch (INTEL_INFO(dev)->gen) {
15224 case 2:
15225 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15226 break;
15227
15228 case 3:
15229 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15230 break;
15231
15232 case 4:
15233 case 5:
15234 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15235 break;
15236
15237 case 6:
15238 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15239 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015240 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070015241 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015242 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15243 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000015244 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000015245 /* Drop through - unsupported since execlist only. */
15246 default:
15247 /* Default just returns -ENODEV to indicate unsupported */
15248 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015249 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020015250
Ville Syrjäläe39b9992014-09-04 14:53:14 +030015251 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070015252}
15253
Jesse Barnesb690e962010-07-19 13:53:12 -070015254/*
15255 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15256 * resume, or other times. This quirk makes sure that's the case for
15257 * affected systems.
15258 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015259static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015260{
15261 struct drm_i915_private *dev_priv = dev->dev_private;
15262
15263 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015264 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015265}
15266
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015267static void quirk_pipeb_force(struct drm_device *dev)
15268{
15269 struct drm_i915_private *dev_priv = dev->dev_private;
15270
15271 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15272 DRM_INFO("applying pipe b force quirk\n");
15273}
15274
Keith Packard435793d2011-07-12 14:56:22 -070015275/*
15276 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15277 */
15278static void quirk_ssc_force_disable(struct drm_device *dev)
15279{
15280 struct drm_i915_private *dev_priv = dev->dev_private;
15281 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015282 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015283}
15284
Carsten Emde4dca20e2012-03-15 15:56:26 +010015285/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015286 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15287 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015288 */
15289static void quirk_invert_brightness(struct drm_device *dev)
15290{
15291 struct drm_i915_private *dev_priv = dev->dev_private;
15292 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015293 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015294}
15295
Scot Doyle9c72cc62014-07-03 23:27:50 +000015296/* Some VBT's incorrectly indicate no backlight is present */
15297static void quirk_backlight_present(struct drm_device *dev)
15298{
15299 struct drm_i915_private *dev_priv = dev->dev_private;
15300 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15301 DRM_INFO("applying backlight present quirk\n");
15302}
15303
Jesse Barnesb690e962010-07-19 13:53:12 -070015304struct intel_quirk {
15305 int device;
15306 int subsystem_vendor;
15307 int subsystem_device;
15308 void (*hook)(struct drm_device *dev);
15309};
15310
Egbert Eich5f85f172012-10-14 15:46:38 +020015311/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15312struct intel_dmi_quirk {
15313 void (*hook)(struct drm_device *dev);
15314 const struct dmi_system_id (*dmi_id_list)[];
15315};
15316
15317static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15318{
15319 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15320 return 1;
15321}
15322
15323static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15324 {
15325 .dmi_id_list = &(const struct dmi_system_id[]) {
15326 {
15327 .callback = intel_dmi_reverse_brightness,
15328 .ident = "NCR Corporation",
15329 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15330 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15331 },
15332 },
15333 { } /* terminating entry */
15334 },
15335 .hook = quirk_invert_brightness,
15336 },
15337};
15338
Ben Widawskyc43b5632012-04-16 14:07:40 -070015339static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015340 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15341 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15342
Jesse Barnesb690e962010-07-19 13:53:12 -070015343 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15344 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15345
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015346 /* 830 needs to leave pipe A & dpll A up */
15347 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15348
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015349 /* 830 needs to leave pipe B & dpll B up */
15350 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15351
Keith Packard435793d2011-07-12 14:56:22 -070015352 /* Lenovo U160 cannot use SSC on LVDS */
15353 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015354
15355 /* Sony Vaio Y cannot use SSC on LVDS */
15356 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015357
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015358 /* Acer Aspire 5734Z must invert backlight brightness */
15359 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15360
15361 /* Acer/eMachines G725 */
15362 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15363
15364 /* Acer/eMachines e725 */
15365 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15366
15367 /* Acer/Packard Bell NCL20 */
15368 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15369
15370 /* Acer Aspire 4736Z */
15371 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015372
15373 /* Acer Aspire 5336 */
15374 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015375
15376 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15377 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015378
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015379 /* Acer C720 Chromebook (Core i3 4005U) */
15380 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15381
jens steinb2a96012014-10-28 20:25:53 +010015382 /* Apple Macbook 2,1 (Core 2 T7400) */
15383 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15384
Jani Nikula1b9448b02015-11-05 11:49:59 +020015385 /* Apple Macbook 4,1 */
15386 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15387
Scot Doyled4967d82014-07-03 23:27:52 +000015388 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15389 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015390
15391 /* HP Chromebook 14 (Celeron 2955U) */
15392 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015393
15394 /* Dell Chromebook 11 */
15395 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015396
15397 /* Dell Chromebook 11 (2015 version) */
15398 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015399};
15400
15401static void intel_init_quirks(struct drm_device *dev)
15402{
15403 struct pci_dev *d = dev->pdev;
15404 int i;
15405
15406 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15407 struct intel_quirk *q = &intel_quirks[i];
15408
15409 if (d->device == q->device &&
15410 (d->subsystem_vendor == q->subsystem_vendor ||
15411 q->subsystem_vendor == PCI_ANY_ID) &&
15412 (d->subsystem_device == q->subsystem_device ||
15413 q->subsystem_device == PCI_ANY_ID))
15414 q->hook(dev);
15415 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015416 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15417 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15418 intel_dmi_quirks[i].hook(dev);
15419 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015420}
15421
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015422/* Disable the VGA plane that we never use */
15423static void i915_disable_vga(struct drm_device *dev)
15424{
15425 struct drm_i915_private *dev_priv = dev->dev_private;
15426 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015427 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015428
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015429 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015430 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015431 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015432 sr1 = inb(VGA_SR_DATA);
15433 outb(sr1 | 1<<5, VGA_SR_DATA);
15434 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15435 udelay(300);
15436
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015437 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015438 POSTING_READ(vga_reg);
15439}
15440
Daniel Vetterf8175862012-04-10 15:50:11 +020015441void intel_modeset_init_hw(struct drm_device *dev)
15442{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015443 struct drm_i915_private *dev_priv = dev->dev_private;
15444
Ville Syrjäläb6283052015-06-03 15:45:07 +030015445 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015446
15447 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15448
Daniel Vetterf8175862012-04-10 15:50:11 +020015449 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015450 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015451}
15452
Matt Roperd93c0372015-12-03 11:37:41 -080015453/*
15454 * Calculate what we think the watermarks should be for the state we've read
15455 * out of the hardware and then immediately program those watermarks so that
15456 * we ensure the hardware settings match our internal state.
15457 *
15458 * We can calculate what we think WM's should be by creating a duplicate of the
15459 * current state (which was constructed during hardware readout) and running it
15460 * through the atomic check code to calculate new watermark values in the
15461 * state object.
15462 */
15463static void sanitize_watermarks(struct drm_device *dev)
15464{
15465 struct drm_i915_private *dev_priv = to_i915(dev);
15466 struct drm_atomic_state *state;
15467 struct drm_crtc *crtc;
15468 struct drm_crtc_state *cstate;
15469 struct drm_modeset_acquire_ctx ctx;
15470 int ret;
15471 int i;
15472
15473 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015474 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015475 return;
15476
15477 /*
15478 * We need to hold connection_mutex before calling duplicate_state so
15479 * that the connector loop is protected.
15480 */
15481 drm_modeset_acquire_init(&ctx, 0);
15482retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015483 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015484 if (ret == -EDEADLK) {
15485 drm_modeset_backoff(&ctx);
15486 goto retry;
15487 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015488 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015489 }
15490
15491 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15492 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015493 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015494
Matt Ropered4a6a72016-02-23 17:20:13 -080015495 /*
15496 * Hardware readout is the only time we don't want to calculate
15497 * intermediate watermarks (since we don't trust the current
15498 * watermarks).
15499 */
15500 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15501
Matt Roperd93c0372015-12-03 11:37:41 -080015502 ret = intel_atomic_check(dev, state);
15503 if (ret) {
15504 /*
15505 * If we fail here, it means that the hardware appears to be
15506 * programmed in a way that shouldn't be possible, given our
15507 * understanding of watermark requirements. This might mean a
15508 * mistake in the hardware readout code or a mistake in the
15509 * watermark calculations for a given platform. Raise a WARN
15510 * so that this is noticeable.
15511 *
15512 * If this actually happens, we'll have to just leave the
15513 * BIOS-programmed watermarks untouched and hope for the best.
15514 */
15515 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015516 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015517 }
15518
15519 /* Write calculated watermark values back */
15520 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15521 for_each_crtc_in_state(state, crtc, cstate, i) {
15522 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15523
Matt Ropered4a6a72016-02-23 17:20:13 -080015524 cs->wm.need_postvbl_update = true;
15525 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015526 }
15527
15528 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015529fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015530 drm_modeset_drop_locks(&ctx);
15531 drm_modeset_acquire_fini(&ctx);
15532}
15533
Jesse Barnes79e53942008-11-07 14:24:08 -080015534void intel_modeset_init(struct drm_device *dev)
15535{
Jesse Barnes652c3932009-08-17 13:31:43 -070015536 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015537 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015538 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015539 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015540
15541 drm_mode_config_init(dev);
15542
15543 dev->mode_config.min_width = 0;
15544 dev->mode_config.min_height = 0;
15545
Dave Airlie019d96c2011-09-29 16:20:42 +010015546 dev->mode_config.preferred_depth = 24;
15547 dev->mode_config.prefer_shadow = 1;
15548
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015549 dev->mode_config.allow_fb_modifiers = true;
15550
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015551 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015552
Jesse Barnesb690e962010-07-19 13:53:12 -070015553 intel_init_quirks(dev);
15554
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015555 intel_init_pm(dev);
15556
Ben Widawskye3c74752013-04-05 13:12:39 -070015557 if (INTEL_INFO(dev)->num_pipes == 0)
15558 return;
15559
Lukas Wunner69f92f62015-07-15 13:57:35 +020015560 /*
15561 * There may be no VBT; and if the BIOS enabled SSC we can
15562 * just keep using it to avoid unnecessary flicker. Whereas if the
15563 * BIOS isn't using it, don't assume it will work even if the VBT
15564 * indicates as much.
15565 */
15566 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15567 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15568 DREF_SSC1_ENABLE);
15569
15570 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15571 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15572 bios_lvds_use_ssc ? "en" : "dis",
15573 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15574 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15575 }
15576 }
15577
Jesse Barnese70236a2009-09-21 10:42:27 -070015578 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015579 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015580
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015581 if (IS_GEN2(dev)) {
15582 dev->mode_config.max_width = 2048;
15583 dev->mode_config.max_height = 2048;
15584 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015585 dev->mode_config.max_width = 4096;
15586 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015587 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015588 dev->mode_config.max_width = 8192;
15589 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015590 }
Damien Lespiau068be562014-03-28 14:17:49 +000015591
Ville Syrjälädc41c152014-08-13 11:57:05 +030015592 if (IS_845G(dev) || IS_I865G(dev)) {
15593 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15594 dev->mode_config.cursor_height = 1023;
15595 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015596 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15597 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15598 } else {
15599 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15600 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15601 }
15602
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015603 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015604
Zhao Yakui28c97732009-10-09 11:39:41 +080015605 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015606 INTEL_INFO(dev)->num_pipes,
15607 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015608
Damien Lespiau055e3932014-08-18 13:49:10 +010015609 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015610 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015611 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015612 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015613 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015614 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015615 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015616 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015617 }
15618
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015619 intel_update_czclk(dev_priv);
15620 intel_update_cdclk(dev);
15621
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015622 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015623
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015624 /* Just disable it once at startup */
15625 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015626 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015627
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015628 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015629 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015630 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015631
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015632 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015633 struct intel_initial_plane_config plane_config = {};
15634
Jesse Barnes46f297f2014-03-07 08:57:48 -080015635 if (!crtc->active)
15636 continue;
15637
Jesse Barnes46f297f2014-03-07 08:57:48 -080015638 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015639 * Note that reserving the BIOS fb up front prevents us
15640 * from stuffing other stolen allocations like the ring
15641 * on top. This prevents some ugliness at boot time, and
15642 * can even allow for smooth boot transitions if the BIOS
15643 * fb is large enough for the active pipe configuration.
15644 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015645 dev_priv->display.get_initial_plane_config(crtc,
15646 &plane_config);
15647
15648 /*
15649 * If the fb is shared between multiple heads, we'll
15650 * just get the first one.
15651 */
15652 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015653 }
Matt Roperd93c0372015-12-03 11:37:41 -080015654
15655 /*
15656 * Make sure hardware watermarks really match the state we read out.
15657 * Note that we need to do this after reconstructing the BIOS fb's
15658 * since the watermark calculation done here will use pstate->fb.
15659 */
15660 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015661}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015662
Daniel Vetter7fad7982012-07-04 17:51:47 +020015663static void intel_enable_pipe_a(struct drm_device *dev)
15664{
15665 struct intel_connector *connector;
15666 struct drm_connector *crt = NULL;
15667 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015668 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015669
15670 /* We can't just switch on the pipe A, we need to set things up with a
15671 * proper mode and output configuration. As a gross hack, enable pipe A
15672 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015673 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015674 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15675 crt = &connector->base;
15676 break;
15677 }
15678 }
15679
15680 if (!crt)
15681 return;
15682
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015683 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015684 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015685}
15686
Daniel Vetterfa555832012-10-10 23:14:00 +020015687static bool
15688intel_check_plane_mapping(struct intel_crtc *crtc)
15689{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015690 struct drm_device *dev = crtc->base.dev;
15691 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015692 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015693
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015694 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015695 return true;
15696
Ville Syrjälä649636e2015-09-22 19:50:01 +030015697 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015698
15699 if ((val & DISPLAY_PLANE_ENABLE) &&
15700 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15701 return false;
15702
15703 return true;
15704}
15705
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015706static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15707{
15708 struct drm_device *dev = crtc->base.dev;
15709 struct intel_encoder *encoder;
15710
15711 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15712 return true;
15713
15714 return false;
15715}
15716
Ville Syrjälädd756192016-02-17 21:28:45 +020015717static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15718{
15719 struct drm_device *dev = encoder->base.dev;
15720 struct intel_connector *connector;
15721
15722 for_each_connector_on_encoder(dev, &encoder->base, connector)
15723 return true;
15724
15725 return false;
15726}
15727
Daniel Vetter24929352012-07-02 20:28:59 +020015728static void intel_sanitize_crtc(struct intel_crtc *crtc)
15729{
15730 struct drm_device *dev = crtc->base.dev;
15731 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015732 i915_reg_t reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015733
Daniel Vetter24929352012-07-02 20:28:59 +020015734 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter24929352012-07-02 20:28:59 +020015735 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15736
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015737 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015738 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015739 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015740 struct intel_plane *plane;
15741
Daniel Vetter96256042015-02-13 21:03:42 +010015742 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015743
15744 /* Disable everything but the primary plane */
15745 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15746 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15747 continue;
15748
15749 plane->disable_plane(&plane->base, &crtc->base);
15750 }
Daniel Vetter96256042015-02-13 21:03:42 +010015751 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015752
Daniel Vetter24929352012-07-02 20:28:59 +020015753 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015754 * disable the crtc (and hence change the state) if it is wrong. Note
15755 * that gen4+ has a fixed plane -> pipe mapping. */
15756 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015757 bool plane;
15758
Daniel Vetter24929352012-07-02 20:28:59 +020015759 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15760 crtc->base.base.id);
15761
15762 /* Pipe has the wrong plane attached and the plane is active.
15763 * Temporarily change the plane mapping and disable everything
15764 * ... */
15765 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015766 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015767 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015768 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015769 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015770 }
Daniel Vetter24929352012-07-02 20:28:59 +020015771
Daniel Vetter7fad7982012-07-04 17:51:47 +020015772 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15773 crtc->pipe == PIPE_A && !crtc->active) {
15774 /* BIOS forgot to enable pipe A, this mostly happens after
15775 * resume. Force-enable the pipe to fix this, the update_dpms
15776 * call below we restore the pipe to the right state, but leave
15777 * the required bits on. */
15778 intel_enable_pipe_a(dev);
15779 }
15780
Daniel Vetter24929352012-07-02 20:28:59 +020015781 /* Adjust the state of the output pipe according to whether we
15782 * have active connectors/encoders. */
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015783 if (!intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015784 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015785
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015786 if (crtc->active != crtc->base.state->active) {
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015787 struct intel_encoder *encoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015788
15789 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015790 * functions or because of calls to intel_crtc_disable_noatomic,
15791 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015792 * pipe A quirk. */
15793 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15794 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015795 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015796 crtc->active ? "enabled" : "disabled");
15797
Maarten Lankhorst4be40c92015-07-14 13:45:32 +020015798 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015799 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015800 crtc->base.enabled = crtc->active;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015801 crtc->base.state->connector_mask = 0;
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015802 crtc->base.state->encoder_mask = 0;
Daniel Vetter24929352012-07-02 20:28:59 +020015803
15804 /* Because we only establish the connector -> encoder ->
15805 * crtc links if something is active, this means the
15806 * crtc is now deactivated. Break the links. connector
15807 * -> encoder links are only establish when things are
15808 * actually up, hence no need to break them. */
15809 WARN_ON(crtc->active);
15810
Maarten Lankhorst2d406bb2015-08-05 12:37:09 +020015811 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetter24929352012-07-02 20:28:59 +020015812 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015813 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015814
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015815 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015816 /*
15817 * We start out with underrun reporting disabled to avoid races.
15818 * For correct bookkeeping mark this on active crtcs.
15819 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015820 * Also on gmch platforms we dont have any hardware bits to
15821 * disable the underrun reporting. Which means we need to start
15822 * out with underrun reporting disabled also on inactive pipes,
15823 * since otherwise we'll complain about the garbage we read when
15824 * e.g. coming up after runtime pm.
15825 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015826 * No protection against concurrent access is required - at
15827 * worst a fifo underrun happens which also sets this to false.
15828 */
15829 crtc->cpu_fifo_underrun_disabled = true;
15830 crtc->pch_fifo_underrun_disabled = true;
15831 }
Daniel Vetter24929352012-07-02 20:28:59 +020015832}
15833
15834static void intel_sanitize_encoder(struct intel_encoder *encoder)
15835{
15836 struct intel_connector *connector;
15837 struct drm_device *dev = encoder->base.dev;
15838
15839 /* We need to check both for a crtc link (meaning that the
15840 * encoder is active and trying to read from a pipe) and the
15841 * pipe itself being active. */
15842 bool has_active_crtc = encoder->base.crtc &&
15843 to_intel_crtc(encoder->base.crtc)->active;
15844
Ville Syrjälädd756192016-02-17 21:28:45 +020015845 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015846 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15847 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015848 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015849
15850 /* Connector is active, but has no active pipe. This is
15851 * fallout from our resume register restoring. Disable
15852 * the encoder manually again. */
15853 if (encoder->base.crtc) {
15854 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15855 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015856 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015857 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015858 if (encoder->post_disable)
15859 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015860 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015861 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015862
15863 /* Inconsistent output/port/pipe state happens presumably due to
15864 * a bug in one of the get_hw_state functions. Or someplace else
15865 * in our code, like the register restore mess on resume. Clamp
15866 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015867 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015868 if (connector->encoder != encoder)
15869 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015870 connector->base.dpms = DRM_MODE_DPMS_OFF;
15871 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015872 }
15873 }
15874 /* Enabled encoders without active connectors will be fixed in
15875 * the crtc fixup. */
15876}
15877
Imre Deak04098752014-02-18 00:02:16 +020015878void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015879{
15880 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015881 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015882
Imre Deak04098752014-02-18 00:02:16 +020015883 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15884 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15885 i915_disable_vga(dev);
15886 }
15887}
15888
15889void i915_redisable_vga(struct drm_device *dev)
15890{
15891 struct drm_i915_private *dev_priv = dev->dev_private;
15892
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015893 /* This function can be called both from intel_modeset_setup_hw_state or
15894 * at a very early point in our resume sequence, where the power well
15895 * structures are not yet restored. Since this function is at a very
15896 * paranoid "someone might have enabled VGA while we were not looking"
15897 * level, just check if the power well is enabled instead of trying to
15898 * follow the "don't touch the power well if we don't need it" policy
15899 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015900 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015901 return;
15902
Imre Deak04098752014-02-18 00:02:16 +020015903 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015904
15905 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015906}
15907
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015908static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015909{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015910 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015911
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015912 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015913}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015914
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015915/* FIXME read out full plane state for all planes */
15916static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015917{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015918 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015919 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015920 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015921
Matt Roper19b8d382015-09-24 15:53:17 -070015922 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015923 primary_get_hw_state(to_intel_plane(primary));
15924
15925 if (plane_state->visible)
15926 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015927}
15928
Daniel Vetter30e984d2013-06-05 13:34:17 +020015929static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015930{
15931 struct drm_i915_private *dev_priv = dev->dev_private;
15932 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015933 struct intel_crtc *crtc;
15934 struct intel_encoder *encoder;
15935 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015936 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015937
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015938 dev_priv->active_crtcs = 0;
15939
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015940 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015941 struct intel_crtc_state *crtc_state = crtc->config;
15942 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015943
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015944 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15945 memset(crtc_state, 0, sizeof(*crtc_state));
15946 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015947
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015948 crtc_state->base.active = crtc_state->base.enable =
15949 dev_priv->display.get_pipe_config(crtc, crtc_state);
15950
15951 crtc->base.enabled = crtc_state->base.enable;
15952 crtc->active = crtc_state->base.active;
15953
15954 if (crtc_state->base.active) {
15955 dev_priv->active_crtcs |= 1 << crtc->pipe;
15956
15957 if (IS_BROADWELL(dev_priv)) {
15958 pixclk = ilk_pipe_pixel_rate(crtc_state);
15959
15960 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15961 if (crtc_state->ips_enabled)
15962 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15963 } else if (IS_VALLEYVIEW(dev_priv) ||
15964 IS_CHERRYVIEW(dev_priv) ||
15965 IS_BROXTON(dev_priv))
15966 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15967 else
15968 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15969 }
15970
15971 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015972
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015973 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015974
15975 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15976 crtc->base.base.id,
15977 crtc->active ? "enabled" : "disabled");
15978 }
15979
Daniel Vetter53589012013-06-05 13:34:16 +020015980 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15981 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15982
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015983 pll->on = pll->get_hw_state(dev_priv, pll,
15984 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015985 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015986 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015987 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015988 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015989 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015990 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015991 }
Daniel Vetter53589012013-06-05 13:34:16 +020015992 }
Daniel Vetter53589012013-06-05 13:34:16 +020015993
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015994 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015995 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015996
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015997 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015998 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015999 }
16000
Damien Lespiaub2784e12014-08-05 11:29:37 +010016001 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016002 pipe = 0;
16003
16004 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070016005 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16006 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016007 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020016008 } else {
16009 encoder->base.crtc = NULL;
16010 }
16011
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016012 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020016013 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016014 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016015 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016016 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020016017 }
16018
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016019 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016020 if (connector->get_hw_state(connector)) {
16021 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016022
16023 encoder = connector->encoder;
16024 connector->base.encoder = &encoder->base;
16025
16026 if (encoder->base.crtc &&
16027 encoder->base.crtc->state->active) {
16028 /*
16029 * This has to be done during hardware readout
16030 * because anything calling .crtc_disable may
16031 * rely on the connector_mask being accurate.
16032 */
16033 encoder->base.crtc->state->connector_mask |=
16034 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010016035 encoder->base.crtc->state->encoder_mask |=
16036 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016037 }
16038
Daniel Vetter24929352012-07-02 20:28:59 +020016039 } else {
16040 connector->base.dpms = DRM_MODE_DPMS_OFF;
16041 connector->base.encoder = NULL;
16042 }
16043 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16044 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030016045 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016046 connector->base.encoder ? "enabled" : "disabled");
16047 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016048
16049 for_each_intel_crtc(dev, crtc) {
16050 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16051
16052 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16053 if (crtc->base.state->active) {
16054 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16055 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16056 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16057
16058 /*
16059 * The initial mode needs to be set in order to keep
16060 * the atomic core happy. It wants a valid mode if the
16061 * crtc's enabled, so we do the above call.
16062 *
16063 * At this point some state updated by the connectors
16064 * in their ->detect() callback has not run yet, so
16065 * no recalculation can be done yet.
16066 *
16067 * Even if we could do a recalculation and modeset
16068 * right now it would cause a double modeset if
16069 * fbdev or userspace chooses a different initial mode.
16070 *
16071 * If that happens, someone indicated they wanted a
16072 * mode change, which means it's safe to do a full
16073 * recalculation.
16074 */
16075 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030016076
16077 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16078 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016079 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020016080
16081 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016082 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020016083}
16084
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016085/* Scan out the current hw modeset state,
16086 * and sanitizes it to the current state
16087 */
16088static void
16089intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020016090{
16091 struct drm_i915_private *dev_priv = dev->dev_private;
16092 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016093 struct intel_crtc *crtc;
16094 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020016095 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016096
16097 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016098
16099 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010016100 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016101 intel_sanitize_encoder(encoder);
16102 }
16103
Damien Lespiau055e3932014-08-18 13:49:10 +010016104 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020016105 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16106 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016107 intel_dump_pipe_config(crtc, crtc->config,
16108 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020016109 }
Daniel Vetter9a935852012-07-05 22:34:27 +020016110
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020016111 intel_modeset_update_connector_atomic_state(dev);
16112
Daniel Vetter35c95372013-07-17 06:55:04 +020016113 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16114 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16115
16116 if (!pll->on || pll->active)
16117 continue;
16118
16119 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16120
16121 pll->disable(dev_priv, pll);
16122 pll->on = false;
16123 }
16124
Wayne Boyer666a4532015-12-09 12:29:35 -080016125 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030016126 vlv_wm_get_hw_state(dev);
16127 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000016128 skl_wm_get_hw_state(dev);
16129 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030016130 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016131
16132 for_each_intel_crtc(dev, crtc) {
16133 unsigned long put_domains;
16134
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010016135 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016136 if (WARN_ON(put_domains))
16137 modeset_put_power_domains(dev_priv, put_domains);
16138 }
16139 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020016140
16141 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016142}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030016143
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016144void intel_display_resume(struct drm_device *dev)
16145{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016146 struct drm_i915_private *dev_priv = to_i915(dev);
16147 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16148 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016149 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016150 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020016151
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016152 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016153
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016154 /*
16155 * This is a cludge because with real atomic modeset mode_config.mutex
16156 * won't be taken. Unfortunately some probed state like
16157 * audio_codec_enable is still protected by mode_config.mutex, so lock
16158 * it here for now.
16159 */
16160 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016161 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016162
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016163retry:
16164 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016165
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016166 if (ret == 0 && !setup) {
16167 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016168
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016169 intel_modeset_setup_hw_state(dev);
16170 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010016171 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020016172
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016173 if (ret == 0 && state) {
16174 struct drm_crtc_state *crtc_state;
16175 struct drm_crtc *crtc;
16176 int i;
16177
16178 state->acquire_ctx = &ctx;
16179
16180 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16181 /*
16182 * Force recalculation even if we restore
16183 * current state. With fast modeset this may not result
16184 * in a modeset when the state is compatible.
16185 */
16186 crtc_state->mode_changed = true;
16187 }
16188
16189 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016190 }
16191
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016192 if (ret == -EDEADLK) {
16193 drm_modeset_backoff(&ctx);
16194 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016195 }
16196
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016197 drm_modeset_drop_locks(&ctx);
16198 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016199 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016200
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016201 if (ret) {
16202 DRM_ERROR("Restoring old state failed with %i\n", ret);
16203 drm_atomic_state_free(state);
16204 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010016205}
16206
16207void intel_modeset_gem_init(struct drm_device *dev)
16208{
Jesse Barnes484b41d2014-03-07 08:57:55 -080016209 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016210 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016211 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016212
Imre Deakae484342014-03-31 15:10:44 +030016213 intel_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +030016214
Chris Wilson1833b132012-05-09 11:56:28 +010016215 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016216
16217 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016218
16219 /*
16220 * Make sure any fbs we allocated at startup are properly
16221 * pinned & fenced. When we do the allocation it's too early
16222 * for this.
16223 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016224 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016225 obj = intel_fb_obj(c->primary->fb);
16226 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016227 continue;
16228
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016229 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016230 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16231 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016232 mutex_unlock(&dev->struct_mutex);
16233 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016234 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16235 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016236 drm_framebuffer_unreference(c->primary->fb);
16237 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016238 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080016239 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016240 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016241 }
16242 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016243
16244 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016245}
16246
Imre Deak4932e2c2014-02-11 17:12:48 +020016247void intel_connector_unregister(struct intel_connector *intel_connector)
16248{
16249 struct drm_connector *connector = &intel_connector->base;
16250
16251 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010016252 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016253}
16254
Jesse Barnes79e53942008-11-07 14:24:08 -080016255void intel_modeset_cleanup(struct drm_device *dev)
16256{
Jesse Barnes652c3932009-08-17 13:31:43 -070016257 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020016258 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070016259
Imre Deak2eb52522014-11-19 15:30:05 +020016260 intel_disable_gt_powersave(dev);
16261
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016262 intel_backlight_unregister(dev);
16263
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016264 /*
16265 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016266 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016267 * experience fancy races otherwise.
16268 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016269 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016270
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016271 /*
16272 * Due to the hpd irq storm handling the hotplug work can re-arm the
16273 * poll handlers. Hence disable polling after hpd handling is shut down.
16274 */
Keith Packardf87ea762010-10-03 19:36:26 -070016275 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016276
Jesse Barnes723bfd72010-10-07 16:01:13 -070016277 intel_unregister_dsm_handler();
16278
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016279 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016280
Chris Wilson1630fe72011-07-08 12:22:42 +010016281 /* flush any delayed tasks or pending work */
16282 flush_scheduled_work();
16283
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016284 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016285 for_each_intel_connector(dev, connector)
16286 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016287
Jesse Barnes79e53942008-11-07 14:24:08 -080016288 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016289
16290 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030016291
Imre Deakae484342014-03-31 15:10:44 +030016292 intel_cleanup_gt_powersave(dev);
Daniel Vetterf5949142016-01-13 11:55:28 +010016293
16294 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016295}
16296
Dave Airlie28d52042009-09-21 14:33:58 +100016297/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016298 * Return which encoder is currently attached for connector.
16299 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016300struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016301{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016302 return &intel_attached_encoder(connector)->base;
16303}
Jesse Barnes79e53942008-11-07 14:24:08 -080016304
Chris Wilsondf0e9242010-09-09 16:20:55 +010016305void intel_connector_attach_encoder(struct intel_connector *connector,
16306 struct intel_encoder *encoder)
16307{
16308 connector->encoder = encoder;
16309 drm_mode_connector_attach_encoder(&connector->base,
16310 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016311}
Dave Airlie28d52042009-09-21 14:33:58 +100016312
16313/*
16314 * set vga decode state - true == enable VGA decode
16315 */
16316int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16317{
16318 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016319 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016320 u16 gmch_ctrl;
16321
Chris Wilson75fa0412014-02-07 18:37:02 -020016322 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16323 DRM_ERROR("failed to read control word\n");
16324 return -EIO;
16325 }
16326
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016327 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16328 return 0;
16329
Dave Airlie28d52042009-09-21 14:33:58 +100016330 if (state)
16331 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16332 else
16333 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016334
16335 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16336 DRM_ERROR("failed to write control word\n");
16337 return -EIO;
16338 }
16339
Dave Airlie28d52042009-09-21 14:33:58 +100016340 return 0;
16341}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016342
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016343struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016344
16345 u32 power_well_driver;
16346
Chris Wilson63b66e52013-08-08 15:12:06 +020016347 int num_transcoders;
16348
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016349 struct intel_cursor_error_state {
16350 u32 control;
16351 u32 position;
16352 u32 base;
16353 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016354 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016355
16356 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016357 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016358 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030016359 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016360 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016361
16362 struct intel_plane_error_state {
16363 u32 control;
16364 u32 stride;
16365 u32 size;
16366 u32 pos;
16367 u32 addr;
16368 u32 surface;
16369 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016370 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016371
16372 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016373 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016374 enum transcoder cpu_transcoder;
16375
16376 u32 conf;
16377
16378 u32 htotal;
16379 u32 hblank;
16380 u32 hsync;
16381 u32 vtotal;
16382 u32 vblank;
16383 u32 vsync;
16384 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016385};
16386
16387struct intel_display_error_state *
16388intel_display_capture_error_state(struct drm_device *dev)
16389{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016390 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016391 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016392 int transcoders[] = {
16393 TRANSCODER_A,
16394 TRANSCODER_B,
16395 TRANSCODER_C,
16396 TRANSCODER_EDP,
16397 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016398 int i;
16399
Chris Wilson63b66e52013-08-08 15:12:06 +020016400 if (INTEL_INFO(dev)->num_pipes == 0)
16401 return NULL;
16402
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016403 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016404 if (error == NULL)
16405 return NULL;
16406
Imre Deak190be112013-11-25 17:15:31 +020016407 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016408 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16409
Damien Lespiau055e3932014-08-18 13:49:10 +010016410 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016411 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016412 __intel_display_power_is_enabled(dev_priv,
16413 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016414 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016415 continue;
16416
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016417 error->cursor[i].control = I915_READ(CURCNTR(i));
16418 error->cursor[i].position = I915_READ(CURPOS(i));
16419 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016420
16421 error->plane[i].control = I915_READ(DSPCNTR(i));
16422 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016423 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016424 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016425 error->plane[i].pos = I915_READ(DSPPOS(i));
16426 }
Paulo Zanonica291362013-03-06 20:03:14 -030016427 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16428 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016429 if (INTEL_INFO(dev)->gen >= 4) {
16430 error->plane[i].surface = I915_READ(DSPSURF(i));
16431 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16432 }
16433
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016434 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030016435
Sonika Jindal3abfce72014-07-21 15:23:43 +053016436 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030016437 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016438 }
16439
16440 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16441 if (HAS_DDI(dev_priv->dev))
16442 error->num_transcoders++; /* Account for eDP. */
16443
16444 for (i = 0; i < error->num_transcoders; i++) {
16445 enum transcoder cpu_transcoder = transcoders[i];
16446
Imre Deakddf9c532013-11-27 22:02:02 +020016447 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016448 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016449 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016450 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016451 continue;
16452
Chris Wilson63b66e52013-08-08 15:12:06 +020016453 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16454
16455 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16456 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16457 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16458 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16459 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16460 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16461 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016462 }
16463
16464 return error;
16465}
16466
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016467#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16468
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016469void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016470intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016471 struct drm_device *dev,
16472 struct intel_display_error_state *error)
16473{
Damien Lespiau055e3932014-08-18 13:49:10 +010016474 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016475 int i;
16476
Chris Wilson63b66e52013-08-08 15:12:06 +020016477 if (!error)
16478 return;
16479
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016480 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016481 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016482 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016483 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016484 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016485 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016486 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016487 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016488 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030016489 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016490
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016491 err_printf(m, "Plane [%d]:\n", i);
16492 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16493 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016494 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016495 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16496 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016497 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016498 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016499 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016500 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016501 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16502 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016503 }
16504
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016505 err_printf(m, "Cursor [%d]:\n", i);
16506 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16507 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16508 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016509 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016510
16511 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010016512 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016513 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016514 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016515 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016516 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16517 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16518 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16519 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16520 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16521 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16522 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16523 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016524}