blob: 305abaa0d00070ec6c02a6ca9262892eed262440 [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>
Jesse Barnes79e53942008-11-07 14:24:08 -080047
Matt Roper465c1202014-05-29 08:06:54 -070048/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010049static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010050 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070052 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010053 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070054};
55
56/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
60 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070061 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010062 DRM_FORMAT_XRGB2101010,
63 DRM_FORMAT_XBGR2101010,
64};
65
66static const uint32_t skl_primary_formats[] = {
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010071 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070072 DRM_FORMAT_ABGR8888,
73 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070074 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053075 DRM_FORMAT_YUYV,
76 DRM_FORMAT_YVYU,
77 DRM_FORMAT_UYVY,
78 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070079};
80
Matt Roper3d7d6512014-06-10 08:28:13 -070081/* Cursor formats */
82static const uint32_t intel_cursor_formats[] = {
83 DRM_FORMAT_ARGB8888,
84};
85
Chris Wilson6b383a72010-09-13 13:54:26 +010086static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080087
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);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100119
Jesse Barnes79e53942008-11-07 14:24:08 -0800120typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400121 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800122} intel_range_t;
123
124typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400125 int dot_limit;
126 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800127} intel_p2_t;
128
Ma Lingd4906092009-03-18 20:13:27 +0800129typedef struct intel_limit intel_limit_t;
130struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400131 intel_range_t dot, vco, n, m, m1, m2, p, p1;
132 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800133};
Jesse Barnes79e53942008-11-07 14:24:08 -0800134
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300135/* returns HPLL frequency in kHz */
136static int valleyview_get_vco(struct drm_i915_private *dev_priv)
137{
138 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
139
140 /* Obtain SKU information */
141 mutex_lock(&dev_priv->sb_lock);
142 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
143 CCK_FUSE_HPLL_FREQ_MASK;
144 mutex_unlock(&dev_priv->sb_lock);
145
146 return vco_freq[hpll_freq] * 1000;
147}
148
149static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
150 const char *name, u32 reg)
151{
152 u32 val;
153 int divider;
154
155 if (dev_priv->hpll_freq == 0)
156 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
157
158 mutex_lock(&dev_priv->sb_lock);
159 val = vlv_cck_read(dev_priv, reg);
160 mutex_unlock(&dev_priv->sb_lock);
161
162 divider = val & CCK_FREQUENCY_VALUES;
163
164 WARN((val & CCK_FREQUENCY_STATUS) !=
165 (divider << CCK_FREQUENCY_STATUS_SHIFT),
166 "%s change in progress\n", name);
167
168 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
169}
170
Daniel Vetterd2acd212012-10-20 20:57:43 +0200171int
172intel_pch_rawclk(struct drm_device *dev)
173{
174 struct drm_i915_private *dev_priv = dev->dev_private;
175
176 WARN_ON(!HAS_PCH_SPLIT(dev));
177
178 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
179}
180
Jani Nikula79e50a42015-08-26 10:58:20 +0300181/* hrawclock is 1/4 the FSB frequency */
182int intel_hrawclk(struct drm_device *dev)
183{
184 struct drm_i915_private *dev_priv = dev->dev_private;
185 uint32_t clkcfg;
186
187 /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
188 if (IS_VALLEYVIEW(dev))
189 return 200;
190
191 clkcfg = I915_READ(CLKCFG);
192 switch (clkcfg & CLKCFG_FSB_MASK) {
193 case CLKCFG_FSB_400:
194 return 100;
195 case CLKCFG_FSB_533:
196 return 133;
197 case CLKCFG_FSB_667:
198 return 166;
199 case CLKCFG_FSB_800:
200 return 200;
201 case CLKCFG_FSB_1067:
202 return 266;
203 case CLKCFG_FSB_1333:
204 return 333;
205 /* these two are just a guess; one of them might be right */
206 case CLKCFG_FSB_1600:
207 case CLKCFG_FSB_1600_ALT:
208 return 400;
209 default:
210 return 133;
211 }
212}
213
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300214static void intel_update_czclk(struct drm_i915_private *dev_priv)
215{
216 if (!IS_VALLEYVIEW(dev_priv))
217 return;
218
219 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
220 CCK_CZ_CLOCK_CONTROL);
221
222 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
223}
224
Chris Wilson021357a2010-09-07 20:54:59 +0100225static inline u32 /* units of 100MHz */
226intel_fdi_link_freq(struct drm_device *dev)
227{
Chris Wilson8b99e682010-10-13 09:59:17 +0100228 if (IS_GEN5(dev)) {
229 struct drm_i915_private *dev_priv = dev->dev_private;
230 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
231 } else
232 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100233}
234
Daniel Vetter5d536e22013-07-06 12:52:06 +0200235static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400236 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200237 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200238 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400239 .m = { .min = 96, .max = 140 },
240 .m1 = { .min = 18, .max = 26 },
241 .m2 = { .min = 6, .max = 16 },
242 .p = { .min = 4, .max = 128 },
243 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700244 .p2 = { .dot_limit = 165000,
245 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700246};
247
Daniel Vetter5d536e22013-07-06 12:52:06 +0200248static const intel_limit_t intel_limits_i8xx_dvo = {
249 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200250 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200251 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200252 .m = { .min = 96, .max = 140 },
253 .m1 = { .min = 18, .max = 26 },
254 .m2 = { .min = 6, .max = 16 },
255 .p = { .min = 4, .max = 128 },
256 .p1 = { .min = 2, .max = 33 },
257 .p2 = { .dot_limit = 165000,
258 .p2_slow = 4, .p2_fast = 4 },
259};
260
Keith Packarde4b36692009-06-05 19:22:17 -0700261static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400262 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200263 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200264 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400265 .m = { .min = 96, .max = 140 },
266 .m1 = { .min = 18, .max = 26 },
267 .m2 = { .min = 6, .max = 16 },
268 .p = { .min = 4, .max = 128 },
269 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700270 .p2 = { .dot_limit = 165000,
271 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700272};
Eric Anholt273e27c2011-03-30 13:01:10 -0700273
Keith Packarde4b36692009-06-05 19:22:17 -0700274static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400275 .dot = { .min = 20000, .max = 400000 },
276 .vco = { .min = 1400000, .max = 2800000 },
277 .n = { .min = 1, .max = 6 },
278 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100279 .m1 = { .min = 8, .max = 18 },
280 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400281 .p = { .min = 5, .max = 80 },
282 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700283 .p2 = { .dot_limit = 200000,
284 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700285};
286
287static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400288 .dot = { .min = 20000, .max = 400000 },
289 .vco = { .min = 1400000, .max = 2800000 },
290 .n = { .min = 1, .max = 6 },
291 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100292 .m1 = { .min = 8, .max = 18 },
293 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400294 .p = { .min = 7, .max = 98 },
295 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700296 .p2 = { .dot_limit = 112000,
297 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700298};
299
Eric Anholt273e27c2011-03-30 13:01:10 -0700300
Keith Packarde4b36692009-06-05 19:22:17 -0700301static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700302 .dot = { .min = 25000, .max = 270000 },
303 .vco = { .min = 1750000, .max = 3500000},
304 .n = { .min = 1, .max = 4 },
305 .m = { .min = 104, .max = 138 },
306 .m1 = { .min = 17, .max = 23 },
307 .m2 = { .min = 5, .max = 11 },
308 .p = { .min = 10, .max = 30 },
309 .p1 = { .min = 1, .max = 3},
310 .p2 = { .dot_limit = 270000,
311 .p2_slow = 10,
312 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800313 },
Keith Packarde4b36692009-06-05 19:22:17 -0700314};
315
316static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700317 .dot = { .min = 22000, .max = 400000 },
318 .vco = { .min = 1750000, .max = 3500000},
319 .n = { .min = 1, .max = 4 },
320 .m = { .min = 104, .max = 138 },
321 .m1 = { .min = 16, .max = 23 },
322 .m2 = { .min = 5, .max = 11 },
323 .p = { .min = 5, .max = 80 },
324 .p1 = { .min = 1, .max = 8},
325 .p2 = { .dot_limit = 165000,
326 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700327};
328
329static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700330 .dot = { .min = 20000, .max = 115000 },
331 .vco = { .min = 1750000, .max = 3500000 },
332 .n = { .min = 1, .max = 3 },
333 .m = { .min = 104, .max = 138 },
334 .m1 = { .min = 17, .max = 23 },
335 .m2 = { .min = 5, .max = 11 },
336 .p = { .min = 28, .max = 112 },
337 .p1 = { .min = 2, .max = 8 },
338 .p2 = { .dot_limit = 0,
339 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800340 },
Keith Packarde4b36692009-06-05 19:22:17 -0700341};
342
343static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700344 .dot = { .min = 80000, .max = 224000 },
345 .vco = { .min = 1750000, .max = 3500000 },
346 .n = { .min = 1, .max = 3 },
347 .m = { .min = 104, .max = 138 },
348 .m1 = { .min = 17, .max = 23 },
349 .m2 = { .min = 5, .max = 11 },
350 .p = { .min = 14, .max = 42 },
351 .p1 = { .min = 2, .max = 6 },
352 .p2 = { .dot_limit = 0,
353 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800354 },
Keith Packarde4b36692009-06-05 19:22:17 -0700355};
356
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500357static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400358 .dot = { .min = 20000, .max = 400000},
359 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700360 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400361 .n = { .min = 3, .max = 6 },
362 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700363 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400364 .m1 = { .min = 0, .max = 0 },
365 .m2 = { .min = 0, .max = 254 },
366 .p = { .min = 5, .max = 80 },
367 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700368 .p2 = { .dot_limit = 200000,
369 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700370};
371
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500372static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400373 .dot = { .min = 20000, .max = 400000 },
374 .vco = { .min = 1700000, .max = 3500000 },
375 .n = { .min = 3, .max = 6 },
376 .m = { .min = 2, .max = 256 },
377 .m1 = { .min = 0, .max = 0 },
378 .m2 = { .min = 0, .max = 254 },
379 .p = { .min = 7, .max = 112 },
380 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700381 .p2 = { .dot_limit = 112000,
382 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700383};
384
Eric Anholt273e27c2011-03-30 13:01:10 -0700385/* Ironlake / Sandybridge
386 *
387 * We calculate clock using (register_value + 2) for N/M1/M2, so here
388 * the range value for them is (actual_value - 2).
389 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800390static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700391 .dot = { .min = 25000, .max = 350000 },
392 .vco = { .min = 1760000, .max = 3510000 },
393 .n = { .min = 1, .max = 5 },
394 .m = { .min = 79, .max = 127 },
395 .m1 = { .min = 12, .max = 22 },
396 .m2 = { .min = 5, .max = 9 },
397 .p = { .min = 5, .max = 80 },
398 .p1 = { .min = 1, .max = 8 },
399 .p2 = { .dot_limit = 225000,
400 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700401};
402
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800403static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700404 .dot = { .min = 25000, .max = 350000 },
405 .vco = { .min = 1760000, .max = 3510000 },
406 .n = { .min = 1, .max = 3 },
407 .m = { .min = 79, .max = 118 },
408 .m1 = { .min = 12, .max = 22 },
409 .m2 = { .min = 5, .max = 9 },
410 .p = { .min = 28, .max = 112 },
411 .p1 = { .min = 2, .max = 8 },
412 .p2 = { .dot_limit = 225000,
413 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800414};
415
416static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700417 .dot = { .min = 25000, .max = 350000 },
418 .vco = { .min = 1760000, .max = 3510000 },
419 .n = { .min = 1, .max = 3 },
420 .m = { .min = 79, .max = 127 },
421 .m1 = { .min = 12, .max = 22 },
422 .m2 = { .min = 5, .max = 9 },
423 .p = { .min = 14, .max = 56 },
424 .p1 = { .min = 2, .max = 8 },
425 .p2 = { .dot_limit = 225000,
426 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800427};
428
Eric Anholt273e27c2011-03-30 13:01:10 -0700429/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800430static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700431 .dot = { .min = 25000, .max = 350000 },
432 .vco = { .min = 1760000, .max = 3510000 },
433 .n = { .min = 1, .max = 2 },
434 .m = { .min = 79, .max = 126 },
435 .m1 = { .min = 12, .max = 22 },
436 .m2 = { .min = 5, .max = 9 },
437 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400438 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700439 .p2 = { .dot_limit = 225000,
440 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800441};
442
443static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700444 .dot = { .min = 25000, .max = 350000 },
445 .vco = { .min = 1760000, .max = 3510000 },
446 .n = { .min = 1, .max = 3 },
447 .m = { .min = 79, .max = 126 },
448 .m1 = { .min = 12, .max = 22 },
449 .m2 = { .min = 5, .max = 9 },
450 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400451 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700452 .p2 = { .dot_limit = 225000,
453 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800454};
455
Ville Syrjälädc730512013-09-24 21:26:30 +0300456static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300457 /*
458 * These are the data rate limits (measured in fast clocks)
459 * since those are the strictest limits we have. The fast
460 * clock and actual rate limits are more relaxed, so checking
461 * them would make no difference.
462 */
463 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200464 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700465 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700466 .m1 = { .min = 2, .max = 3 },
467 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300468 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300469 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700470};
471
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300472static const intel_limit_t intel_limits_chv = {
473 /*
474 * These are the data rate limits (measured in fast clocks)
475 * since those are the strictest limits we have. The fast
476 * clock and actual rate limits are more relaxed, so checking
477 * them would make no difference.
478 */
479 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200480 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300481 .n = { .min = 1, .max = 1 },
482 .m1 = { .min = 2, .max = 2 },
483 .m2 = { .min = 24 << 22, .max = 175 << 22 },
484 .p1 = { .min = 2, .max = 4 },
485 .p2 = { .p2_slow = 1, .p2_fast = 14 },
486};
487
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200488static const intel_limit_t intel_limits_bxt = {
489 /* FIXME: find real dot limits */
490 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530491 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200492 .n = { .min = 1, .max = 1 },
493 .m1 = { .min = 2, .max = 2 },
494 /* FIXME: find real m2 limits */
495 .m2 = { .min = 2 << 22, .max = 255 << 22 },
496 .p1 = { .min = 2, .max = 4 },
497 .p2 = { .p2_slow = 1, .p2_fast = 20 },
498};
499
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200500static bool
501needs_modeset(struct drm_crtc_state *state)
502{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200503 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200504}
505
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300506/**
507 * Returns whether any output on the specified pipe is of the specified type
508 */
Damien Lespiau40935612014-10-29 11:16:59 +0000509bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300510{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300511 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300512 struct intel_encoder *encoder;
513
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300514 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300515 if (encoder->type == type)
516 return true;
517
518 return false;
519}
520
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200521/**
522 * Returns whether any output on the specified pipe will have the specified
523 * type after a staged modeset is complete, i.e., the same as
524 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
525 * encoder->crtc.
526 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200527static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
528 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200529{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200530 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300531 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200532 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200533 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200534 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200535
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300536 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200537 if (connector_state->crtc != crtc_state->base.crtc)
538 continue;
539
540 num_connectors++;
541
542 encoder = to_intel_encoder(connector_state->best_encoder);
543 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200544 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200545 }
546
547 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200548
549 return false;
550}
551
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200552static const intel_limit_t *
553intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800554{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200555 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800556 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800557
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200558 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100559 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000560 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800561 limit = &intel_limits_ironlake_dual_lvds_100m;
562 else
563 limit = &intel_limits_ironlake_dual_lvds;
564 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000565 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800566 limit = &intel_limits_ironlake_single_lvds_100m;
567 else
568 limit = &intel_limits_ironlake_single_lvds;
569 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200570 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800571 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800572
573 return limit;
574}
575
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200576static const intel_limit_t *
577intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800578{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200579 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800580 const intel_limit_t *limit;
581
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200582 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100583 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700584 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800585 else
Keith Packarde4b36692009-06-05 19:22:17 -0700586 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200587 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
588 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700589 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200590 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700591 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800592 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700593 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800594
595 return limit;
596}
597
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200598static const intel_limit_t *
599intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800600{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200601 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800602 const intel_limit_t *limit;
603
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200604 if (IS_BROXTON(dev))
605 limit = &intel_limits_bxt;
606 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200607 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800608 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200609 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500610 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200611 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500612 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800613 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500614 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300615 } else if (IS_CHERRYVIEW(dev)) {
616 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700617 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300618 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100619 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200620 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100621 limit = &intel_limits_i9xx_lvds;
622 else
623 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800624 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200625 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700626 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200627 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700628 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200629 else
630 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800631 }
632 return limit;
633}
634
Imre Deakdccbea32015-06-22 23:35:51 +0300635/*
636 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
637 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
638 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
639 * The helpers' return value is the rate of the clock that is fed to the
640 * display engine's pipe which can be the above fast dot clock rate or a
641 * divided-down version of it.
642 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500643/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300644static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800645{
Shaohua Li21778322009-02-23 15:19:16 +0800646 clock->m = clock->m2 + 2;
647 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200648 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300649 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300650 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
651 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300652
653 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800654}
655
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200656static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
657{
658 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
659}
660
Imre Deakdccbea32015-06-22 23:35:51 +0300661static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800662{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200663 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800664 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200665 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300666 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300667 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
668 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300669
670 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800671}
672
Imre Deakdccbea32015-06-22 23:35:51 +0300673static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300674{
675 clock->m = clock->m1 * clock->m2;
676 clock->p = clock->p1 * clock->p2;
677 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300678 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300679 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
680 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300681
682 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300683}
684
Imre Deakdccbea32015-06-22 23:35:51 +0300685int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300686{
687 clock->m = clock->m1 * clock->m2;
688 clock->p = clock->p1 * clock->p2;
689 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300690 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300691 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
692 clock->n << 22);
693 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300694
695 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300696}
697
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800698#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800699/**
700 * Returns whether the given set of divisors are valid for a given refclk with
701 * the given connectors.
702 */
703
Chris Wilson1b894b52010-12-14 20:04:54 +0000704static bool intel_PLL_is_valid(struct drm_device *dev,
705 const intel_limit_t *limit,
706 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800707{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300708 if (clock->n < limit->n.min || limit->n.max < clock->n)
709 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800710 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400711 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800712 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400713 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800714 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400715 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300716
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200717 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300718 if (clock->m1 <= clock->m2)
719 INTELPllInvalid("m1 <= m2\n");
720
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200721 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300722 if (clock->p < limit->p.min || limit->p.max < clock->p)
723 INTELPllInvalid("p out of range\n");
724 if (clock->m < limit->m.min || limit->m.max < clock->m)
725 INTELPllInvalid("m out of range\n");
726 }
727
Jesse Barnes79e53942008-11-07 14:24:08 -0800728 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400729 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800730 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
731 * connector, etc., rather than just a single range.
732 */
733 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400734 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800735
736 return true;
737}
738
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300739static int
740i9xx_select_p2_div(const intel_limit_t *limit,
741 const struct intel_crtc_state *crtc_state,
742 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800743{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300744 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800745
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200746 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800747 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100748 * For LVDS just rely on its current settings for dual-channel.
749 * We haven't figured out how to reliably set up different
750 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800751 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100752 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300753 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800754 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300755 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800756 } else {
757 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300758 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800759 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300760 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800761 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300762}
763
764static bool
765i9xx_find_best_dpll(const intel_limit_t *limit,
766 struct intel_crtc_state *crtc_state,
767 int target, int refclk, intel_clock_t *match_clock,
768 intel_clock_t *best_clock)
769{
770 struct drm_device *dev = crtc_state->base.crtc->dev;
771 intel_clock_t clock;
772 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800773
Akshay Joshi0206e352011-08-16 15:34:10 -0400774 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800775
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300776 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
777
Zhao Yakui42158662009-11-20 11:24:18 +0800778 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
779 clock.m1++) {
780 for (clock.m2 = limit->m2.min;
781 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200782 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800783 break;
784 for (clock.n = limit->n.min;
785 clock.n <= limit->n.max; clock.n++) {
786 for (clock.p1 = limit->p1.min;
787 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800788 int this_err;
789
Imre Deakdccbea32015-06-22 23:35:51 +0300790 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000791 if (!intel_PLL_is_valid(dev, limit,
792 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800793 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800794 if (match_clock &&
795 clock.p != match_clock->p)
796 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800797
798 this_err = abs(clock.dot - target);
799 if (this_err < err) {
800 *best_clock = clock;
801 err = this_err;
802 }
803 }
804 }
805 }
806 }
807
808 return (err != target);
809}
810
Ma Lingd4906092009-03-18 20:13:27 +0800811static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200812pnv_find_best_dpll(const intel_limit_t *limit,
813 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200814 int target, int refclk, intel_clock_t *match_clock,
815 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200816{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300817 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200818 intel_clock_t clock;
819 int err = target;
820
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200821 memset(best_clock, 0, sizeof(*best_clock));
822
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300823 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
824
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200825 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
826 clock.m1++) {
827 for (clock.m2 = limit->m2.min;
828 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200829 for (clock.n = limit->n.min;
830 clock.n <= limit->n.max; clock.n++) {
831 for (clock.p1 = limit->p1.min;
832 clock.p1 <= limit->p1.max; clock.p1++) {
833 int this_err;
834
Imre Deakdccbea32015-06-22 23:35:51 +0300835 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800836 if (!intel_PLL_is_valid(dev, limit,
837 &clock))
838 continue;
839 if (match_clock &&
840 clock.p != match_clock->p)
841 continue;
842
843 this_err = abs(clock.dot - target);
844 if (this_err < err) {
845 *best_clock = clock;
846 err = this_err;
847 }
848 }
849 }
850 }
851 }
852
853 return (err != target);
854}
855
Ma Lingd4906092009-03-18 20:13:27 +0800856static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200857g4x_find_best_dpll(const intel_limit_t *limit,
858 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200859 int target, int refclk, intel_clock_t *match_clock,
860 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800861{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300862 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800863 intel_clock_t clock;
864 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300865 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400866 /* approximately equals target * 0.00585 */
867 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800868
869 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300870
871 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
872
Ma Lingd4906092009-03-18 20:13:27 +0800873 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200874 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800875 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200876 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800877 for (clock.m1 = limit->m1.max;
878 clock.m1 >= limit->m1.min; clock.m1--) {
879 for (clock.m2 = limit->m2.max;
880 clock.m2 >= limit->m2.min; clock.m2--) {
881 for (clock.p1 = limit->p1.max;
882 clock.p1 >= limit->p1.min; clock.p1--) {
883 int this_err;
884
Imre Deakdccbea32015-06-22 23:35:51 +0300885 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000886 if (!intel_PLL_is_valid(dev, limit,
887 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800888 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000889
890 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800891 if (this_err < err_most) {
892 *best_clock = clock;
893 err_most = this_err;
894 max_n = clock.n;
895 found = true;
896 }
897 }
898 }
899 }
900 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800901 return found;
902}
Ma Lingd4906092009-03-18 20:13:27 +0800903
Imre Deakd5dd62b2015-03-17 11:40:03 +0200904/*
905 * Check if the calculated PLL configuration is more optimal compared to the
906 * best configuration and error found so far. Return the calculated error.
907 */
908static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
909 const intel_clock_t *calculated_clock,
910 const intel_clock_t *best_clock,
911 unsigned int best_error_ppm,
912 unsigned int *error_ppm)
913{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200914 /*
915 * For CHV ignore the error and consider only the P value.
916 * Prefer a bigger P value based on HW requirements.
917 */
918 if (IS_CHERRYVIEW(dev)) {
919 *error_ppm = 0;
920
921 return calculated_clock->p > best_clock->p;
922 }
923
Imre Deak24be4e42015-03-17 11:40:04 +0200924 if (WARN_ON_ONCE(!target_freq))
925 return false;
926
Imre Deakd5dd62b2015-03-17 11:40:03 +0200927 *error_ppm = div_u64(1000000ULL *
928 abs(target_freq - calculated_clock->dot),
929 target_freq);
930 /*
931 * Prefer a better P value over a better (smaller) error if the error
932 * is small. Ensure this preference for future configurations too by
933 * setting the error to 0.
934 */
935 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
936 *error_ppm = 0;
937
938 return true;
939 }
940
941 return *error_ppm + 10 < best_error_ppm;
942}
943
Zhenyu Wang2c072452009-06-05 15:38:42 +0800944static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200945vlv_find_best_dpll(const intel_limit_t *limit,
946 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200947 int target, int refclk, intel_clock_t *match_clock,
948 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700949{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200950 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300951 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300952 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300953 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300954 /* min update 19.2 MHz */
955 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300956 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700957
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300958 target *= 5; /* fast clock */
959
960 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700961
962 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300963 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300964 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300965 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300966 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300967 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700968 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300969 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200970 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300971
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300972 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
973 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300974
Imre Deakdccbea32015-06-22 23:35:51 +0300975 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300976
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300977 if (!intel_PLL_is_valid(dev, limit,
978 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300979 continue;
980
Imre Deakd5dd62b2015-03-17 11:40:03 +0200981 if (!vlv_PLL_is_optimal(dev, target,
982 &clock,
983 best_clock,
984 bestppm, &ppm))
985 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300986
Imre Deakd5dd62b2015-03-17 11:40:03 +0200987 *best_clock = clock;
988 bestppm = ppm;
989 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700990 }
991 }
992 }
993 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700994
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300995 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700996}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700997
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300998static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200999chv_find_best_dpll(const intel_limit_t *limit,
1000 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001001 int target, int refclk, intel_clock_t *match_clock,
1002 intel_clock_t *best_clock)
1003{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02001004 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03001005 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +02001006 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001007 intel_clock_t clock;
1008 uint64_t m2;
1009 int found = false;
1010
1011 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +02001012 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001013
1014 /*
1015 * Based on hardware doc, the n always set to 1, and m1 always
1016 * set to 2. If requires to support 200Mhz refclk, we need to
1017 * revisit this because n may not 1 anymore.
1018 */
1019 clock.n = 1, clock.m1 = 2;
1020 target *= 5; /* fast clock */
1021
1022 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1023 for (clock.p2 = limit->p2.p2_fast;
1024 clock.p2 >= limit->p2.p2_slow;
1025 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001026 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001027
1028 clock.p = clock.p1 * clock.p2;
1029
1030 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1031 clock.n) << 22, refclk * clock.m1);
1032
1033 if (m2 > INT_MAX/clock.m1)
1034 continue;
1035
1036 clock.m2 = m2;
1037
Imre Deakdccbea32015-06-22 23:35:51 +03001038 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001039
1040 if (!intel_PLL_is_valid(dev, limit, &clock))
1041 continue;
1042
Imre Deak9ca3ba02015-03-17 11:40:05 +02001043 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1044 best_error_ppm, &error_ppm))
1045 continue;
1046
1047 *best_clock = clock;
1048 best_error_ppm = error_ppm;
1049 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001050 }
1051 }
1052
1053 return found;
1054}
1055
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001056bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1057 intel_clock_t *best_clock)
1058{
1059 int refclk = i9xx_get_refclk(crtc_state, 0);
1060
1061 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
1062 target_clock, refclk, NULL, best_clock);
1063}
1064
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001065bool intel_crtc_active(struct drm_crtc *crtc)
1066{
1067 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1068
1069 /* Be paranoid as we can arrive here with only partial
1070 * state retrieved from the hardware during setup.
1071 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001072 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001073 * as Haswell has gained clock readout/fastboot support.
1074 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001075 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001076 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001077 *
1078 * FIXME: The intel_crtc->active here should be switched to
1079 * crtc->state->active once we have proper CRTC states wired up
1080 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001081 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001082 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001083 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001084}
1085
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001086enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1087 enum pipe pipe)
1088{
1089 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1090 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1091
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001092 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001093}
1094
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001095static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1096{
1097 struct drm_i915_private *dev_priv = dev->dev_private;
1098 u32 reg = PIPEDSL(pipe);
1099 u32 line1, line2;
1100 u32 line_mask;
1101
1102 if (IS_GEN2(dev))
1103 line_mask = DSL_LINEMASK_GEN2;
1104 else
1105 line_mask = DSL_LINEMASK_GEN3;
1106
1107 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001108 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001109 line2 = I915_READ(reg) & line_mask;
1110
1111 return line1 == line2;
1112}
1113
Keith Packardab7ad7f2010-10-03 00:33:06 -07001114/*
1115 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001116 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001117 *
1118 * After disabling a pipe, we can't wait for vblank in the usual way,
1119 * spinning on the vblank interrupt status bit, since we won't actually
1120 * see an interrupt when the pipe is disabled.
1121 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001122 * On Gen4 and above:
1123 * wait for the pipe register state bit to turn off
1124 *
1125 * Otherwise:
1126 * wait for the display line value to settle (it usually
1127 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001128 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001129 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001130static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001131{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001132 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001133 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001134 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001135 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001136
Keith Packardab7ad7f2010-10-03 00:33:06 -07001137 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001138 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001139
Keith Packardab7ad7f2010-10-03 00:33:06 -07001140 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001141 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1142 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001143 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001144 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001145 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001146 if (wait_for(pipe_dsl_stopped(dev, pipe), 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 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001149}
1150
Jesse Barnesb24e7172011-01-04 15:09:30 -08001151static const char *state_string(bool enabled)
1152{
1153 return enabled ? "on" : "off";
1154}
1155
1156/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001157void assert_pll(struct drm_i915_private *dev_priv,
1158 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001159{
1160 int reg;
1161 u32 val;
1162 bool cur_state;
1163
1164 reg = DPLL(pipe);
1165 val = I915_READ(reg);
1166 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001167 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001168 "PLL state assertion failure (expected %s, current %s)\n",
1169 state_string(state), state_string(cur_state));
1170}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001171
Jani Nikula23538ef2013-08-27 15:12:22 +03001172/* XXX: the dsi pll is shared between MIPI DSI ports */
1173static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1174{
1175 u32 val;
1176 bool cur_state;
1177
Ville Syrjäläa5805162015-05-26 20:42:30 +03001178 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001179 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001180 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001181
1182 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001183 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001184 "DSI PLL state assertion failure (expected %s, current %s)\n",
1185 state_string(state), state_string(cur_state));
1186}
1187#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1188#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1189
Daniel Vetter55607e82013-06-16 21:42:39 +02001190struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001191intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001192{
Daniel Vettere2b78262013-06-07 23:10:03 +02001193 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1194
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001195 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001196 return NULL;
1197
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001198 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001199}
1200
Jesse Barnesb24e7172011-01-04 15:09:30 -08001201/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001202void assert_shared_dpll(struct drm_i915_private *dev_priv,
1203 struct intel_shared_dpll *pll,
1204 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001205{
Jesse Barnes040484a2011-01-03 12:14:26 -08001206 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001207 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001208
Chris Wilson92b27b02012-05-20 18:10:50 +01001209 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001210 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001211 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001212
Daniel Vetter53589012013-06-05 13:34:16 +02001213 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001214 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001215 "%s assertion failure (expected %s, current %s)\n",
1216 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001217}
Jesse Barnes040484a2011-01-03 12:14:26 -08001218
1219static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1220 enum pipe pipe, bool state)
1221{
1222 int reg;
1223 u32 val;
1224 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001225 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1226 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001227
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001228 if (HAS_DDI(dev_priv->dev)) {
1229 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001230 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001231 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001232 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001233 } else {
1234 reg = FDI_TX_CTL(pipe);
1235 val = I915_READ(reg);
1236 cur_state = !!(val & FDI_TX_ENABLE);
1237 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001238 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001239 "FDI TX state assertion failure (expected %s, current %s)\n",
1240 state_string(state), state_string(cur_state));
1241}
1242#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1243#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1244
1245static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1246 enum pipe pipe, bool state)
1247{
1248 int reg;
1249 u32 val;
1250 bool cur_state;
1251
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001252 reg = FDI_RX_CTL(pipe);
1253 val = I915_READ(reg);
1254 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001255 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001256 "FDI RX state assertion failure (expected %s, current %s)\n",
1257 state_string(state), state_string(cur_state));
1258}
1259#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1260#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1261
1262static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1263 enum pipe pipe)
1264{
1265 int reg;
1266 u32 val;
1267
1268 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001269 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001270 return;
1271
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001272 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001273 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001274 return;
1275
Jesse Barnes040484a2011-01-03 12:14:26 -08001276 reg = FDI_TX_CTL(pipe);
1277 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001278 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 -08001279}
1280
Daniel Vetter55607e82013-06-16 21:42:39 +02001281void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1282 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001283{
1284 int reg;
1285 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001286 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001287
1288 reg = FDI_RX_CTL(pipe);
1289 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001290 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001291 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001292 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1293 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001294}
1295
Daniel Vetterb680c372014-09-19 18:27:27 +02001296void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1297 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001298{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001299 struct drm_device *dev = dev_priv->dev;
1300 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001301 u32 val;
1302 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001303 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001304
Jani Nikulabedd4db2014-08-22 15:04:13 +03001305 if (WARN_ON(HAS_DDI(dev)))
1306 return;
1307
1308 if (HAS_PCH_SPLIT(dev)) {
1309 u32 port_sel;
1310
Jesse Barnesea0760c2011-01-04 15:09:32 -08001311 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001312 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1313
1314 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1315 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1316 panel_pipe = PIPE_B;
1317 /* XXX: else fix for eDP */
1318 } else if (IS_VALLEYVIEW(dev)) {
1319 /* presumably write lock depends on pipe, not port select */
1320 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1321 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001322 } else {
1323 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001324 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1325 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001326 }
1327
1328 val = I915_READ(pp_reg);
1329 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001330 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001331 locked = false;
1332
Rob Clarke2c719b2014-12-15 13:56:32 -05001333 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001334 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001335 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001336}
1337
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001338static void assert_cursor(struct drm_i915_private *dev_priv,
1339 enum pipe pipe, bool state)
1340{
1341 struct drm_device *dev = dev_priv->dev;
1342 bool cur_state;
1343
Paulo Zanonid9d82082014-02-27 16:30:56 -03001344 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001345 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001346 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001347 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001348
Rob Clarke2c719b2014-12-15 13:56:32 -05001349 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001350 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1351 pipe_name(pipe), state_string(state), state_string(cur_state));
1352}
1353#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1354#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1355
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001356void assert_pipe(struct drm_i915_private *dev_priv,
1357 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001358{
1359 int reg;
1360 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001361 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001362 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1363 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001364
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001365 /* if we need the pipe quirk it must be always on */
1366 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1367 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001368 state = true;
1369
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001370 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001371 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001372 cur_state = false;
1373 } else {
1374 reg = PIPECONF(cpu_transcoder);
1375 val = I915_READ(reg);
1376 cur_state = !!(val & PIPECONF_ENABLE);
1377 }
1378
Rob Clarke2c719b2014-12-15 13:56:32 -05001379 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001380 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001381 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001382}
1383
Chris Wilson931872f2012-01-16 23:01:13 +00001384static void assert_plane(struct drm_i915_private *dev_priv,
1385 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001386{
1387 int reg;
1388 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001389 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001390
1391 reg = DSPCNTR(plane);
1392 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001393 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001394 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001395 "plane %c assertion failure (expected %s, current %s)\n",
1396 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001397}
1398
Chris Wilson931872f2012-01-16 23:01:13 +00001399#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1400#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1401
Jesse Barnesb24e7172011-01-04 15:09:30 -08001402static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1403 enum pipe pipe)
1404{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001405 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001406 int reg, i;
1407 u32 val;
1408 int cur_pipe;
1409
Ville Syrjälä653e1022013-06-04 13:49:05 +03001410 /* Primary planes are fixed to pipes on gen4+ */
1411 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001412 reg = DSPCNTR(pipe);
1413 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001414 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001415 "plane %c assertion failure, should be disabled but not\n",
1416 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001417 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001418 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001419
Jesse Barnesb24e7172011-01-04 15:09:30 -08001420 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001421 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001422 reg = DSPCNTR(i);
1423 val = I915_READ(reg);
1424 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1425 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001426 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001427 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1428 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001429 }
1430}
1431
Jesse Barnes19332d72013-03-28 09:55:38 -07001432static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1433 enum pipe pipe)
1434{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001435 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001436 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001437 u32 val;
1438
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001439 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001440 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001441 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001442 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001443 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1444 sprite, pipe_name(pipe));
1445 }
1446 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001447 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001448 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001449 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001450 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001451 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001452 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001453 }
1454 } else if (INTEL_INFO(dev)->gen >= 7) {
1455 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001456 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001457 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001458 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001459 plane_name(pipe), pipe_name(pipe));
1460 } else if (INTEL_INFO(dev)->gen >= 5) {
1461 reg = DVSCNTR(pipe);
1462 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001463 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001464 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1465 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001466 }
1467}
1468
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001469static void assert_vblank_disabled(struct drm_crtc *crtc)
1470{
Rob Clarke2c719b2014-12-15 13:56:32 -05001471 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001472 drm_crtc_vblank_put(crtc);
1473}
1474
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001475static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001476{
1477 u32 val;
1478 bool enabled;
1479
Rob Clarke2c719b2014-12-15 13:56:32 -05001480 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001481
Jesse Barnes92f25842011-01-04 15:09:34 -08001482 val = I915_READ(PCH_DREF_CONTROL);
1483 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1484 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001485 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001486}
1487
Daniel Vetterab9412b2013-05-03 11:49:46 +02001488static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1489 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001490{
1491 int reg;
1492 u32 val;
1493 bool enabled;
1494
Daniel Vetterab9412b2013-05-03 11:49:46 +02001495 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001496 val = I915_READ(reg);
1497 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001498 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001499 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1500 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001501}
1502
Keith Packard4e634382011-08-06 10:39:45 -07001503static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1504 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001505{
1506 if ((val & DP_PORT_EN) == 0)
1507 return false;
1508
1509 if (HAS_PCH_CPT(dev_priv->dev)) {
1510 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1511 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1512 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1513 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001514 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1515 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1516 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001517 } else {
1518 if ((val & DP_PIPE_MASK) != (pipe << 30))
1519 return false;
1520 }
1521 return true;
1522}
1523
Keith Packard1519b992011-08-06 10:35:34 -07001524static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1525 enum pipe pipe, u32 val)
1526{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001527 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001528 return false;
1529
1530 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001531 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001532 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001533 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1534 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1535 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001536 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001537 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001538 return false;
1539 }
1540 return true;
1541}
1542
1543static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1544 enum pipe pipe, u32 val)
1545{
1546 if ((val & LVDS_PORT_EN) == 0)
1547 return false;
1548
1549 if (HAS_PCH_CPT(dev_priv->dev)) {
1550 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1551 return false;
1552 } else {
1553 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1554 return false;
1555 }
1556 return true;
1557}
1558
1559static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1560 enum pipe pipe, u32 val)
1561{
1562 if ((val & ADPA_DAC_ENABLE) == 0)
1563 return false;
1564 if (HAS_PCH_CPT(dev_priv->dev)) {
1565 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1566 return false;
1567 } else {
1568 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1569 return false;
1570 }
1571 return true;
1572}
1573
Jesse Barnes291906f2011-02-02 12:28:03 -08001574static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001575 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001576{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001577 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001578 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001579 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001580 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001581
Rob Clarke2c719b2014-12-15 13:56:32 -05001582 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001583 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001584 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001585}
1586
1587static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1588 enum pipe pipe, int reg)
1589{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001590 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001591 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001592 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001593 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001594
Rob Clarke2c719b2014-12-15 13:56:32 -05001595 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001596 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001597 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001598}
1599
1600static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1601 enum pipe pipe)
1602{
1603 int reg;
1604 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001605
Keith Packardf0575e92011-07-25 22:12:43 -07001606 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1607 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1608 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001609
1610 reg = PCH_ADPA;
1611 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001612 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001613 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001614 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001615
1616 reg = PCH_LVDS;
1617 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001618 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001619 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001620 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001621
Paulo Zanonie2debe92013-02-18 19:00:27 -03001622 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1623 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1624 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001625}
1626
Ville Syrjäläd288f652014-10-28 13:20:22 +02001627static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001628 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001629{
Daniel Vetter426115c2013-07-11 22:13:42 +02001630 struct drm_device *dev = crtc->base.dev;
1631 struct drm_i915_private *dev_priv = dev->dev_private;
1632 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001633 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001634
Daniel Vetter426115c2013-07-11 22:13:42 +02001635 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001636
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001637 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001638 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1639
1640 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001641 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001642 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001643
Daniel Vetter426115c2013-07-11 22:13:42 +02001644 I915_WRITE(reg, dpll);
1645 POSTING_READ(reg);
1646 udelay(150);
1647
1648 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1649 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1650
Ville Syrjäläd288f652014-10-28 13:20:22 +02001651 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001652 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001653
1654 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001655 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001656 POSTING_READ(reg);
1657 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001658 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001659 POSTING_READ(reg);
1660 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001661 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001662 POSTING_READ(reg);
1663 udelay(150); /* wait for warmup */
1664}
1665
Ville Syrjäläd288f652014-10-28 13:20:22 +02001666static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001667 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001668{
1669 struct drm_device *dev = crtc->base.dev;
1670 struct drm_i915_private *dev_priv = dev->dev_private;
1671 int pipe = crtc->pipe;
1672 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001673 u32 tmp;
1674
1675 assert_pipe_disabled(dev_priv, crtc->pipe);
1676
1677 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1678
Ville Syrjäläa5805162015-05-26 20:42:30 +03001679 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001680
1681 /* Enable back the 10bit clock to display controller */
1682 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1683 tmp |= DPIO_DCLKP_EN;
1684 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1685
Ville Syrjälä54433e92015-05-26 20:42:31 +03001686 mutex_unlock(&dev_priv->sb_lock);
1687
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001688 /*
1689 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1690 */
1691 udelay(1);
1692
1693 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001694 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001695
1696 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001697 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001698 DRM_ERROR("PLL %d failed to lock\n", pipe);
1699
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001700 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001701 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001702 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001703}
1704
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001705static int intel_num_dvo_pipes(struct drm_device *dev)
1706{
1707 struct intel_crtc *crtc;
1708 int count = 0;
1709
1710 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001711 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001712 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001713
1714 return count;
1715}
1716
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001717static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001718{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001719 struct drm_device *dev = crtc->base.dev;
1720 struct drm_i915_private *dev_priv = dev->dev_private;
1721 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001722 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001723
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001724 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001725
1726 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001727 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001728
1729 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001730 if (IS_MOBILE(dev) && !IS_I830(dev))
1731 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001732
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001733 /* Enable DVO 2x clock on both PLLs if necessary */
1734 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1735 /*
1736 * It appears to be important that we don't enable this
1737 * for the current pipe before otherwise configuring the
1738 * PLL. No idea how this should be handled if multiple
1739 * DVO outputs are enabled simultaneosly.
1740 */
1741 dpll |= DPLL_DVO_2X_MODE;
1742 I915_WRITE(DPLL(!crtc->pipe),
1743 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1744 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001745
1746 /* Wait for the clocks to stabilize. */
1747 POSTING_READ(reg);
1748 udelay(150);
1749
1750 if (INTEL_INFO(dev)->gen >= 4) {
1751 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001752 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001753 } else {
1754 /* The pixel multiplier can only be updated once the
1755 * DPLL is enabled and the clocks are stable.
1756 *
1757 * So write it again.
1758 */
1759 I915_WRITE(reg, dpll);
1760 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001761
1762 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001763 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001764 POSTING_READ(reg);
1765 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001766 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001767 POSTING_READ(reg);
1768 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001769 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001770 POSTING_READ(reg);
1771 udelay(150); /* wait for warmup */
1772}
1773
1774/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001775 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001776 * @dev_priv: i915 private structure
1777 * @pipe: pipe PLL to disable
1778 *
1779 * Disable the PLL for @pipe, making sure the pipe is off first.
1780 *
1781 * Note! This is for pre-ILK only.
1782 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001783static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001784{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001785 struct drm_device *dev = crtc->base.dev;
1786 struct drm_i915_private *dev_priv = dev->dev_private;
1787 enum pipe pipe = crtc->pipe;
1788
1789 /* Disable DVO 2x clock on both PLLs if necessary */
1790 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001791 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001792 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001793 I915_WRITE(DPLL(PIPE_B),
1794 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1795 I915_WRITE(DPLL(PIPE_A),
1796 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1797 }
1798
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001799 /* Don't disable pipe or pipe PLLs if needed */
1800 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1801 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001802 return;
1803
1804 /* Make sure the pipe isn't still relying on us */
1805 assert_pipe_disabled(dev_priv, pipe);
1806
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001807 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001808 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001809}
1810
Jesse Barnesf6071162013-10-01 10:41:38 -07001811static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1812{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001813 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001814
1815 /* Make sure the pipe isn't still relying on us */
1816 assert_pipe_disabled(dev_priv, pipe);
1817
Imre Deake5cbfbf2014-01-09 17:08:16 +02001818 /*
1819 * Leave integrated clock source and reference clock enabled for pipe B.
1820 * The latter is needed for VGA hotplug / manual detection.
1821 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001822 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001823 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001824 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001825 I915_WRITE(DPLL(pipe), val);
1826 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001827
1828}
1829
1830static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1831{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001832 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001833 u32 val;
1834
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001835 /* Make sure the pipe isn't still relying on us */
1836 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001837
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001838 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001839 val = DPLL_SSC_REF_CLK_CHV |
1840 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001841 if (pipe != PIPE_A)
1842 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1843 I915_WRITE(DPLL(pipe), val);
1844 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001845
Ville Syrjäläa5805162015-05-26 20:42:30 +03001846 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001847
1848 /* Disable 10bit clock to display controller */
1849 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1850 val &= ~DPIO_DCLKP_EN;
1851 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1852
Ville Syrjäläa5805162015-05-26 20:42:30 +03001853 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001854}
1855
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001856void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001857 struct intel_digital_port *dport,
1858 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001859{
1860 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001861 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001862
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001863 switch (dport->port) {
1864 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001865 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001866 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001867 break;
1868 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001869 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001870 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001871 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001872 break;
1873 case PORT_D:
1874 port_mask = DPLL_PORTD_READY_MASK;
1875 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001876 break;
1877 default:
1878 BUG();
1879 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001880
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001881 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1882 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1883 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001884}
1885
Daniel Vetterb14b1052014-04-24 23:55:13 +02001886static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1887{
1888 struct drm_device *dev = crtc->base.dev;
1889 struct drm_i915_private *dev_priv = dev->dev_private;
1890 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1891
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001892 if (WARN_ON(pll == NULL))
1893 return;
1894
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001895 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001896 if (pll->active == 0) {
1897 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1898 WARN_ON(pll->on);
1899 assert_shared_dpll_disabled(dev_priv, pll);
1900
1901 pll->mode_set(dev_priv, pll);
1902 }
1903}
1904
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001905/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001906 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001907 * @dev_priv: i915 private structure
1908 * @pipe: pipe PLL to enable
1909 *
1910 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1911 * drives the transcoder clock.
1912 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001913static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001914{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001915 struct drm_device *dev = crtc->base.dev;
1916 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001917 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001918
Daniel Vetter87a875b2013-06-05 13:34:19 +02001919 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001920 return;
1921
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001922 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001923 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001924
Damien Lespiau74dd6922014-07-29 18:06:17 +01001925 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001926 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001927 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001928
Daniel Vettercdbd2312013-06-05 13:34:03 +02001929 if (pll->active++) {
1930 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001931 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001932 return;
1933 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001934 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001935
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001936 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1937
Daniel Vetter46edb022013-06-05 13:34:12 +02001938 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001939 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001940 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001941}
1942
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001943static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001944{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001945 struct drm_device *dev = crtc->base.dev;
1946 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001947 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001948
Jesse Barnes92f25842011-01-04 15:09:34 -08001949 /* PCH only available on ILK+ */
Jesse Barnes80aa9312015-08-03 13:09:11 -07001950 if (INTEL_INFO(dev)->gen < 5)
1951 return;
1952
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001953 if (pll == NULL)
1954 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001955
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001956 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
Chris Wilson48da64a2012-05-13 20:16:12 +01001957 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001958
Daniel Vetter46edb022013-06-05 13:34:12 +02001959 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1960 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001961 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001962
Chris Wilson48da64a2012-05-13 20:16:12 +01001963 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001964 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001965 return;
1966 }
1967
Daniel Vettere9d69442013-06-05 13:34:15 +02001968 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001969 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001970 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001971 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001972
Daniel Vetter46edb022013-06-05 13:34:12 +02001973 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001974 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001975 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001976
1977 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001978}
1979
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001980static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1981 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001982{
Daniel Vetter23670b322012-11-01 09:15:30 +01001983 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001984 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001986 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001987
1988 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001989 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001990
1991 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001992 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001993 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001994
1995 /* FDI must be feeding us bits for PCH ports */
1996 assert_fdi_tx_enabled(dev_priv, pipe);
1997 assert_fdi_rx_enabled(dev_priv, pipe);
1998
Daniel Vetter23670b322012-11-01 09:15:30 +01001999 if (HAS_PCH_CPT(dev)) {
2000 /* Workaround: Set the timing override bit before enabling the
2001 * pch transcoder. */
2002 reg = TRANS_CHICKEN2(pipe);
2003 val = I915_READ(reg);
2004 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2005 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03002006 }
Daniel Vetter23670b322012-11-01 09:15:30 +01002007
Daniel Vetterab9412b2013-05-03 11:49:46 +02002008 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002009 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002010 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07002011
2012 if (HAS_PCH_IBX(dev_priv->dev)) {
2013 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03002014 * Make the BPC in transcoder be consistent with
2015 * that in pipeconf reg. For HDMI we must use 8bpc
2016 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07002017 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002018 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03002019 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
2020 val |= PIPECONF_8BPC;
2021 else
2022 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002023 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002024
2025 val &= ~TRANS_INTERLACE_MASK;
2026 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002027 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002028 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002029 val |= TRANS_LEGACY_INTERLACED_ILK;
2030 else
2031 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002032 else
2033 val |= TRANS_PROGRESSIVE;
2034
Jesse Barnes040484a2011-01-03 12:14:26 -08002035 I915_WRITE(reg, val | TRANS_ENABLE);
2036 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002037 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002038}
2039
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002040static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002041 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002042{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002043 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002044
2045 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002046 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002047
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002048 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002049 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002050 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002051
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002052 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002053 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01002054 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002055 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002056
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002057 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002058 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002059
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002060 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2061 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002062 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002063 else
2064 val |= TRANS_PROGRESSIVE;
2065
Daniel Vetterab9412b2013-05-03 11:49:46 +02002066 I915_WRITE(LPT_TRANSCONF, val);
2067 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002068 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002069}
2070
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002071static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2072 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002073{
Daniel Vetter23670b322012-11-01 09:15:30 +01002074 struct drm_device *dev = dev_priv->dev;
2075 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002076
2077 /* FDI relies on the transcoder */
2078 assert_fdi_tx_disabled(dev_priv, pipe);
2079 assert_fdi_rx_disabled(dev_priv, pipe);
2080
Jesse Barnes291906f2011-02-02 12:28:03 -08002081 /* Ports must be off as well */
2082 assert_pch_ports_disabled(dev_priv, pipe);
2083
Daniel Vetterab9412b2013-05-03 11:49:46 +02002084 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002085 val = I915_READ(reg);
2086 val &= ~TRANS_ENABLE;
2087 I915_WRITE(reg, val);
2088 /* wait for PCH transcoder off, transcoder state */
2089 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002090 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002091
2092 if (!HAS_PCH_IBX(dev)) {
2093 /* Workaround: Clear the timing override chicken bit again. */
2094 reg = TRANS_CHICKEN2(pipe);
2095 val = I915_READ(reg);
2096 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2097 I915_WRITE(reg, val);
2098 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002099}
2100
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002101static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002102{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002103 u32 val;
2104
Daniel Vetterab9412b2013-05-03 11:49:46 +02002105 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002106 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002107 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002108 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002109 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002110 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002111
2112 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002113 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01002114 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002115 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002116}
2117
2118/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002119 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002120 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002121 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002122 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002123 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002124 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002125static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002126{
Paulo Zanoni03722642014-01-17 13:51:09 -02002127 struct drm_device *dev = crtc->base.dev;
2128 struct drm_i915_private *dev_priv = dev->dev_private;
2129 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002130 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2131 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002132 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002133 int reg;
2134 u32 val;
2135
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002136 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
2137
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002138 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002139 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002140 assert_sprites_disabled(dev_priv, pipe);
2141
Paulo Zanoni681e5812012-12-06 11:12:38 -02002142 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002143 pch_transcoder = TRANSCODER_A;
2144 else
2145 pch_transcoder = pipe;
2146
Jesse Barnesb24e7172011-01-04 15:09:30 -08002147 /*
2148 * A pipe without a PLL won't actually be able to drive bits from
2149 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2150 * need the check.
2151 */
Imre Deak50360402015-01-16 00:55:16 -08002152 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002153 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002154 assert_dsi_pll_enabled(dev_priv);
2155 else
2156 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002157 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002158 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002159 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002160 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002161 assert_fdi_tx_pll_enabled(dev_priv,
2162 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002163 }
2164 /* FIXME: assert CPU port conditions for SNB+ */
2165 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002166
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002167 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002168 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002169 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002170 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2171 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002172 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002173 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002174
2175 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002176 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002177}
2178
2179/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002180 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002181 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002182 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002183 * Disable the pipe of @crtc, making sure that various hardware
2184 * specific requirements are met, if applicable, e.g. plane
2185 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002186 *
2187 * Will wait until the pipe has shut down before returning.
2188 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002189static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002190{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002191 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002192 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002193 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002194 int reg;
2195 u32 val;
2196
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002197 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2198
Jesse Barnesb24e7172011-01-04 15:09:30 -08002199 /*
2200 * Make sure planes won't keep trying to pump pixels to us,
2201 * or we might hang the display.
2202 */
2203 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002204 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002205 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002206
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002207 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002208 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002209 if ((val & PIPECONF_ENABLE) == 0)
2210 return;
2211
Ville Syrjälä67adc642014-08-15 01:21:57 +03002212 /*
2213 * Double wide has implications for planes
2214 * so best keep it disabled when not needed.
2215 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002216 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002217 val &= ~PIPECONF_DOUBLE_WIDE;
2218
2219 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002220 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2221 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002222 val &= ~PIPECONF_ENABLE;
2223
2224 I915_WRITE(reg, val);
2225 if ((val & PIPECONF_ENABLE) == 0)
2226 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002227}
2228
Chris Wilson693db182013-03-05 14:52:39 +00002229static bool need_vtd_wa(struct drm_device *dev)
2230{
2231#ifdef CONFIG_INTEL_IOMMU
2232 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2233 return true;
2234#endif
2235 return false;
2236}
2237
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002238unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002239intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01002240 uint64_t fb_format_modifier, unsigned int plane)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002241{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002242 unsigned int tile_height;
2243 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002244
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002245 switch (fb_format_modifier) {
2246 case DRM_FORMAT_MOD_NONE:
2247 tile_height = 1;
2248 break;
2249 case I915_FORMAT_MOD_X_TILED:
2250 tile_height = IS_GEN2(dev) ? 16 : 8;
2251 break;
2252 case I915_FORMAT_MOD_Y_TILED:
2253 tile_height = 32;
2254 break;
2255 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01002256 pixel_bytes = drm_format_plane_cpp(pixel_format, plane);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002257 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002258 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002259 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002260 tile_height = 64;
2261 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002262 case 2:
2263 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002264 tile_height = 32;
2265 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002266 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002267 tile_height = 16;
2268 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002269 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002270 WARN_ONCE(1,
2271 "128-bit pixels are not supported for display!");
2272 tile_height = 16;
2273 break;
2274 }
2275 break;
2276 default:
2277 MISSING_CASE(fb_format_modifier);
2278 tile_height = 1;
2279 break;
2280 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002281
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002282 return tile_height;
2283}
2284
2285unsigned int
2286intel_fb_align_height(struct drm_device *dev, unsigned int height,
2287 uint32_t pixel_format, uint64_t fb_format_modifier)
2288{
2289 return ALIGN(height, intel_tile_height(dev, pixel_format,
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01002290 fb_format_modifier, 0));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002291}
2292
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002293static int
2294intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2295 const struct drm_plane_state *plane_state)
2296{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002297 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002298 unsigned int tile_height, tile_pitch;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002299
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002300 *view = i915_ggtt_view_normal;
2301
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002302 if (!plane_state)
2303 return 0;
2304
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002305 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002306 return 0;
2307
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002308 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002309
2310 info->height = fb->height;
2311 info->pixel_format = fb->pixel_format;
2312 info->pitch = fb->pitches[0];
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002313 info->uv_offset = fb->offsets[1];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002314 info->fb_modifier = fb->modifier[0];
2315
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002316 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01002317 fb->modifier[0], 0);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002318 tile_pitch = PAGE_SIZE / tile_height;
2319 info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2320 info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
2321 info->size = info->width_pages * info->height_pages * PAGE_SIZE;
2322
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002323 if (info->pixel_format == DRM_FORMAT_NV12) {
2324 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
2325 fb->modifier[0], 1);
2326 tile_pitch = PAGE_SIZE / tile_height;
2327 info->width_pages_uv = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2328 info->height_pages_uv = DIV_ROUND_UP(fb->height / 2,
2329 tile_height);
2330 info->size_uv = info->width_pages_uv * info->height_pages_uv *
2331 PAGE_SIZE;
2332 }
2333
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002334 return 0;
2335}
2336
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002337static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2338{
2339 if (INTEL_INFO(dev_priv)->gen >= 9)
2340 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002341 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2342 IS_VALLEYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002343 return 128 * 1024;
2344 else if (INTEL_INFO(dev_priv)->gen >= 4)
2345 return 4 * 1024;
2346 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002347 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002348}
2349
Chris Wilson127bd2a2010-07-23 23:32:05 +01002350int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002351intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2352 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002353 const struct drm_plane_state *plane_state,
John Harrison91af1272015-06-18 13:14:56 +01002354 struct intel_engine_cs *pipelined,
2355 struct drm_i915_gem_request **pipelined_request)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002356{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002357 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002358 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002359 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002360 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002361 u32 alignment;
2362 int ret;
2363
Matt Roperebcdd392014-07-09 16:22:11 -07002364 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2365
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002366 switch (fb->modifier[0]) {
2367 case DRM_FORMAT_MOD_NONE:
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002368 alignment = intel_linear_alignment(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002369 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002370 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002371 if (INTEL_INFO(dev)->gen >= 9)
2372 alignment = 256 * 1024;
2373 else {
2374 /* pin() will align the object as required by fence */
2375 alignment = 0;
2376 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002377 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002378 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002379 case I915_FORMAT_MOD_Yf_TILED:
2380 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2381 "Y tiling bo slipped through, driver bug!\n"))
2382 return -EINVAL;
2383 alignment = 1 * 1024 * 1024;
2384 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002385 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002386 MISSING_CASE(fb->modifier[0]);
2387 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002388 }
2389
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002390 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2391 if (ret)
2392 return ret;
2393
Chris Wilson693db182013-03-05 14:52:39 +00002394 /* Note that the w/a also requires 64 PTE of padding following the
2395 * bo. We currently fill all unused PTE with the shadow page and so
2396 * we should always have valid PTE following the scanout preventing
2397 * the VT-d warning.
2398 */
2399 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2400 alignment = 256 * 1024;
2401
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002402 /*
2403 * Global gtt pte registers are special registers which actually forward
2404 * writes to a chunk of system memory. Which means that there is no risk
2405 * that the register values disappear as soon as we call
2406 * intel_runtime_pm_put(), so it is correct to wrap only the
2407 * pin/unpin/fence and not more.
2408 */
2409 intel_runtime_pm_get(dev_priv);
2410
Chris Wilsonce453d82011-02-21 14:43:56 +00002411 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002412 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
John Harrison91af1272015-06-18 13:14:56 +01002413 pipelined_request, &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002414 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002415 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002416
2417 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2418 * fence, whereas 965+ only requires a fence if using
2419 * framebuffer compression. For simplicity, we always install
2420 * a fence as the cost is not that onerous.
2421 */
Chris Wilson06d98132012-04-17 15:31:24 +01002422 ret = i915_gem_object_get_fence(obj);
Maarten Lankhorst842315e2015-08-05 12:37:11 +02002423 if (ret == -EDEADLK) {
2424 /*
2425 * -EDEADLK means there are no free fences
2426 * no pending flips.
2427 *
2428 * This is propagated to atomic, but it uses
2429 * -EDEADLK to force a locking recovery, so
2430 * change the returned error to -EBUSY.
2431 */
2432 ret = -EBUSY;
2433 goto err_unpin;
2434 } else if (ret)
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002435 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002436
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002437 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002438
Chris Wilsonce453d82011-02-21 14:43:56 +00002439 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002440 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002441 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002442
2443err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002444 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002445err_interruptible:
2446 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002447 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002448 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002449}
2450
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002451static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2452 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002453{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002454 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002455 struct i915_ggtt_view view;
2456 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002457
Matt Roperebcdd392014-07-09 16:22:11 -07002458 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2459
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002460 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2461 WARN_ONCE(ret, "Couldn't get view from plane state!");
2462
Chris Wilson1690e1e2011-12-14 13:57:08 +01002463 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002464 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002465}
2466
Daniel Vetterc2c75132012-07-05 12:17:30 +02002467/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2468 * is assumed to be a power-of-two. */
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002469unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2470 int *x, int *y,
Chris Wilsonbc752862013-02-21 20:04:31 +00002471 unsigned int tiling_mode,
2472 unsigned int cpp,
2473 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002474{
Chris Wilsonbc752862013-02-21 20:04:31 +00002475 if (tiling_mode != I915_TILING_NONE) {
2476 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002477
Chris Wilsonbc752862013-02-21 20:04:31 +00002478 tile_rows = *y / 8;
2479 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002480
Chris Wilsonbc752862013-02-21 20:04:31 +00002481 tiles = *x / (512/cpp);
2482 *x %= 512/cpp;
2483
2484 return tile_rows * pitch * 8 + tiles * 4096;
2485 } else {
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002486 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
Chris Wilsonbc752862013-02-21 20:04:31 +00002487 unsigned int offset;
2488
2489 offset = *y * pitch + *x * cpp;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002490 *y = (offset & alignment) / pitch;
2491 *x = ((offset & alignment) - *y * pitch) / cpp;
2492 return offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002493 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002494}
2495
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002496static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002497{
2498 switch (format) {
2499 case DISPPLANE_8BPP:
2500 return DRM_FORMAT_C8;
2501 case DISPPLANE_BGRX555:
2502 return DRM_FORMAT_XRGB1555;
2503 case DISPPLANE_BGRX565:
2504 return DRM_FORMAT_RGB565;
2505 default:
2506 case DISPPLANE_BGRX888:
2507 return DRM_FORMAT_XRGB8888;
2508 case DISPPLANE_RGBX888:
2509 return DRM_FORMAT_XBGR8888;
2510 case DISPPLANE_BGRX101010:
2511 return DRM_FORMAT_XRGB2101010;
2512 case DISPPLANE_RGBX101010:
2513 return DRM_FORMAT_XBGR2101010;
2514 }
2515}
2516
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002517static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2518{
2519 switch (format) {
2520 case PLANE_CTL_FORMAT_RGB_565:
2521 return DRM_FORMAT_RGB565;
2522 default:
2523 case PLANE_CTL_FORMAT_XRGB_8888:
2524 if (rgb_order) {
2525 if (alpha)
2526 return DRM_FORMAT_ABGR8888;
2527 else
2528 return DRM_FORMAT_XBGR8888;
2529 } else {
2530 if (alpha)
2531 return DRM_FORMAT_ARGB8888;
2532 else
2533 return DRM_FORMAT_XRGB8888;
2534 }
2535 case PLANE_CTL_FORMAT_XRGB_2101010:
2536 if (rgb_order)
2537 return DRM_FORMAT_XBGR2101010;
2538 else
2539 return DRM_FORMAT_XRGB2101010;
2540 }
2541}
2542
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002543static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002544intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2545 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002546{
2547 struct drm_device *dev = crtc->base.dev;
2548 struct drm_i915_gem_object *obj = NULL;
2549 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002550 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002551 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2552 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2553 PAGE_SIZE);
2554
2555 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002556
Chris Wilsonff2652e2014-03-10 08:07:02 +00002557 if (plane_config->size == 0)
2558 return false;
2559
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002560 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2561 base_aligned,
2562 base_aligned,
2563 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002564 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002565 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002566
Damien Lespiau49af4492015-01-20 12:51:44 +00002567 obj->tiling_mode = plane_config->tiling;
2568 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002569 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002570
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002571 mode_cmd.pixel_format = fb->pixel_format;
2572 mode_cmd.width = fb->width;
2573 mode_cmd.height = fb->height;
2574 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002575 mode_cmd.modifier[0] = fb->modifier[0];
2576 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002577
2578 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002579 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002580 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002581 DRM_DEBUG_KMS("intel fb init failed\n");
2582 goto out_unref_obj;
2583 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002584 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002585
Daniel Vetterf6936e22015-03-26 12:17:05 +01002586 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002587 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002588
2589out_unref_obj:
2590 drm_gem_object_unreference(&obj->base);
2591 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002592 return false;
2593}
2594
Matt Roperafd65eb2015-02-03 13:10:04 -08002595/* Update plane->state->fb to match plane->fb after driver-internal updates */
2596static void
2597update_state_fb(struct drm_plane *plane)
2598{
2599 if (plane->fb == plane->state->fb)
2600 return;
2601
2602 if (plane->state->fb)
2603 drm_framebuffer_unreference(plane->state->fb);
2604 plane->state->fb = plane->fb;
2605 if (plane->state->fb)
2606 drm_framebuffer_reference(plane->state->fb);
2607}
2608
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002609static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002610intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2611 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002612{
2613 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002614 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002615 struct drm_crtc *c;
2616 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002617 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002618 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002619 struct drm_plane_state *plane_state = primary->state;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002620 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002621
Damien Lespiau2d140302015-02-05 17:22:18 +00002622 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002623 return;
2624
Daniel Vetterf6936e22015-03-26 12:17:05 +01002625 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002626 fb = &plane_config->fb->base;
2627 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002628 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002629
Damien Lespiau2d140302015-02-05 17:22:18 +00002630 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002631
2632 /*
2633 * Failed to alloc the obj, check to see if we should share
2634 * an fb with another CRTC instead
2635 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002636 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002637 i = to_intel_crtc(c);
2638
2639 if (c == &intel_crtc->base)
2640 continue;
2641
Matt Roper2ff8fde2014-07-08 07:50:07 -07002642 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002643 continue;
2644
Daniel Vetter88595ac2015-03-26 12:42:24 +01002645 fb = c->primary->fb;
2646 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002647 continue;
2648
Daniel Vetter88595ac2015-03-26 12:42:24 +01002649 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002650 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002651 drm_framebuffer_reference(fb);
2652 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002653 }
2654 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002655
2656 return;
2657
2658valid_fb:
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002659 plane_state->src_x = plane_state->src_y = 0;
2660 plane_state->src_w = fb->width << 16;
2661 plane_state->src_h = fb->height << 16;
2662
2663 plane_state->crtc_x = plane_state->src_y = 0;
2664 plane_state->crtc_w = fb->width;
2665 plane_state->crtc_h = fb->height;
2666
Daniel Vetter88595ac2015-03-26 12:42:24 +01002667 obj = intel_fb_obj(fb);
2668 if (obj->tiling_mode != I915_TILING_NONE)
2669 dev_priv->preserve_bios_swizzle = true;
2670
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002671 drm_framebuffer_reference(fb);
2672 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002673 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002674 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002675 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002676}
2677
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002678static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2679 struct drm_framebuffer *fb,
2680 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002681{
2682 struct drm_device *dev = crtc->dev;
2683 struct drm_i915_private *dev_priv = dev->dev_private;
2684 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002685 struct drm_plane *primary = crtc->primary;
2686 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002687 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002688 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002689 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002690 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002691 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302692 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002693
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002694 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002695 I915_WRITE(reg, 0);
2696 if (INTEL_INFO(dev)->gen >= 4)
2697 I915_WRITE(DSPSURF(plane), 0);
2698 else
2699 I915_WRITE(DSPADDR(plane), 0);
2700 POSTING_READ(reg);
2701 return;
2702 }
2703
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002704 obj = intel_fb_obj(fb);
2705 if (WARN_ON(obj == NULL))
2706 return;
2707
2708 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2709
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002710 dspcntr = DISPPLANE_GAMMA_ENABLE;
2711
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002712 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002713
2714 if (INTEL_INFO(dev)->gen < 4) {
2715 if (intel_crtc->pipe == PIPE_B)
2716 dspcntr |= DISPPLANE_SEL_PIPE_B;
2717
2718 /* pipesrc and dspsize control the size that is scaled from,
2719 * which should always be the user's requested size.
2720 */
2721 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002722 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2723 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002724 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002725 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2726 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002727 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2728 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002729 I915_WRITE(PRIMPOS(plane), 0);
2730 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002731 }
2732
Ville Syrjälä57779d02012-10-31 17:50:14 +02002733 switch (fb->pixel_format) {
2734 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002735 dspcntr |= DISPPLANE_8BPP;
2736 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002737 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002738 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002739 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002740 case DRM_FORMAT_RGB565:
2741 dspcntr |= DISPPLANE_BGRX565;
2742 break;
2743 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002744 dspcntr |= DISPPLANE_BGRX888;
2745 break;
2746 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002747 dspcntr |= DISPPLANE_RGBX888;
2748 break;
2749 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002750 dspcntr |= DISPPLANE_BGRX101010;
2751 break;
2752 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002753 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002754 break;
2755 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002756 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002757 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002758
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002759 if (INTEL_INFO(dev)->gen >= 4 &&
2760 obj->tiling_mode != I915_TILING_NONE)
2761 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002762
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002763 if (IS_G4X(dev))
2764 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2765
Ville Syrjäläb98971272014-08-27 16:51:22 +03002766 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002767
Daniel Vetterc2c75132012-07-05 12:17:30 +02002768 if (INTEL_INFO(dev)->gen >= 4) {
2769 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002770 intel_gen4_compute_page_offset(dev_priv,
2771 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002772 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002773 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002774 linear_offset -= intel_crtc->dspaddr_offset;
2775 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002776 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002777 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002778
Matt Roper8e7d6882015-01-21 16:35:41 -08002779 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302780 dspcntr |= DISPPLANE_ROTATE_180;
2781
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002782 x += (intel_crtc->config->pipe_src_w - 1);
2783 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302784
2785 /* Finding the last pixel of the last line of the display
2786 data and adding to linear_offset*/
2787 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002788 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2789 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302790 }
2791
Paulo Zanoni2db33662015-09-14 15:20:03 -03002792 intel_crtc->adjusted_x = x;
2793 intel_crtc->adjusted_y = y;
2794
Sonika Jindal48404c12014-08-22 14:06:04 +05302795 I915_WRITE(reg, dspcntr);
2796
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002797 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002798 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002799 I915_WRITE(DSPSURF(plane),
2800 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002801 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002802 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002803 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002804 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002805 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002806}
2807
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002808static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2809 struct drm_framebuffer *fb,
2810 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002811{
2812 struct drm_device *dev = crtc->dev;
2813 struct drm_i915_private *dev_priv = dev->dev_private;
2814 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002815 struct drm_plane *primary = crtc->primary;
2816 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002817 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002818 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002819 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002820 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002821 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302822 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002823
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002824 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002825 I915_WRITE(reg, 0);
2826 I915_WRITE(DSPSURF(plane), 0);
2827 POSTING_READ(reg);
2828 return;
2829 }
2830
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002831 obj = intel_fb_obj(fb);
2832 if (WARN_ON(obj == NULL))
2833 return;
2834
2835 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2836
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002837 dspcntr = DISPPLANE_GAMMA_ENABLE;
2838
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002839 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002840
2841 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2842 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2843
Ville Syrjälä57779d02012-10-31 17:50:14 +02002844 switch (fb->pixel_format) {
2845 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002846 dspcntr |= DISPPLANE_8BPP;
2847 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002848 case DRM_FORMAT_RGB565:
2849 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002850 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002851 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002852 dspcntr |= DISPPLANE_BGRX888;
2853 break;
2854 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002855 dspcntr |= DISPPLANE_RGBX888;
2856 break;
2857 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002858 dspcntr |= DISPPLANE_BGRX101010;
2859 break;
2860 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002861 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002862 break;
2863 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002864 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002865 }
2866
2867 if (obj->tiling_mode != I915_TILING_NONE)
2868 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002869
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002870 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002871 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002872
Ville Syrjäläb98971272014-08-27 16:51:22 +03002873 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002874 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002875 intel_gen4_compute_page_offset(dev_priv,
2876 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002877 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002878 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002879 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002880 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302881 dspcntr |= DISPPLANE_ROTATE_180;
2882
2883 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002884 x += (intel_crtc->config->pipe_src_w - 1);
2885 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302886
2887 /* Finding the last pixel of the last line of the display
2888 data and adding to linear_offset*/
2889 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002890 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2891 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302892 }
2893 }
2894
Paulo Zanoni2db33662015-09-14 15:20:03 -03002895 intel_crtc->adjusted_x = x;
2896 intel_crtc->adjusted_y = y;
2897
Sonika Jindal48404c12014-08-22 14:06:04 +05302898 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002899
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002900 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002901 I915_WRITE(DSPSURF(plane),
2902 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002903 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002904 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2905 } else {
2906 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2907 I915_WRITE(DSPLINOFF(plane), linear_offset);
2908 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002909 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002910}
2911
Damien Lespiaub3218032015-02-27 11:15:18 +00002912u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2913 uint32_t pixel_format)
2914{
2915 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2916
2917 /*
2918 * The stride is either expressed as a multiple of 64 bytes
2919 * chunks for linear buffers or in number of tiles for tiled
2920 * buffers.
2921 */
2922 switch (fb_modifier) {
2923 case DRM_FORMAT_MOD_NONE:
2924 return 64;
2925 case I915_FORMAT_MOD_X_TILED:
2926 if (INTEL_INFO(dev)->gen == 2)
2927 return 128;
2928 return 512;
2929 case I915_FORMAT_MOD_Y_TILED:
2930 /* No need to check for old gens and Y tiling since this is
2931 * about the display engine and those will be blocked before
2932 * we get here.
2933 */
2934 return 128;
2935 case I915_FORMAT_MOD_Yf_TILED:
2936 if (bits_per_pixel == 8)
2937 return 64;
2938 else
2939 return 128;
2940 default:
2941 MISSING_CASE(fb_modifier);
2942 return 64;
2943 }
2944}
2945
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002946unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002947 struct drm_i915_gem_object *obj,
2948 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002949{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002950 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002951 struct i915_vma *vma;
2952 unsigned char *offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002953
2954 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002955 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002956
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002957 vma = i915_gem_obj_to_ggtt_view(obj, view);
2958 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
2959 view->type))
2960 return -1;
2961
2962 offset = (unsigned char *)vma->node.start;
2963
2964 if (plane == 1) {
2965 offset += vma->ggtt_view.rotation_info.uv_start_page *
2966 PAGE_SIZE;
2967 }
2968
2969 return (unsigned long)offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002970}
2971
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002972static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2973{
2974 struct drm_device *dev = intel_crtc->base.dev;
2975 struct drm_i915_private *dev_priv = dev->dev_private;
2976
2977 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2978 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2979 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002980}
2981
Chandra Kondurua1b22782015-04-07 15:28:45 -07002982/*
2983 * This function detaches (aka. unbinds) unused scalers in hardware
2984 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002985static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002986{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002987 struct intel_crtc_scaler_state *scaler_state;
2988 int i;
2989
Chandra Kondurua1b22782015-04-07 15:28:45 -07002990 scaler_state = &intel_crtc->config->scaler_state;
2991
2992 /* loop through and disable scalers that aren't in use */
2993 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002994 if (!scaler_state->scalers[i].in_use)
2995 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002996 }
2997}
2998
Chandra Konduru6156a452015-04-27 13:48:39 -07002999u32 skl_plane_ctl_format(uint32_t pixel_format)
3000{
Chandra Konduru6156a452015-04-27 13:48:39 -07003001 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01003002 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003003 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07003004 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003005 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07003006 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003007 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003008 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003009 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003010 /*
3011 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3012 * to be already pre-multiplied. We need to add a knob (or a different
3013 * DRM_FORMAT) for user-space to configure that.
3014 */
3015 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003016 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07003017 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003018 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003019 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07003020 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003021 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003022 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003023 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003024 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003025 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003026 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003027 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003028 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003029 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003030 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003031 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003032 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003033 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003034 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003035 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003036
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003037 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003038}
3039
3040u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3041{
Chandra Konduru6156a452015-04-27 13:48:39 -07003042 switch (fb_modifier) {
3043 case DRM_FORMAT_MOD_NONE:
3044 break;
3045 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003046 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003047 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003048 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003049 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003050 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003051 default:
3052 MISSING_CASE(fb_modifier);
3053 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003054
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003055 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003056}
3057
3058u32 skl_plane_ctl_rotation(unsigned int rotation)
3059{
Chandra Konduru6156a452015-04-27 13:48:39 -07003060 switch (rotation) {
3061 case BIT(DRM_ROTATE_0):
3062 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303063 /*
3064 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3065 * while i915 HW rotation is clockwise, thats why this swapping.
3066 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003067 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303068 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003069 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003070 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003071 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303072 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003073 default:
3074 MISSING_CASE(rotation);
3075 }
3076
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003077 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003078}
3079
Damien Lespiau70d21f02013-07-03 21:06:04 +01003080static void skylake_update_primary_plane(struct drm_crtc *crtc,
3081 struct drm_framebuffer *fb,
3082 int x, int y)
3083{
3084 struct drm_device *dev = crtc->dev;
3085 struct drm_i915_private *dev_priv = dev->dev_private;
3086 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003087 struct drm_plane *plane = crtc->primary;
3088 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003089 struct drm_i915_gem_object *obj;
3090 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303091 u32 plane_ctl, stride_div, stride;
3092 u32 tile_height, plane_offset, plane_size;
3093 unsigned int rotation;
3094 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003095 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003096 struct intel_crtc_state *crtc_state = intel_crtc->config;
3097 struct intel_plane_state *plane_state;
3098 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3099 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3100 int scaler_id = -1;
3101
Chandra Konduru6156a452015-04-27 13:48:39 -07003102 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003103
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003104 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003105 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3106 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3107 POSTING_READ(PLANE_CTL(pipe, 0));
3108 return;
3109 }
3110
3111 plane_ctl = PLANE_CTL_ENABLE |
3112 PLANE_CTL_PIPE_GAMMA_ENABLE |
3113 PLANE_CTL_PIPE_CSC_ENABLE;
3114
Chandra Konduru6156a452015-04-27 13:48:39 -07003115 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3116 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003117 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303118
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303119 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003120 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003121
Damien Lespiaub3218032015-02-27 11:15:18 +00003122 obj = intel_fb_obj(fb);
3123 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3124 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003125 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303126
Chandra Konduru6156a452015-04-27 13:48:39 -07003127 /*
3128 * FIXME: intel_plane_state->src, dst aren't set when transitional
3129 * update_plane helpers are called from legacy paths.
3130 * Once full atomic crtc is available, below check can be avoided.
3131 */
3132 if (drm_rect_width(&plane_state->src)) {
3133 scaler_id = plane_state->scaler_id;
3134 src_x = plane_state->src.x1 >> 16;
3135 src_y = plane_state->src.y1 >> 16;
3136 src_w = drm_rect_width(&plane_state->src) >> 16;
3137 src_h = drm_rect_height(&plane_state->src) >> 16;
3138 dst_x = plane_state->dst.x1;
3139 dst_y = plane_state->dst.y1;
3140 dst_w = drm_rect_width(&plane_state->dst);
3141 dst_h = drm_rect_height(&plane_state->dst);
3142
3143 WARN_ON(x != src_x || y != src_y);
3144 } else {
3145 src_w = intel_crtc->config->pipe_src_w;
3146 src_h = intel_crtc->config->pipe_src_h;
3147 }
3148
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303149 if (intel_rotation_90_or_270(rotation)) {
3150 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003151 tile_height = intel_tile_height(dev, fb->pixel_format,
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01003152 fb->modifier[0], 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303153 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003154 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303155 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003156 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303157 } else {
3158 stride = fb->pitches[0] / stride_div;
3159 x_offset = x;
3160 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003161 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303162 }
3163 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003164
Paulo Zanoni2db33662015-09-14 15:20:03 -03003165 intel_crtc->adjusted_x = x_offset;
3166 intel_crtc->adjusted_y = y_offset;
3167
Damien Lespiau70d21f02013-07-03 21:06:04 +01003168 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303169 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3170 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3171 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003172
3173 if (scaler_id >= 0) {
3174 uint32_t ps_ctrl = 0;
3175
3176 WARN_ON(!dst_w || !dst_h);
3177 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3178 crtc_state->scaler_state.scalers[scaler_id].mode;
3179 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3180 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3181 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3182 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3183 I915_WRITE(PLANE_POS(pipe, 0), 0);
3184 } else {
3185 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3186 }
3187
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003188 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003189
3190 POSTING_READ(PLANE_SURF(pipe, 0));
3191}
3192
Jesse Barnes17638cd2011-06-24 12:19:23 -07003193/* Assume fb object is pinned & idle & fenced and just update base pointers */
3194static int
3195intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3196 int x, int y, enum mode_set_atomic state)
3197{
3198 struct drm_device *dev = crtc->dev;
3199 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003200
Paulo Zanoniff2a3112015-07-07 15:26:03 -03003201 if (dev_priv->fbc.disable_fbc)
Paulo Zanoni7733b492015-07-07 15:26:04 -03003202 dev_priv->fbc.disable_fbc(dev_priv);
Jesse Barnes81255562010-08-02 12:07:50 -07003203
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003204 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3205
3206 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003207}
3208
Ville Syrjälä75147472014-11-24 18:28:11 +02003209static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003210{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003211 struct drm_crtc *crtc;
3212
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003213 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003214 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3215 enum plane plane = intel_crtc->plane;
3216
3217 intel_prepare_page_flip(dev, plane);
3218 intel_finish_page_flip_plane(dev, plane);
3219 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003220}
3221
3222static void intel_update_primary_planes(struct drm_device *dev)
3223{
Ville Syrjälä75147472014-11-24 18:28:11 +02003224 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003225
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003226 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003227 struct intel_plane *plane = to_intel_plane(crtc->primary);
3228 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003229
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003230 drm_modeset_lock_crtc(crtc, &plane->base);
3231
3232 plane_state = to_intel_plane_state(plane->base.state);
3233
3234 if (plane_state->base.fb)
3235 plane->commit_plane(&plane->base, plane_state);
3236
3237 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003238 }
3239}
3240
Ville Syrjälä75147472014-11-24 18:28:11 +02003241void intel_prepare_reset(struct drm_device *dev)
3242{
3243 /* no reset support for gen2 */
3244 if (IS_GEN2(dev))
3245 return;
3246
3247 /* reset doesn't touch the display */
3248 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3249 return;
3250
3251 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003252 /*
3253 * Disabling the crtcs gracefully seems nicer. Also the
3254 * g33 docs say we should at least disable all the planes.
3255 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003256 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003257}
3258
3259void intel_finish_reset(struct drm_device *dev)
3260{
3261 struct drm_i915_private *dev_priv = to_i915(dev);
3262
3263 /*
3264 * Flips in the rings will be nuked by the reset,
3265 * so complete all pending flips so that user space
3266 * will get its events and not get stuck.
3267 */
3268 intel_complete_page_flips(dev);
3269
3270 /* no reset support for gen2 */
3271 if (IS_GEN2(dev))
3272 return;
3273
3274 /* reset doesn't touch the display */
3275 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3276 /*
3277 * Flips in the rings have been nuked by the reset,
3278 * so update the base address of all primary
3279 * planes to the the last fb to make sure we're
3280 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003281 *
3282 * FIXME: Atomic will make this obsolete since we won't schedule
3283 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003284 */
3285 intel_update_primary_planes(dev);
3286 return;
3287 }
3288
3289 /*
3290 * The display has been reset as well,
3291 * so need a full re-initialization.
3292 */
3293 intel_runtime_pm_disable_interrupts(dev_priv);
3294 intel_runtime_pm_enable_interrupts(dev_priv);
3295
3296 intel_modeset_init_hw(dev);
3297
3298 spin_lock_irq(&dev_priv->irq_lock);
3299 if (dev_priv->display.hpd_irq_setup)
3300 dev_priv->display.hpd_irq_setup(dev);
3301 spin_unlock_irq(&dev_priv->irq_lock);
3302
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003303 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003304
3305 intel_hpd_init(dev_priv);
3306
3307 drm_modeset_unlock_all(dev);
3308}
3309
Chris Wilson2e2f3512015-04-27 13:41:14 +01003310static void
Chris Wilson14667a42012-04-03 17:58:35 +01003311intel_finish_fb(struct drm_framebuffer *old_fb)
3312{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003313 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003314 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003315 bool was_interruptible = dev_priv->mm.interruptible;
3316 int ret;
3317
Chris Wilson14667a42012-04-03 17:58:35 +01003318 /* Big Hammer, we also need to ensure that any pending
3319 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3320 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003321 * framebuffer. Note that we rely on userspace rendering
3322 * into the buffer attached to the pipe they are waiting
3323 * on. If not, userspace generates a GPU hang with IPEHR
3324 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003325 *
3326 * This should only fail upon a hung GPU, in which case we
3327 * can safely continue.
3328 */
3329 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003330 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003331 dev_priv->mm.interruptible = was_interruptible;
3332
Chris Wilson2e2f3512015-04-27 13:41:14 +01003333 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003334}
3335
Chris Wilson7d5e3792014-03-04 13:15:08 +00003336static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3337{
3338 struct drm_device *dev = crtc->dev;
3339 struct drm_i915_private *dev_priv = dev->dev_private;
3340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003341 bool pending;
3342
3343 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3344 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3345 return false;
3346
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003347 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003348 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003349 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003350
3351 return pending;
3352}
3353
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003354static void intel_update_pipe_config(struct intel_crtc *crtc,
3355 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003356{
3357 struct drm_device *dev = crtc->base.dev;
3358 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003359 struct intel_crtc_state *pipe_config =
3360 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003361
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003362 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3363 crtc->base.mode = crtc->base.state->mode;
3364
3365 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3366 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3367 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003368
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003369 if (HAS_DDI(dev))
3370 intel_set_pipe_csc(&crtc->base);
3371
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003372 /*
3373 * Update pipe size and adjust fitter if needed: the reason for this is
3374 * that in compute_mode_changes we check the native mode (not the pfit
3375 * mode) to see if we can flip rather than do a full mode set. In the
3376 * fastboot case, we'll flip, but if we don't update the pipesrc and
3377 * pfit state, we'll end up with a big fb scanned out into the wrong
3378 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003379 */
3380
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003381 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003382 ((pipe_config->pipe_src_w - 1) << 16) |
3383 (pipe_config->pipe_src_h - 1));
3384
3385 /* on skylake this is done by detaching scalers */
3386 if (INTEL_INFO(dev)->gen >= 9) {
3387 skl_detach_scalers(crtc);
3388
3389 if (pipe_config->pch_pfit.enabled)
3390 skylake_pfit_enable(crtc);
3391 } else if (HAS_PCH_SPLIT(dev)) {
3392 if (pipe_config->pch_pfit.enabled)
3393 ironlake_pfit_enable(crtc);
3394 else if (old_crtc_state->pch_pfit.enabled)
3395 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003396 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003397}
3398
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003399static void intel_fdi_normal_train(struct drm_crtc *crtc)
3400{
3401 struct drm_device *dev = crtc->dev;
3402 struct drm_i915_private *dev_priv = dev->dev_private;
3403 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3404 int pipe = intel_crtc->pipe;
3405 u32 reg, temp;
3406
3407 /* enable normal train */
3408 reg = FDI_TX_CTL(pipe);
3409 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003410 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003411 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3412 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003413 } else {
3414 temp &= ~FDI_LINK_TRAIN_NONE;
3415 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003416 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003417 I915_WRITE(reg, temp);
3418
3419 reg = FDI_RX_CTL(pipe);
3420 temp = I915_READ(reg);
3421 if (HAS_PCH_CPT(dev)) {
3422 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3423 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3424 } else {
3425 temp &= ~FDI_LINK_TRAIN_NONE;
3426 temp |= FDI_LINK_TRAIN_NONE;
3427 }
3428 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3429
3430 /* wait one idle pattern time */
3431 POSTING_READ(reg);
3432 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003433
3434 /* IVB wants error correction enabled */
3435 if (IS_IVYBRIDGE(dev))
3436 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3437 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003438}
3439
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003440/* The FDI link training functions for ILK/Ibexpeak. */
3441static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3442{
3443 struct drm_device *dev = crtc->dev;
3444 struct drm_i915_private *dev_priv = dev->dev_private;
3445 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3446 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003447 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003448
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003449 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003450 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003451
Adam Jacksone1a44742010-06-25 15:32:14 -04003452 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3453 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003454 reg = FDI_RX_IMR(pipe);
3455 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003456 temp &= ~FDI_RX_SYMBOL_LOCK;
3457 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003458 I915_WRITE(reg, temp);
3459 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003460 udelay(150);
3461
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003462 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003463 reg = FDI_TX_CTL(pipe);
3464 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003465 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003466 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467 temp &= ~FDI_LINK_TRAIN_NONE;
3468 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003470
Chris Wilson5eddb702010-09-11 13:48:45 +01003471 reg = FDI_RX_CTL(pipe);
3472 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473 temp &= ~FDI_LINK_TRAIN_NONE;
3474 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003475 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3476
3477 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003478 udelay(150);
3479
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003480 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003481 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3482 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3483 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003484
Chris Wilson5eddb702010-09-11 13:48:45 +01003485 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003486 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003487 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003488 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3489
3490 if ((temp & FDI_RX_BIT_LOCK)) {
3491 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003492 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003493 break;
3494 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003495 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003496 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003497 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003498
3499 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003500 reg = FDI_TX_CTL(pipe);
3501 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003502 temp &= ~FDI_LINK_TRAIN_NONE;
3503 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003504 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003505
Chris Wilson5eddb702010-09-11 13:48:45 +01003506 reg = FDI_RX_CTL(pipe);
3507 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003508 temp &= ~FDI_LINK_TRAIN_NONE;
3509 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003510 I915_WRITE(reg, temp);
3511
3512 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003513 udelay(150);
3514
Chris Wilson5eddb702010-09-11 13:48:45 +01003515 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003516 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003517 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003518 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3519
3520 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003521 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003522 DRM_DEBUG_KMS("FDI train 2 done.\n");
3523 break;
3524 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003525 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003526 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003527 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003528
3529 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003530
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003531}
3532
Akshay Joshi0206e352011-08-16 15:34:10 -04003533static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003534 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3535 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3536 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3537 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3538};
3539
3540/* The FDI link training functions for SNB/Cougarpoint. */
3541static void gen6_fdi_link_train(struct drm_crtc *crtc)
3542{
3543 struct drm_device *dev = crtc->dev;
3544 struct drm_i915_private *dev_priv = dev->dev_private;
3545 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3546 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003547 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003548
Adam Jacksone1a44742010-06-25 15:32:14 -04003549 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3550 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003551 reg = FDI_RX_IMR(pipe);
3552 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003553 temp &= ~FDI_RX_SYMBOL_LOCK;
3554 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003555 I915_WRITE(reg, temp);
3556
3557 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003558 udelay(150);
3559
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003560 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003561 reg = FDI_TX_CTL(pipe);
3562 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003563 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003564 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003565 temp &= ~FDI_LINK_TRAIN_NONE;
3566 temp |= FDI_LINK_TRAIN_PATTERN_1;
3567 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3568 /* SNB-B */
3569 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003570 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003571
Daniel Vetterd74cf322012-10-26 10:58:13 +02003572 I915_WRITE(FDI_RX_MISC(pipe),
3573 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3574
Chris Wilson5eddb702010-09-11 13:48:45 +01003575 reg = FDI_RX_CTL(pipe);
3576 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003577 if (HAS_PCH_CPT(dev)) {
3578 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3579 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3580 } else {
3581 temp &= ~FDI_LINK_TRAIN_NONE;
3582 temp |= FDI_LINK_TRAIN_PATTERN_1;
3583 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003584 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3585
3586 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003587 udelay(150);
3588
Akshay Joshi0206e352011-08-16 15:34:10 -04003589 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003590 reg = FDI_TX_CTL(pipe);
3591 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003592 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3593 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003594 I915_WRITE(reg, temp);
3595
3596 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003597 udelay(500);
3598
Sean Paulfa37d392012-03-02 12:53:39 -05003599 for (retry = 0; retry < 5; retry++) {
3600 reg = FDI_RX_IIR(pipe);
3601 temp = I915_READ(reg);
3602 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3603 if (temp & FDI_RX_BIT_LOCK) {
3604 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3605 DRM_DEBUG_KMS("FDI train 1 done.\n");
3606 break;
3607 }
3608 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003609 }
Sean Paulfa37d392012-03-02 12:53:39 -05003610 if (retry < 5)
3611 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003612 }
3613 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003614 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003615
3616 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003617 reg = FDI_TX_CTL(pipe);
3618 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003619 temp &= ~FDI_LINK_TRAIN_NONE;
3620 temp |= FDI_LINK_TRAIN_PATTERN_2;
3621 if (IS_GEN6(dev)) {
3622 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3623 /* SNB-B */
3624 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3625 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003626 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003627
Chris Wilson5eddb702010-09-11 13:48:45 +01003628 reg = FDI_RX_CTL(pipe);
3629 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003630 if (HAS_PCH_CPT(dev)) {
3631 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3632 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3633 } else {
3634 temp &= ~FDI_LINK_TRAIN_NONE;
3635 temp |= FDI_LINK_TRAIN_PATTERN_2;
3636 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003637 I915_WRITE(reg, temp);
3638
3639 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003640 udelay(150);
3641
Akshay Joshi0206e352011-08-16 15:34:10 -04003642 for (i = 0; i < 4; i++) {
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_VOL_EMP_MASK;
3646 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003647 I915_WRITE(reg, temp);
3648
3649 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003650 udelay(500);
3651
Sean Paulfa37d392012-03-02 12:53:39 -05003652 for (retry = 0; retry < 5; retry++) {
3653 reg = FDI_RX_IIR(pipe);
3654 temp = I915_READ(reg);
3655 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3656 if (temp & FDI_RX_SYMBOL_LOCK) {
3657 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3658 DRM_DEBUG_KMS("FDI train 2 done.\n");
3659 break;
3660 }
3661 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003662 }
Sean Paulfa37d392012-03-02 12:53:39 -05003663 if (retry < 5)
3664 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003665 }
3666 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003667 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003668
3669 DRM_DEBUG_KMS("FDI train done.\n");
3670}
3671
Jesse Barnes357555c2011-04-28 15:09:55 -07003672/* Manual link training for Ivy Bridge A0 parts */
3673static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3674{
3675 struct drm_device *dev = crtc->dev;
3676 struct drm_i915_private *dev_priv = dev->dev_private;
3677 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3678 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003679 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003680
3681 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3682 for train result */
3683 reg = FDI_RX_IMR(pipe);
3684 temp = I915_READ(reg);
3685 temp &= ~FDI_RX_SYMBOL_LOCK;
3686 temp &= ~FDI_RX_BIT_LOCK;
3687 I915_WRITE(reg, temp);
3688
3689 POSTING_READ(reg);
3690 udelay(150);
3691
Daniel Vetter01a415f2012-10-27 15:58:40 +02003692 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3693 I915_READ(FDI_RX_IIR(pipe)));
3694
Jesse Barnes139ccd32013-08-19 11:04:55 -07003695 /* Try each vswing and preemphasis setting twice before moving on */
3696 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3697 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003698 reg = FDI_TX_CTL(pipe);
3699 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003700 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3701 temp &= ~FDI_TX_ENABLE;
3702 I915_WRITE(reg, temp);
3703
3704 reg = FDI_RX_CTL(pipe);
3705 temp = I915_READ(reg);
3706 temp &= ~FDI_LINK_TRAIN_AUTO;
3707 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3708 temp &= ~FDI_RX_ENABLE;
3709 I915_WRITE(reg, temp);
3710
3711 /* enable CPU FDI TX and PCH FDI RX */
3712 reg = FDI_TX_CTL(pipe);
3713 temp = I915_READ(reg);
3714 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003715 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003716 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003717 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003718 temp |= snb_b_fdi_train_param[j/2];
3719 temp |= FDI_COMPOSITE_SYNC;
3720 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3721
3722 I915_WRITE(FDI_RX_MISC(pipe),
3723 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3724
3725 reg = FDI_RX_CTL(pipe);
3726 temp = I915_READ(reg);
3727 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3728 temp |= FDI_COMPOSITE_SYNC;
3729 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3730
3731 POSTING_READ(reg);
3732 udelay(1); /* should be 0.5us */
3733
3734 for (i = 0; i < 4; i++) {
3735 reg = FDI_RX_IIR(pipe);
3736 temp = I915_READ(reg);
3737 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3738
3739 if (temp & FDI_RX_BIT_LOCK ||
3740 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3741 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3742 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3743 i);
3744 break;
3745 }
3746 udelay(1); /* should be 0.5us */
3747 }
3748 if (i == 4) {
3749 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3750 continue;
3751 }
3752
3753 /* Train 2 */
3754 reg = FDI_TX_CTL(pipe);
3755 temp = I915_READ(reg);
3756 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3757 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3758 I915_WRITE(reg, temp);
3759
3760 reg = FDI_RX_CTL(pipe);
3761 temp = I915_READ(reg);
3762 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3763 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003764 I915_WRITE(reg, temp);
3765
3766 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003767 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003768
Jesse Barnes139ccd32013-08-19 11:04:55 -07003769 for (i = 0; i < 4; i++) {
3770 reg = FDI_RX_IIR(pipe);
3771 temp = I915_READ(reg);
3772 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003773
Jesse Barnes139ccd32013-08-19 11:04:55 -07003774 if (temp & FDI_RX_SYMBOL_LOCK ||
3775 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3776 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3777 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3778 i);
3779 goto train_done;
3780 }
3781 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003782 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003783 if (i == 4)
3784 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003785 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003786
Jesse Barnes139ccd32013-08-19 11:04:55 -07003787train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003788 DRM_DEBUG_KMS("FDI train done.\n");
3789}
3790
Daniel Vetter88cefb62012-08-12 19:27:14 +02003791static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003792{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003793 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003794 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003795 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003796 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003797
Jesse Barnesc64e3112010-09-10 11:27:03 -07003798
Jesse Barnes0e23b992010-09-10 11:10:00 -07003799 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003800 reg = FDI_RX_CTL(pipe);
3801 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003802 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003803 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003804 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003805 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3806
3807 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003808 udelay(200);
3809
3810 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003811 temp = I915_READ(reg);
3812 I915_WRITE(reg, temp | FDI_PCDCLK);
3813
3814 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003815 udelay(200);
3816
Paulo Zanoni20749732012-11-23 15:30:38 -02003817 /* Enable CPU FDI TX PLL, always on for Ironlake */
3818 reg = FDI_TX_CTL(pipe);
3819 temp = I915_READ(reg);
3820 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3821 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003822
Paulo Zanoni20749732012-11-23 15:30:38 -02003823 POSTING_READ(reg);
3824 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003825 }
3826}
3827
Daniel Vetter88cefb62012-08-12 19:27:14 +02003828static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3829{
3830 struct drm_device *dev = intel_crtc->base.dev;
3831 struct drm_i915_private *dev_priv = dev->dev_private;
3832 int pipe = intel_crtc->pipe;
3833 u32 reg, temp;
3834
3835 /* Switch from PCDclk to Rawclk */
3836 reg = FDI_RX_CTL(pipe);
3837 temp = I915_READ(reg);
3838 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3839
3840 /* Disable CPU FDI TX PLL */
3841 reg = FDI_TX_CTL(pipe);
3842 temp = I915_READ(reg);
3843 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3844
3845 POSTING_READ(reg);
3846 udelay(100);
3847
3848 reg = FDI_RX_CTL(pipe);
3849 temp = I915_READ(reg);
3850 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3851
3852 /* Wait for the clocks to turn off. */
3853 POSTING_READ(reg);
3854 udelay(100);
3855}
3856
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003857static void ironlake_fdi_disable(struct drm_crtc *crtc)
3858{
3859 struct drm_device *dev = crtc->dev;
3860 struct drm_i915_private *dev_priv = dev->dev_private;
3861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3862 int pipe = intel_crtc->pipe;
3863 u32 reg, temp;
3864
3865 /* disable CPU FDI tx and PCH FDI rx */
3866 reg = FDI_TX_CTL(pipe);
3867 temp = I915_READ(reg);
3868 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3869 POSTING_READ(reg);
3870
3871 reg = FDI_RX_CTL(pipe);
3872 temp = I915_READ(reg);
3873 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003874 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003875 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3876
3877 POSTING_READ(reg);
3878 udelay(100);
3879
3880 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003881 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003882 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003883
3884 /* still set train pattern 1 */
3885 reg = FDI_TX_CTL(pipe);
3886 temp = I915_READ(reg);
3887 temp &= ~FDI_LINK_TRAIN_NONE;
3888 temp |= FDI_LINK_TRAIN_PATTERN_1;
3889 I915_WRITE(reg, temp);
3890
3891 reg = FDI_RX_CTL(pipe);
3892 temp = I915_READ(reg);
3893 if (HAS_PCH_CPT(dev)) {
3894 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3895 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3896 } else {
3897 temp &= ~FDI_LINK_TRAIN_NONE;
3898 temp |= FDI_LINK_TRAIN_PATTERN_1;
3899 }
3900 /* BPC in FDI rx is consistent with that in PIPECONF */
3901 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003902 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003903 I915_WRITE(reg, temp);
3904
3905 POSTING_READ(reg);
3906 udelay(100);
3907}
3908
Chris Wilson5dce5b932014-01-20 10:17:36 +00003909bool intel_has_pending_fb_unpin(struct drm_device *dev)
3910{
3911 struct intel_crtc *crtc;
3912
3913 /* Note that we don't need to be called with mode_config.lock here
3914 * as our list of CRTC objects is static for the lifetime of the
3915 * device and so cannot disappear as we iterate. Similarly, we can
3916 * happily treat the predicates as racy, atomic checks as userspace
3917 * cannot claim and pin a new fb without at least acquring the
3918 * struct_mutex and so serialising with us.
3919 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003920 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003921 if (atomic_read(&crtc->unpin_work_count) == 0)
3922 continue;
3923
3924 if (crtc->unpin_work)
3925 intel_wait_for_vblank(dev, crtc->pipe);
3926
3927 return true;
3928 }
3929
3930 return false;
3931}
3932
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003933static void page_flip_completed(struct intel_crtc *intel_crtc)
3934{
3935 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3936 struct intel_unpin_work *work = intel_crtc->unpin_work;
3937
3938 /* ensure that the unpin work is consistent wrt ->pending. */
3939 smp_rmb();
3940 intel_crtc->unpin_work = NULL;
3941
3942 if (work->event)
3943 drm_send_vblank_event(intel_crtc->base.dev,
3944 intel_crtc->pipe,
3945 work->event);
3946
3947 drm_crtc_vblank_put(&intel_crtc->base);
3948
3949 wake_up_all(&dev_priv->pending_flip_queue);
3950 queue_work(dev_priv->wq, &work->work);
3951
3952 trace_i915_flip_complete(intel_crtc->plane,
3953 work->pending_flip_obj);
3954}
3955
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003956void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003957{
Chris Wilson0f911282012-04-17 10:05:38 +01003958 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003959 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003960
Daniel Vetter2c10d572012-12-20 21:24:07 +01003961 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003962 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3963 !intel_crtc_has_pending_flip(crtc),
3964 60*HZ) == 0)) {
3965 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003966
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003967 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003968 if (intel_crtc->unpin_work) {
3969 WARN_ONCE(1, "Removing stuck page flip\n");
3970 page_flip_completed(intel_crtc);
3971 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003972 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003973 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003974
Chris Wilson975d5682014-08-20 13:13:34 +01003975 if (crtc->primary->fb) {
3976 mutex_lock(&dev->struct_mutex);
3977 intel_finish_fb(crtc->primary->fb);
3978 mutex_unlock(&dev->struct_mutex);
3979 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003980}
3981
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003982/* Program iCLKIP clock to the desired frequency */
3983static void lpt_program_iclkip(struct drm_crtc *crtc)
3984{
3985 struct drm_device *dev = crtc->dev;
3986 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003987 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003988 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3989 u32 temp;
3990
Ville Syrjäläa5805162015-05-26 20:42:30 +03003991 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003992
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003993 /* It is necessary to ungate the pixclk gate prior to programming
3994 * the divisors, and gate it back when it is done.
3995 */
3996 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3997
3998 /* Disable SSCCTL */
3999 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004000 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
4001 SBI_SSCCTL_DISABLE,
4002 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004003
4004 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03004005 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004006 auxdiv = 1;
4007 divsel = 0x41;
4008 phaseinc = 0x20;
4009 } else {
4010 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01004011 * but the adjusted_mode->crtc_clock in in KHz. To get the
4012 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004013 * convert the virtual clock precision to KHz here for higher
4014 * precision.
4015 */
4016 u32 iclk_virtual_root_freq = 172800 * 1000;
4017 u32 iclk_pi_range = 64;
4018 u32 desired_divisor, msb_divisor_value, pi_value;
4019
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03004020 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004021 msb_divisor_value = desired_divisor / iclk_pi_range;
4022 pi_value = desired_divisor % iclk_pi_range;
4023
4024 auxdiv = 0;
4025 divsel = msb_divisor_value - 2;
4026 phaseinc = pi_value;
4027 }
4028
4029 /* This should not happen with any sane values */
4030 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4031 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4032 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4033 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4034
4035 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 +03004036 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004037 auxdiv,
4038 divsel,
4039 phasedir,
4040 phaseinc);
4041
4042 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004043 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004044 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4045 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4046 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4047 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4048 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4049 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004050 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004051
4052 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004053 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004054 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4055 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004056 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004057
4058 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004059 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004060 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004061 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004062
4063 /* Wait for initialization time */
4064 udelay(24);
4065
4066 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004067
Ville Syrjäläa5805162015-05-26 20:42:30 +03004068 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004069}
4070
Daniel Vetter275f01b22013-05-03 11:49:47 +02004071static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4072 enum pipe pch_transcoder)
4073{
4074 struct drm_device *dev = crtc->base.dev;
4075 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004076 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004077
4078 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4079 I915_READ(HTOTAL(cpu_transcoder)));
4080 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4081 I915_READ(HBLANK(cpu_transcoder)));
4082 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4083 I915_READ(HSYNC(cpu_transcoder)));
4084
4085 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4086 I915_READ(VTOTAL(cpu_transcoder)));
4087 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4088 I915_READ(VBLANK(cpu_transcoder)));
4089 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4090 I915_READ(VSYNC(cpu_transcoder)));
4091 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4092 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4093}
4094
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004095static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004096{
4097 struct drm_i915_private *dev_priv = dev->dev_private;
4098 uint32_t temp;
4099
4100 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004101 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004102 return;
4103
4104 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4105 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4106
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004107 temp &= ~FDI_BC_BIFURCATION_SELECT;
4108 if (enable)
4109 temp |= FDI_BC_BIFURCATION_SELECT;
4110
4111 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004112 I915_WRITE(SOUTH_CHICKEN1, temp);
4113 POSTING_READ(SOUTH_CHICKEN1);
4114}
4115
4116static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4117{
4118 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004119
4120 switch (intel_crtc->pipe) {
4121 case PIPE_A:
4122 break;
4123 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004124 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004125 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004126 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004127 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004128
4129 break;
4130 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004131 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004132
4133 break;
4134 default:
4135 BUG();
4136 }
4137}
4138
Jesse Barnesf67a5592011-01-05 10:31:48 -08004139/*
4140 * Enable PCH resources required for PCH ports:
4141 * - PCH PLLs
4142 * - FDI training & RX/TX
4143 * - update transcoder timings
4144 * - DP transcoding bits
4145 * - transcoder
4146 */
4147static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004148{
4149 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004150 struct drm_i915_private *dev_priv = dev->dev_private;
4151 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4152 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004153 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004154
Daniel Vetterab9412b2013-05-03 11:49:46 +02004155 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004156
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004157 if (IS_IVYBRIDGE(dev))
4158 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4159
Daniel Vettercd986ab2012-10-26 10:58:12 +02004160 /* Write the TU size bits before fdi link training, so that error
4161 * detection works. */
4162 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4163 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4164
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004165 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004166 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004167
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004168 /* We need to program the right clock selection before writing the pixel
4169 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004170 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004171 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004172
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004173 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004174 temp |= TRANS_DPLL_ENABLE(pipe);
4175 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004176 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004177 temp |= sel;
4178 else
4179 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004180 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004181 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004182
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004183 /* XXX: pch pll's can be enabled any time before we enable the PCH
4184 * transcoder, and we actually should do this to not upset any PCH
4185 * transcoder that already use the clock when we share it.
4186 *
4187 * Note that enable_shared_dpll tries to do the right thing, but
4188 * get_shared_dpll unconditionally resets the pll - we need that to have
4189 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004190 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004191
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004192 /* set transcoder timing, panel must allow it */
4193 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004194 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004195
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004196 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004197
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004198 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004199 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004200 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004201 reg = TRANS_DP_CTL(pipe);
4202 temp = I915_READ(reg);
4203 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004204 TRANS_DP_SYNC_MASK |
4205 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004206 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004207 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004208
4209 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004210 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004211 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004212 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004213
4214 switch (intel_trans_dp_port_sel(crtc)) {
4215 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004216 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004217 break;
4218 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004219 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004220 break;
4221 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004222 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004223 break;
4224 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004225 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004226 }
4227
Chris Wilson5eddb702010-09-11 13:48:45 +01004228 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004229 }
4230
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004231 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004232}
4233
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004234static void lpt_pch_enable(struct drm_crtc *crtc)
4235{
4236 struct drm_device *dev = crtc->dev;
4237 struct drm_i915_private *dev_priv = dev->dev_private;
4238 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004239 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004240
Daniel Vetterab9412b2013-05-03 11:49:46 +02004241 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004242
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004243 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004244
Paulo Zanoni0540e482012-10-31 18:12:40 -02004245 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004246 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004247
Paulo Zanoni937bb612012-10-31 18:12:47 -02004248 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004249}
4250
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004251struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4252 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004253{
Daniel Vettere2b78262013-06-07 23:10:03 +02004254 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004255 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004256 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004257 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004258
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004259 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4260
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004261 if (HAS_PCH_IBX(dev_priv->dev)) {
4262 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004263 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004264 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004265
Daniel Vetter46edb022013-06-05 13:34:12 +02004266 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4267 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004268
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004269 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004270
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004271 goto found;
4272 }
4273
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304274 if (IS_BROXTON(dev_priv->dev)) {
4275 /* PLL is attached to port in bxt */
4276 struct intel_encoder *encoder;
4277 struct intel_digital_port *intel_dig_port;
4278
4279 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4280 if (WARN_ON(!encoder))
4281 return NULL;
4282
4283 intel_dig_port = enc_to_dig_port(&encoder->base);
4284 /* 1:1 mapping between ports and PLLs */
4285 i = (enum intel_dpll_id)intel_dig_port->port;
4286 pll = &dev_priv->shared_dplls[i];
4287 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4288 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004289 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304290
4291 goto found;
4292 }
4293
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004294 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4295 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004296
4297 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004298 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004299 continue;
4300
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004301 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004302 &shared_dpll[i].hw_state,
4303 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004304 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004305 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004306 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004307 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004308 goto found;
4309 }
4310 }
4311
4312 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004313 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4314 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004315 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004316 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4317 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004318 goto found;
4319 }
4320 }
4321
4322 return NULL;
4323
4324found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004325 if (shared_dpll[i].crtc_mask == 0)
4326 shared_dpll[i].hw_state =
4327 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004328
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004329 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004330 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4331 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004332
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004333 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004334
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004335 return pll;
4336}
4337
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004338static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004339{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004340 struct drm_i915_private *dev_priv = to_i915(state->dev);
4341 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004342 struct intel_shared_dpll *pll;
4343 enum intel_dpll_id i;
4344
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004345 if (!to_intel_atomic_state(state)->dpll_set)
4346 return;
4347
4348 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004349 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4350 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004351 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004352 }
4353}
4354
Daniel Vettera1520312013-05-03 11:49:50 +02004355static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004356{
4357 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004358 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004359 u32 temp;
4360
4361 temp = I915_READ(dslreg);
4362 udelay(500);
4363 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004364 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004365 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004366 }
4367}
4368
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004369static int
4370skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4371 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4372 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004373{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004374 struct intel_crtc_scaler_state *scaler_state =
4375 &crtc_state->scaler_state;
4376 struct intel_crtc *intel_crtc =
4377 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004378 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004379
4380 need_scaling = intel_rotation_90_or_270(rotation) ?
4381 (src_h != dst_w || src_w != dst_h):
4382 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004383
4384 /*
4385 * if plane is being disabled or scaler is no more required or force detach
4386 * - free scaler binded to this plane/crtc
4387 * - in order to do this, update crtc->scaler_usage
4388 *
4389 * Here scaler state in crtc_state is set free so that
4390 * scaler can be assigned to other user. Actual register
4391 * update to free the scaler is done in plane/panel-fit programming.
4392 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4393 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004394 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004395 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004396 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004397 scaler_state->scalers[*scaler_id].in_use = 0;
4398
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004399 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4400 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4401 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004402 scaler_state->scaler_users);
4403 *scaler_id = -1;
4404 }
4405 return 0;
4406 }
4407
4408 /* range checks */
4409 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4410 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4411
4412 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4413 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004414 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004415 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004416 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004417 return -EINVAL;
4418 }
4419
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004420 /* mark this plane as a scaler user in crtc_state */
4421 scaler_state->scaler_users |= (1 << scaler_user);
4422 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4423 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4424 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4425 scaler_state->scaler_users);
4426
4427 return 0;
4428}
4429
4430/**
4431 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4432 *
4433 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004434 *
4435 * Return
4436 * 0 - scaler_usage updated successfully
4437 * error - requested scaling cannot be supported or other error condition
4438 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004439int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004440{
4441 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004442 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004443
4444 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4445 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4446
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004447 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004448 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4449 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004450 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004451}
4452
4453/**
4454 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4455 *
4456 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004457 * @plane_state: atomic plane state to update
4458 *
4459 * Return
4460 * 0 - scaler_usage updated successfully
4461 * error - requested scaling cannot be supported or other error condition
4462 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004463static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4464 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004465{
4466
4467 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004468 struct intel_plane *intel_plane =
4469 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004470 struct drm_framebuffer *fb = plane_state->base.fb;
4471 int ret;
4472
4473 bool force_detach = !fb || !plane_state->visible;
4474
4475 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4476 intel_plane->base.base.id, intel_crtc->pipe,
4477 drm_plane_index(&intel_plane->base));
4478
4479 ret = skl_update_scaler(crtc_state, force_detach,
4480 drm_plane_index(&intel_plane->base),
4481 &plane_state->scaler_id,
4482 plane_state->base.rotation,
4483 drm_rect_width(&plane_state->src) >> 16,
4484 drm_rect_height(&plane_state->src) >> 16,
4485 drm_rect_width(&plane_state->dst),
4486 drm_rect_height(&plane_state->dst));
4487
4488 if (ret || plane_state->scaler_id < 0)
4489 return ret;
4490
Chandra Kondurua1b22782015-04-07 15:28:45 -07004491 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004492 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004493 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004494 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004495 return -EINVAL;
4496 }
4497
4498 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004499 switch (fb->pixel_format) {
4500 case DRM_FORMAT_RGB565:
4501 case DRM_FORMAT_XBGR8888:
4502 case DRM_FORMAT_XRGB8888:
4503 case DRM_FORMAT_ABGR8888:
4504 case DRM_FORMAT_ARGB8888:
4505 case DRM_FORMAT_XRGB2101010:
4506 case DRM_FORMAT_XBGR2101010:
4507 case DRM_FORMAT_YUYV:
4508 case DRM_FORMAT_YVYU:
4509 case DRM_FORMAT_UYVY:
4510 case DRM_FORMAT_VYUY:
4511 break;
4512 default:
4513 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4514 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4515 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004516 }
4517
Chandra Kondurua1b22782015-04-07 15:28:45 -07004518 return 0;
4519}
4520
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004521static void skylake_scaler_disable(struct intel_crtc *crtc)
4522{
4523 int i;
4524
4525 for (i = 0; i < crtc->num_scalers; i++)
4526 skl_detach_scaler(crtc, i);
4527}
4528
4529static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004530{
4531 struct drm_device *dev = crtc->base.dev;
4532 struct drm_i915_private *dev_priv = dev->dev_private;
4533 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004534 struct intel_crtc_scaler_state *scaler_state =
4535 &crtc->config->scaler_state;
4536
4537 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4538
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004539 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004540 int id;
4541
4542 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4543 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4544 return;
4545 }
4546
4547 id = scaler_state->scaler_id;
4548 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4549 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4550 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4551 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4552
4553 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004554 }
4555}
4556
Jesse Barnesb074cec2013-04-25 12:55:02 -07004557static void ironlake_pfit_enable(struct intel_crtc *crtc)
4558{
4559 struct drm_device *dev = crtc->base.dev;
4560 struct drm_i915_private *dev_priv = dev->dev_private;
4561 int pipe = crtc->pipe;
4562
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004563 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004564 /* Force use of hard-coded filter coefficients
4565 * as some pre-programmed values are broken,
4566 * e.g. x201.
4567 */
4568 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4569 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4570 PF_PIPE_SEL_IVB(pipe));
4571 else
4572 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004573 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4574 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004575 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004576}
4577
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004578void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004579{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004580 struct drm_device *dev = crtc->base.dev;
4581 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004582
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004583 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004584 return;
4585
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004586 /* We can only enable IPS after we enable a plane and wait for a vblank */
4587 intel_wait_for_vblank(dev, crtc->pipe);
4588
Paulo Zanonid77e4532013-09-24 13:52:55 -03004589 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004590 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004591 mutex_lock(&dev_priv->rps.hw_lock);
4592 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4593 mutex_unlock(&dev_priv->rps.hw_lock);
4594 /* Quoting Art Runyan: "its not safe to expect any particular
4595 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004596 * mailbox." Moreover, the mailbox may return a bogus state,
4597 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004598 */
4599 } else {
4600 I915_WRITE(IPS_CTL, IPS_ENABLE);
4601 /* The bit only becomes 1 in the next vblank, so this wait here
4602 * is essentially intel_wait_for_vblank. If we don't have this
4603 * and don't wait for vblanks until the end of crtc_enable, then
4604 * the HW state readout code will complain that the expected
4605 * IPS_CTL value is not the one we read. */
4606 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4607 DRM_ERROR("Timed out waiting for IPS enable\n");
4608 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004609}
4610
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004611void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004612{
4613 struct drm_device *dev = crtc->base.dev;
4614 struct drm_i915_private *dev_priv = dev->dev_private;
4615
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004616 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004617 return;
4618
4619 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004620 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004621 mutex_lock(&dev_priv->rps.hw_lock);
4622 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4623 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004624 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4625 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4626 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004627 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004628 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004629 POSTING_READ(IPS_CTL);
4630 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004631
4632 /* We need to wait for a vblank before we can disable the plane. */
4633 intel_wait_for_vblank(dev, crtc->pipe);
4634}
4635
4636/** Loads the palette/gamma unit for the CRTC with the prepared values */
4637static void intel_crtc_load_lut(struct drm_crtc *crtc)
4638{
4639 struct drm_device *dev = crtc->dev;
4640 struct drm_i915_private *dev_priv = dev->dev_private;
4641 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4642 enum pipe pipe = intel_crtc->pipe;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004643 int i;
4644 bool reenable_ips = false;
4645
4646 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004647 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004648 return;
4649
Imre Deak50360402015-01-16 00:55:16 -08004650 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004651 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004652 assert_dsi_pll_enabled(dev_priv);
4653 else
4654 assert_pll_enabled(dev_priv, pipe);
4655 }
4656
Paulo Zanonid77e4532013-09-24 13:52:55 -03004657 /* Workaround : Do not read or write the pipe palette/gamma data while
4658 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4659 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004660 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004661 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4662 GAMMA_MODE_MODE_SPLIT)) {
4663 hsw_disable_ips(intel_crtc);
4664 reenable_ips = true;
4665 }
4666
4667 for (i = 0; i < 256; i++) {
Ville Syrjäläf65a9c52015-09-18 20:03:28 +03004668 u32 palreg;
4669
4670 if (HAS_GMCH_DISPLAY(dev))
4671 palreg = PALETTE(pipe, i);
4672 else
4673 palreg = LGC_PALETTE(pipe, i);
4674
4675 I915_WRITE(palreg,
Paulo Zanonid77e4532013-09-24 13:52:55 -03004676 (intel_crtc->lut_r[i] << 16) |
4677 (intel_crtc->lut_g[i] << 8) |
4678 intel_crtc->lut_b[i]);
4679 }
4680
4681 if (reenable_ips)
4682 hsw_enable_ips(intel_crtc);
4683}
4684
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004685static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004686{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004687 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004688 struct drm_device *dev = intel_crtc->base.dev;
4689 struct drm_i915_private *dev_priv = dev->dev_private;
4690
4691 mutex_lock(&dev->struct_mutex);
4692 dev_priv->mm.interruptible = false;
4693 (void) intel_overlay_switch_off(intel_crtc->overlay);
4694 dev_priv->mm.interruptible = true;
4695 mutex_unlock(&dev->struct_mutex);
4696 }
4697
4698 /* Let userspace switch the overlay on again. In most cases userspace
4699 * has to recompute where to put it anyway.
4700 */
4701}
4702
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004703/**
4704 * intel_post_enable_primary - Perform operations after enabling primary plane
4705 * @crtc: the CRTC whose primary plane was just enabled
4706 *
4707 * Performs potentially sleeping operations that must be done after the primary
4708 * plane is enabled, such as updating FBC and IPS. Note that this may be
4709 * called due to an explicit primary plane update, or due to an implicit
4710 * re-enable that is caused when a sprite plane is updated to no longer
4711 * completely hide the primary plane.
4712 */
4713static void
4714intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004715{
4716 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004717 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004718 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4719 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004720
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004721 /*
4722 * BDW signals flip done immediately if the plane
4723 * is disabled, even if the plane enable is already
4724 * armed to occur at the next vblank :(
4725 */
4726 if (IS_BROADWELL(dev))
4727 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004728
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004729 /*
4730 * FIXME IPS should be fine as long as one plane is
4731 * enabled, but in practice it seems to have problems
4732 * when going from primary only to sprite only and vice
4733 * versa.
4734 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004735 hsw_enable_ips(intel_crtc);
4736
Daniel Vetterf99d7062014-06-19 16:01:59 +02004737 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004738 * Gen2 reports pipe underruns whenever all planes are disabled.
4739 * So don't enable underrun reporting before at least some planes
4740 * are enabled.
4741 * FIXME: Need to fix the logic to work when we turn off all planes
4742 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004743 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004744 if (IS_GEN2(dev))
4745 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4746
4747 /* Underruns don't raise interrupts, so check manually. */
4748 if (HAS_GMCH_DISPLAY(dev))
4749 i9xx_check_fifo_underruns(dev_priv);
4750}
4751
4752/**
4753 * intel_pre_disable_primary - Perform operations before disabling primary plane
4754 * @crtc: the CRTC whose primary plane is to be disabled
4755 *
4756 * Performs potentially sleeping operations that must be done before the
4757 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4758 * be called due to an explicit primary plane update, or due to an implicit
4759 * disable that is caused when a sprite plane completely hides the primary
4760 * plane.
4761 */
4762static void
4763intel_pre_disable_primary(struct drm_crtc *crtc)
4764{
4765 struct drm_device *dev = crtc->dev;
4766 struct drm_i915_private *dev_priv = dev->dev_private;
4767 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4768 int pipe = intel_crtc->pipe;
4769
4770 /*
4771 * Gen2 reports pipe underruns whenever all planes are disabled.
4772 * So diasble underrun reporting before all the planes get disabled.
4773 * FIXME: Need to fix the logic to work when we turn off all planes
4774 * but leave the pipe running.
4775 */
4776 if (IS_GEN2(dev))
4777 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4778
4779 /*
4780 * Vblank time updates from the shadow to live plane control register
4781 * are blocked if the memory self-refresh mode is active at that
4782 * moment. So to make sure the plane gets truly disabled, disable
4783 * first the self-refresh mode. The self-refresh enable bit in turn
4784 * will be checked/applied by the HW only at the next frame start
4785 * event which is after the vblank start event, so we need to have a
4786 * wait-for-vblank between disabling the plane and the pipe.
4787 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004788 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004789 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004790 dev_priv->wm.vlv.cxsr = false;
4791 intel_wait_for_vblank(dev, pipe);
4792 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004793
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004794 /*
4795 * FIXME IPS should be fine as long as one plane is
4796 * enabled, but in practice it seems to have problems
4797 * when going from primary only to sprite only and vice
4798 * versa.
4799 */
4800 hsw_disable_ips(intel_crtc);
4801}
4802
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004803static void intel_post_plane_update(struct intel_crtc *crtc)
4804{
4805 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4806 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni7733b492015-07-07 15:26:04 -03004807 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004808 struct drm_plane *plane;
4809
4810 if (atomic->wait_vblank)
4811 intel_wait_for_vblank(dev, crtc->pipe);
4812
4813 intel_frontbuffer_flip(dev, atomic->fb_bits);
4814
Ville Syrjälä852eb002015-06-24 22:00:07 +03004815 if (atomic->disable_cxsr)
4816 crtc->wm.cxsr_allowed = true;
4817
Ville Syrjäläf015c552015-06-24 22:00:02 +03004818 if (crtc->atomic.update_wm_post)
4819 intel_update_watermarks(&crtc->base);
4820
Paulo Zanonic80ac852015-07-02 19:25:13 -03004821 if (atomic->update_fbc)
Paulo Zanoni7733b492015-07-07 15:26:04 -03004822 intel_fbc_update(dev_priv);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004823
4824 if (atomic->post_enable_primary)
4825 intel_post_enable_primary(&crtc->base);
4826
4827 drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4828 intel_update_sprite_watermarks(plane, &crtc->base,
4829 0, 0, 0, false, false);
4830
4831 memset(atomic, 0, sizeof(*atomic));
4832}
4833
4834static void intel_pre_plane_update(struct intel_crtc *crtc)
4835{
4836 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004837 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004838 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4839 struct drm_plane *p;
4840
4841 /* Track fb's for any planes being disabled */
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004842 drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4843 struct intel_plane *plane = to_intel_plane(p);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004844
4845 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03004846 i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
4847 plane->frontbuffer_bit);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004848 mutex_unlock(&dev->struct_mutex);
4849 }
4850
4851 if (atomic->wait_for_flips)
4852 intel_crtc_wait_for_pending_flips(&crtc->base);
4853
Paulo Zanonic80ac852015-07-02 19:25:13 -03004854 if (atomic->disable_fbc)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03004855 intel_fbc_disable_crtc(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004856
Rodrigo Vivi066cf55b2015-06-26 13:55:54 -07004857 if (crtc->atomic.disable_ips)
4858 hsw_disable_ips(crtc);
4859
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004860 if (atomic->pre_disable_primary)
4861 intel_pre_disable_primary(&crtc->base);
Ville Syrjälä852eb002015-06-24 22:00:07 +03004862
4863 if (atomic->disable_cxsr) {
4864 crtc->wm.cxsr_allowed = false;
4865 intel_set_memory_cxsr(dev_priv, false);
4866 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004867}
4868
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004869static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004870{
4871 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004872 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004873 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004874 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004875
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004876 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004877
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004878 drm_for_each_plane_mask(p, dev, plane_mask)
4879 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004880
Daniel Vetterf99d7062014-06-19 16:01:59 +02004881 /*
4882 * FIXME: Once we grow proper nuclear flip support out of this we need
4883 * to compute the mask of flip planes precisely. For the time being
4884 * consider this a flip to a NULL plane.
4885 */
4886 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004887}
4888
Jesse Barnesf67a5592011-01-05 10:31:48 -08004889static void ironlake_crtc_enable(struct drm_crtc *crtc)
4890{
4891 struct drm_device *dev = crtc->dev;
4892 struct drm_i915_private *dev_priv = dev->dev_private;
4893 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004894 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004895 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004896
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004897 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004898 return;
4899
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004900 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004901 intel_prepare_shared_dpll(intel_crtc);
4902
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004903 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304904 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004905
4906 intel_set_pipe_timings(intel_crtc);
4907
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004908 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004909 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004910 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004911 }
4912
4913 ironlake_set_pipeconf(crtc);
4914
Jesse Barnesf67a5592011-01-05 10:31:48 -08004915 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004916
Daniel Vettera72e4c92014-09-30 10:56:47 +02004917 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4918 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004919
Daniel Vetterf6736a12013-06-05 13:34:30 +02004920 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004921 if (encoder->pre_enable)
4922 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004923
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004924 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004925 /* Note: FDI PLL enabling _must_ be done before we enable the
4926 * cpu pipes, hence this is separate from all the other fdi/pch
4927 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004928 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004929 } else {
4930 assert_fdi_tx_disabled(dev_priv, pipe);
4931 assert_fdi_rx_disabled(dev_priv, pipe);
4932 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004933
Jesse Barnesb074cec2013-04-25 12:55:02 -07004934 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004935
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004936 /*
4937 * On ILK+ LUT must be loaded before the pipe is running but with
4938 * clocks enabled
4939 */
4940 intel_crtc_load_lut(crtc);
4941
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004942 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004943 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004944
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004945 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004946 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004947
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004948 assert_vblank_disabled(crtc);
4949 drm_crtc_vblank_on(crtc);
4950
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004951 for_each_encoder_on_crtc(dev, crtc, encoder)
4952 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004953
4954 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004955 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004956}
4957
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004958/* IPS only exists on ULT machines and is tied to pipe A. */
4959static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4960{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004961 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004962}
4963
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004964static void haswell_crtc_enable(struct drm_crtc *crtc)
4965{
4966 struct drm_device *dev = crtc->dev;
4967 struct drm_i915_private *dev_priv = dev->dev_private;
4968 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4969 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004970 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4971 struct intel_crtc_state *pipe_config =
4972 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004973
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004974 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004975 return;
4976
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004977 if (intel_crtc_to_shared_dpll(intel_crtc))
4978 intel_enable_shared_dpll(intel_crtc);
4979
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004980 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304981 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004982
4983 intel_set_pipe_timings(intel_crtc);
4984
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004985 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4986 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4987 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004988 }
4989
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004990 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004991 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004992 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004993 }
4994
4995 haswell_set_pipeconf(crtc);
4996
4997 intel_set_pipe_csc(crtc);
4998
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004999 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005000
Daniel Vettera72e4c92014-09-30 10:56:47 +02005001 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005002 for_each_encoder_on_crtc(dev, crtc, encoder)
5003 if (encoder->pre_enable)
5004 encoder->pre_enable(encoder);
5005
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005006 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02005007 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5008 true);
Imre Deak4fe94672014-06-25 22:01:49 +03005009 dev_priv->display.fdi_link_train(crtc);
5010 }
5011
Paulo Zanoni1f544382012-10-24 11:32:00 -02005012 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005013
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005014 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005015 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005016 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005017 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005018
5019 /*
5020 * On ILK+ LUT must be loaded before the pipe is running but with
5021 * clocks enabled
5022 */
5023 intel_crtc_load_lut(crtc);
5024
Paulo Zanoni1f544382012-10-24 11:32:00 -02005025 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00005026 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005027
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005028 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005029 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005030
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005031 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005032 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005033
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005034 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005035 intel_ddi_set_vc_payload_alloc(crtc, true);
5036
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005037 assert_vblank_disabled(crtc);
5038 drm_crtc_vblank_on(crtc);
5039
Jani Nikula8807e552013-08-30 19:40:32 +03005040 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005041 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005042 intel_opregion_notify_encoder(encoder, true);
5043 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005044
Paulo Zanonie4916942013-09-20 16:21:19 -03005045 /* If we change the relative order between pipe/planes enabling, we need
5046 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005047 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5048 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5049 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5050 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5051 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005052}
5053
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005054static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005055{
5056 struct drm_device *dev = crtc->base.dev;
5057 struct drm_i915_private *dev_priv = dev->dev_private;
5058 int pipe = crtc->pipe;
5059
5060 /* To avoid upsetting the power well on haswell only disable the pfit if
5061 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005062 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005063 I915_WRITE(PF_CTL(pipe), 0);
5064 I915_WRITE(PF_WIN_POS(pipe), 0);
5065 I915_WRITE(PF_WIN_SZ(pipe), 0);
5066 }
5067}
5068
Jesse Barnes6be4a602010-09-10 10:26:01 -07005069static void ironlake_crtc_disable(struct drm_crtc *crtc)
5070{
5071 struct drm_device *dev = crtc->dev;
5072 struct drm_i915_private *dev_priv = dev->dev_private;
5073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005074 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005075 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005076 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005077
Daniel Vetterea9d7582012-07-10 10:42:52 +02005078 for_each_encoder_on_crtc(dev, crtc, encoder)
5079 encoder->disable(encoder);
5080
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005081 drm_crtc_vblank_off(crtc);
5082 assert_vblank_disabled(crtc);
5083
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005084 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005085 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005086
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005087 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005088
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005089 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005090
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005091 if (intel_crtc->config->has_pch_encoder)
5092 ironlake_fdi_disable(crtc);
5093
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005094 for_each_encoder_on_crtc(dev, crtc, encoder)
5095 if (encoder->post_disable)
5096 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005097
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005098 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005099 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005100
Daniel Vetterd925c592013-06-05 13:34:04 +02005101 if (HAS_PCH_CPT(dev)) {
5102 /* disable TRANS_DP_CTL */
5103 reg = TRANS_DP_CTL(pipe);
5104 temp = I915_READ(reg);
5105 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5106 TRANS_DP_PORT_SEL_MASK);
5107 temp |= TRANS_DP_PORT_SEL_NONE;
5108 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005109
Daniel Vetterd925c592013-06-05 13:34:04 +02005110 /* disable DPLL_SEL */
5111 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005112 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005113 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005114 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005115
Daniel Vetterd925c592013-06-05 13:34:04 +02005116 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005117 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07005118}
5119
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005120static void haswell_crtc_disable(struct drm_crtc *crtc)
5121{
5122 struct drm_device *dev = crtc->dev;
5123 struct drm_i915_private *dev_priv = dev->dev_private;
5124 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5125 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005126 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005127
Jani Nikula8807e552013-08-30 19:40:32 +03005128 for_each_encoder_on_crtc(dev, crtc, encoder) {
5129 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005130 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005131 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005132
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005133 drm_crtc_vblank_off(crtc);
5134 assert_vblank_disabled(crtc);
5135
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005136 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005137 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5138 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005139 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005140
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005141 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005142 intel_ddi_set_vc_payload_alloc(crtc, false);
5143
Paulo Zanoniad80a812012-10-24 16:06:19 -02005144 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005145
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005146 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005147 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005148 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005149 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005150
Paulo Zanoni1f544382012-10-24 11:32:00 -02005151 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005152
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005153 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005154 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005155 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005156 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005157
Imre Deak97b040a2014-06-25 22:01:50 +03005158 for_each_encoder_on_crtc(dev, crtc, encoder)
5159 if (encoder->post_disable)
5160 encoder->post_disable(encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005161}
5162
Jesse Barnes2dd24552013-04-25 12:55:01 -07005163static void i9xx_pfit_enable(struct intel_crtc *crtc)
5164{
5165 struct drm_device *dev = crtc->base.dev;
5166 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005167 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005168
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005169 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005170 return;
5171
Daniel Vetterc0b03412013-05-28 12:05:54 +02005172 /*
5173 * The panel fitter should only be adjusted whilst the pipe is disabled,
5174 * according to register description and PRM.
5175 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005176 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5177 assert_pipe_disabled(dev_priv, crtc->pipe);
5178
Jesse Barnesb074cec2013-04-25 12:55:02 -07005179 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5180 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005181
5182 /* Border color in case we don't scale up to the full screen. Black by
5183 * default, change to something else for debugging. */
5184 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005185}
5186
Dave Airlied05410f2014-06-05 13:22:59 +10005187static enum intel_display_power_domain port_to_power_domain(enum port port)
5188{
5189 switch (port) {
5190 case PORT_A:
5191 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5192 case PORT_B:
5193 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5194 case PORT_C:
5195 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5196 case PORT_D:
5197 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005198 case PORT_E:
5199 return POWER_DOMAIN_PORT_DDI_E_2_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005200 default:
5201 WARN_ON_ONCE(1);
5202 return POWER_DOMAIN_PORT_OTHER;
5203 }
5204}
5205
Imre Deak77d22dc2014-03-05 16:20:52 +02005206#define for_each_power_domain(domain, mask) \
5207 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5208 if ((1 << (domain)) & (mask))
5209
Imre Deak319be8a2014-03-04 19:22:57 +02005210enum intel_display_power_domain
5211intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005212{
Imre Deak319be8a2014-03-04 19:22:57 +02005213 struct drm_device *dev = intel_encoder->base.dev;
5214 struct intel_digital_port *intel_dig_port;
5215
5216 switch (intel_encoder->type) {
5217 case INTEL_OUTPUT_UNKNOWN:
5218 /* Only DDI platforms should ever use this output type */
5219 WARN_ON_ONCE(!HAS_DDI(dev));
5220 case INTEL_OUTPUT_DISPLAYPORT:
5221 case INTEL_OUTPUT_HDMI:
5222 case INTEL_OUTPUT_EDP:
5223 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005224 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005225 case INTEL_OUTPUT_DP_MST:
5226 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5227 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005228 case INTEL_OUTPUT_ANALOG:
5229 return POWER_DOMAIN_PORT_CRT;
5230 case INTEL_OUTPUT_DSI:
5231 return POWER_DOMAIN_PORT_DSI;
5232 default:
5233 return POWER_DOMAIN_PORT_OTHER;
5234 }
5235}
5236
5237static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5238{
5239 struct drm_device *dev = crtc->dev;
5240 struct intel_encoder *intel_encoder;
5241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5242 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005243 unsigned long mask;
5244 enum transcoder transcoder;
5245
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005246 if (!crtc->state->active)
5247 return 0;
5248
Imre Deak77d22dc2014-03-05 16:20:52 +02005249 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5250
5251 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5252 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005253 if (intel_crtc->config->pch_pfit.enabled ||
5254 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005255 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5256
Imre Deak319be8a2014-03-04 19:22:57 +02005257 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5258 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5259
Imre Deak77d22dc2014-03-05 16:20:52 +02005260 return mask;
5261}
5262
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005263static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
5264{
5265 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5266 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5267 enum intel_display_power_domain domain;
5268 unsigned long domains, new_domains, old_domains;
5269
5270 old_domains = intel_crtc->enabled_power_domains;
5271 intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
5272
5273 domains = new_domains & ~old_domains;
5274
5275 for_each_power_domain(domain, domains)
5276 intel_display_power_get(dev_priv, domain);
5277
5278 return old_domains & ~new_domains;
5279}
5280
5281static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5282 unsigned long domains)
5283{
5284 enum intel_display_power_domain domain;
5285
5286 for_each_power_domain(domain, domains)
5287 intel_display_power_put(dev_priv, domain);
5288}
5289
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005290static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005291{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005292 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005293 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005294 unsigned long put_domains[I915_MAX_PIPES] = {};
5295 struct drm_crtc_state *crtc_state;
5296 struct drm_crtc *crtc;
5297 int i;
Imre Deak77d22dc2014-03-05 16:20:52 +02005298
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005299 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5300 if (needs_modeset(crtc->state))
5301 put_domains[to_intel_crtc(crtc)->pipe] =
5302 modeset_get_crtc_power_domains(crtc);
Imre Deak77d22dc2014-03-05 16:20:52 +02005303 }
5304
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005305 if (dev_priv->display.modeset_commit_cdclk) {
5306 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5307
5308 if (cdclk != dev_priv->cdclk_freq &&
5309 !WARN_ON(!state->allow_modeset))
5310 dev_priv->display.modeset_commit_cdclk(state);
5311 }
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005312
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005313 for (i = 0; i < I915_MAX_PIPES; i++)
5314 if (put_domains[i])
5315 modeset_put_power_domains(dev_priv, put_domains[i]);
Imre Deak77d22dc2014-03-05 16:20:52 +02005316}
5317
Mika Kaholaadafdc62015-08-18 14:36:59 +03005318static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5319{
5320 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5321
5322 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5323 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5324 return max_cdclk_freq;
5325 else if (IS_CHERRYVIEW(dev_priv))
5326 return max_cdclk_freq*95/100;
5327 else if (INTEL_INFO(dev_priv)->gen < 4)
5328 return 2*max_cdclk_freq*90/100;
5329 else
5330 return max_cdclk_freq*90/100;
5331}
5332
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005333static void intel_update_max_cdclk(struct drm_device *dev)
5334{
5335 struct drm_i915_private *dev_priv = dev->dev_private;
5336
5337 if (IS_SKYLAKE(dev)) {
5338 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5339
5340 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5341 dev_priv->max_cdclk_freq = 675000;
5342 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5343 dev_priv->max_cdclk_freq = 540000;
5344 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5345 dev_priv->max_cdclk_freq = 450000;
5346 else
5347 dev_priv->max_cdclk_freq = 337500;
5348 } else if (IS_BROADWELL(dev)) {
5349 /*
5350 * FIXME with extra cooling we can allow
5351 * 540 MHz for ULX and 675 Mhz for ULT.
5352 * How can we know if extra cooling is
5353 * available? PCI ID, VTB, something else?
5354 */
5355 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5356 dev_priv->max_cdclk_freq = 450000;
5357 else if (IS_BDW_ULX(dev))
5358 dev_priv->max_cdclk_freq = 450000;
5359 else if (IS_BDW_ULT(dev))
5360 dev_priv->max_cdclk_freq = 540000;
5361 else
5362 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005363 } else if (IS_CHERRYVIEW(dev)) {
5364 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005365 } else if (IS_VALLEYVIEW(dev)) {
5366 dev_priv->max_cdclk_freq = 400000;
5367 } else {
5368 /* otherwise assume cdclk is fixed */
5369 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5370 }
5371
Mika Kaholaadafdc62015-08-18 14:36:59 +03005372 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5373
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005374 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5375 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005376
5377 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5378 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005379}
5380
5381static void intel_update_cdclk(struct drm_device *dev)
5382{
5383 struct drm_i915_private *dev_priv = dev->dev_private;
5384
5385 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5386 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5387 dev_priv->cdclk_freq);
5388
5389 /*
5390 * Program the gmbus_freq based on the cdclk frequency.
5391 * BSpec erroneously claims we should aim for 4MHz, but
5392 * in fact 1MHz is the correct frequency.
5393 */
5394 if (IS_VALLEYVIEW(dev)) {
5395 /*
5396 * Program the gmbus_freq based on the cdclk frequency.
5397 * BSpec erroneously claims we should aim for 4MHz, but
5398 * in fact 1MHz is the correct frequency.
5399 */
5400 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5401 }
5402
5403 if (dev_priv->max_cdclk_freq == 0)
5404 intel_update_max_cdclk(dev);
5405}
5406
Damien Lespiau70d0c572015-06-04 18:21:29 +01005407static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305408{
5409 struct drm_i915_private *dev_priv = dev->dev_private;
5410 uint32_t divider;
5411 uint32_t ratio;
5412 uint32_t current_freq;
5413 int ret;
5414
5415 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5416 switch (frequency) {
5417 case 144000:
5418 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5419 ratio = BXT_DE_PLL_RATIO(60);
5420 break;
5421 case 288000:
5422 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5423 ratio = BXT_DE_PLL_RATIO(60);
5424 break;
5425 case 384000:
5426 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5427 ratio = BXT_DE_PLL_RATIO(60);
5428 break;
5429 case 576000:
5430 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5431 ratio = BXT_DE_PLL_RATIO(60);
5432 break;
5433 case 624000:
5434 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5435 ratio = BXT_DE_PLL_RATIO(65);
5436 break;
5437 case 19200:
5438 /*
5439 * Bypass frequency with DE PLL disabled. Init ratio, divider
5440 * to suppress GCC warning.
5441 */
5442 ratio = 0;
5443 divider = 0;
5444 break;
5445 default:
5446 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5447
5448 return;
5449 }
5450
5451 mutex_lock(&dev_priv->rps.hw_lock);
5452 /* Inform power controller of upcoming frequency change */
5453 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5454 0x80000000);
5455 mutex_unlock(&dev_priv->rps.hw_lock);
5456
5457 if (ret) {
5458 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5459 ret, frequency);
5460 return;
5461 }
5462
5463 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5464 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5465 current_freq = current_freq * 500 + 1000;
5466
5467 /*
5468 * DE PLL has to be disabled when
5469 * - setting to 19.2MHz (bypass, PLL isn't used)
5470 * - before setting to 624MHz (PLL needs toggling)
5471 * - before setting to any frequency from 624MHz (PLL needs toggling)
5472 */
5473 if (frequency == 19200 || frequency == 624000 ||
5474 current_freq == 624000) {
5475 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5476 /* Timeout 200us */
5477 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5478 1))
5479 DRM_ERROR("timout waiting for DE PLL unlock\n");
5480 }
5481
5482 if (frequency != 19200) {
5483 uint32_t val;
5484
5485 val = I915_READ(BXT_DE_PLL_CTL);
5486 val &= ~BXT_DE_PLL_RATIO_MASK;
5487 val |= ratio;
5488 I915_WRITE(BXT_DE_PLL_CTL, val);
5489
5490 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5491 /* Timeout 200us */
5492 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5493 DRM_ERROR("timeout waiting for DE PLL lock\n");
5494
5495 val = I915_READ(CDCLK_CTL);
5496 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5497 val |= divider;
5498 /*
5499 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5500 * enable otherwise.
5501 */
5502 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5503 if (frequency >= 500000)
5504 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5505
5506 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5507 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5508 val |= (frequency - 1000) / 500;
5509 I915_WRITE(CDCLK_CTL, val);
5510 }
5511
5512 mutex_lock(&dev_priv->rps.hw_lock);
5513 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5514 DIV_ROUND_UP(frequency, 25000));
5515 mutex_unlock(&dev_priv->rps.hw_lock);
5516
5517 if (ret) {
5518 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5519 ret, frequency);
5520 return;
5521 }
5522
Damien Lespiaua47871b2015-06-04 18:21:34 +01005523 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305524}
5525
5526void broxton_init_cdclk(struct drm_device *dev)
5527{
5528 struct drm_i915_private *dev_priv = dev->dev_private;
5529 uint32_t val;
5530
5531 /*
5532 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5533 * or else the reset will hang because there is no PCH to respond.
5534 * Move the handshake programming to initialization sequence.
5535 * Previously was left up to BIOS.
5536 */
5537 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5538 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5539 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5540
5541 /* Enable PG1 for cdclk */
5542 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5543
5544 /* check if cd clock is enabled */
5545 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5546 DRM_DEBUG_KMS("Display already initialized\n");
5547 return;
5548 }
5549
5550 /*
5551 * FIXME:
5552 * - The initial CDCLK needs to be read from VBT.
5553 * Need to make this change after VBT has changes for BXT.
5554 * - check if setting the max (or any) cdclk freq is really necessary
5555 * here, it belongs to modeset time
5556 */
5557 broxton_set_cdclk(dev, 624000);
5558
5559 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005560 POSTING_READ(DBUF_CTL);
5561
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305562 udelay(10);
5563
5564 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5565 DRM_ERROR("DBuf power enable timeout!\n");
5566}
5567
5568void broxton_uninit_cdclk(struct drm_device *dev)
5569{
5570 struct drm_i915_private *dev_priv = dev->dev_private;
5571
5572 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005573 POSTING_READ(DBUF_CTL);
5574
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305575 udelay(10);
5576
5577 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5578 DRM_ERROR("DBuf power disable timeout!\n");
5579
5580 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5581 broxton_set_cdclk(dev, 19200);
5582
5583 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5584}
5585
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005586static const struct skl_cdclk_entry {
5587 unsigned int freq;
5588 unsigned int vco;
5589} skl_cdclk_frequencies[] = {
5590 { .freq = 308570, .vco = 8640 },
5591 { .freq = 337500, .vco = 8100 },
5592 { .freq = 432000, .vco = 8640 },
5593 { .freq = 450000, .vco = 8100 },
5594 { .freq = 540000, .vco = 8100 },
5595 { .freq = 617140, .vco = 8640 },
5596 { .freq = 675000, .vco = 8100 },
5597};
5598
5599static unsigned int skl_cdclk_decimal(unsigned int freq)
5600{
5601 return (freq - 1000) / 500;
5602}
5603
5604static unsigned int skl_cdclk_get_vco(unsigned int freq)
5605{
5606 unsigned int i;
5607
5608 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5609 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5610
5611 if (e->freq == freq)
5612 return e->vco;
5613 }
5614
5615 return 8100;
5616}
5617
5618static void
5619skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5620{
5621 unsigned int min_freq;
5622 u32 val;
5623
5624 /* select the minimum CDCLK before enabling DPLL 0 */
5625 val = I915_READ(CDCLK_CTL);
5626 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5627 val |= CDCLK_FREQ_337_308;
5628
5629 if (required_vco == 8640)
5630 min_freq = 308570;
5631 else
5632 min_freq = 337500;
5633
5634 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5635
5636 I915_WRITE(CDCLK_CTL, val);
5637 POSTING_READ(CDCLK_CTL);
5638
5639 /*
5640 * We always enable DPLL0 with the lowest link rate possible, but still
5641 * taking into account the VCO required to operate the eDP panel at the
5642 * desired frequency. The usual DP link rates operate with a VCO of
5643 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5644 * The modeset code is responsible for the selection of the exact link
5645 * rate later on, with the constraint of choosing a frequency that
5646 * works with required_vco.
5647 */
5648 val = I915_READ(DPLL_CTRL1);
5649
5650 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5651 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5652 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5653 if (required_vco == 8640)
5654 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5655 SKL_DPLL0);
5656 else
5657 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5658 SKL_DPLL0);
5659
5660 I915_WRITE(DPLL_CTRL1, val);
5661 POSTING_READ(DPLL_CTRL1);
5662
5663 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5664
5665 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5666 DRM_ERROR("DPLL0 not locked\n");
5667}
5668
5669static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5670{
5671 int ret;
5672 u32 val;
5673
5674 /* inform PCU we want to change CDCLK */
5675 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5676 mutex_lock(&dev_priv->rps.hw_lock);
5677 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5678 mutex_unlock(&dev_priv->rps.hw_lock);
5679
5680 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5681}
5682
5683static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5684{
5685 unsigned int i;
5686
5687 for (i = 0; i < 15; i++) {
5688 if (skl_cdclk_pcu_ready(dev_priv))
5689 return true;
5690 udelay(10);
5691 }
5692
5693 return false;
5694}
5695
5696static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5697{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005698 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005699 u32 freq_select, pcu_ack;
5700
5701 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5702
5703 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5704 DRM_ERROR("failed to inform PCU about cdclk change\n");
5705 return;
5706 }
5707
5708 /* set CDCLK_CTL */
5709 switch(freq) {
5710 case 450000:
5711 case 432000:
5712 freq_select = CDCLK_FREQ_450_432;
5713 pcu_ack = 1;
5714 break;
5715 case 540000:
5716 freq_select = CDCLK_FREQ_540;
5717 pcu_ack = 2;
5718 break;
5719 case 308570:
5720 case 337500:
5721 default:
5722 freq_select = CDCLK_FREQ_337_308;
5723 pcu_ack = 0;
5724 break;
5725 case 617140:
5726 case 675000:
5727 freq_select = CDCLK_FREQ_675_617;
5728 pcu_ack = 3;
5729 break;
5730 }
5731
5732 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5733 POSTING_READ(CDCLK_CTL);
5734
5735 /* inform PCU of the change */
5736 mutex_lock(&dev_priv->rps.hw_lock);
5737 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5738 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005739
5740 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005741}
5742
5743void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5744{
5745 /* disable DBUF power */
5746 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5747 POSTING_READ(DBUF_CTL);
5748
5749 udelay(10);
5750
5751 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5752 DRM_ERROR("DBuf power disable timeout\n");
5753
Animesh Manna4e961e42015-08-26 01:36:08 +05305754 /*
5755 * DMC assumes ownership of LCPLL and will get confused if we touch it.
5756 */
5757 if (dev_priv->csr.dmc_payload) {
5758 /* disable DPLL0 */
5759 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) &
5760 ~LCPLL_PLL_ENABLE);
5761 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5762 DRM_ERROR("Couldn't disable DPLL0\n");
5763 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005764
5765 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5766}
5767
5768void skl_init_cdclk(struct drm_i915_private *dev_priv)
5769{
5770 u32 val;
5771 unsigned int required_vco;
5772
5773 /* enable PCH reset handshake */
5774 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5775 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5776
5777 /* enable PG1 and Misc I/O */
5778 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5779
Gary Wang39d9b852015-08-28 16:40:34 +08005780 /* DPLL0 not enabled (happens on early BIOS versions) */
5781 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5782 /* enable DPLL0 */
5783 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5784 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005785 }
5786
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005787 /* set CDCLK to the frequency the BIOS chose */
5788 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5789
5790 /* enable DBUF power */
5791 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5792 POSTING_READ(DBUF_CTL);
5793
5794 udelay(10);
5795
5796 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5797 DRM_ERROR("DBuf power enable timeout\n");
5798}
5799
Jesse Barnes30a970c2013-11-04 13:48:12 -08005800/* Adjust CDclk dividers to allow high res or save power if possible */
5801static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5802{
5803 struct drm_i915_private *dev_priv = dev->dev_private;
5804 u32 val, cmd;
5805
Vandana Kannan164dfd22014-11-24 13:37:41 +05305806 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5807 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005808
Ville Syrjälädfcab172014-06-13 13:37:47 +03005809 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005810 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005811 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005812 cmd = 1;
5813 else
5814 cmd = 0;
5815
5816 mutex_lock(&dev_priv->rps.hw_lock);
5817 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5818 val &= ~DSPFREQGUAR_MASK;
5819 val |= (cmd << DSPFREQGUAR_SHIFT);
5820 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5821 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5822 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5823 50)) {
5824 DRM_ERROR("timed out waiting for CDclk change\n");
5825 }
5826 mutex_unlock(&dev_priv->rps.hw_lock);
5827
Ville Syrjälä54433e92015-05-26 20:42:31 +03005828 mutex_lock(&dev_priv->sb_lock);
5829
Ville Syrjälädfcab172014-06-13 13:37:47 +03005830 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005831 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005832
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005833 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005834
Jesse Barnes30a970c2013-11-04 13:48:12 -08005835 /* adjust cdclk divider */
5836 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005837 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005838 val |= divider;
5839 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005840
5841 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005842 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005843 50))
5844 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005845 }
5846
Jesse Barnes30a970c2013-11-04 13:48:12 -08005847 /* adjust self-refresh exit latency value */
5848 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5849 val &= ~0x7f;
5850
5851 /*
5852 * For high bandwidth configs, we set a higher latency in the bunit
5853 * so that the core display fetch happens in time to avoid underruns.
5854 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005855 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005856 val |= 4500 / 250; /* 4.5 usec */
5857 else
5858 val |= 3000 / 250; /* 3.0 usec */
5859 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005860
Ville Syrjäläa5805162015-05-26 20:42:30 +03005861 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005862
Ville Syrjäläb6283052015-06-03 15:45:07 +03005863 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005864}
5865
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005866static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5867{
5868 struct drm_i915_private *dev_priv = dev->dev_private;
5869 u32 val, cmd;
5870
Vandana Kannan164dfd22014-11-24 13:37:41 +05305871 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5872 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005873
5874 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005875 case 333333:
5876 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005877 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005878 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005879 break;
5880 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005881 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005882 return;
5883 }
5884
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005885 /*
5886 * Specs are full of misinformation, but testing on actual
5887 * hardware has shown that we just need to write the desired
5888 * CCK divider into the Punit register.
5889 */
5890 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5891
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005892 mutex_lock(&dev_priv->rps.hw_lock);
5893 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5894 val &= ~DSPFREQGUAR_MASK_CHV;
5895 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5896 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5897 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5898 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5899 50)) {
5900 DRM_ERROR("timed out waiting for CDclk change\n");
5901 }
5902 mutex_unlock(&dev_priv->rps.hw_lock);
5903
Ville Syrjäläb6283052015-06-03 15:45:07 +03005904 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005905}
5906
Jesse Barnes30a970c2013-11-04 13:48:12 -08005907static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5908 int max_pixclk)
5909{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005910 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005911 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005912
Jesse Barnes30a970c2013-11-04 13:48:12 -08005913 /*
5914 * Really only a few cases to deal with, as only 4 CDclks are supported:
5915 * 200MHz
5916 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005917 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005918 * 400MHz (VLV only)
5919 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5920 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005921 *
5922 * We seem to get an unstable or solid color picture at 200MHz.
5923 * Not sure what's wrong. For now use 200MHz only when all pipes
5924 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005925 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005926 if (!IS_CHERRYVIEW(dev_priv) &&
5927 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005928 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005929 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005930 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005931 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005932 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005933 else
5934 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005935}
5936
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305937static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5938 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005939{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305940 /*
5941 * FIXME:
5942 * - remove the guardband, it's not needed on BXT
5943 * - set 19.2MHz bypass frequency if there are no active pipes
5944 */
5945 if (max_pixclk > 576000*9/10)
5946 return 624000;
5947 else if (max_pixclk > 384000*9/10)
5948 return 576000;
5949 else if (max_pixclk > 288000*9/10)
5950 return 384000;
5951 else if (max_pixclk > 144000*9/10)
5952 return 288000;
5953 else
5954 return 144000;
5955}
5956
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005957/* Compute the max pixel clock for new configuration. Uses atomic state if
5958 * that's non-NULL, look at current state otherwise. */
5959static int intel_mode_max_pixclk(struct drm_device *dev,
5960 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005961{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005962 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005963 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005964 int max_pixclk = 0;
5965
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005966 for_each_intel_crtc(dev, intel_crtc) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005967 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005968 if (IS_ERR(crtc_state))
5969 return PTR_ERR(crtc_state);
5970
5971 if (!crtc_state->base.enable)
5972 continue;
5973
5974 max_pixclk = max(max_pixclk,
5975 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005976 }
5977
5978 return max_pixclk;
5979}
5980
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005981static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005982{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005983 struct drm_device *dev = state->dev;
5984 struct drm_i915_private *dev_priv = dev->dev_private;
5985 int max_pixclk = intel_mode_max_pixclk(dev, state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005986
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005987 if (max_pixclk < 0)
5988 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005989
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005990 to_intel_atomic_state(state)->cdclk =
5991 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305992
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005993 return 0;
5994}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005995
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005996static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5997{
5998 struct drm_device *dev = state->dev;
5999 struct drm_i915_private *dev_priv = dev->dev_private;
6000 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006001
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006002 if (max_pixclk < 0)
6003 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006004
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006005 to_intel_atomic_state(state)->cdclk =
6006 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006007
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006008 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006009}
6010
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006011static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6012{
6013 unsigned int credits, default_credits;
6014
6015 if (IS_CHERRYVIEW(dev_priv))
6016 default_credits = PFI_CREDIT(12);
6017 else
6018 default_credits = PFI_CREDIT(8);
6019
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006020 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006021 /* CHV suggested value is 31 or 63 */
6022 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006023 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006024 else
6025 credits = PFI_CREDIT(15);
6026 } else {
6027 credits = default_credits;
6028 }
6029
6030 /*
6031 * WA - write default credits before re-programming
6032 * FIXME: should we also set the resend bit here?
6033 */
6034 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6035 default_credits);
6036
6037 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6038 credits | PFI_CREDIT_RESEND);
6039
6040 /*
6041 * FIXME is this guaranteed to clear
6042 * immediately or should we poll for it?
6043 */
6044 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6045}
6046
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006047static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006048{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006049 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006050 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006051 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006052
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006053 /*
6054 * FIXME: We can end up here with all power domains off, yet
6055 * with a CDCLK frequency other than the minimum. To account
6056 * for this take the PIPE-A power domain, which covers the HW
6057 * blocks needed for the following programming. This can be
6058 * removed once it's guaranteed that we get here either with
6059 * the minimum CDCLK set, or the required power domains
6060 * enabled.
6061 */
6062 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006063
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006064 if (IS_CHERRYVIEW(dev))
6065 cherryview_set_cdclk(dev, req_cdclk);
6066 else
6067 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006068
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006069 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006070
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006071 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006072}
6073
Jesse Barnes89b667f2013-04-18 14:51:36 -07006074static void valleyview_crtc_enable(struct drm_crtc *crtc)
6075{
6076 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006077 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6079 struct intel_encoder *encoder;
6080 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006081 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006082
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006083 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006084 return;
6085
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006086 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306087
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006088 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306089 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006090
6091 intel_set_pipe_timings(intel_crtc);
6092
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006093 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6094 struct drm_i915_private *dev_priv = dev->dev_private;
6095
6096 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6097 I915_WRITE(CHV_CANVAS(pipe), 0);
6098 }
6099
Daniel Vetter5b18e572014-04-24 23:55:06 +02006100 i9xx_set_pipeconf(intel_crtc);
6101
Jesse Barnes89b667f2013-04-18 14:51:36 -07006102 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006103
Daniel Vettera72e4c92014-09-30 10:56:47 +02006104 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006105
Jesse Barnes89b667f2013-04-18 14:51:36 -07006106 for_each_encoder_on_crtc(dev, crtc, encoder)
6107 if (encoder->pre_pll_enable)
6108 encoder->pre_pll_enable(encoder);
6109
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006110 if (!is_dsi) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006111 if (IS_CHERRYVIEW(dev)) {
6112 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006113 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006114 } else {
6115 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006116 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006117 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006118 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006119
6120 for_each_encoder_on_crtc(dev, crtc, encoder)
6121 if (encoder->pre_enable)
6122 encoder->pre_enable(encoder);
6123
Jesse Barnes2dd24552013-04-25 12:55:01 -07006124 i9xx_pfit_enable(intel_crtc);
6125
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006126 intel_crtc_load_lut(crtc);
6127
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006128 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006129
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006130 assert_vblank_disabled(crtc);
6131 drm_crtc_vblank_on(crtc);
6132
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006133 for_each_encoder_on_crtc(dev, crtc, encoder)
6134 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006135}
6136
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006137static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6138{
6139 struct drm_device *dev = crtc->base.dev;
6140 struct drm_i915_private *dev_priv = dev->dev_private;
6141
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006142 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6143 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006144}
6145
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006146static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006147{
6148 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006149 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006150 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006151 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006152 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006153
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006154 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006155 return;
6156
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006157 i9xx_set_pll_dividers(intel_crtc);
6158
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006159 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306160 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006161
6162 intel_set_pipe_timings(intel_crtc);
6163
Daniel Vetter5b18e572014-04-24 23:55:06 +02006164 i9xx_set_pipeconf(intel_crtc);
6165
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006166 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006167
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006168 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006169 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006170
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006171 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006172 if (encoder->pre_enable)
6173 encoder->pre_enable(encoder);
6174
Daniel Vetterf6736a12013-06-05 13:34:30 +02006175 i9xx_enable_pll(intel_crtc);
6176
Jesse Barnes2dd24552013-04-25 12:55:01 -07006177 i9xx_pfit_enable(intel_crtc);
6178
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006179 intel_crtc_load_lut(crtc);
6180
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006181 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006182 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006183
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006184 assert_vblank_disabled(crtc);
6185 drm_crtc_vblank_on(crtc);
6186
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006187 for_each_encoder_on_crtc(dev, crtc, encoder)
6188 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006189}
6190
Daniel Vetter87476d62013-04-11 16:29:06 +02006191static void i9xx_pfit_disable(struct intel_crtc *crtc)
6192{
6193 struct drm_device *dev = crtc->base.dev;
6194 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006195
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006196 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006197 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006198
6199 assert_pipe_disabled(dev_priv, crtc->pipe);
6200
Daniel Vetter328d8e82013-05-08 10:36:31 +02006201 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6202 I915_READ(PFIT_CONTROL));
6203 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006204}
6205
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006206static void i9xx_crtc_disable(struct drm_crtc *crtc)
6207{
6208 struct drm_device *dev = crtc->dev;
6209 struct drm_i915_private *dev_priv = dev->dev_private;
6210 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006211 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006212 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006213
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006214 /*
6215 * On gen2 planes are double buffered but the pipe isn't, so we must
6216 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006217 * We also need to wait on all gmch platforms because of the
6218 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006219 */
Imre Deak564ed192014-06-13 14:54:21 +03006220 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006221
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006222 for_each_encoder_on_crtc(dev, crtc, encoder)
6223 encoder->disable(encoder);
6224
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006225 drm_crtc_vblank_off(crtc);
6226 assert_vblank_disabled(crtc);
6227
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006228 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006229
Daniel Vetter87476d62013-04-11 16:29:06 +02006230 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006231
Jesse Barnes89b667f2013-04-18 14:51:36 -07006232 for_each_encoder_on_crtc(dev, crtc, encoder)
6233 if (encoder->post_disable)
6234 encoder->post_disable(encoder);
6235
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006236 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006237 if (IS_CHERRYVIEW(dev))
6238 chv_disable_pll(dev_priv, pipe);
6239 else if (IS_VALLEYVIEW(dev))
6240 vlv_disable_pll(dev_priv, pipe);
6241 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006242 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006243 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006244
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006245 for_each_encoder_on_crtc(dev, crtc, encoder)
6246 if (encoder->post_pll_disable)
6247 encoder->post_pll_disable(encoder);
6248
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006249 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006250 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006251}
6252
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006253static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006254{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006255 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006256 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006257 enum intel_display_power_domain domain;
6258 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006259
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006260 if (!intel_crtc->active)
6261 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006262
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006263 if (to_intel_plane_state(crtc->primary->state)->visible) {
6264 intel_crtc_wait_for_pending_flips(crtc);
6265 intel_pre_disable_primary(crtc);
6266 }
6267
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02006268 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006269 dev_priv->display.crtc_disable(crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006270 intel_crtc->active = false;
6271 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006272 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006273
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006274 domains = intel_crtc->enabled_power_domains;
6275 for_each_power_domain(domain, domains)
6276 intel_display_power_put(dev_priv, domain);
6277 intel_crtc->enabled_power_domains = 0;
6278}
6279
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006280/*
6281 * turn all crtc's off, but do not adjust state
6282 * This has to be paired with a call to intel_modeset_setup_hw_state.
6283 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006284int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006285{
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006286 struct drm_mode_config *config = &dev->mode_config;
6287 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
6288 struct drm_atomic_state *state;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006289 struct drm_crtc *crtc;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006290 unsigned crtc_mask = 0;
6291 int ret = 0;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006292
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006293 if (WARN_ON(!ctx))
6294 return 0;
6295
6296 lockdep_assert_held(&ctx->ww_ctx);
6297 state = drm_atomic_state_alloc(dev);
6298 if (WARN_ON(!state))
6299 return -ENOMEM;
6300
6301 state->acquire_ctx = ctx;
6302 state->allow_modeset = true;
6303
6304 for_each_crtc(dev, crtc) {
6305 struct drm_crtc_state *crtc_state =
6306 drm_atomic_get_crtc_state(state, crtc);
6307
6308 ret = PTR_ERR_OR_ZERO(crtc_state);
6309 if (ret)
6310 goto free;
6311
6312 if (!crtc_state->active)
6313 continue;
6314
6315 crtc_state->active = false;
6316 crtc_mask |= 1 << drm_crtc_index(crtc);
6317 }
6318
6319 if (crtc_mask) {
Maarten Lankhorst74c090b2015-07-13 16:30:30 +02006320 ret = drm_atomic_commit(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006321
6322 if (!ret) {
6323 for_each_crtc(dev, crtc)
6324 if (crtc_mask & (1 << drm_crtc_index(crtc)))
6325 crtc->state->active = true;
6326
6327 return ret;
6328 }
6329 }
6330
6331free:
6332 if (ret)
6333 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
6334 drm_atomic_state_free(state);
6335 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006336}
6337
Chris Wilsonea5b2132010-08-04 13:50:23 +01006338void intel_encoder_destroy(struct drm_encoder *encoder)
6339{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006340 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006341
Chris Wilsonea5b2132010-08-04 13:50:23 +01006342 drm_encoder_cleanup(encoder);
6343 kfree(intel_encoder);
6344}
6345
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006346/* Cross check the actual hw state with our own modeset state tracking (and it's
6347 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006348static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006349{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006350 struct drm_crtc *crtc = connector->base.state->crtc;
6351
6352 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6353 connector->base.base.id,
6354 connector->base.name);
6355
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006356 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006357 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006358 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006359
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006360 I915_STATE_WARN(!crtc,
6361 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006362
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006363 if (!crtc)
6364 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006365
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006366 I915_STATE_WARN(!crtc->state->active,
6367 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006368
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006369 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006370 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006371
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006372 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006373 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006374
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006375 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006376 "attached encoder crtc differs from connector crtc\n");
6377 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006378 I915_STATE_WARN(crtc && crtc->state->active,
6379 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006380 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6381 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006382 }
6383}
6384
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006385int intel_connector_init(struct intel_connector *connector)
6386{
6387 struct drm_connector_state *connector_state;
6388
6389 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6390 if (!connector_state)
6391 return -ENOMEM;
6392
6393 connector->base.state = connector_state;
6394 return 0;
6395}
6396
6397struct intel_connector *intel_connector_alloc(void)
6398{
6399 struct intel_connector *connector;
6400
6401 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6402 if (!connector)
6403 return NULL;
6404
6405 if (intel_connector_init(connector) < 0) {
6406 kfree(connector);
6407 return NULL;
6408 }
6409
6410 return connector;
6411}
6412
Daniel Vetterf0947c32012-07-02 13:10:34 +02006413/* Simple connector->get_hw_state implementation for encoders that support only
6414 * one connector and no cloning and hence the encoder state determines the state
6415 * of the connector. */
6416bool intel_connector_get_hw_state(struct intel_connector *connector)
6417{
Daniel Vetter24929352012-07-02 20:28:59 +02006418 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006419 struct intel_encoder *encoder = connector->encoder;
6420
6421 return encoder->get_hw_state(encoder, &pipe);
6422}
6423
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006424static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006425{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006426 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6427 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006428
6429 return 0;
6430}
6431
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006432static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006433 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006434{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006435 struct drm_atomic_state *state = pipe_config->base.state;
6436 struct intel_crtc *other_crtc;
6437 struct intel_crtc_state *other_crtc_state;
6438
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006439 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6440 pipe_name(pipe), pipe_config->fdi_lanes);
6441 if (pipe_config->fdi_lanes > 4) {
6442 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6443 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006444 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006445 }
6446
Paulo Zanonibafb6552013-11-02 21:07:44 -07006447 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006448 if (pipe_config->fdi_lanes > 2) {
6449 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6450 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006451 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006452 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006453 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006454 }
6455 }
6456
6457 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006458 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006459
6460 /* Ivybridge 3 pipe is really complicated */
6461 switch (pipe) {
6462 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006463 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006464 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006465 if (pipe_config->fdi_lanes <= 2)
6466 return 0;
6467
6468 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6469 other_crtc_state =
6470 intel_atomic_get_crtc_state(state, other_crtc);
6471 if (IS_ERR(other_crtc_state))
6472 return PTR_ERR(other_crtc_state);
6473
6474 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006475 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6476 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006477 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006478 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006479 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006480 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006481 if (pipe_config->fdi_lanes > 2) {
6482 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6483 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006484 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006485 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006486
6487 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6488 other_crtc_state =
6489 intel_atomic_get_crtc_state(state, other_crtc);
6490 if (IS_ERR(other_crtc_state))
6491 return PTR_ERR(other_crtc_state);
6492
6493 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006494 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006495 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006496 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006497 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006498 default:
6499 BUG();
6500 }
6501}
6502
Daniel Vettere29c22c2013-02-21 00:00:16 +01006503#define RETRY 1
6504static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006505 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006506{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006507 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006508 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006509 int lane, link_bw, fdi_dotclock, ret;
6510 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006511
Daniel Vettere29c22c2013-02-21 00:00:16 +01006512retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006513 /* FDI is a binary signal running at ~2.7GHz, encoding
6514 * each output octet as 10 bits. The actual frequency
6515 * is stored as a divider into a 100MHz clock, and the
6516 * mode pixel clock is stored in units of 1KHz.
6517 * Hence the bw of each lane in terms of the mode signal
6518 * is:
6519 */
6520 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6521
Damien Lespiau241bfc32013-09-25 16:45:37 +01006522 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006523
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006524 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006525 pipe_config->pipe_bpp);
6526
6527 pipe_config->fdi_lanes = lane;
6528
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006529 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006530 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006531
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006532 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6533 intel_crtc->pipe, pipe_config);
6534 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006535 pipe_config->pipe_bpp -= 2*3;
6536 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6537 pipe_config->pipe_bpp);
6538 needs_recompute = true;
6539 pipe_config->bw_constrained = true;
6540
6541 goto retry;
6542 }
6543
6544 if (needs_recompute)
6545 return RETRY;
6546
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006547 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006548}
6549
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006550static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6551 struct intel_crtc_state *pipe_config)
6552{
6553 if (pipe_config->pipe_bpp > 24)
6554 return false;
6555
6556 /* HSW can handle pixel rate up to cdclk? */
6557 if (IS_HASWELL(dev_priv->dev))
6558 return true;
6559
6560 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006561 * We compare against max which means we must take
6562 * the increased cdclk requirement into account when
6563 * calculating the new cdclk.
6564 *
6565 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006566 */
6567 return ilk_pipe_pixel_rate(pipe_config) <=
6568 dev_priv->max_cdclk_freq * 95 / 100;
6569}
6570
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006571static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006572 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006573{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006574 struct drm_device *dev = crtc->base.dev;
6575 struct drm_i915_private *dev_priv = dev->dev_private;
6576
Jani Nikulad330a952014-01-21 11:24:25 +02006577 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006578 hsw_crtc_supports_ips(crtc) &&
6579 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006580}
6581
Daniel Vettera43f6e02013-06-07 23:10:32 +02006582static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006583 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006584{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006585 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006586 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006587 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006588
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006589 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006590 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006591 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006592
6593 /*
6594 * Enable pixel doubling when the dot clock
6595 * is > 90% of the (display) core speed.
6596 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006597 * GDG double wide on either pipe,
6598 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006599 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006600 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006601 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006602 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006603 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006604 }
6605
Damien Lespiau241bfc32013-09-25 16:45:37 +01006606 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006607 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006608 }
Chris Wilson89749352010-09-12 18:25:19 +01006609
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006610 /*
6611 * Pipe horizontal size must be even in:
6612 * - DVO ganged mode
6613 * - LVDS dual channel mode
6614 * - Double wide pipe
6615 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006616 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006617 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6618 pipe_config->pipe_src_w &= ~1;
6619
Damien Lespiau8693a822013-05-03 18:48:11 +01006620 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6621 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006622 */
6623 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006624 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006625 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006626
Damien Lespiauf5adf942013-06-24 18:29:34 +01006627 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006628 hsw_compute_ips_config(crtc, pipe_config);
6629
Daniel Vetter877d48d2013-04-19 11:24:43 +02006630 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006631 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006632
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006633 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006634}
6635
Ville Syrjälä1652d192015-03-31 14:12:01 +03006636static int skylake_get_display_clock_speed(struct drm_device *dev)
6637{
6638 struct drm_i915_private *dev_priv = to_i915(dev);
6639 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6640 uint32_t cdctl = I915_READ(CDCLK_CTL);
6641 uint32_t linkrate;
6642
Damien Lespiau414355a2015-06-04 18:21:31 +01006643 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006644 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006645
6646 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6647 return 540000;
6648
6649 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006650 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006651
Damien Lespiau71cd8422015-04-30 16:39:17 +01006652 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6653 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006654 /* vco 8640 */
6655 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6656 case CDCLK_FREQ_450_432:
6657 return 432000;
6658 case CDCLK_FREQ_337_308:
6659 return 308570;
6660 case CDCLK_FREQ_675_617:
6661 return 617140;
6662 default:
6663 WARN(1, "Unknown cd freq selection\n");
6664 }
6665 } else {
6666 /* vco 8100 */
6667 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6668 case CDCLK_FREQ_450_432:
6669 return 450000;
6670 case CDCLK_FREQ_337_308:
6671 return 337500;
6672 case CDCLK_FREQ_675_617:
6673 return 675000;
6674 default:
6675 WARN(1, "Unknown cd freq selection\n");
6676 }
6677 }
6678
6679 /* error case, do as if DPLL0 isn't enabled */
6680 return 24000;
6681}
6682
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006683static int broxton_get_display_clock_speed(struct drm_device *dev)
6684{
6685 struct drm_i915_private *dev_priv = to_i915(dev);
6686 uint32_t cdctl = I915_READ(CDCLK_CTL);
6687 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6688 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6689 int cdclk;
6690
6691 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6692 return 19200;
6693
6694 cdclk = 19200 * pll_ratio / 2;
6695
6696 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6697 case BXT_CDCLK_CD2X_DIV_SEL_1:
6698 return cdclk; /* 576MHz or 624MHz */
6699 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6700 return cdclk * 2 / 3; /* 384MHz */
6701 case BXT_CDCLK_CD2X_DIV_SEL_2:
6702 return cdclk / 2; /* 288MHz */
6703 case BXT_CDCLK_CD2X_DIV_SEL_4:
6704 return cdclk / 4; /* 144MHz */
6705 }
6706
6707 /* error case, do as if DE PLL isn't enabled */
6708 return 19200;
6709}
6710
Ville Syrjälä1652d192015-03-31 14:12:01 +03006711static int broadwell_get_display_clock_speed(struct drm_device *dev)
6712{
6713 struct drm_i915_private *dev_priv = dev->dev_private;
6714 uint32_t lcpll = I915_READ(LCPLL_CTL);
6715 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6716
6717 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6718 return 800000;
6719 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6720 return 450000;
6721 else if (freq == LCPLL_CLK_FREQ_450)
6722 return 450000;
6723 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6724 return 540000;
6725 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6726 return 337500;
6727 else
6728 return 675000;
6729}
6730
6731static int haswell_get_display_clock_speed(struct drm_device *dev)
6732{
6733 struct drm_i915_private *dev_priv = dev->dev_private;
6734 uint32_t lcpll = I915_READ(LCPLL_CTL);
6735 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6736
6737 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6738 return 800000;
6739 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6740 return 450000;
6741 else if (freq == LCPLL_CLK_FREQ_450)
6742 return 450000;
6743 else if (IS_HSW_ULT(dev))
6744 return 337500;
6745 else
6746 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006747}
6748
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006749static int valleyview_get_display_clock_speed(struct drm_device *dev)
6750{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006751 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6752 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006753}
6754
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006755static int ilk_get_display_clock_speed(struct drm_device *dev)
6756{
6757 return 450000;
6758}
6759
Jesse Barnese70236a2009-09-21 10:42:27 -07006760static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006761{
Jesse Barnese70236a2009-09-21 10:42:27 -07006762 return 400000;
6763}
Jesse Barnes79e53942008-11-07 14:24:08 -08006764
Jesse Barnese70236a2009-09-21 10:42:27 -07006765static int i915_get_display_clock_speed(struct drm_device *dev)
6766{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006767 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006768}
Jesse Barnes79e53942008-11-07 14:24:08 -08006769
Jesse Barnese70236a2009-09-21 10:42:27 -07006770static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6771{
6772 return 200000;
6773}
Jesse Barnes79e53942008-11-07 14:24:08 -08006774
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006775static int pnv_get_display_clock_speed(struct drm_device *dev)
6776{
6777 u16 gcfgc = 0;
6778
6779 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6780
6781 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6782 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006783 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006784 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006785 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006786 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006787 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006788 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6789 return 200000;
6790 default:
6791 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6792 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006793 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006794 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006795 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006796 }
6797}
6798
Jesse Barnese70236a2009-09-21 10:42:27 -07006799static int i915gm_get_display_clock_speed(struct drm_device *dev)
6800{
6801 u16 gcfgc = 0;
6802
6803 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6804
6805 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006806 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006807 else {
6808 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6809 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006810 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006811 default:
6812 case GC_DISPLAY_CLOCK_190_200_MHZ:
6813 return 190000;
6814 }
6815 }
6816}
Jesse Barnes79e53942008-11-07 14:24:08 -08006817
Jesse Barnese70236a2009-09-21 10:42:27 -07006818static int i865_get_display_clock_speed(struct drm_device *dev)
6819{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006820 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006821}
6822
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006823static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006824{
6825 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006826
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006827 /*
6828 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6829 * encoding is different :(
6830 * FIXME is this the right way to detect 852GM/852GMV?
6831 */
6832 if (dev->pdev->revision == 0x1)
6833 return 133333;
6834
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006835 pci_bus_read_config_word(dev->pdev->bus,
6836 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6837
Jesse Barnese70236a2009-09-21 10:42:27 -07006838 /* Assume that the hardware is in the high speed state. This
6839 * should be the default.
6840 */
6841 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6842 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006843 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006844 case GC_CLOCK_100_200:
6845 return 200000;
6846 case GC_CLOCK_166_250:
6847 return 250000;
6848 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006849 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006850 case GC_CLOCK_133_266:
6851 case GC_CLOCK_133_266_2:
6852 case GC_CLOCK_166_266:
6853 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006854 }
6855
6856 /* Shouldn't happen */
6857 return 0;
6858}
6859
6860static int i830_get_display_clock_speed(struct drm_device *dev)
6861{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006862 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006863}
6864
Ville Syrjälä34edce22015-05-22 11:22:33 +03006865static unsigned int intel_hpll_vco(struct drm_device *dev)
6866{
6867 struct drm_i915_private *dev_priv = dev->dev_private;
6868 static const unsigned int blb_vco[8] = {
6869 [0] = 3200000,
6870 [1] = 4000000,
6871 [2] = 5333333,
6872 [3] = 4800000,
6873 [4] = 6400000,
6874 };
6875 static const unsigned int pnv_vco[8] = {
6876 [0] = 3200000,
6877 [1] = 4000000,
6878 [2] = 5333333,
6879 [3] = 4800000,
6880 [4] = 2666667,
6881 };
6882 static const unsigned int cl_vco[8] = {
6883 [0] = 3200000,
6884 [1] = 4000000,
6885 [2] = 5333333,
6886 [3] = 6400000,
6887 [4] = 3333333,
6888 [5] = 3566667,
6889 [6] = 4266667,
6890 };
6891 static const unsigned int elk_vco[8] = {
6892 [0] = 3200000,
6893 [1] = 4000000,
6894 [2] = 5333333,
6895 [3] = 4800000,
6896 };
6897 static const unsigned int ctg_vco[8] = {
6898 [0] = 3200000,
6899 [1] = 4000000,
6900 [2] = 5333333,
6901 [3] = 6400000,
6902 [4] = 2666667,
6903 [5] = 4266667,
6904 };
6905 const unsigned int *vco_table;
6906 unsigned int vco;
6907 uint8_t tmp = 0;
6908
6909 /* FIXME other chipsets? */
6910 if (IS_GM45(dev))
6911 vco_table = ctg_vco;
6912 else if (IS_G4X(dev))
6913 vco_table = elk_vco;
6914 else if (IS_CRESTLINE(dev))
6915 vco_table = cl_vco;
6916 else if (IS_PINEVIEW(dev))
6917 vco_table = pnv_vco;
6918 else if (IS_G33(dev))
6919 vco_table = blb_vco;
6920 else
6921 return 0;
6922
6923 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6924
6925 vco = vco_table[tmp & 0x7];
6926 if (vco == 0)
6927 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6928 else
6929 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6930
6931 return vco;
6932}
6933
6934static int gm45_get_display_clock_speed(struct drm_device *dev)
6935{
6936 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6937 uint16_t tmp = 0;
6938
6939 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6940
6941 cdclk_sel = (tmp >> 12) & 0x1;
6942
6943 switch (vco) {
6944 case 2666667:
6945 case 4000000:
6946 case 5333333:
6947 return cdclk_sel ? 333333 : 222222;
6948 case 3200000:
6949 return cdclk_sel ? 320000 : 228571;
6950 default:
6951 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6952 return 222222;
6953 }
6954}
6955
6956static int i965gm_get_display_clock_speed(struct drm_device *dev)
6957{
6958 static const uint8_t div_3200[] = { 16, 10, 8 };
6959 static const uint8_t div_4000[] = { 20, 12, 10 };
6960 static const uint8_t div_5333[] = { 24, 16, 14 };
6961 const uint8_t *div_table;
6962 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6963 uint16_t tmp = 0;
6964
6965 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6966
6967 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6968
6969 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6970 goto fail;
6971
6972 switch (vco) {
6973 case 3200000:
6974 div_table = div_3200;
6975 break;
6976 case 4000000:
6977 div_table = div_4000;
6978 break;
6979 case 5333333:
6980 div_table = div_5333;
6981 break;
6982 default:
6983 goto fail;
6984 }
6985
6986 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6987
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006988fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006989 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6990 return 200000;
6991}
6992
6993static int g33_get_display_clock_speed(struct drm_device *dev)
6994{
6995 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6996 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6997 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6998 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6999 const uint8_t *div_table;
7000 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7001 uint16_t tmp = 0;
7002
7003 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7004
7005 cdclk_sel = (tmp >> 4) & 0x7;
7006
7007 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7008 goto fail;
7009
7010 switch (vco) {
7011 case 3200000:
7012 div_table = div_3200;
7013 break;
7014 case 4000000:
7015 div_table = div_4000;
7016 break;
7017 case 4800000:
7018 div_table = div_4800;
7019 break;
7020 case 5333333:
7021 div_table = div_5333;
7022 break;
7023 default:
7024 goto fail;
7025 }
7026
7027 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7028
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007029fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007030 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7031 return 190476;
7032}
7033
Zhenyu Wang2c072452009-06-05 15:38:42 +08007034static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007035intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007036{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007037 while (*num > DATA_LINK_M_N_MASK ||
7038 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007039 *num >>= 1;
7040 *den >>= 1;
7041 }
7042}
7043
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007044static void compute_m_n(unsigned int m, unsigned int n,
7045 uint32_t *ret_m, uint32_t *ret_n)
7046{
7047 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7048 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7049 intel_reduce_m_n_ratio(ret_m, ret_n);
7050}
7051
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007052void
7053intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7054 int pixel_clock, int link_clock,
7055 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007056{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007057 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007058
7059 compute_m_n(bits_per_pixel * pixel_clock,
7060 link_clock * nlanes * 8,
7061 &m_n->gmch_m, &m_n->gmch_n);
7062
7063 compute_m_n(pixel_clock, link_clock,
7064 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007065}
7066
Chris Wilsona7615032011-01-12 17:04:08 +00007067static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7068{
Jani Nikulad330a952014-01-21 11:24:25 +02007069 if (i915.panel_use_ssc >= 0)
7070 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007071 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007072 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007073}
7074
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007075static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7076 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007077{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007078 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007079 struct drm_i915_private *dev_priv = dev->dev_private;
7080 int refclk;
7081
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007082 WARN_ON(!crtc_state->base.state);
7083
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007084 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007085 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007086 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007087 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007088 refclk = dev_priv->vbt.lvds_ssc_freq;
7089 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007090 } else if (!IS_GEN2(dev)) {
7091 refclk = 96000;
7092 } else {
7093 refclk = 48000;
7094 }
7095
7096 return refclk;
7097}
7098
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007099static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007100{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007101 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007102}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007103
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007104static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7105{
7106 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007107}
7108
Daniel Vetterf47709a2013-03-28 10:42:02 +01007109static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007110 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007111 intel_clock_t *reduced_clock)
7112{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007113 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007114 u32 fp, fp2 = 0;
7115
7116 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007117 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007118 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007119 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007120 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007121 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007122 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007123 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007124 }
7125
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007126 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007127
Daniel Vetterf47709a2013-03-28 10:42:02 +01007128 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007129 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007130 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007131 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007132 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007133 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007134 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007135 }
7136}
7137
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007138static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7139 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007140{
7141 u32 reg_val;
7142
7143 /*
7144 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7145 * and set it to a reasonable value instead.
7146 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007147 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007148 reg_val &= 0xffffff00;
7149 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007150 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007151
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007152 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007153 reg_val &= 0x8cffffff;
7154 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007155 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007156
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007157 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007158 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007159 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007160
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007161 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007162 reg_val &= 0x00ffffff;
7163 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007164 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007165}
7166
Daniel Vetterb5518422013-05-03 11:49:48 +02007167static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7168 struct intel_link_m_n *m_n)
7169{
7170 struct drm_device *dev = crtc->base.dev;
7171 struct drm_i915_private *dev_priv = dev->dev_private;
7172 int pipe = crtc->pipe;
7173
Daniel Vettere3b95f12013-05-03 11:49:49 +02007174 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7175 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7176 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7177 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007178}
7179
7180static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007181 struct intel_link_m_n *m_n,
7182 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007183{
7184 struct drm_device *dev = crtc->base.dev;
7185 struct drm_i915_private *dev_priv = dev->dev_private;
7186 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007187 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007188
7189 if (INTEL_INFO(dev)->gen >= 5) {
7190 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7191 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7192 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7193 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007194 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7195 * for gen < 8) and if DRRS is supported (to make sure the
7196 * registers are not unnecessarily accessed).
7197 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307198 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007199 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007200 I915_WRITE(PIPE_DATA_M2(transcoder),
7201 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7202 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7203 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7204 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7205 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007206 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007207 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7208 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7209 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7210 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007211 }
7212}
7213
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307214void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007215{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307216 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7217
7218 if (m_n == M1_N1) {
7219 dp_m_n = &crtc->config->dp_m_n;
7220 dp_m2_n2 = &crtc->config->dp_m2_n2;
7221 } else if (m_n == M2_N2) {
7222
7223 /*
7224 * M2_N2 registers are not supported. Hence m2_n2 divider value
7225 * needs to be programmed into M1_N1.
7226 */
7227 dp_m_n = &crtc->config->dp_m2_n2;
7228 } else {
7229 DRM_ERROR("Unsupported divider value\n");
7230 return;
7231 }
7232
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007233 if (crtc->config->has_pch_encoder)
7234 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007235 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307236 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007237}
7238
Daniel Vetter251ac862015-06-18 10:30:24 +02007239static void vlv_compute_dpll(struct intel_crtc *crtc,
7240 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007241{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007242 u32 dpll, dpll_md;
7243
7244 /*
7245 * Enable DPIO clock input. We should never disable the reference
7246 * clock for pipe B, since VGA hotplug / manual detection depends
7247 * on it.
7248 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007249 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7250 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007251 /* We should never disable this, set it here for state tracking */
7252 if (crtc->pipe == PIPE_B)
7253 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7254 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007255 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007256
Ville Syrjäläd288f652014-10-28 13:20:22 +02007257 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007258 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007259 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007260}
7261
Ville Syrjäläd288f652014-10-28 13:20:22 +02007262static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007263 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007264{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007265 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007266 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007267 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007268 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007269 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007270 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007271
Ville Syrjäläa5805162015-05-26 20:42:30 +03007272 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007273
Ville Syrjäläd288f652014-10-28 13:20:22 +02007274 bestn = pipe_config->dpll.n;
7275 bestm1 = pipe_config->dpll.m1;
7276 bestm2 = pipe_config->dpll.m2;
7277 bestp1 = pipe_config->dpll.p1;
7278 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007279
Jesse Barnes89b667f2013-04-18 14:51:36 -07007280 /* See eDP HDMI DPIO driver vbios notes doc */
7281
7282 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007283 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007284 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007285
7286 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007287 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007288
7289 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007290 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007291 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007292 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007293
7294 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007295 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007296
7297 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007298 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7299 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7300 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007301 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007302
7303 /*
7304 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7305 * but we don't support that).
7306 * Note: don't use the DAC post divider as it seems unstable.
7307 */
7308 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007309 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007310
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007311 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007312 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007313
Jesse Barnes89b667f2013-04-18 14:51:36 -07007314 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007315 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007316 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7317 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007318 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007319 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007320 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007321 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007322 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007323
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007324 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007325 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007326 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007327 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007328 0x0df40000);
7329 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007330 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007331 0x0df70000);
7332 } else { /* HDMI or VGA */
7333 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007334 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007335 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007336 0x0df70000);
7337 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007338 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007339 0x0df40000);
7340 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007341
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007342 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007343 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007344 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7345 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007346 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007347 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007348
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007349 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007350 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007351}
7352
Daniel Vetter251ac862015-06-18 10:30:24 +02007353static void chv_compute_dpll(struct intel_crtc *crtc,
7354 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007355{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007356 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7357 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007358 DPLL_VCO_ENABLE;
7359 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007360 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007361
Ville Syrjäläd288f652014-10-28 13:20:22 +02007362 pipe_config->dpll_hw_state.dpll_md =
7363 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007364}
7365
Ville Syrjäläd288f652014-10-28 13:20:22 +02007366static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007367 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007368{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007369 struct drm_device *dev = crtc->base.dev;
7370 struct drm_i915_private *dev_priv = dev->dev_private;
7371 int pipe = crtc->pipe;
7372 int dpll_reg = DPLL(crtc->pipe);
7373 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307374 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007375 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307376 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307377 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007378
Ville Syrjäläd288f652014-10-28 13:20:22 +02007379 bestn = pipe_config->dpll.n;
7380 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7381 bestm1 = pipe_config->dpll.m1;
7382 bestm2 = pipe_config->dpll.m2 >> 22;
7383 bestp1 = pipe_config->dpll.p1;
7384 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307385 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307386 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307387 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007388
7389 /*
7390 * Enable Refclk and SSC
7391 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007392 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007393 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007394
Ville Syrjäläa5805162015-05-26 20:42:30 +03007395 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007396
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007397 /* p1 and p2 divider */
7398 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7399 5 << DPIO_CHV_S1_DIV_SHIFT |
7400 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7401 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7402 1 << DPIO_CHV_K_DIV_SHIFT);
7403
7404 /* Feedback post-divider - m2 */
7405 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7406
7407 /* Feedback refclk divider - n and m1 */
7408 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7409 DPIO_CHV_M1_DIV_BY_2 |
7410 1 << DPIO_CHV_N_DIV_SHIFT);
7411
7412 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007413 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007414
7415 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307416 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7417 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7418 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7419 if (bestm2_frac)
7420 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7421 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007422
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307423 /* Program digital lock detect threshold */
7424 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7425 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7426 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7427 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7428 if (!bestm2_frac)
7429 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7430 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7431
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007432 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307433 if (vco == 5400000) {
7434 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7435 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7436 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7437 tribuf_calcntr = 0x9;
7438 } else if (vco <= 6200000) {
7439 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7440 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7441 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7442 tribuf_calcntr = 0x9;
7443 } else if (vco <= 6480000) {
7444 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7445 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7446 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7447 tribuf_calcntr = 0x8;
7448 } else {
7449 /* Not supported. Apply the same limits as in the max case */
7450 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7451 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7452 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7453 tribuf_calcntr = 0;
7454 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007455 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7456
Ville Syrjälä968040b2015-03-11 22:52:08 +02007457 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307458 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7459 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7460 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7461
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007462 /* AFC Recal */
7463 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7464 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7465 DPIO_AFC_RECAL);
7466
Ville Syrjäläa5805162015-05-26 20:42:30 +03007467 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007468}
7469
Ville Syrjäläd288f652014-10-28 13:20:22 +02007470/**
7471 * vlv_force_pll_on - forcibly enable just the PLL
7472 * @dev_priv: i915 private structure
7473 * @pipe: pipe PLL to enable
7474 * @dpll: PLL configuration
7475 *
7476 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7477 * in cases where we need the PLL enabled even when @pipe is not going to
7478 * be enabled.
7479 */
7480void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7481 const struct dpll *dpll)
7482{
7483 struct intel_crtc *crtc =
7484 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007485 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007486 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007487 .pixel_multiplier = 1,
7488 .dpll = *dpll,
7489 };
7490
7491 if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007492 chv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007493 chv_prepare_pll(crtc, &pipe_config);
7494 chv_enable_pll(crtc, &pipe_config);
7495 } else {
Daniel Vetter251ac862015-06-18 10:30:24 +02007496 vlv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007497 vlv_prepare_pll(crtc, &pipe_config);
7498 vlv_enable_pll(crtc, &pipe_config);
7499 }
7500}
7501
7502/**
7503 * vlv_force_pll_off - forcibly disable just the PLL
7504 * @dev_priv: i915 private structure
7505 * @pipe: pipe PLL to disable
7506 *
7507 * Disable the PLL for @pipe. To be used in cases where we need
7508 * the PLL enabled even when @pipe is not going to be enabled.
7509 */
7510void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7511{
7512 if (IS_CHERRYVIEW(dev))
7513 chv_disable_pll(to_i915(dev), pipe);
7514 else
7515 vlv_disable_pll(to_i915(dev), pipe);
7516}
7517
Daniel Vetter251ac862015-06-18 10:30:24 +02007518static void i9xx_compute_dpll(struct intel_crtc *crtc,
7519 struct intel_crtc_state *crtc_state,
7520 intel_clock_t *reduced_clock,
7521 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007522{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007523 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007524 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007525 u32 dpll;
7526 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007527 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007528
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007529 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307530
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007531 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7532 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007533
7534 dpll = DPLL_VGA_MODE_DIS;
7535
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007536 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007537 dpll |= DPLLB_MODE_LVDS;
7538 else
7539 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007540
Daniel Vetteref1b4602013-06-01 17:17:04 +02007541 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007542 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007543 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007544 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007545
7546 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007547 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007548
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007549 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007550 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007551
7552 /* compute bitmask from p1 value */
7553 if (IS_PINEVIEW(dev))
7554 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7555 else {
7556 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7557 if (IS_G4X(dev) && reduced_clock)
7558 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7559 }
7560 switch (clock->p2) {
7561 case 5:
7562 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7563 break;
7564 case 7:
7565 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7566 break;
7567 case 10:
7568 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7569 break;
7570 case 14:
7571 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7572 break;
7573 }
7574 if (INTEL_INFO(dev)->gen >= 4)
7575 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7576
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007577 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007578 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007579 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007580 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7581 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7582 else
7583 dpll |= PLL_REF_INPUT_DREFCLK;
7584
7585 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007586 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007587
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007588 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007589 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007590 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007591 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007592 }
7593}
7594
Daniel Vetter251ac862015-06-18 10:30:24 +02007595static void i8xx_compute_dpll(struct intel_crtc *crtc,
7596 struct intel_crtc_state *crtc_state,
7597 intel_clock_t *reduced_clock,
7598 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007599{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007600 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007601 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007602 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007603 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007604
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007605 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307606
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007607 dpll = DPLL_VGA_MODE_DIS;
7608
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007609 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007610 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7611 } else {
7612 if (clock->p1 == 2)
7613 dpll |= PLL_P1_DIVIDE_BY_TWO;
7614 else
7615 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7616 if (clock->p2 == 4)
7617 dpll |= PLL_P2_DIVIDE_BY_4;
7618 }
7619
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007620 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007621 dpll |= DPLL_DVO_2X_MODE;
7622
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007623 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007624 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7625 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7626 else
7627 dpll |= PLL_REF_INPUT_DREFCLK;
7628
7629 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007630 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007631}
7632
Daniel Vetter8a654f32013-06-01 17:16:22 +02007633static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007634{
7635 struct drm_device *dev = intel_crtc->base.dev;
7636 struct drm_i915_private *dev_priv = dev->dev_private;
7637 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007638 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007639 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007640 uint32_t crtc_vtotal, crtc_vblank_end;
7641 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007642
7643 /* We need to be careful not to changed the adjusted mode, for otherwise
7644 * the hw state checker will get angry at the mismatch. */
7645 crtc_vtotal = adjusted_mode->crtc_vtotal;
7646 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007647
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007648 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007649 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007650 crtc_vtotal -= 1;
7651 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007652
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007653 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007654 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7655 else
7656 vsyncshift = adjusted_mode->crtc_hsync_start -
7657 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007658 if (vsyncshift < 0)
7659 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007660 }
7661
7662 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007663 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007664
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007665 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007666 (adjusted_mode->crtc_hdisplay - 1) |
7667 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007668 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007669 (adjusted_mode->crtc_hblank_start - 1) |
7670 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007671 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007672 (adjusted_mode->crtc_hsync_start - 1) |
7673 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7674
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007675 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007676 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007677 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007678 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007679 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007680 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007681 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007682 (adjusted_mode->crtc_vsync_start - 1) |
7683 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7684
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007685 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7686 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7687 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7688 * bits. */
7689 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7690 (pipe == PIPE_B || pipe == PIPE_C))
7691 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7692
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007693 /* pipesrc controls the size that is scaled from, which should
7694 * always be the user's requested size.
7695 */
7696 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007697 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7698 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007699}
7700
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007701static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007702 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007703{
7704 struct drm_device *dev = crtc->base.dev;
7705 struct drm_i915_private *dev_priv = dev->dev_private;
7706 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7707 uint32_t tmp;
7708
7709 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007710 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7711 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007712 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007713 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7714 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007715 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007716 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7717 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007718
7719 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007720 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7721 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007722 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007723 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7724 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007725 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007726 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7727 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007728
7729 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007730 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7731 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7732 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007733 }
7734
7735 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007736 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7737 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7738
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007739 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7740 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007741}
7742
Daniel Vetterf6a83282014-02-11 15:28:57 -08007743void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007744 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007745{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007746 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7747 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7748 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7749 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007750
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007751 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7752 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7753 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7754 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007755
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007756 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007757 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007758
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007759 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7760 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007761
7762 mode->hsync = drm_mode_hsync(mode);
7763 mode->vrefresh = drm_mode_vrefresh(mode);
7764 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007765}
7766
Daniel Vetter84b046f2013-02-19 18:48:54 +01007767static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7768{
7769 struct drm_device *dev = intel_crtc->base.dev;
7770 struct drm_i915_private *dev_priv = dev->dev_private;
7771 uint32_t pipeconf;
7772
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007773 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007774
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007775 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7776 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7777 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007778
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007779 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007780 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007781
Daniel Vetterff9ce462013-04-24 14:57:17 +02007782 /* only g4x and later have fancy bpc/dither controls */
7783 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007784 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007785 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007786 pipeconf |= PIPECONF_DITHER_EN |
7787 PIPECONF_DITHER_TYPE_SP;
7788
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007789 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007790 case 18:
7791 pipeconf |= PIPECONF_6BPC;
7792 break;
7793 case 24:
7794 pipeconf |= PIPECONF_8BPC;
7795 break;
7796 case 30:
7797 pipeconf |= PIPECONF_10BPC;
7798 break;
7799 default:
7800 /* Case prevented by intel_choose_pipe_bpp_dither. */
7801 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007802 }
7803 }
7804
7805 if (HAS_PIPE_CXSR(dev)) {
7806 if (intel_crtc->lowfreq_avail) {
7807 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7808 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7809 } else {
7810 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007811 }
7812 }
7813
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007814 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007815 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007816 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007817 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7818 else
7819 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7820 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007821 pipeconf |= PIPECONF_PROGRESSIVE;
7822
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007823 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007824 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007825
Daniel Vetter84b046f2013-02-19 18:48:54 +01007826 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7827 POSTING_READ(PIPECONF(intel_crtc->pipe));
7828}
7829
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007830static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7831 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007832{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007833 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007834 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007835 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007836 intel_clock_t clock;
7837 bool ok;
7838 bool is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007839 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007840 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007841 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007842 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007843 struct drm_connector_state *connector_state;
7844 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007845
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007846 memset(&crtc_state->dpll_hw_state, 0,
7847 sizeof(crtc_state->dpll_hw_state));
7848
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007849 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007850 if (connector_state->crtc != &crtc->base)
7851 continue;
7852
7853 encoder = to_intel_encoder(connector_state->best_encoder);
7854
Chris Wilson5eddb702010-09-11 13:48:45 +01007855 switch (encoder->type) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007856 case INTEL_OUTPUT_DSI:
7857 is_dsi = true;
7858 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007859 default:
7860 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007861 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007862
Eric Anholtc751ce42010-03-25 11:48:48 -07007863 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007864 }
7865
Jani Nikulaf2335332013-09-13 11:03:09 +03007866 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007867 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007868
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007869 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007870 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007871
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007872 /*
7873 * Returns a set of divisors for the desired target clock with
7874 * the given refclk, or FALSE. The returned values represent
7875 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7876 * 2) / p1 / p2.
7877 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007878 limit = intel_limit(crtc_state, refclk);
7879 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007880 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007881 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007882 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007883 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7884 return -EINVAL;
7885 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007886
Jani Nikulaf2335332013-09-13 11:03:09 +03007887 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007888 crtc_state->dpll.n = clock.n;
7889 crtc_state->dpll.m1 = clock.m1;
7890 crtc_state->dpll.m2 = clock.m2;
7891 crtc_state->dpll.p1 = clock.p1;
7892 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007893 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007894
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007895 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007896 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007897 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007898 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007899 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007900 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007901 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007902 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007903 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007904 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007905 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007906
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007907 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007908}
7909
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007910static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007911 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007912{
7913 struct drm_device *dev = crtc->base.dev;
7914 struct drm_i915_private *dev_priv = dev->dev_private;
7915 uint32_t tmp;
7916
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007917 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7918 return;
7919
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007920 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007921 if (!(tmp & PFIT_ENABLE))
7922 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007923
Daniel Vetter06922822013-07-11 13:35:40 +02007924 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007925 if (INTEL_INFO(dev)->gen < 4) {
7926 if (crtc->pipe != PIPE_B)
7927 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007928 } else {
7929 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7930 return;
7931 }
7932
Daniel Vetter06922822013-07-11 13:35:40 +02007933 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007934 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7935 if (INTEL_INFO(dev)->gen < 5)
7936 pipe_config->gmch_pfit.lvds_border_bits =
7937 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7938}
7939
Jesse Barnesacbec812013-09-20 11:29:32 -07007940static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007941 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007942{
7943 struct drm_device *dev = crtc->base.dev;
7944 struct drm_i915_private *dev_priv = dev->dev_private;
7945 int pipe = pipe_config->cpu_transcoder;
7946 intel_clock_t clock;
7947 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007948 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007949
Shobhit Kumarf573de52014-07-30 20:32:37 +05307950 /* In case of MIPI DPLL will not even be used */
7951 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7952 return;
7953
Ville Syrjäläa5805162015-05-26 20:42:30 +03007954 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007955 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007956 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007957
7958 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7959 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7960 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7961 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7962 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7963
Imre Deakdccbea32015-06-22 23:35:51 +03007964 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007965}
7966
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007967static void
7968i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7969 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007970{
7971 struct drm_device *dev = crtc->base.dev;
7972 struct drm_i915_private *dev_priv = dev->dev_private;
7973 u32 val, base, offset;
7974 int pipe = crtc->pipe, plane = crtc->plane;
7975 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007976 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007977 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007978 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007979
Damien Lespiau42a7b082015-02-05 19:35:13 +00007980 val = I915_READ(DSPCNTR(plane));
7981 if (!(val & DISPLAY_PLANE_ENABLE))
7982 return;
7983
Damien Lespiaud9806c92015-01-21 14:07:19 +00007984 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007985 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007986 DRM_DEBUG_KMS("failed to alloc fb\n");
7987 return;
7988 }
7989
Damien Lespiau1b842c82015-01-21 13:50:54 +00007990 fb = &intel_fb->base;
7991
Daniel Vetter18c52472015-02-10 17:16:09 +00007992 if (INTEL_INFO(dev)->gen >= 4) {
7993 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007994 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007995 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7996 }
7997 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007998
7999 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008000 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008001 fb->pixel_format = fourcc;
8002 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008003
8004 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008005 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008006 offset = I915_READ(DSPTILEOFF(plane));
8007 else
8008 offset = I915_READ(DSPLINOFF(plane));
8009 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8010 } else {
8011 base = I915_READ(DSPADDR(plane));
8012 }
8013 plane_config->base = base;
8014
8015 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008016 fb->width = ((val >> 16) & 0xfff) + 1;
8017 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008018
8019 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008020 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008021
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008022 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008023 fb->pixel_format,
8024 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008025
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008026 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008027
Damien Lespiau2844a922015-01-20 12:51:48 +00008028 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8029 pipe_name(pipe), plane, fb->width, fb->height,
8030 fb->bits_per_pixel, base, fb->pitches[0],
8031 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008032
Damien Lespiau2d140302015-02-05 17:22:18 +00008033 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008034}
8035
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008036static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008037 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008038{
8039 struct drm_device *dev = crtc->base.dev;
8040 struct drm_i915_private *dev_priv = dev->dev_private;
8041 int pipe = pipe_config->cpu_transcoder;
8042 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8043 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008044 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008045 int refclk = 100000;
8046
Ville Syrjäläa5805162015-05-26 20:42:30 +03008047 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008048 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8049 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8050 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8051 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008052 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008053 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008054
8055 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008056 clock.m2 = (pll_dw0 & 0xff) << 22;
8057 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8058 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008059 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8060 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8061 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8062
Imre Deakdccbea32015-06-22 23:35:51 +03008063 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008064}
8065
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008066static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008067 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008068{
8069 struct drm_device *dev = crtc->base.dev;
8070 struct drm_i915_private *dev_priv = dev->dev_private;
8071 uint32_t tmp;
8072
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008073 if (!intel_display_power_is_enabled(dev_priv,
8074 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008075 return false;
8076
Daniel Vettere143a212013-07-04 12:01:15 +02008077 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008078 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008079
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008080 tmp = I915_READ(PIPECONF(crtc->pipe));
8081 if (!(tmp & PIPECONF_ENABLE))
8082 return false;
8083
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008084 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8085 switch (tmp & PIPECONF_BPC_MASK) {
8086 case PIPECONF_6BPC:
8087 pipe_config->pipe_bpp = 18;
8088 break;
8089 case PIPECONF_8BPC:
8090 pipe_config->pipe_bpp = 24;
8091 break;
8092 case PIPECONF_10BPC:
8093 pipe_config->pipe_bpp = 30;
8094 break;
8095 default:
8096 break;
8097 }
8098 }
8099
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008100 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8101 pipe_config->limited_color_range = true;
8102
Ville Syrjälä282740f2013-09-04 18:30:03 +03008103 if (INTEL_INFO(dev)->gen < 4)
8104 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8105
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008106 intel_get_pipe_timings(crtc, pipe_config);
8107
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008108 i9xx_get_pfit_config(crtc, pipe_config);
8109
Daniel Vetter6c49f242013-06-06 12:45:25 +02008110 if (INTEL_INFO(dev)->gen >= 4) {
8111 tmp = I915_READ(DPLL_MD(crtc->pipe));
8112 pipe_config->pixel_multiplier =
8113 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8114 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008115 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008116 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8117 tmp = I915_READ(DPLL(crtc->pipe));
8118 pipe_config->pixel_multiplier =
8119 ((tmp & SDVO_MULTIPLIER_MASK)
8120 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8121 } else {
8122 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8123 * port and will be fixed up in the encoder->get_config
8124 * function. */
8125 pipe_config->pixel_multiplier = 1;
8126 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008127 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8128 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008129 /*
8130 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8131 * on 830. Filter it out here so that we don't
8132 * report errors due to that.
8133 */
8134 if (IS_I830(dev))
8135 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8136
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008137 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8138 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008139 } else {
8140 /* Mask out read-only status bits. */
8141 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8142 DPLL_PORTC_READY_MASK |
8143 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008144 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008145
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008146 if (IS_CHERRYVIEW(dev))
8147 chv_crtc_clock_get(crtc, pipe_config);
8148 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008149 vlv_crtc_clock_get(crtc, pipe_config);
8150 else
8151 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008152
Ville Syrjälä0f646142015-08-26 19:39:18 +03008153 /*
8154 * Normally the dotclock is filled in by the encoder .get_config()
8155 * but in case the pipe is enabled w/o any ports we need a sane
8156 * default.
8157 */
8158 pipe_config->base.adjusted_mode.crtc_clock =
8159 pipe_config->port_clock / pipe_config->pixel_multiplier;
8160
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008161 return true;
8162}
8163
Paulo Zanonidde86e22012-12-01 12:04:25 -02008164static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008165{
8166 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008167 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008168 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008169 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008170 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008171 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008172 bool has_ck505 = false;
8173 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008174
8175 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008176 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008177 switch (encoder->type) {
8178 case INTEL_OUTPUT_LVDS:
8179 has_panel = true;
8180 has_lvds = true;
8181 break;
8182 case INTEL_OUTPUT_EDP:
8183 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008184 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008185 has_cpu_edp = true;
8186 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008187 default:
8188 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008189 }
8190 }
8191
Keith Packard99eb6a02011-09-26 14:29:12 -07008192 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008193 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008194 can_ssc = has_ck505;
8195 } else {
8196 has_ck505 = false;
8197 can_ssc = true;
8198 }
8199
Imre Deak2de69052013-05-08 13:14:04 +03008200 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8201 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008202
8203 /* Ironlake: try to setup display ref clock before DPLL
8204 * enabling. This is only under driver's control after
8205 * PCH B stepping, previous chipset stepping should be
8206 * ignoring this setting.
8207 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008208 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008209
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008210 /* As we must carefully and slowly disable/enable each source in turn,
8211 * compute the final state we want first and check if we need to
8212 * make any changes at all.
8213 */
8214 final = val;
8215 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008216 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008217 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008218 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008219 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8220
8221 final &= ~DREF_SSC_SOURCE_MASK;
8222 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8223 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008224
Keith Packard199e5d72011-09-22 12:01:57 -07008225 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008226 final |= DREF_SSC_SOURCE_ENABLE;
8227
8228 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8229 final |= DREF_SSC1_ENABLE;
8230
8231 if (has_cpu_edp) {
8232 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8233 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8234 else
8235 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8236 } else
8237 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8238 } else {
8239 final |= DREF_SSC_SOURCE_DISABLE;
8240 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8241 }
8242
8243 if (final == val)
8244 return;
8245
8246 /* Always enable nonspread source */
8247 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8248
8249 if (has_ck505)
8250 val |= DREF_NONSPREAD_CK505_ENABLE;
8251 else
8252 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8253
8254 if (has_panel) {
8255 val &= ~DREF_SSC_SOURCE_MASK;
8256 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008257
Keith Packard199e5d72011-09-22 12:01:57 -07008258 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008259 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008260 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008261 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008262 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008263 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008264
8265 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008266 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008267 POSTING_READ(PCH_DREF_CONTROL);
8268 udelay(200);
8269
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008270 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008271
8272 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008273 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008274 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008275 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008276 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008277 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008278 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008279 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008280 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008281
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008282 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008283 POSTING_READ(PCH_DREF_CONTROL);
8284 udelay(200);
8285 } else {
8286 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8287
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008288 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008289
8290 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008291 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008292
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008293 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008294 POSTING_READ(PCH_DREF_CONTROL);
8295 udelay(200);
8296
8297 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008298 val &= ~DREF_SSC_SOURCE_MASK;
8299 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008300
8301 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008302 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008303
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008304 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008305 POSTING_READ(PCH_DREF_CONTROL);
8306 udelay(200);
8307 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008308
8309 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008310}
8311
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008312static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008313{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008314 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008315
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008316 tmp = I915_READ(SOUTH_CHICKEN2);
8317 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8318 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008319
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008320 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8321 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8322 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008323
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008324 tmp = I915_READ(SOUTH_CHICKEN2);
8325 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8326 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008327
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008328 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8329 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8330 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008331}
8332
8333/* WaMPhyProgramming:hsw */
8334static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8335{
8336 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008337
8338 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8339 tmp &= ~(0xFF << 24);
8340 tmp |= (0x12 << 24);
8341 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8342
Paulo Zanonidde86e22012-12-01 12:04:25 -02008343 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8344 tmp |= (1 << 11);
8345 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8346
8347 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8348 tmp |= (1 << 11);
8349 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8350
Paulo Zanonidde86e22012-12-01 12:04:25 -02008351 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8352 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8353 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8354
8355 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8356 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8357 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8358
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008359 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8360 tmp &= ~(7 << 13);
8361 tmp |= (5 << 13);
8362 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008363
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008364 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8365 tmp &= ~(7 << 13);
8366 tmp |= (5 << 13);
8367 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008368
8369 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8370 tmp &= ~0xFF;
8371 tmp |= 0x1C;
8372 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8373
8374 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8375 tmp &= ~0xFF;
8376 tmp |= 0x1C;
8377 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8378
8379 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8380 tmp &= ~(0xFF << 16);
8381 tmp |= (0x1C << 16);
8382 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8383
8384 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8385 tmp &= ~(0xFF << 16);
8386 tmp |= (0x1C << 16);
8387 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8388
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008389 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8390 tmp |= (1 << 27);
8391 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008392
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008393 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8394 tmp |= (1 << 27);
8395 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008396
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008397 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8398 tmp &= ~(0xF << 28);
8399 tmp |= (4 << 28);
8400 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008401
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008402 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8403 tmp &= ~(0xF << 28);
8404 tmp |= (4 << 28);
8405 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008406}
8407
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008408/* Implements 3 different sequences from BSpec chapter "Display iCLK
8409 * Programming" based on the parameters passed:
8410 * - Sequence to enable CLKOUT_DP
8411 * - Sequence to enable CLKOUT_DP without spread
8412 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8413 */
8414static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8415 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008416{
8417 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008418 uint32_t reg, tmp;
8419
8420 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8421 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008422 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008423 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008424
Ville Syrjäläa5805162015-05-26 20:42:30 +03008425 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008426
8427 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8428 tmp &= ~SBI_SSCCTL_DISABLE;
8429 tmp |= SBI_SSCCTL_PATHALT;
8430 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8431
8432 udelay(24);
8433
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008434 if (with_spread) {
8435 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8436 tmp &= ~SBI_SSCCTL_PATHALT;
8437 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008438
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008439 if (with_fdi) {
8440 lpt_reset_fdi_mphy(dev_priv);
8441 lpt_program_fdi_mphy(dev_priv);
8442 }
8443 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008444
Ville Syrjäläc2699522015-08-27 23:55:59 +03008445 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008446 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8447 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8448 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008449
Ville Syrjäläa5805162015-05-26 20:42:30 +03008450 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008451}
8452
Paulo Zanoni47701c32013-07-23 11:19:25 -03008453/* Sequence to disable CLKOUT_DP */
8454static void lpt_disable_clkout_dp(struct drm_device *dev)
8455{
8456 struct drm_i915_private *dev_priv = dev->dev_private;
8457 uint32_t reg, tmp;
8458
Ville Syrjäläa5805162015-05-26 20:42:30 +03008459 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008460
Ville Syrjäläc2699522015-08-27 23:55:59 +03008461 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008462 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8463 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8464 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8465
8466 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8467 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8468 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8469 tmp |= SBI_SSCCTL_PATHALT;
8470 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8471 udelay(32);
8472 }
8473 tmp |= SBI_SSCCTL_DISABLE;
8474 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8475 }
8476
Ville Syrjäläa5805162015-05-26 20:42:30 +03008477 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008478}
8479
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008480static void lpt_init_pch_refclk(struct drm_device *dev)
8481{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008482 struct intel_encoder *encoder;
8483 bool has_vga = false;
8484
Damien Lespiaub2784e12014-08-05 11:29:37 +01008485 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008486 switch (encoder->type) {
8487 case INTEL_OUTPUT_ANALOG:
8488 has_vga = true;
8489 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008490 default:
8491 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008492 }
8493 }
8494
Paulo Zanoni47701c32013-07-23 11:19:25 -03008495 if (has_vga)
8496 lpt_enable_clkout_dp(dev, true, true);
8497 else
8498 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008499}
8500
Paulo Zanonidde86e22012-12-01 12:04:25 -02008501/*
8502 * Initialize reference clocks when the driver loads
8503 */
8504void intel_init_pch_refclk(struct drm_device *dev)
8505{
8506 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8507 ironlake_init_pch_refclk(dev);
8508 else if (HAS_PCH_LPT(dev))
8509 lpt_init_pch_refclk(dev);
8510}
8511
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008512static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008513{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008514 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008515 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008516 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008517 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008518 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008519 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008520 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008521 bool is_lvds = false;
8522
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008523 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008524 if (connector_state->crtc != crtc_state->base.crtc)
8525 continue;
8526
8527 encoder = to_intel_encoder(connector_state->best_encoder);
8528
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008529 switch (encoder->type) {
8530 case INTEL_OUTPUT_LVDS:
8531 is_lvds = true;
8532 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008533 default:
8534 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008535 }
8536 num_connectors++;
8537 }
8538
8539 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008540 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008541 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008542 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008543 }
8544
8545 return 120000;
8546}
8547
Daniel Vetter6ff93602013-04-19 11:24:36 +02008548static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008549{
8550 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8551 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8552 int pipe = intel_crtc->pipe;
8553 uint32_t val;
8554
Daniel Vetter78114072013-06-13 00:54:57 +02008555 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008556
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008557 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008558 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008559 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008560 break;
8561 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008562 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008563 break;
8564 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008565 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008566 break;
8567 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008568 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008569 break;
8570 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008571 /* Case prevented by intel_choose_pipe_bpp_dither. */
8572 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008573 }
8574
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008575 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008576 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8577
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008578 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008579 val |= PIPECONF_INTERLACED_ILK;
8580 else
8581 val |= PIPECONF_PROGRESSIVE;
8582
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008583 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008584 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008585
Paulo Zanonic8203562012-09-12 10:06:29 -03008586 I915_WRITE(PIPECONF(pipe), val);
8587 POSTING_READ(PIPECONF(pipe));
8588}
8589
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008590/*
8591 * Set up the pipe CSC unit.
8592 *
8593 * Currently only full range RGB to limited range RGB conversion
8594 * is supported, but eventually this should handle various
8595 * RGB<->YCbCr scenarios as well.
8596 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008597static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008598{
8599 struct drm_device *dev = crtc->dev;
8600 struct drm_i915_private *dev_priv = dev->dev_private;
8601 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8602 int pipe = intel_crtc->pipe;
8603 uint16_t coeff = 0x7800; /* 1.0 */
8604
8605 /*
8606 * TODO: Check what kind of values actually come out of the pipe
8607 * with these coeff/postoff values and adjust to get the best
8608 * accuracy. Perhaps we even need to take the bpc value into
8609 * consideration.
8610 */
8611
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008612 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008613 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8614
8615 /*
8616 * GY/GU and RY/RU should be the other way around according
8617 * to BSpec, but reality doesn't agree. Just set them up in
8618 * a way that results in the correct picture.
8619 */
8620 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8621 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8622
8623 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8624 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8625
8626 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8627 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8628
8629 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8630 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8631 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8632
8633 if (INTEL_INFO(dev)->gen > 6) {
8634 uint16_t postoff = 0;
8635
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008636 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008637 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008638
8639 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8640 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8641 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8642
8643 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8644 } else {
8645 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8646
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008647 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008648 mode |= CSC_BLACK_SCREEN_OFFSET;
8649
8650 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8651 }
8652}
8653
Daniel Vetter6ff93602013-04-19 11:24:36 +02008654static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008655{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008656 struct drm_device *dev = crtc->dev;
8657 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008658 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008659 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008660 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008661 uint32_t val;
8662
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008663 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008664
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008665 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008666 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8667
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008668 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008669 val |= PIPECONF_INTERLACED_ILK;
8670 else
8671 val |= PIPECONF_PROGRESSIVE;
8672
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008673 I915_WRITE(PIPECONF(cpu_transcoder), val);
8674 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008675
8676 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8677 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008678
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308679 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008680 val = 0;
8681
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008682 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008683 case 18:
8684 val |= PIPEMISC_DITHER_6_BPC;
8685 break;
8686 case 24:
8687 val |= PIPEMISC_DITHER_8_BPC;
8688 break;
8689 case 30:
8690 val |= PIPEMISC_DITHER_10_BPC;
8691 break;
8692 case 36:
8693 val |= PIPEMISC_DITHER_12_BPC;
8694 break;
8695 default:
8696 /* Case prevented by pipe_config_set_bpp. */
8697 BUG();
8698 }
8699
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008700 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008701 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8702
8703 I915_WRITE(PIPEMISC(pipe), val);
8704 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008705}
8706
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008707static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008708 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008709 intel_clock_t *clock,
8710 bool *has_reduced_clock,
8711 intel_clock_t *reduced_clock)
8712{
8713 struct drm_device *dev = crtc->dev;
8714 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008715 int refclk;
8716 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008717 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008718
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008719 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008720
8721 /*
8722 * Returns a set of divisors for the desired target clock with the given
8723 * refclk, or FALSE. The returned values represent the clock equation:
8724 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8725 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008726 limit = intel_limit(crtc_state, refclk);
8727 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008728 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008729 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008730 if (!ret)
8731 return false;
8732
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008733 return true;
8734}
8735
Paulo Zanonid4b19312012-11-29 11:29:32 -02008736int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8737{
8738 /*
8739 * Account for spread spectrum to avoid
8740 * oversubscribing the link. Max center spread
8741 * is 2.5%; use 5% for safety's sake.
8742 */
8743 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008744 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008745}
8746
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008747static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008748{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008749 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008750}
8751
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008752static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008753 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008754 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008755 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008756{
8757 struct drm_crtc *crtc = &intel_crtc->base;
8758 struct drm_device *dev = crtc->dev;
8759 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008760 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008761 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008762 struct drm_connector_state *connector_state;
8763 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008764 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008765 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008766 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008767
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008768 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008769 if (connector_state->crtc != crtc_state->base.crtc)
8770 continue;
8771
8772 encoder = to_intel_encoder(connector_state->best_encoder);
8773
8774 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008775 case INTEL_OUTPUT_LVDS:
8776 is_lvds = true;
8777 break;
8778 case INTEL_OUTPUT_SDVO:
8779 case INTEL_OUTPUT_HDMI:
8780 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008781 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008782 default:
8783 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008784 }
8785
8786 num_connectors++;
8787 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008788
Chris Wilsonc1858122010-12-03 21:35:48 +00008789 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008790 factor = 21;
8791 if (is_lvds) {
8792 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008793 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008794 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008795 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008796 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008797 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008798
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008799 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008800 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008801
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008802 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8803 *fp2 |= FP_CB_TUNE;
8804
Chris Wilson5eddb702010-09-11 13:48:45 +01008805 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008806
Eric Anholta07d6782011-03-30 13:01:08 -07008807 if (is_lvds)
8808 dpll |= DPLLB_MODE_LVDS;
8809 else
8810 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008811
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008812 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008813 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008814
8815 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008816 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008817 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008818 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008819
Eric Anholta07d6782011-03-30 13:01:08 -07008820 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008821 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008822 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008823 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008824
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008825 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008826 case 5:
8827 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8828 break;
8829 case 7:
8830 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8831 break;
8832 case 10:
8833 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8834 break;
8835 case 14:
8836 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8837 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008838 }
8839
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008840 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008841 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008842 else
8843 dpll |= PLL_REF_INPUT_DREFCLK;
8844
Daniel Vetter959e16d2013-06-05 13:34:21 +02008845 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008846}
8847
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008848static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8849 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008850{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008851 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008852 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008853 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008854 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008855 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008856 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008857
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008858 memset(&crtc_state->dpll_hw_state, 0,
8859 sizeof(crtc_state->dpll_hw_state));
8860
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008861 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008862
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008863 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8864 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8865
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008866 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008867 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008868 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008869 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8870 return -EINVAL;
8871 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008872 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008873 if (!crtc_state->clock_set) {
8874 crtc_state->dpll.n = clock.n;
8875 crtc_state->dpll.m1 = clock.m1;
8876 crtc_state->dpll.m2 = clock.m2;
8877 crtc_state->dpll.p1 = clock.p1;
8878 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008879 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008880
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008881 /* 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 +02008882 if (crtc_state->has_pch_encoder) {
8883 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008884 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008885 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008886
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008887 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008888 &fp, &reduced_clock,
8889 has_reduced_clock ? &fp2 : NULL);
8890
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008891 crtc_state->dpll_hw_state.dpll = dpll;
8892 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008893 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008894 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008895 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008896 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008897
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008898 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008899 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008900 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008901 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008902 return -EINVAL;
8903 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008904 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008905
Rodrigo Viviab585de2015-03-24 12:40:09 -07008906 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008907 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008908 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008909 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008910
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008911 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008912}
8913
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008914static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8915 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008916{
8917 struct drm_device *dev = crtc->base.dev;
8918 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008919 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008920
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008921 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8922 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8923 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8924 & ~TU_SIZE_MASK;
8925 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8926 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8927 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8928}
8929
8930static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8931 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008932 struct intel_link_m_n *m_n,
8933 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008934{
8935 struct drm_device *dev = crtc->base.dev;
8936 struct drm_i915_private *dev_priv = dev->dev_private;
8937 enum pipe pipe = crtc->pipe;
8938
8939 if (INTEL_INFO(dev)->gen >= 5) {
8940 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8941 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8942 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8943 & ~TU_SIZE_MASK;
8944 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8945 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8946 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008947 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8948 * gen < 8) and if DRRS is supported (to make sure the
8949 * registers are not unnecessarily read).
8950 */
8951 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008952 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008953 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8954 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8955 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8956 & ~TU_SIZE_MASK;
8957 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8958 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8959 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8960 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008961 } else {
8962 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8963 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8964 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8965 & ~TU_SIZE_MASK;
8966 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8967 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8968 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8969 }
8970}
8971
8972void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008973 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008974{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008975 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008976 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8977 else
8978 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008979 &pipe_config->dp_m_n,
8980 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008981}
8982
Daniel Vetter72419202013-04-04 13:28:53 +02008983static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008984 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008985{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008986 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008987 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008988}
8989
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008990static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008991 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008992{
8993 struct drm_device *dev = crtc->base.dev;
8994 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008995 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8996 uint32_t ps_ctrl = 0;
8997 int id = -1;
8998 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008999
Chandra Kondurua1b22782015-04-07 15:28:45 -07009000 /* find scaler attached to this pipe */
9001 for (i = 0; i < crtc->num_scalers; i++) {
9002 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9003 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9004 id = i;
9005 pipe_config->pch_pfit.enabled = true;
9006 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9007 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9008 break;
9009 }
9010 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009011
Chandra Kondurua1b22782015-04-07 15:28:45 -07009012 scaler_state->scaler_id = id;
9013 if (id >= 0) {
9014 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9015 } else {
9016 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009017 }
9018}
9019
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009020static void
9021skylake_get_initial_plane_config(struct intel_crtc *crtc,
9022 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009023{
9024 struct drm_device *dev = crtc->base.dev;
9025 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009026 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009027 int pipe = crtc->pipe;
9028 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009029 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009030 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009031 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009032
Damien Lespiaud9806c92015-01-21 14:07:19 +00009033 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009034 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009035 DRM_DEBUG_KMS("failed to alloc fb\n");
9036 return;
9037 }
9038
Damien Lespiau1b842c82015-01-21 13:50:54 +00009039 fb = &intel_fb->base;
9040
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009041 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009042 if (!(val & PLANE_CTL_ENABLE))
9043 goto error;
9044
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009045 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9046 fourcc = skl_format_to_fourcc(pixel_format,
9047 val & PLANE_CTL_ORDER_RGBX,
9048 val & PLANE_CTL_ALPHA_MASK);
9049 fb->pixel_format = fourcc;
9050 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9051
Damien Lespiau40f46282015-02-27 11:15:21 +00009052 tiling = val & PLANE_CTL_TILED_MASK;
9053 switch (tiling) {
9054 case PLANE_CTL_TILED_LINEAR:
9055 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9056 break;
9057 case PLANE_CTL_TILED_X:
9058 plane_config->tiling = I915_TILING_X;
9059 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9060 break;
9061 case PLANE_CTL_TILED_Y:
9062 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9063 break;
9064 case PLANE_CTL_TILED_YF:
9065 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9066 break;
9067 default:
9068 MISSING_CASE(tiling);
9069 goto error;
9070 }
9071
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009072 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9073 plane_config->base = base;
9074
9075 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9076
9077 val = I915_READ(PLANE_SIZE(pipe, 0));
9078 fb->height = ((val >> 16) & 0xfff) + 1;
9079 fb->width = ((val >> 0) & 0x1fff) + 1;
9080
9081 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009082 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9083 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009084 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9085
9086 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009087 fb->pixel_format,
9088 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009089
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009090 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009091
9092 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9093 pipe_name(pipe), fb->width, fb->height,
9094 fb->bits_per_pixel, base, fb->pitches[0],
9095 plane_config->size);
9096
Damien Lespiau2d140302015-02-05 17:22:18 +00009097 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009098 return;
9099
9100error:
9101 kfree(fb);
9102}
9103
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009104static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009105 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009106{
9107 struct drm_device *dev = crtc->base.dev;
9108 struct drm_i915_private *dev_priv = dev->dev_private;
9109 uint32_t tmp;
9110
9111 tmp = I915_READ(PF_CTL(crtc->pipe));
9112
9113 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009114 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009115 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9116 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009117
9118 /* We currently do not free assignements of panel fitters on
9119 * ivb/hsw (since we don't use the higher upscaling modes which
9120 * differentiates them) so just WARN about this case for now. */
9121 if (IS_GEN7(dev)) {
9122 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9123 PF_PIPE_SEL_IVB(crtc->pipe));
9124 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009125 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009126}
9127
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009128static void
9129ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9130 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009131{
9132 struct drm_device *dev = crtc->base.dev;
9133 struct drm_i915_private *dev_priv = dev->dev_private;
9134 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009135 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009136 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009137 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009138 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009139 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009140
Damien Lespiau42a7b082015-02-05 19:35:13 +00009141 val = I915_READ(DSPCNTR(pipe));
9142 if (!(val & DISPLAY_PLANE_ENABLE))
9143 return;
9144
Damien Lespiaud9806c92015-01-21 14:07:19 +00009145 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009146 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009147 DRM_DEBUG_KMS("failed to alloc fb\n");
9148 return;
9149 }
9150
Damien Lespiau1b842c82015-01-21 13:50:54 +00009151 fb = &intel_fb->base;
9152
Daniel Vetter18c52472015-02-10 17:16:09 +00009153 if (INTEL_INFO(dev)->gen >= 4) {
9154 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009155 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009156 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9157 }
9158 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009159
9160 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009161 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009162 fb->pixel_format = fourcc;
9163 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009164
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009165 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009166 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009167 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009168 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009169 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009170 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009171 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009172 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009173 }
9174 plane_config->base = base;
9175
9176 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009177 fb->width = ((val >> 16) & 0xfff) + 1;
9178 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009179
9180 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009181 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009182
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009183 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009184 fb->pixel_format,
9185 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009186
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009187 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009188
Damien Lespiau2844a922015-01-20 12:51:48 +00009189 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9190 pipe_name(pipe), fb->width, fb->height,
9191 fb->bits_per_pixel, base, fb->pitches[0],
9192 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009193
Damien Lespiau2d140302015-02-05 17:22:18 +00009194 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009195}
9196
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009197static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009198 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009199{
9200 struct drm_device *dev = crtc->base.dev;
9201 struct drm_i915_private *dev_priv = dev->dev_private;
9202 uint32_t tmp;
9203
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009204 if (!intel_display_power_is_enabled(dev_priv,
9205 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009206 return false;
9207
Daniel Vettere143a212013-07-04 12:01:15 +02009208 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009209 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009210
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009211 tmp = I915_READ(PIPECONF(crtc->pipe));
9212 if (!(tmp & PIPECONF_ENABLE))
9213 return false;
9214
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009215 switch (tmp & PIPECONF_BPC_MASK) {
9216 case PIPECONF_6BPC:
9217 pipe_config->pipe_bpp = 18;
9218 break;
9219 case PIPECONF_8BPC:
9220 pipe_config->pipe_bpp = 24;
9221 break;
9222 case PIPECONF_10BPC:
9223 pipe_config->pipe_bpp = 30;
9224 break;
9225 case PIPECONF_12BPC:
9226 pipe_config->pipe_bpp = 36;
9227 break;
9228 default:
9229 break;
9230 }
9231
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009232 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9233 pipe_config->limited_color_range = true;
9234
Daniel Vetterab9412b2013-05-03 11:49:46 +02009235 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009236 struct intel_shared_dpll *pll;
9237
Daniel Vetter88adfff2013-03-28 10:42:01 +01009238 pipe_config->has_pch_encoder = true;
9239
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009240 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9241 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9242 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009243
9244 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009245
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009246 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009247 pipe_config->shared_dpll =
9248 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009249 } else {
9250 tmp = I915_READ(PCH_DPLL_SEL);
9251 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9252 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9253 else
9254 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9255 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009256
9257 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9258
9259 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9260 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009261
9262 tmp = pipe_config->dpll_hw_state.dpll;
9263 pipe_config->pixel_multiplier =
9264 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9265 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009266
9267 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009268 } else {
9269 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009270 }
9271
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009272 intel_get_pipe_timings(crtc, pipe_config);
9273
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009274 ironlake_get_pfit_config(crtc, pipe_config);
9275
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009276 return true;
9277}
9278
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009279static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9280{
9281 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009282 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009283
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009284 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009285 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009286 pipe_name(crtc->pipe));
9287
Rob Clarke2c719b2014-12-15 13:56:32 -05009288 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9289 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9290 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9291 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9292 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9293 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009294 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009295 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009296 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009297 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009298 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009299 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009300 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009301 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009302 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009303
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009304 /*
9305 * In theory we can still leave IRQs enabled, as long as only the HPD
9306 * interrupts remain enabled. We used to check for that, but since it's
9307 * gen-specific and since we only disable LCPLL after we fully disable
9308 * the interrupts, the check below should be enough.
9309 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009310 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009311}
9312
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009313static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9314{
9315 struct drm_device *dev = dev_priv->dev;
9316
9317 if (IS_HASWELL(dev))
9318 return I915_READ(D_COMP_HSW);
9319 else
9320 return I915_READ(D_COMP_BDW);
9321}
9322
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009323static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9324{
9325 struct drm_device *dev = dev_priv->dev;
9326
9327 if (IS_HASWELL(dev)) {
9328 mutex_lock(&dev_priv->rps.hw_lock);
9329 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9330 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009331 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009332 mutex_unlock(&dev_priv->rps.hw_lock);
9333 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009334 I915_WRITE(D_COMP_BDW, val);
9335 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009336 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009337}
9338
9339/*
9340 * This function implements pieces of two sequences from BSpec:
9341 * - Sequence for display software to disable LCPLL
9342 * - Sequence for display software to allow package C8+
9343 * The steps implemented here are just the steps that actually touch the LCPLL
9344 * register. Callers should take care of disabling all the display engine
9345 * functions, doing the mode unset, fixing interrupts, etc.
9346 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009347static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9348 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009349{
9350 uint32_t val;
9351
9352 assert_can_disable_lcpll(dev_priv);
9353
9354 val = I915_READ(LCPLL_CTL);
9355
9356 if (switch_to_fclk) {
9357 val |= LCPLL_CD_SOURCE_FCLK;
9358 I915_WRITE(LCPLL_CTL, val);
9359
9360 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9361 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9362 DRM_ERROR("Switching to FCLK failed\n");
9363
9364 val = I915_READ(LCPLL_CTL);
9365 }
9366
9367 val |= LCPLL_PLL_DISABLE;
9368 I915_WRITE(LCPLL_CTL, val);
9369 POSTING_READ(LCPLL_CTL);
9370
9371 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9372 DRM_ERROR("LCPLL still locked\n");
9373
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009374 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009375 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009376 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009377 ndelay(100);
9378
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009379 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9380 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009381 DRM_ERROR("D_COMP RCOMP still in progress\n");
9382
9383 if (allow_power_down) {
9384 val = I915_READ(LCPLL_CTL);
9385 val |= LCPLL_POWER_DOWN_ALLOW;
9386 I915_WRITE(LCPLL_CTL, val);
9387 POSTING_READ(LCPLL_CTL);
9388 }
9389}
9390
9391/*
9392 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9393 * source.
9394 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009395static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009396{
9397 uint32_t val;
9398
9399 val = I915_READ(LCPLL_CTL);
9400
9401 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9402 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9403 return;
9404
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009405 /*
9406 * Make sure we're not on PC8 state before disabling PC8, otherwise
9407 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009408 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009409 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009410
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009411 if (val & LCPLL_POWER_DOWN_ALLOW) {
9412 val &= ~LCPLL_POWER_DOWN_ALLOW;
9413 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009414 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009415 }
9416
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009417 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009418 val |= D_COMP_COMP_FORCE;
9419 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009420 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009421
9422 val = I915_READ(LCPLL_CTL);
9423 val &= ~LCPLL_PLL_DISABLE;
9424 I915_WRITE(LCPLL_CTL, val);
9425
9426 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9427 DRM_ERROR("LCPLL not locked yet\n");
9428
9429 if (val & LCPLL_CD_SOURCE_FCLK) {
9430 val = I915_READ(LCPLL_CTL);
9431 val &= ~LCPLL_CD_SOURCE_FCLK;
9432 I915_WRITE(LCPLL_CTL, val);
9433
9434 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9435 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9436 DRM_ERROR("Switching back to LCPLL failed\n");
9437 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009438
Mika Kuoppala59bad942015-01-16 11:34:40 +02009439 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009440 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009441}
9442
Paulo Zanoni765dab672014-03-07 20:08:18 -03009443/*
9444 * Package states C8 and deeper are really deep PC states that can only be
9445 * reached when all the devices on the system allow it, so even if the graphics
9446 * device allows PC8+, it doesn't mean the system will actually get to these
9447 * states. Our driver only allows PC8+ when going into runtime PM.
9448 *
9449 * The requirements for PC8+ are that all the outputs are disabled, the power
9450 * well is disabled and most interrupts are disabled, and these are also
9451 * requirements for runtime PM. When these conditions are met, we manually do
9452 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9453 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9454 * hang the machine.
9455 *
9456 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9457 * the state of some registers, so when we come back from PC8+ we need to
9458 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9459 * need to take care of the registers kept by RC6. Notice that this happens even
9460 * if we don't put the device in PCI D3 state (which is what currently happens
9461 * because of the runtime PM support).
9462 *
9463 * For more, read "Display Sequences for Package C8" on the hardware
9464 * documentation.
9465 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009466void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009467{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009468 struct drm_device *dev = dev_priv->dev;
9469 uint32_t val;
9470
Paulo Zanonic67a4702013-08-19 13:18:09 -03009471 DRM_DEBUG_KMS("Enabling package C8+\n");
9472
Ville Syrjäläc2699522015-08-27 23:55:59 +03009473 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009474 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9475 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9476 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9477 }
9478
9479 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009480 hsw_disable_lcpll(dev_priv, true, true);
9481}
9482
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009483void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009484{
9485 struct drm_device *dev = dev_priv->dev;
9486 uint32_t val;
9487
Paulo Zanonic67a4702013-08-19 13:18:09 -03009488 DRM_DEBUG_KMS("Disabling package C8+\n");
9489
9490 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009491 lpt_init_pch_refclk(dev);
9492
Ville Syrjäläc2699522015-08-27 23:55:59 +03009493 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009494 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9495 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9496 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9497 }
9498
9499 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009500}
9501
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009502static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309503{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009504 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009505 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309506
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009507 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309508}
9509
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009510/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009511static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009512{
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009513 struct intel_crtc *intel_crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009514 struct intel_crtc_state *crtc_state;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009515 int max_pixel_rate = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009516
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009517 for_each_intel_crtc(state->dev, intel_crtc) {
9518 int pixel_rate;
9519
9520 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9521 if (IS_ERR(crtc_state))
9522 return PTR_ERR(crtc_state);
9523
9524 if (!crtc_state->base.enable)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009525 continue;
9526
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009527 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009528
9529 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009530 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009531 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9532
9533 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9534 }
9535
9536 return max_pixel_rate;
9537}
9538
9539static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9540{
9541 struct drm_i915_private *dev_priv = dev->dev_private;
9542 uint32_t val, data;
9543 int ret;
9544
9545 if (WARN((I915_READ(LCPLL_CTL) &
9546 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9547 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9548 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9549 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9550 "trying to change cdclk frequency with cdclk not enabled\n"))
9551 return;
9552
9553 mutex_lock(&dev_priv->rps.hw_lock);
9554 ret = sandybridge_pcode_write(dev_priv,
9555 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9556 mutex_unlock(&dev_priv->rps.hw_lock);
9557 if (ret) {
9558 DRM_ERROR("failed to inform pcode about cdclk change\n");
9559 return;
9560 }
9561
9562 val = I915_READ(LCPLL_CTL);
9563 val |= LCPLL_CD_SOURCE_FCLK;
9564 I915_WRITE(LCPLL_CTL, val);
9565
9566 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9567 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9568 DRM_ERROR("Switching to FCLK failed\n");
9569
9570 val = I915_READ(LCPLL_CTL);
9571 val &= ~LCPLL_CLK_FREQ_MASK;
9572
9573 switch (cdclk) {
9574 case 450000:
9575 val |= LCPLL_CLK_FREQ_450;
9576 data = 0;
9577 break;
9578 case 540000:
9579 val |= LCPLL_CLK_FREQ_54O_BDW;
9580 data = 1;
9581 break;
9582 case 337500:
9583 val |= LCPLL_CLK_FREQ_337_5_BDW;
9584 data = 2;
9585 break;
9586 case 675000:
9587 val |= LCPLL_CLK_FREQ_675_BDW;
9588 data = 3;
9589 break;
9590 default:
9591 WARN(1, "invalid cdclk frequency\n");
9592 return;
9593 }
9594
9595 I915_WRITE(LCPLL_CTL, val);
9596
9597 val = I915_READ(LCPLL_CTL);
9598 val &= ~LCPLL_CD_SOURCE_FCLK;
9599 I915_WRITE(LCPLL_CTL, val);
9600
9601 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9602 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9603 DRM_ERROR("Switching back to LCPLL failed\n");
9604
9605 mutex_lock(&dev_priv->rps.hw_lock);
9606 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9607 mutex_unlock(&dev_priv->rps.hw_lock);
9608
9609 intel_update_cdclk(dev);
9610
9611 WARN(cdclk != dev_priv->cdclk_freq,
9612 "cdclk requested %d kHz but got %d kHz\n",
9613 cdclk, dev_priv->cdclk_freq);
9614}
9615
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009616static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009617{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009618 struct drm_i915_private *dev_priv = to_i915(state->dev);
9619 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009620 int cdclk;
9621
9622 /*
9623 * FIXME should also account for plane ratio
9624 * once 64bpp pixel formats are supported.
9625 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009626 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009627 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009628 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009629 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009630 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009631 cdclk = 450000;
9632 else
9633 cdclk = 337500;
9634
9635 /*
9636 * FIXME move the cdclk caclulation to
9637 * compute_config() so we can fail gracegully.
9638 */
9639 if (cdclk > dev_priv->max_cdclk_freq) {
9640 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9641 cdclk, dev_priv->max_cdclk_freq);
9642 cdclk = dev_priv->max_cdclk_freq;
9643 }
9644
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009645 to_intel_atomic_state(state)->cdclk = cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009646
9647 return 0;
9648}
9649
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009650static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009651{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009652 struct drm_device *dev = old_state->dev;
9653 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009654
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009655 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009656}
9657
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009658static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9659 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009660{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009661 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009662 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009663
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009664 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009665
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009666 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009667}
9668
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309669static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9670 enum port port,
9671 struct intel_crtc_state *pipe_config)
9672{
9673 switch (port) {
9674 case PORT_A:
9675 pipe_config->ddi_pll_sel = SKL_DPLL0;
9676 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9677 break;
9678 case PORT_B:
9679 pipe_config->ddi_pll_sel = SKL_DPLL1;
9680 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9681 break;
9682 case PORT_C:
9683 pipe_config->ddi_pll_sel = SKL_DPLL2;
9684 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9685 break;
9686 default:
9687 DRM_ERROR("Incorrect port type\n");
9688 }
9689}
9690
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009691static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9692 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009693 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009694{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009695 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009696
9697 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9698 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9699
9700 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009701 case SKL_DPLL0:
9702 /*
9703 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9704 * of the shared DPLL framework and thus needs to be read out
9705 * separately
9706 */
9707 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9708 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9709 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009710 case SKL_DPLL1:
9711 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9712 break;
9713 case SKL_DPLL2:
9714 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9715 break;
9716 case SKL_DPLL3:
9717 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9718 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009719 }
9720}
9721
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009722static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9723 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009724 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009725{
9726 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9727
9728 switch (pipe_config->ddi_pll_sel) {
9729 case PORT_CLK_SEL_WRPLL1:
9730 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9731 break;
9732 case PORT_CLK_SEL_WRPLL2:
9733 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9734 break;
9735 }
9736}
9737
Daniel Vetter26804af2014-06-25 22:01:55 +03009738static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009739 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009740{
9741 struct drm_device *dev = crtc->base.dev;
9742 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009743 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009744 enum port port;
9745 uint32_t tmp;
9746
9747 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9748
9749 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9750
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009751 if (IS_SKYLAKE(dev))
9752 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309753 else if (IS_BROXTON(dev))
9754 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009755 else
9756 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009757
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009758 if (pipe_config->shared_dpll >= 0) {
9759 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9760
9761 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9762 &pipe_config->dpll_hw_state));
9763 }
9764
Daniel Vetter26804af2014-06-25 22:01:55 +03009765 /*
9766 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9767 * DDI E. So just check whether this pipe is wired to DDI E and whether
9768 * the PCH transcoder is on.
9769 */
Damien Lespiauca370452013-12-03 13:56:24 +00009770 if (INTEL_INFO(dev)->gen < 9 &&
9771 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009772 pipe_config->has_pch_encoder = true;
9773
9774 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9775 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9776 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9777
9778 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9779 }
9780}
9781
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009782static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009783 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009784{
9785 struct drm_device *dev = crtc->base.dev;
9786 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009787 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009788 uint32_t tmp;
9789
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009790 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009791 POWER_DOMAIN_PIPE(crtc->pipe)))
9792 return false;
9793
Daniel Vettere143a212013-07-04 12:01:15 +02009794 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009795 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9796
Daniel Vettereccb1402013-05-22 00:50:22 +02009797 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9798 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9799 enum pipe trans_edp_pipe;
9800 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9801 default:
9802 WARN(1, "unknown pipe linked to edp transcoder\n");
9803 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9804 case TRANS_DDI_EDP_INPUT_A_ON:
9805 trans_edp_pipe = PIPE_A;
9806 break;
9807 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9808 trans_edp_pipe = PIPE_B;
9809 break;
9810 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9811 trans_edp_pipe = PIPE_C;
9812 break;
9813 }
9814
9815 if (trans_edp_pipe == crtc->pipe)
9816 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9817 }
9818
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009819 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009820 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009821 return false;
9822
Daniel Vettereccb1402013-05-22 00:50:22 +02009823 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009824 if (!(tmp & PIPECONF_ENABLE))
9825 return false;
9826
Daniel Vetter26804af2014-06-25 22:01:55 +03009827 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009828
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009829 intel_get_pipe_timings(crtc, pipe_config);
9830
Chandra Kondurua1b22782015-04-07 15:28:45 -07009831 if (INTEL_INFO(dev)->gen >= 9) {
9832 skl_init_scalers(dev, crtc, pipe_config);
9833 }
9834
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009835 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ceda2015-05-11 14:35:47 -07009836
9837 if (INTEL_INFO(dev)->gen >= 9) {
9838 pipe_config->scaler_state.scaler_id = -1;
9839 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9840 }
9841
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009842 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009843 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009844 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009845 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009846 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009847 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009848
Jesse Barnese59150d2014-01-07 13:30:45 -08009849 if (IS_HASWELL(dev))
9850 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9851 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009852
Clint Taylorebb69c92014-09-30 10:30:22 -07009853 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9854 pipe_config->pixel_multiplier =
9855 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9856 } else {
9857 pipe_config->pixel_multiplier = 1;
9858 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009859
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009860 return true;
9861}
9862
Chris Wilson560b85b2010-08-07 11:01:38 +01009863static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9864{
9865 struct drm_device *dev = crtc->dev;
9866 struct drm_i915_private *dev_priv = dev->dev_private;
9867 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009868 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009869
Ville Syrjälädc41c152014-08-13 11:57:05 +03009870 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009871 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9872 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009873 unsigned int stride = roundup_pow_of_two(width) * 4;
9874
9875 switch (stride) {
9876 default:
9877 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9878 width, stride);
9879 stride = 256;
9880 /* fallthrough */
9881 case 256:
9882 case 512:
9883 case 1024:
9884 case 2048:
9885 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009886 }
9887
Ville Syrjälädc41c152014-08-13 11:57:05 +03009888 cntl |= CURSOR_ENABLE |
9889 CURSOR_GAMMA_ENABLE |
9890 CURSOR_FORMAT_ARGB |
9891 CURSOR_STRIDE(stride);
9892
9893 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009894 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009895
Ville Syrjälädc41c152014-08-13 11:57:05 +03009896 if (intel_crtc->cursor_cntl != 0 &&
9897 (intel_crtc->cursor_base != base ||
9898 intel_crtc->cursor_size != size ||
9899 intel_crtc->cursor_cntl != cntl)) {
9900 /* On these chipsets we can only modify the base/size/stride
9901 * whilst the cursor is disabled.
9902 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009903 I915_WRITE(CURCNTR(PIPE_A), 0);
9904 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +03009905 intel_crtc->cursor_cntl = 0;
9906 }
9907
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009908 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009909 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009910 intel_crtc->cursor_base = base;
9911 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009912
9913 if (intel_crtc->cursor_size != size) {
9914 I915_WRITE(CURSIZE, size);
9915 intel_crtc->cursor_size = size;
9916 }
9917
Chris Wilson4b0e3332014-05-30 16:35:26 +03009918 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009919 I915_WRITE(CURCNTR(PIPE_A), cntl);
9920 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +03009921 intel_crtc->cursor_cntl = cntl;
9922 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009923}
9924
9925static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9926{
9927 struct drm_device *dev = crtc->dev;
9928 struct drm_i915_private *dev_priv = dev->dev_private;
9929 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9930 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009931 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009932
Chris Wilson4b0e3332014-05-30 16:35:26 +03009933 cntl = 0;
9934 if (base) {
9935 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009936 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309937 case 64:
9938 cntl |= CURSOR_MODE_64_ARGB_AX;
9939 break;
9940 case 128:
9941 cntl |= CURSOR_MODE_128_ARGB_AX;
9942 break;
9943 case 256:
9944 cntl |= CURSOR_MODE_256_ARGB_AX;
9945 break;
9946 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009947 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309948 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009949 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009950 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009951
9952 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9953 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009954 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009955
Matt Roper8e7d6882015-01-21 16:35:41 -08009956 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009957 cntl |= CURSOR_ROTATE_180;
9958
Chris Wilson4b0e3332014-05-30 16:35:26 +03009959 if (intel_crtc->cursor_cntl != cntl) {
9960 I915_WRITE(CURCNTR(pipe), cntl);
9961 POSTING_READ(CURCNTR(pipe));
9962 intel_crtc->cursor_cntl = cntl;
9963 }
9964
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009965 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009966 I915_WRITE(CURBASE(pipe), base);
9967 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009968
9969 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009970}
9971
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009972/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009973static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9974 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009975{
9976 struct drm_device *dev = crtc->dev;
9977 struct drm_i915_private *dev_priv = dev->dev_private;
9978 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9979 int pipe = intel_crtc->pipe;
Maarten Lankhorst9b4101b2015-09-10 16:07:59 +02009980 struct drm_plane_state *cursor_state = crtc->cursor->state;
9981 int x = cursor_state->crtc_x;
9982 int y = cursor_state->crtc_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009983 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009984
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009985 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009986 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009987
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009988 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009989 base = 0;
9990
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009991 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009992 base = 0;
9993
9994 if (x < 0) {
Maarten Lankhorst9b4101b2015-09-10 16:07:59 +02009995 if (x + cursor_state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009996 base = 0;
9997
9998 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9999 x = -x;
10000 }
10001 pos |= x << CURSOR_X_SHIFT;
10002
10003 if (y < 0) {
Maarten Lankhorst9b4101b2015-09-10 16:07:59 +020010004 if (y + cursor_state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010005 base = 0;
10006
10007 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10008 y = -y;
10009 }
10010 pos |= y << CURSOR_Y_SHIFT;
10011
Chris Wilson4b0e3332014-05-30 16:35:26 +030010012 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010013 return;
10014
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010015 I915_WRITE(CURPOS(pipe), pos);
10016
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010017 /* ILK+ do this automagically */
10018 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -080010019 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Maarten Lankhorst9b4101b2015-09-10 16:07:59 +020010020 base += (cursor_state->crtc_h *
10021 cursor_state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010022 }
10023
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010024 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010025 i845_update_cursor(crtc, base);
10026 else
10027 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010028}
10029
Ville Syrjälädc41c152014-08-13 11:57:05 +030010030static bool cursor_size_ok(struct drm_device *dev,
10031 uint32_t width, uint32_t height)
10032{
10033 if (width == 0 || height == 0)
10034 return false;
10035
10036 /*
10037 * 845g/865g are special in that they are only limited by
10038 * the width of their cursors, the height is arbitrary up to
10039 * the precision of the register. Everything else requires
10040 * square cursors, limited to a few power-of-two sizes.
10041 */
10042 if (IS_845G(dev) || IS_I865G(dev)) {
10043 if ((width & 63) != 0)
10044 return false;
10045
10046 if (width > (IS_845G(dev) ? 64 : 512))
10047 return false;
10048
10049 if (height > 1023)
10050 return false;
10051 } else {
10052 switch (width | height) {
10053 case 256:
10054 case 128:
10055 if (IS_GEN2(dev))
10056 return false;
10057 case 64:
10058 break;
10059 default:
10060 return false;
10061 }
10062 }
10063
10064 return true;
10065}
10066
Jesse Barnes79e53942008-11-07 14:24:08 -080010067static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010068 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010069{
James Simmons72034252010-08-03 01:33:19 +010010070 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010071 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010072
James Simmons72034252010-08-03 01:33:19 +010010073 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010074 intel_crtc->lut_r[i] = red[i] >> 8;
10075 intel_crtc->lut_g[i] = green[i] >> 8;
10076 intel_crtc->lut_b[i] = blue[i] >> 8;
10077 }
10078
10079 intel_crtc_load_lut(crtc);
10080}
10081
Jesse Barnes79e53942008-11-07 14:24:08 -080010082/* VESA 640x480x72Hz mode to set on the pipe */
10083static struct drm_display_mode load_detect_mode = {
10084 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10085 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10086};
10087
Daniel Vettera8bb6812014-02-10 18:00:39 +010010088struct drm_framebuffer *
10089__intel_framebuffer_create(struct drm_device *dev,
10090 struct drm_mode_fb_cmd2 *mode_cmd,
10091 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010092{
10093 struct intel_framebuffer *intel_fb;
10094 int ret;
10095
10096 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10097 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010098 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010099 return ERR_PTR(-ENOMEM);
10100 }
10101
10102 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010103 if (ret)
10104 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010105
10106 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010107err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010108 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010109 kfree(intel_fb);
10110
10111 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010112}
10113
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010114static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010115intel_framebuffer_create(struct drm_device *dev,
10116 struct drm_mode_fb_cmd2 *mode_cmd,
10117 struct drm_i915_gem_object *obj)
10118{
10119 struct drm_framebuffer *fb;
10120 int ret;
10121
10122 ret = i915_mutex_lock_interruptible(dev);
10123 if (ret)
10124 return ERR_PTR(ret);
10125 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10126 mutex_unlock(&dev->struct_mutex);
10127
10128 return fb;
10129}
10130
Chris Wilsond2dff872011-04-19 08:36:26 +010010131static u32
10132intel_framebuffer_pitch_for_width(int width, int bpp)
10133{
10134 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10135 return ALIGN(pitch, 64);
10136}
10137
10138static u32
10139intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10140{
10141 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010142 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010143}
10144
10145static struct drm_framebuffer *
10146intel_framebuffer_create_for_mode(struct drm_device *dev,
10147 struct drm_display_mode *mode,
10148 int depth, int bpp)
10149{
10150 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010151 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010152
10153 obj = i915_gem_alloc_object(dev,
10154 intel_framebuffer_size_for_mode(mode, bpp));
10155 if (obj == NULL)
10156 return ERR_PTR(-ENOMEM);
10157
10158 mode_cmd.width = mode->hdisplay;
10159 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010160 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10161 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010162 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010163
10164 return intel_framebuffer_create(dev, &mode_cmd, obj);
10165}
10166
10167static struct drm_framebuffer *
10168mode_fits_in_fbdev(struct drm_device *dev,
10169 struct drm_display_mode *mode)
10170{
Daniel Vetter06957262015-08-10 13:34:08 +020010171#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010172 struct drm_i915_private *dev_priv = dev->dev_private;
10173 struct drm_i915_gem_object *obj;
10174 struct drm_framebuffer *fb;
10175
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010176 if (!dev_priv->fbdev)
10177 return NULL;
10178
10179 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010180 return NULL;
10181
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010182 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010183 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010184
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010185 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010186 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10187 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010188 return NULL;
10189
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010190 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010191 return NULL;
10192
10193 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010194#else
10195 return NULL;
10196#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010197}
10198
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010199static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10200 struct drm_crtc *crtc,
10201 struct drm_display_mode *mode,
10202 struct drm_framebuffer *fb,
10203 int x, int y)
10204{
10205 struct drm_plane_state *plane_state;
10206 int hdisplay, vdisplay;
10207 int ret;
10208
10209 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10210 if (IS_ERR(plane_state))
10211 return PTR_ERR(plane_state);
10212
10213 if (mode)
10214 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10215 else
10216 hdisplay = vdisplay = 0;
10217
10218 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10219 if (ret)
10220 return ret;
10221 drm_atomic_set_fb_for_plane(plane_state, fb);
10222 plane_state->crtc_x = 0;
10223 plane_state->crtc_y = 0;
10224 plane_state->crtc_w = hdisplay;
10225 plane_state->crtc_h = vdisplay;
10226 plane_state->src_x = x << 16;
10227 plane_state->src_y = y << 16;
10228 plane_state->src_w = hdisplay << 16;
10229 plane_state->src_h = vdisplay << 16;
10230
10231 return 0;
10232}
10233
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010234bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010235 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010236 struct intel_load_detect_pipe *old,
10237 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010238{
10239 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010240 struct intel_encoder *intel_encoder =
10241 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010242 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010243 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010244 struct drm_crtc *crtc = NULL;
10245 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010246 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010247 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010248 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010249 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010250 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010251 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010252
Chris Wilsond2dff872011-04-19 08:36:26 +010010253 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010254 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010255 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010256
Rob Clark51fd3712013-11-19 12:10:12 -050010257retry:
10258 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10259 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010260 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010261
Jesse Barnes79e53942008-11-07 14:24:08 -080010262 /*
10263 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010264 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010265 * - if the connector already has an assigned crtc, use it (but make
10266 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010267 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010268 * - try to find the first unused crtc that can drive this connector,
10269 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010270 */
10271
10272 /* See if we already have a CRTC for this connector */
10273 if (encoder->crtc) {
10274 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010275
Rob Clark51fd3712013-11-19 12:10:12 -050010276 ret = drm_modeset_lock(&crtc->mutex, ctx);
10277 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010278 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010279 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10280 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010281 goto fail;
Daniel Vetter7b240562012-12-12 00:35:33 +010010282
Daniel Vetter24218aa2012-08-12 19:27:11 +020010283 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010284 old->load_detect_temp = false;
10285
10286 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010287 if (connector->dpms != DRM_MODE_DPMS_ON)
10288 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010289
Chris Wilson71731882011-04-19 23:10:58 +010010290 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010291 }
10292
10293 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010294 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010295 i++;
10296 if (!(encoder->possible_crtcs & (1 << i)))
10297 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010298 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010299 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +030010300
10301 crtc = possible_crtc;
10302 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010303 }
10304
10305 /*
10306 * If we didn't find an unused CRTC, don't use any.
10307 */
10308 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010309 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010310 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010311 }
10312
Rob Clark51fd3712013-11-19 12:10:12 -050010313 ret = drm_modeset_lock(&crtc->mutex, ctx);
10314 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010315 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010316 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10317 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010318 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010319
10320 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +020010321 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010322 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010323 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010324
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010325 state = drm_atomic_state_alloc(dev);
10326 if (!state)
10327 return false;
10328
10329 state->acquire_ctx = ctx;
10330
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010331 connector_state = drm_atomic_get_connector_state(state, connector);
10332 if (IS_ERR(connector_state)) {
10333 ret = PTR_ERR(connector_state);
10334 goto fail;
10335 }
10336
10337 connector_state->crtc = crtc;
10338 connector_state->best_encoder = &intel_encoder->base;
10339
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010340 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10341 if (IS_ERR(crtc_state)) {
10342 ret = PTR_ERR(crtc_state);
10343 goto fail;
10344 }
10345
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010346 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010347
Chris Wilson64927112011-04-20 07:25:26 +010010348 if (!mode)
10349 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010350
Chris Wilsond2dff872011-04-19 08:36:26 +010010351 /* We need a framebuffer large enough to accommodate all accesses
10352 * that the plane may generate whilst we perform load detection.
10353 * We can not rely on the fbcon either being present (we get called
10354 * during its initialisation to detect all boot displays, or it may
10355 * not even exist) or that it is large enough to satisfy the
10356 * requested mode.
10357 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010358 fb = mode_fits_in_fbdev(dev, mode);
10359 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010360 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010361 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10362 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010363 } else
10364 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010365 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010366 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010367 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010368 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010369
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010370 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10371 if (ret)
10372 goto fail;
10373
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010374 drm_mode_copy(&crtc_state->base.mode, mode);
10375
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020010376 if (drm_atomic_commit(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010377 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010378 if (old->release_fb)
10379 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010380 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010381 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010382 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010383
Jesse Barnes79e53942008-11-07 14:24:08 -080010384 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010385 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010386 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010387
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010388fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010389 drm_atomic_state_free(state);
10390 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010391
Rob Clark51fd3712013-11-19 12:10:12 -050010392 if (ret == -EDEADLK) {
10393 drm_modeset_backoff(ctx);
10394 goto retry;
10395 }
10396
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010397 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010398}
10399
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010400void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010401 struct intel_load_detect_pipe *old,
10402 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010403{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010404 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010405 struct intel_encoder *intel_encoder =
10406 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010407 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010408 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010409 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010410 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010411 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010412 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010413 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010414
Chris Wilsond2dff872011-04-19 08:36:26 +010010415 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010416 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010417 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010418
Chris Wilson8261b192011-04-19 23:18:09 +010010419 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010420 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010421 if (!state)
10422 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010423
10424 state->acquire_ctx = ctx;
10425
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010426 connector_state = drm_atomic_get_connector_state(state, connector);
10427 if (IS_ERR(connector_state))
10428 goto fail;
10429
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010430 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10431 if (IS_ERR(crtc_state))
10432 goto fail;
10433
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010434 connector_state->best_encoder = NULL;
10435 connector_state->crtc = NULL;
10436
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010437 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010438
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010439 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10440 0, 0);
10441 if (ret)
10442 goto fail;
10443
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020010444 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010445 if (ret)
10446 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010447
Daniel Vetter36206362012-12-10 20:42:17 +010010448 if (old->release_fb) {
10449 drm_framebuffer_unregister_private(old->release_fb);
10450 drm_framebuffer_unreference(old->release_fb);
10451 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010452
Chris Wilson0622a532011-04-21 09:32:11 +010010453 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010454 }
10455
Eric Anholtc751ce42010-03-25 11:48:48 -070010456 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010457 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10458 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010459
10460 return;
10461fail:
10462 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10463 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010464}
10465
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010466static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010467 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010468{
10469 struct drm_i915_private *dev_priv = dev->dev_private;
10470 u32 dpll = pipe_config->dpll_hw_state.dpll;
10471
10472 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010473 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010474 else if (HAS_PCH_SPLIT(dev))
10475 return 120000;
10476 else if (!IS_GEN2(dev))
10477 return 96000;
10478 else
10479 return 48000;
10480}
10481
Jesse Barnes79e53942008-11-07 14:24:08 -080010482/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010483static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010484 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010485{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010486 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010487 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010488 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010489 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010490 u32 fp;
10491 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010492 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010493 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010494
10495 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010496 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010497 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010498 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010499
10500 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010501 if (IS_PINEVIEW(dev)) {
10502 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10503 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010504 } else {
10505 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10506 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10507 }
10508
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010509 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010510 if (IS_PINEVIEW(dev))
10511 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10512 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010513 else
10514 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010515 DPLL_FPA01_P1_POST_DIV_SHIFT);
10516
10517 switch (dpll & DPLL_MODE_MASK) {
10518 case DPLLB_MODE_DAC_SERIAL:
10519 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10520 5 : 10;
10521 break;
10522 case DPLLB_MODE_LVDS:
10523 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10524 7 : 14;
10525 break;
10526 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010527 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010528 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010529 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010530 }
10531
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010532 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010533 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010534 else
Imre Deakdccbea32015-06-22 23:35:51 +030010535 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010536 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010537 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010538 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010539
10540 if (is_lvds) {
10541 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10542 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010543
10544 if (lvds & LVDS_CLKB_POWER_UP)
10545 clock.p2 = 7;
10546 else
10547 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010548 } else {
10549 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10550 clock.p1 = 2;
10551 else {
10552 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10553 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10554 }
10555 if (dpll & PLL_P2_DIVIDE_BY_4)
10556 clock.p2 = 4;
10557 else
10558 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010559 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010560
Imre Deakdccbea32015-06-22 23:35:51 +030010561 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010562 }
10563
Ville Syrjälä18442d02013-09-13 16:00:08 +030010564 /*
10565 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010566 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010567 * encoder's get_config() function.
10568 */
Imre Deakdccbea32015-06-22 23:35:51 +030010569 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010570}
10571
Ville Syrjälä6878da02013-09-13 15:59:11 +030010572int intel_dotclock_calculate(int link_freq,
10573 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010574{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010575 /*
10576 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010577 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010578 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010579 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010580 *
10581 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010582 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010583 */
10584
Ville Syrjälä6878da02013-09-13 15:59:11 +030010585 if (!m_n->link_n)
10586 return 0;
10587
10588 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10589}
10590
Ville Syrjälä18442d02013-09-13 16:00:08 +030010591static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010592 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010593{
10594 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010595
10596 /* read out port_clock from the DPLL */
10597 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010598
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010599 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010600 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010601 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010602 * agree once we know their relationship in the encoder's
10603 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010604 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010605 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010606 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10607 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010608}
10609
10610/** Returns the currently programmed mode of the given pipe. */
10611struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10612 struct drm_crtc *crtc)
10613{
Jesse Barnes548f2452011-02-17 10:40:53 -080010614 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010615 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010616 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010617 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010618 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010619 int htot = I915_READ(HTOTAL(cpu_transcoder));
10620 int hsync = I915_READ(HSYNC(cpu_transcoder));
10621 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10622 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010623 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010624
10625 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10626 if (!mode)
10627 return NULL;
10628
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010629 /*
10630 * Construct a pipe_config sufficient for getting the clock info
10631 * back out of crtc_clock_get.
10632 *
10633 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10634 * to use a real value here instead.
10635 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010636 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010637 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010638 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10639 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10640 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010641 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10642
Ville Syrjälä773ae032013-09-23 17:48:20 +030010643 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010644 mode->hdisplay = (htot & 0xffff) + 1;
10645 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10646 mode->hsync_start = (hsync & 0xffff) + 1;
10647 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10648 mode->vdisplay = (vtot & 0xffff) + 1;
10649 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10650 mode->vsync_start = (vsync & 0xffff) + 1;
10651 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10652
10653 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010654
10655 return mode;
10656}
10657
Chris Wilsonf047e392012-07-21 12:31:41 +010010658void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010659{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010660 struct drm_i915_private *dev_priv = dev->dev_private;
10661
Chris Wilsonf62a0072014-02-21 17:55:39 +000010662 if (dev_priv->mm.busy)
10663 return;
10664
Paulo Zanoni43694d62014-03-07 20:08:08 -030010665 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010666 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010667 if (INTEL_INFO(dev)->gen >= 6)
10668 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010669 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010670}
10671
10672void intel_mark_idle(struct drm_device *dev)
10673{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010674 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010675
Chris Wilsonf62a0072014-02-21 17:55:39 +000010676 if (!dev_priv->mm.busy)
10677 return;
10678
10679 dev_priv->mm.busy = false;
10680
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010681 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010682 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010683
Paulo Zanoni43694d62014-03-07 20:08:08 -030010684 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010685}
10686
Jesse Barnes79e53942008-11-07 14:24:08 -080010687static void intel_crtc_destroy(struct drm_crtc *crtc)
10688{
10689 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010690 struct drm_device *dev = crtc->dev;
10691 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010692
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010693 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010694 work = intel_crtc->unpin_work;
10695 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010696 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010697
10698 if (work) {
10699 cancel_work_sync(&work->work);
10700 kfree(work);
10701 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010702
10703 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010704
Jesse Barnes79e53942008-11-07 14:24:08 -080010705 kfree(intel_crtc);
10706}
10707
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010708static void intel_unpin_work_fn(struct work_struct *__work)
10709{
10710 struct intel_unpin_work *work =
10711 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010712 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10713 struct drm_device *dev = crtc->base.dev;
10714 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010715
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010716 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010717 intel_unpin_fb_obj(work->old_fb, primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010718 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010719
John Harrisonf06cc1b2014-11-24 18:49:37 +000010720 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010721 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010722 mutex_unlock(&dev->struct_mutex);
10723
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010724 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010725 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010726
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010727 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10728 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010729
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010730 kfree(work);
10731}
10732
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010733static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010734 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010735{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010736 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10737 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010738 unsigned long flags;
10739
10740 /* Ignore early vblank irqs */
10741 if (intel_crtc == NULL)
10742 return;
10743
Daniel Vetterf3260382014-09-15 14:55:23 +020010744 /*
10745 * This is called both by irq handlers and the reset code (to complete
10746 * lost pageflips) so needs the full irqsave spinlocks.
10747 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010748 spin_lock_irqsave(&dev->event_lock, flags);
10749 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010750
10751 /* Ensure we don't miss a work->pending update ... */
10752 smp_rmb();
10753
10754 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010755 spin_unlock_irqrestore(&dev->event_lock, flags);
10756 return;
10757 }
10758
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010759 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010760
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010761 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010762}
10763
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010764void intel_finish_page_flip(struct drm_device *dev, int pipe)
10765{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010766 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010767 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10768
Mario Kleiner49b14a52010-12-09 07:00:07 +010010769 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010770}
10771
10772void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10773{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010774 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010775 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10776
Mario Kleiner49b14a52010-12-09 07:00:07 +010010777 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010778}
10779
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010780/* Is 'a' after or equal to 'b'? */
10781static bool g4x_flip_count_after_eq(u32 a, u32 b)
10782{
10783 return !((a - b) & 0x80000000);
10784}
10785
10786static bool page_flip_finished(struct intel_crtc *crtc)
10787{
10788 struct drm_device *dev = crtc->base.dev;
10789 struct drm_i915_private *dev_priv = dev->dev_private;
10790
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010791 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10792 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10793 return true;
10794
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010795 /*
10796 * The relevant registers doen't exist on pre-ctg.
10797 * As the flip done interrupt doesn't trigger for mmio
10798 * flips on gmch platforms, a flip count check isn't
10799 * really needed there. But since ctg has the registers,
10800 * include it in the check anyway.
10801 */
10802 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10803 return true;
10804
10805 /*
10806 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10807 * used the same base address. In that case the mmio flip might
10808 * have completed, but the CS hasn't even executed the flip yet.
10809 *
10810 * A flip count check isn't enough as the CS might have updated
10811 * the base address just after start of vblank, but before we
10812 * managed to process the interrupt. This means we'd complete the
10813 * CS flip too soon.
10814 *
10815 * Combining both checks should get us a good enough result. It may
10816 * still happen that the CS flip has been executed, but has not
10817 * yet actually completed. But in case the base address is the same
10818 * anyway, we don't really care.
10819 */
10820 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10821 crtc->unpin_work->gtt_offset &&
10822 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10823 crtc->unpin_work->flip_count);
10824}
10825
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010826void intel_prepare_page_flip(struct drm_device *dev, int plane)
10827{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010828 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010829 struct intel_crtc *intel_crtc =
10830 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10831 unsigned long flags;
10832
Daniel Vetterf3260382014-09-15 14:55:23 +020010833
10834 /*
10835 * This is called both by irq handlers and the reset code (to complete
10836 * lost pageflips) so needs the full irqsave spinlocks.
10837 *
10838 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010839 * generate a page-flip completion irq, i.e. every modeset
10840 * is also accompanied by a spurious intel_prepare_page_flip().
10841 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010842 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010843 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010844 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010845 spin_unlock_irqrestore(&dev->event_lock, flags);
10846}
10847
Robin Schroereba905b2014-05-18 02:24:50 +020010848static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010849{
10850 /* Ensure that the work item is consistent when activating it ... */
10851 smp_wmb();
10852 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10853 /* and that it is marked active as soon as the irq could fire. */
10854 smp_wmb();
10855}
10856
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010857static int intel_gen2_queue_flip(struct drm_device *dev,
10858 struct drm_crtc *crtc,
10859 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010860 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010861 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010862 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010863{
John Harrison6258fbe2015-05-29 17:43:48 +010010864 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010865 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010866 u32 flip_mask;
10867 int ret;
10868
John Harrison5fb9de12015-05-29 17:44:07 +010010869 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010870 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010871 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010872
10873 /* Can't queue multiple flips, so wait for the previous
10874 * one to finish before executing the next.
10875 */
10876 if (intel_crtc->plane)
10877 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10878 else
10879 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010880 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10881 intel_ring_emit(ring, MI_NOOP);
10882 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10883 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10884 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010885 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010886 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010887
10888 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010889 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010890}
10891
10892static int intel_gen3_queue_flip(struct drm_device *dev,
10893 struct drm_crtc *crtc,
10894 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010895 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010896 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010897 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010898{
John Harrison6258fbe2015-05-29 17:43:48 +010010899 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010900 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010901 u32 flip_mask;
10902 int ret;
10903
John Harrison5fb9de12015-05-29 17:44:07 +010010904 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010905 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010906 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010907
10908 if (intel_crtc->plane)
10909 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10910 else
10911 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010912 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10913 intel_ring_emit(ring, MI_NOOP);
10914 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10915 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10916 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010917 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010918 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010919
Chris Wilsone7d841c2012-12-03 11:36:30 +000010920 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010921 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010922}
10923
10924static int intel_gen4_queue_flip(struct drm_device *dev,
10925 struct drm_crtc *crtc,
10926 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010927 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010928 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010929 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010930{
John Harrison6258fbe2015-05-29 17:43:48 +010010931 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010932 struct drm_i915_private *dev_priv = dev->dev_private;
10933 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10934 uint32_t pf, pipesrc;
10935 int ret;
10936
John Harrison5fb9de12015-05-29 17:44:07 +010010937 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010938 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010939 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010940
10941 /* i965+ uses the linear or tiled offsets from the
10942 * Display Registers (which do not change across a page-flip)
10943 * so we need only reprogram the base address.
10944 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010945 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10946 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10947 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010948 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010949 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010950
10951 /* XXX Enabling the panel-fitter across page-flip is so far
10952 * untested on non-native modes, so ignore it for now.
10953 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10954 */
10955 pf = 0;
10956 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010957 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010958
10959 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010960 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010961}
10962
10963static int intel_gen6_queue_flip(struct drm_device *dev,
10964 struct drm_crtc *crtc,
10965 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010966 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010967 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010968 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010969{
John Harrison6258fbe2015-05-29 17:43:48 +010010970 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010971 struct drm_i915_private *dev_priv = dev->dev_private;
10972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10973 uint32_t pf, pipesrc;
10974 int ret;
10975
John Harrison5fb9de12015-05-29 17:44:07 +010010976 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010977 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010978 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010979
Daniel Vetter6d90c952012-04-26 23:28:05 +020010980 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10981 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10982 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010983 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010984
Chris Wilson99d9acd2012-04-17 20:37:00 +010010985 /* Contrary to the suggestions in the documentation,
10986 * "Enable Panel Fitter" does not seem to be required when page
10987 * flipping with a non-native mode, and worse causes a normal
10988 * modeset to fail.
10989 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10990 */
10991 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010992 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010993 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010994
10995 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010996 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010997}
10998
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010999static int intel_gen7_queue_flip(struct drm_device *dev,
11000 struct drm_crtc *crtc,
11001 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011002 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011003 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011004 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011005{
John Harrison6258fbe2015-05-29 17:43:48 +010011006 struct intel_engine_cs *ring = req->ring;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011008 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011009 int len, ret;
11010
Robin Schroereba905b2014-05-18 02:24:50 +020011011 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011012 case PLANE_A:
11013 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11014 break;
11015 case PLANE_B:
11016 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11017 break;
11018 case PLANE_C:
11019 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11020 break;
11021 default:
11022 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011023 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011024 }
11025
Chris Wilsonffe74d72013-08-26 20:58:12 +010011026 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011027 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011028 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011029 /*
11030 * On Gen 8, SRM is now taking an extra dword to accommodate
11031 * 48bits addresses, and we need a NOOP for the batch size to
11032 * stay even.
11033 */
11034 if (IS_GEN8(dev))
11035 len += 2;
11036 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011037
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011038 /*
11039 * BSpec MI_DISPLAY_FLIP for IVB:
11040 * "The full packet must be contained within the same cache line."
11041 *
11042 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11043 * cacheline, if we ever start emitting more commands before
11044 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11045 * then do the cacheline alignment, and finally emit the
11046 * MI_DISPLAY_FLIP.
11047 */
John Harrisonbba09b12015-05-29 17:44:06 +010011048 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011049 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011050 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011051
John Harrison5fb9de12015-05-29 17:44:07 +010011052 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011053 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011054 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011055
Chris Wilsonffe74d72013-08-26 20:58:12 +010011056 /* Unmask the flip-done completion message. Note that the bspec says that
11057 * we should do this for both the BCS and RCS, and that we must not unmask
11058 * more than one flip event at any time (or ensure that one flip message
11059 * can be sent by waiting for flip-done prior to queueing new flips).
11060 * Experimentation says that BCS works despite DERRMR masking all
11061 * flip-done completion events and that unmasking all planes at once
11062 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11063 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11064 */
11065 if (ring->id == RCS) {
11066 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11067 intel_ring_emit(ring, DERRMR);
11068 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11069 DERRMR_PIPEB_PRI_FLIP_DONE |
11070 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011071 if (IS_GEN8(dev))
Arun Siluveryf1afe242015-08-04 16:22:20 +010011072 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011073 MI_SRM_LRM_GLOBAL_GTT);
11074 else
Arun Siluveryf1afe242015-08-04 16:22:20 +010011075 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011076 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011077 intel_ring_emit(ring, DERRMR);
11078 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011079 if (IS_GEN8(dev)) {
11080 intel_ring_emit(ring, 0);
11081 intel_ring_emit(ring, MI_NOOP);
11082 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011083 }
11084
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011085 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011086 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011087 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011088 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011089
11090 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011091 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011092}
11093
Sourab Gupta84c33a62014-06-02 16:47:17 +053011094static bool use_mmio_flip(struct intel_engine_cs *ring,
11095 struct drm_i915_gem_object *obj)
11096{
11097 /*
11098 * This is not being used for older platforms, because
11099 * non-availability of flip done interrupt forces us to use
11100 * CS flips. Older platforms derive flip done using some clever
11101 * tricks involving the flip_pending status bits and vblank irqs.
11102 * So using MMIO flips there would disrupt this mechanism.
11103 */
11104
Chris Wilson8e09bf82014-07-08 10:40:30 +010011105 if (ring == NULL)
11106 return true;
11107
Sourab Gupta84c33a62014-06-02 16:47:17 +053011108 if (INTEL_INFO(ring->dev)->gen < 5)
11109 return false;
11110
11111 if (i915.use_mmio_flip < 0)
11112 return false;
11113 else if (i915.use_mmio_flip > 0)
11114 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011115 else if (i915.enable_execlists)
11116 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011117 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011118 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011119}
11120
Damien Lespiauff944562014-11-20 14:58:16 +000011121static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11122{
11123 struct drm_device *dev = intel_crtc->base.dev;
11124 struct drm_i915_private *dev_priv = dev->dev_private;
11125 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011126 const enum pipe pipe = intel_crtc->pipe;
11127 u32 ctl, stride;
11128
11129 ctl = I915_READ(PLANE_CTL(pipe, 0));
11130 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011131 switch (fb->modifier[0]) {
11132 case DRM_FORMAT_MOD_NONE:
11133 break;
11134 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011135 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011136 break;
11137 case I915_FORMAT_MOD_Y_TILED:
11138 ctl |= PLANE_CTL_TILED_Y;
11139 break;
11140 case I915_FORMAT_MOD_Yf_TILED:
11141 ctl |= PLANE_CTL_TILED_YF;
11142 break;
11143 default:
11144 MISSING_CASE(fb->modifier[0]);
11145 }
Damien Lespiauff944562014-11-20 14:58:16 +000011146
11147 /*
11148 * The stride is either expressed as a multiple of 64 bytes chunks for
11149 * linear buffers or in number of tiles for tiled buffers.
11150 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011151 stride = fb->pitches[0] /
11152 intel_fb_stride_alignment(dev, fb->modifier[0],
11153 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011154
11155 /*
11156 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11157 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11158 */
11159 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11160 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11161
11162 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11163 POSTING_READ(PLANE_SURF(pipe, 0));
11164}
11165
11166static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011167{
11168 struct drm_device *dev = intel_crtc->base.dev;
11169 struct drm_i915_private *dev_priv = dev->dev_private;
11170 struct intel_framebuffer *intel_fb =
11171 to_intel_framebuffer(intel_crtc->base.primary->fb);
11172 struct drm_i915_gem_object *obj = intel_fb->obj;
11173 u32 dspcntr;
11174 u32 reg;
11175
Sourab Gupta84c33a62014-06-02 16:47:17 +053011176 reg = DSPCNTR(intel_crtc->plane);
11177 dspcntr = I915_READ(reg);
11178
Damien Lespiauc5d97472014-10-25 00:11:11 +010011179 if (obj->tiling_mode != I915_TILING_NONE)
11180 dspcntr |= DISPPLANE_TILED;
11181 else
11182 dspcntr &= ~DISPPLANE_TILED;
11183
Sourab Gupta84c33a62014-06-02 16:47:17 +053011184 I915_WRITE(reg, dspcntr);
11185
11186 I915_WRITE(DSPSURF(intel_crtc->plane),
11187 intel_crtc->unpin_work->gtt_offset);
11188 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011189
Damien Lespiauff944562014-11-20 14:58:16 +000011190}
11191
11192/*
11193 * XXX: This is the temporary way to update the plane registers until we get
11194 * around to using the usual plane update functions for MMIO flips
11195 */
11196static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11197{
11198 struct drm_device *dev = intel_crtc->base.dev;
Damien Lespiauff944562014-11-20 14:58:16 +000011199
11200 intel_mark_page_flip_active(intel_crtc);
11201
Maarten Lankhorst34e0adb2015-08-31 13:04:25 +020011202 intel_pipe_update_start(intel_crtc);
Damien Lespiauff944562014-11-20 14:58:16 +000011203
11204 if (INTEL_INFO(dev)->gen >= 9)
11205 skl_do_mmio_flip(intel_crtc);
11206 else
11207 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11208 ilk_do_mmio_flip(intel_crtc);
11209
Maarten Lankhorst34e0adb2015-08-31 13:04:25 +020011210 intel_pipe_update_end(intel_crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011211}
11212
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011213static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011214{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011215 struct intel_mmio_flip *mmio_flip =
11216 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011217
Daniel Vettereed29a52015-05-21 14:21:25 +020011218 if (mmio_flip->req)
11219 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011220 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011221 false, NULL,
11222 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011223
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011224 intel_do_mmio_flip(mmio_flip->crtc);
11225
Daniel Vettereed29a52015-05-21 14:21:25 +020011226 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011227 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011228}
11229
11230static int intel_queue_mmio_flip(struct drm_device *dev,
11231 struct drm_crtc *crtc,
11232 struct drm_framebuffer *fb,
11233 struct drm_i915_gem_object *obj,
11234 struct intel_engine_cs *ring,
11235 uint32_t flags)
11236{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011237 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011238
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011239 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11240 if (mmio_flip == NULL)
11241 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011242
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011243 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011244 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011245 mmio_flip->crtc = to_intel_crtc(crtc);
11246
11247 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11248 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011249
Sourab Gupta84c33a62014-06-02 16:47:17 +053011250 return 0;
11251}
11252
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011253static int intel_default_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{
11260 return -ENODEV;
11261}
11262
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011263static bool __intel_pageflip_stall_check(struct drm_device *dev,
11264 struct drm_crtc *crtc)
11265{
11266 struct drm_i915_private *dev_priv = dev->dev_private;
11267 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11268 struct intel_unpin_work *work = intel_crtc->unpin_work;
11269 u32 addr;
11270
11271 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11272 return true;
11273
Chris Wilson908565c2015-08-12 13:08:22 +010011274 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11275 return false;
11276
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011277 if (!work->enable_stall_check)
11278 return false;
11279
11280 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011281 if (work->flip_queued_req &&
11282 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011283 return false;
11284
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011285 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011286 }
11287
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011288 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011289 return false;
11290
11291 /* Potential stall - if we see that the flip has happened,
11292 * assume a missed interrupt. */
11293 if (INTEL_INFO(dev)->gen >= 4)
11294 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11295 else
11296 addr = I915_READ(DSPADDR(intel_crtc->plane));
11297
11298 /* There is a potential issue here with a false positive after a flip
11299 * to the same address. We could address this by checking for a
11300 * non-incrementing frame counter.
11301 */
11302 return addr == work->gtt_offset;
11303}
11304
11305void intel_check_page_flip(struct drm_device *dev, int pipe)
11306{
11307 struct drm_i915_private *dev_priv = dev->dev_private;
11308 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11309 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011310 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011311
Dave Gordon6c51d462015-03-06 15:34:26 +000011312 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011313
11314 if (crtc == NULL)
11315 return;
11316
Daniel Vetterf3260382014-09-15 14:55:23 +020011317 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011318 work = intel_crtc->unpin_work;
11319 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011320 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011321 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011322 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011323 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011324 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011325 if (work != NULL &&
11326 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11327 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011328 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011329}
11330
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011331static int intel_crtc_page_flip(struct drm_crtc *crtc,
11332 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011333 struct drm_pending_vblank_event *event,
11334 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011335{
11336 struct drm_device *dev = crtc->dev;
11337 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011338 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011339 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011341 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011342 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011343 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011344 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011345 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011346 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011347 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011348
Matt Roper2ff8fde2014-07-08 07:50:07 -070011349 /*
11350 * drm_mode_page_flip_ioctl() should already catch this, but double
11351 * check to be safe. In the future we may enable pageflipping from
11352 * a disabled primary plane.
11353 */
11354 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11355 return -EBUSY;
11356
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011357 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011358 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011359 return -EINVAL;
11360
11361 /*
11362 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11363 * Note that pitch changes could also affect these register.
11364 */
11365 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011366 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11367 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011368 return -EINVAL;
11369
Chris Wilsonf900db42014-02-20 09:26:13 +000011370 if (i915_terminally_wedged(&dev_priv->gpu_error))
11371 goto out_hang;
11372
Daniel Vetterb14c5672013-09-19 12:18:32 +020011373 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011374 if (work == NULL)
11375 return -ENOMEM;
11376
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011377 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011378 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011379 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011380 INIT_WORK(&work->work, intel_unpin_work_fn);
11381
Daniel Vetter87b6b102014-05-15 15:33:46 +020011382 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011383 if (ret)
11384 goto free_work;
11385
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011386 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011387 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011388 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011389 /* Before declaring the flip queue wedged, check if
11390 * the hardware completed the operation behind our backs.
11391 */
11392 if (__intel_pageflip_stall_check(dev, crtc)) {
11393 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11394 page_flip_completed(intel_crtc);
11395 } else {
11396 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011397 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011398
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011399 drm_crtc_vblank_put(crtc);
11400 kfree(work);
11401 return -EBUSY;
11402 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011403 }
11404 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011405 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011406
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011407 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11408 flush_workqueue(dev_priv->wq);
11409
Jesse Barnes75dfca82010-02-10 15:09:44 -080011410 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011411 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011412 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011413
Matt Roperf4510a22014-04-01 15:22:40 -070011414 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011415 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011416
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011417 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011418
Chris Wilson89ed88b2015-02-16 14:31:49 +000011419 ret = i915_mutex_lock_interruptible(dev);
11420 if (ret)
11421 goto cleanup;
11422
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011423 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011424 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011425
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011426 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011427 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011428
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011429 if (IS_VALLEYVIEW(dev)) {
11430 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011431 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011432 /* vlv: DISPLAY_FLIP fails to change tiling */
11433 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011434 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011435 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011436 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011437 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011438 if (ring == NULL || ring->id != RCS)
11439 ring = &dev_priv->ring[BCS];
11440 } else {
11441 ring = &dev_priv->ring[RCS];
11442 }
11443
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011444 mmio_flip = use_mmio_flip(ring, obj);
11445
11446 /* When using CS flips, we want to emit semaphores between rings.
11447 * However, when using mmio flips we will create a task to do the
11448 * synchronisation, so all we want here is to pin the framebuffer
11449 * into the display plane and skip any waits.
11450 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011451 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011452 crtc->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010011453 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011454 if (ret)
11455 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011456
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011457 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11458 obj, 0);
11459 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011460
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011461 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011462 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11463 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011464 if (ret)
11465 goto cleanup_unpin;
11466
John Harrisonf06cc1b2014-11-24 18:49:37 +000011467 i915_gem_request_assign(&work->flip_queued_req,
11468 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011469 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011470 if (!request) {
11471 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11472 if (ret)
11473 goto cleanup_unpin;
11474 }
11475
11476 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011477 page_flip_flags);
11478 if (ret)
11479 goto cleanup_unpin;
11480
John Harrison6258fbe2015-05-29 17:43:48 +010011481 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011482 }
11483
John Harrison91af1272015-06-18 13:14:56 +010011484 if (request)
John Harrison75289872015-05-29 17:43:49 +010011485 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011486
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011487 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011488 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011489
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011490 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011491 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011492 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011493
Paulo Zanoni4e1e26f2015-07-14 16:29:13 -030011494 intel_fbc_disable_crtc(intel_crtc);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011495 intel_frontbuffer_flip_prepare(dev,
11496 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011497
Jesse Barnese5510fa2010-07-01 16:48:37 -070011498 trace_i915_flip_request(intel_crtc->plane, obj);
11499
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011500 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011501
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011502cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011503 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011504cleanup_pending:
John Harrison91af1272015-06-18 13:14:56 +010011505 if (request)
11506 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011507 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011508 mutex_unlock(&dev->struct_mutex);
11509cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011510 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011511 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011512
Chris Wilson89ed88b2015-02-16 14:31:49 +000011513 drm_gem_object_unreference_unlocked(&obj->base);
11514 drm_framebuffer_unreference(work->old_fb);
11515
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011516 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011517 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011518 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011519
Daniel Vetter87b6b102014-05-15 15:33:46 +020011520 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011521free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011522 kfree(work);
11523
Chris Wilsonf900db42014-02-20 09:26:13 +000011524 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011525 struct drm_atomic_state *state;
11526 struct drm_plane_state *plane_state;
11527
Chris Wilsonf900db42014-02-20 09:26:13 +000011528out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011529 state = drm_atomic_state_alloc(dev);
11530 if (!state)
11531 return -ENOMEM;
11532 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11533
11534retry:
11535 plane_state = drm_atomic_get_plane_state(state, primary);
11536 ret = PTR_ERR_OR_ZERO(plane_state);
11537 if (!ret) {
11538 drm_atomic_set_fb_for_plane(plane_state, fb);
11539
11540 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11541 if (!ret)
11542 ret = drm_atomic_commit(state);
11543 }
11544
11545 if (ret == -EDEADLK) {
11546 drm_modeset_backoff(state->acquire_ctx);
11547 drm_atomic_state_clear(state);
11548 goto retry;
11549 }
11550
11551 if (ret)
11552 drm_atomic_state_free(state);
11553
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011554 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011555 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011556 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011557 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011558 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011559 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011560 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011561}
11562
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011563
11564/**
11565 * intel_wm_need_update - Check whether watermarks need updating
11566 * @plane: drm plane
11567 * @state: new plane state
11568 *
11569 * Check current plane state versus the new one to determine whether
11570 * watermarks need to be recalculated.
11571 *
11572 * Returns true or false.
11573 */
11574static bool intel_wm_need_update(struct drm_plane *plane,
11575 struct drm_plane_state *state)
11576{
11577 /* Update watermarks on tiling changes. */
11578 if (!plane->state->fb || !state->fb ||
11579 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11580 plane->state->rotation != state->rotation)
11581 return true;
11582
11583 if (plane->state->crtc_w != state->crtc_w)
11584 return true;
11585
11586 return false;
11587}
11588
11589int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11590 struct drm_plane_state *plane_state)
11591{
11592 struct drm_crtc *crtc = crtc_state->crtc;
11593 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11594 struct drm_plane *plane = plane_state->plane;
11595 struct drm_device *dev = crtc->dev;
11596 struct drm_i915_private *dev_priv = dev->dev_private;
11597 struct intel_plane_state *old_plane_state =
11598 to_intel_plane_state(plane->state);
11599 int idx = intel_crtc->base.base.id, ret;
11600 int i = drm_plane_index(plane);
11601 bool mode_changed = needs_modeset(crtc_state);
11602 bool was_crtc_enabled = crtc->state->active;
11603 bool is_crtc_enabled = crtc_state->active;
11604
11605 bool turn_off, turn_on, visible, was_visible;
11606 struct drm_framebuffer *fb = plane_state->fb;
11607
11608 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11609 plane->type != DRM_PLANE_TYPE_CURSOR) {
11610 ret = skl_update_scaler_plane(
11611 to_intel_crtc_state(crtc_state),
11612 to_intel_plane_state(plane_state));
11613 if (ret)
11614 return ret;
11615 }
11616
11617 /*
11618 * Disabling a plane is always okay; we just need to update
11619 * fb tracking in a special way since cleanup_fb() won't
11620 * get called by the plane helpers.
11621 */
11622 if (old_plane_state->base.fb && !fb)
11623 intel_crtc->atomic.disabled_planes |= 1 << i;
11624
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011625 was_visible = old_plane_state->visible;
11626 visible = to_intel_plane_state(plane_state)->visible;
11627
11628 if (!was_crtc_enabled && WARN_ON(was_visible))
11629 was_visible = false;
11630
11631 if (!is_crtc_enabled && WARN_ON(visible))
11632 visible = false;
11633
11634 if (!was_visible && !visible)
11635 return 0;
11636
11637 turn_off = was_visible && (!visible || mode_changed);
11638 turn_on = visible && (!was_visible || mode_changed);
11639
11640 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11641 plane->base.id, fb ? fb->base.id : -1);
11642
11643 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11644 plane->base.id, was_visible, visible,
11645 turn_off, turn_on, mode_changed);
11646
Ville Syrjälä852eb002015-06-24 22:00:07 +030011647 if (turn_on) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011648 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011649 /* must disable cxsr around plane enable/disable */
11650 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11651 intel_crtc->atomic.disable_cxsr = true;
11652 /* to potentially re-enable cxsr */
11653 intel_crtc->atomic.wait_vblank = true;
11654 intel_crtc->atomic.update_wm_post = true;
11655 }
11656 } else if (turn_off) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011657 intel_crtc->atomic.update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011658 /* must disable cxsr around plane enable/disable */
11659 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11660 if (is_crtc_enabled)
11661 intel_crtc->atomic.wait_vblank = true;
11662 intel_crtc->atomic.disable_cxsr = true;
11663 }
11664 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011665 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011666 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011667
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011668 if (visible || was_visible)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011669 intel_crtc->atomic.fb_bits |=
11670 to_intel_plane(plane)->frontbuffer_bit;
11671
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011672 switch (plane->type) {
11673 case DRM_PLANE_TYPE_PRIMARY:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011674 intel_crtc->atomic.wait_for_flips = true;
11675 intel_crtc->atomic.pre_disable_primary = turn_off;
11676 intel_crtc->atomic.post_enable_primary = turn_on;
11677
Rodrigo Vivi066cf55b2015-06-26 13:55:54 -070011678 if (turn_off) {
11679 /*
11680 * FIXME: Actually if we will still have any other
11681 * plane enabled on the pipe we could let IPS enabled
11682 * still, but for now lets consider that when we make
11683 * primary invisible by setting DSPCNTR to 0 on
11684 * update_primary_plane function IPS needs to be
11685 * disable.
11686 */
11687 intel_crtc->atomic.disable_ips = true;
11688
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011689 intel_crtc->atomic.disable_fbc = true;
Rodrigo Vivi066cf55b2015-06-26 13:55:54 -070011690 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011691
11692 /*
11693 * FBC does not work on some platforms for rotated
11694 * planes, so disable it when rotation is not 0 and
11695 * update it when rotation is set back to 0.
11696 *
11697 * FIXME: This is redundant with the fbc update done in
11698 * the primary plane enable function except that that
11699 * one is done too late. We eventually need to unify
11700 * this.
11701 */
11702
11703 if (visible &&
11704 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11705 dev_priv->fbc.crtc == intel_crtc &&
11706 plane_state->rotation != BIT(DRM_ROTATE_0))
11707 intel_crtc->atomic.disable_fbc = true;
11708
11709 /*
11710 * BDW signals flip done immediately if the plane
11711 * is disabled, even if the plane enable is already
11712 * armed to occur at the next vblank :(
11713 */
11714 if (turn_on && IS_BROADWELL(dev))
11715 intel_crtc->atomic.wait_vblank = true;
11716
11717 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11718 break;
11719 case DRM_PLANE_TYPE_CURSOR:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011720 break;
11721 case DRM_PLANE_TYPE_OVERLAY:
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011722 if (turn_off && !mode_changed) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011723 intel_crtc->atomic.wait_vblank = true;
11724 intel_crtc->atomic.update_sprite_watermarks |=
11725 1 << i;
11726 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011727 }
11728 return 0;
11729}
11730
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011731static bool encoders_cloneable(const struct intel_encoder *a,
11732 const struct intel_encoder *b)
11733{
11734 /* masks could be asymmetric, so check both ways */
11735 return a == b || (a->cloneable & (1 << b->type) &&
11736 b->cloneable & (1 << a->type));
11737}
11738
11739static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11740 struct intel_crtc *crtc,
11741 struct intel_encoder *encoder)
11742{
11743 struct intel_encoder *source_encoder;
11744 struct drm_connector *connector;
11745 struct drm_connector_state *connector_state;
11746 int i;
11747
11748 for_each_connector_in_state(state, connector, connector_state, i) {
11749 if (connector_state->crtc != &crtc->base)
11750 continue;
11751
11752 source_encoder =
11753 to_intel_encoder(connector_state->best_encoder);
11754 if (!encoders_cloneable(encoder, source_encoder))
11755 return false;
11756 }
11757
11758 return true;
11759}
11760
11761static bool check_encoder_cloning(struct drm_atomic_state *state,
11762 struct intel_crtc *crtc)
11763{
11764 struct intel_encoder *encoder;
11765 struct drm_connector *connector;
11766 struct drm_connector_state *connector_state;
11767 int i;
11768
11769 for_each_connector_in_state(state, connector, connector_state, i) {
11770 if (connector_state->crtc != &crtc->base)
11771 continue;
11772
11773 encoder = to_intel_encoder(connector_state->best_encoder);
11774 if (!check_single_encoder_cloning(state, crtc, encoder))
11775 return false;
11776 }
11777
11778 return true;
11779}
11780
11781static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11782 struct drm_crtc_state *crtc_state)
11783{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011784 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011785 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011786 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011787 struct intel_crtc_state *pipe_config =
11788 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011789 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011790 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011791 bool mode_changed = needs_modeset(crtc_state);
11792
11793 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11794 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11795 return -EINVAL;
11796 }
11797
Ville Syrjälä852eb002015-06-24 22:00:07 +030011798 if (mode_changed && !crtc_state->active)
11799 intel_crtc->atomic.update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011800
Maarten Lankhorstad421372015-06-15 12:33:42 +020011801 if (mode_changed && crtc_state->enable &&
11802 dev_priv->display.crtc_compute_clock &&
11803 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11804 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11805 pipe_config);
11806 if (ret)
11807 return ret;
11808 }
11809
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011810 ret = 0;
11811 if (INTEL_INFO(dev)->gen >= 9) {
11812 if (mode_changed)
11813 ret = skl_update_scaler_crtc(pipe_config);
11814
11815 if (!ret)
11816 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11817 pipe_config);
11818 }
11819
11820 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011821}
11822
Jani Nikula65b38e02015-04-13 11:26:56 +030011823static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011824 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11825 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011826 .atomic_begin = intel_begin_crtc_commit,
11827 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011828 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011829};
11830
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011831static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11832{
11833 struct intel_connector *connector;
11834
11835 for_each_intel_connector(dev, connector) {
11836 if (connector->base.encoder) {
11837 connector->base.state->best_encoder =
11838 connector->base.encoder;
11839 connector->base.state->crtc =
11840 connector->base.encoder->crtc;
11841 } else {
11842 connector->base.state->best_encoder = NULL;
11843 connector->base.state->crtc = NULL;
11844 }
11845 }
11846}
11847
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011848static void
Robin Schroereba905b2014-05-18 02:24:50 +020011849connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011850 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011851{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011852 int bpp = pipe_config->pipe_bpp;
11853
11854 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11855 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011856 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011857
11858 /* Don't use an invalid EDID bpc value */
11859 if (connector->base.display_info.bpc &&
11860 connector->base.display_info.bpc * 3 < bpp) {
11861 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11862 bpp, connector->base.display_info.bpc*3);
11863 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11864 }
11865
11866 /* Clamp bpp to 8 on screens without EDID 1.4 */
11867 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11868 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11869 bpp);
11870 pipe_config->pipe_bpp = 24;
11871 }
11872}
11873
11874static int
11875compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011876 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011877{
11878 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011879 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011880 struct drm_connector *connector;
11881 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011882 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011883
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011884 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011885 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011886 else if (INTEL_INFO(dev)->gen >= 5)
11887 bpp = 12*3;
11888 else
11889 bpp = 8*3;
11890
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011891
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011892 pipe_config->pipe_bpp = bpp;
11893
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011894 state = pipe_config->base.state;
11895
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011896 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011897 for_each_connector_in_state(state, connector, connector_state, i) {
11898 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011899 continue;
11900
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011901 connected_sink_compute_bpp(to_intel_connector(connector),
11902 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011903 }
11904
11905 return bpp;
11906}
11907
Daniel Vetter644db712013-09-19 14:53:58 +020011908static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11909{
11910 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11911 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011912 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011913 mode->crtc_hdisplay, mode->crtc_hsync_start,
11914 mode->crtc_hsync_end, mode->crtc_htotal,
11915 mode->crtc_vdisplay, mode->crtc_vsync_start,
11916 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11917}
11918
Daniel Vetterc0b03412013-05-28 12:05:54 +020011919static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011920 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011921 const char *context)
11922{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011923 struct drm_device *dev = crtc->base.dev;
11924 struct drm_plane *plane;
11925 struct intel_plane *intel_plane;
11926 struct intel_plane_state *state;
11927 struct drm_framebuffer *fb;
11928
11929 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11930 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011931
11932 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11933 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11934 pipe_config->pipe_bpp, pipe_config->dither);
11935 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11936 pipe_config->has_pch_encoder,
11937 pipe_config->fdi_lanes,
11938 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11939 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11940 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011941 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 +030011942 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011943 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011944 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11945 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11946 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011947
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011948 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 -070011949 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011950 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011951 pipe_config->dp_m2_n2.gmch_m,
11952 pipe_config->dp_m2_n2.gmch_n,
11953 pipe_config->dp_m2_n2.link_m,
11954 pipe_config->dp_m2_n2.link_n,
11955 pipe_config->dp_m2_n2.tu);
11956
Daniel Vetter55072d12014-11-20 16:10:28 +010011957 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11958 pipe_config->has_audio,
11959 pipe_config->has_infoframe);
11960
Daniel Vetterc0b03412013-05-28 12:05:54 +020011961 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011962 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011963 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011964 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11965 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011966 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011967 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11968 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011969 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11970 crtc->num_scalers,
11971 pipe_config->scaler_state.scaler_users,
11972 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011973 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11974 pipe_config->gmch_pfit.control,
11975 pipe_config->gmch_pfit.pgm_ratios,
11976 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011977 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011978 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011979 pipe_config->pch_pfit.size,
11980 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011981 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011982 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011983
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011984 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030011985 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011986 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030011987 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011988 pipe_config->ddi_pll_sel,
11989 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030011990 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011991 pipe_config->dpll_hw_state.pll0,
11992 pipe_config->dpll_hw_state.pll1,
11993 pipe_config->dpll_hw_state.pll2,
11994 pipe_config->dpll_hw_state.pll3,
11995 pipe_config->dpll_hw_state.pll6,
11996 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030011997 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030011998 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011999 pipe_config->dpll_hw_state.pcsdw12);
12000 } else if (IS_SKYLAKE(dev)) {
12001 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12002 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12003 pipe_config->ddi_pll_sel,
12004 pipe_config->dpll_hw_state.ctrl1,
12005 pipe_config->dpll_hw_state.cfgcr1,
12006 pipe_config->dpll_hw_state.cfgcr2);
12007 } else if (HAS_DDI(dev)) {
12008 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
12009 pipe_config->ddi_pll_sel,
12010 pipe_config->dpll_hw_state.wrpll);
12011 } else {
12012 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12013 "fp0: 0x%x, fp1: 0x%x\n",
12014 pipe_config->dpll_hw_state.dpll,
12015 pipe_config->dpll_hw_state.dpll_md,
12016 pipe_config->dpll_hw_state.fp0,
12017 pipe_config->dpll_hw_state.fp1);
12018 }
12019
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012020 DRM_DEBUG_KMS("planes on this crtc\n");
12021 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12022 intel_plane = to_intel_plane(plane);
12023 if (intel_plane->pipe != crtc->pipe)
12024 continue;
12025
12026 state = to_intel_plane_state(plane->state);
12027 fb = state->base.fb;
12028 if (!fb) {
12029 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12030 "disabled, scaler_id = %d\n",
12031 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12032 plane->base.id, intel_plane->pipe,
12033 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12034 drm_plane_index(plane), state->scaler_id);
12035 continue;
12036 }
12037
12038 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12039 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12040 plane->base.id, intel_plane->pipe,
12041 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12042 drm_plane_index(plane));
12043 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12044 fb->base.id, fb->width, fb->height, fb->pixel_format);
12045 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12046 state->scaler_id,
12047 state->src.x1 >> 16, state->src.y1 >> 16,
12048 drm_rect_width(&state->src) >> 16,
12049 drm_rect_height(&state->src) >> 16,
12050 state->dst.x1, state->dst.y1,
12051 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12052 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012053}
12054
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012055static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012056{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012057 struct drm_device *dev = state->dev;
12058 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012059 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012060 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012061 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012062 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012063
12064 /*
12065 * Walk the connector list instead of the encoder
12066 * list to detect the problem on ddi platforms
12067 * where there's just one encoder per digital port.
12068 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012069 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012070 if (!connector_state->best_encoder)
12071 continue;
12072
12073 encoder = to_intel_encoder(connector_state->best_encoder);
12074
12075 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012076
12077 switch (encoder->type) {
12078 unsigned int port_mask;
12079 case INTEL_OUTPUT_UNKNOWN:
12080 if (WARN_ON(!HAS_DDI(dev)))
12081 break;
12082 case INTEL_OUTPUT_DISPLAYPORT:
12083 case INTEL_OUTPUT_HDMI:
12084 case INTEL_OUTPUT_EDP:
12085 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12086
12087 /* the same port mustn't appear more than once */
12088 if (used_ports & port_mask)
12089 return false;
12090
12091 used_ports |= port_mask;
12092 default:
12093 break;
12094 }
12095 }
12096
12097 return true;
12098}
12099
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012100static void
12101clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12102{
12103 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012104 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012105 struct intel_dpll_hw_state dpll_hw_state;
12106 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012107 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012108 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012109
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012110 /* FIXME: before the switch to atomic started, a new pipe_config was
12111 * kzalloc'd. Code that depends on any field being zero should be
12112 * fixed, so that the crtc_state can be safely duplicated. For now,
12113 * only fields that are know to not cause problems are preserved. */
12114
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012115 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012116 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012117 shared_dpll = crtc_state->shared_dpll;
12118 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012119 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012120 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012121
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012122 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012123
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012124 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012125 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012126 crtc_state->shared_dpll = shared_dpll;
12127 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012128 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012129 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012130}
12131
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012132static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012133intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012134 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012135{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012136 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012137 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012138 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012139 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012140 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012141 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012142 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012143
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012144 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012145
Daniel Vettere143a212013-07-04 12:01:15 +020012146 pipe_config->cpu_transcoder =
12147 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012148
Imre Deak2960bc92013-07-30 13:36:32 +030012149 /*
12150 * Sanitize sync polarity flags based on requested ones. If neither
12151 * positive or negative polarity is requested, treat this as meaning
12152 * negative polarity.
12153 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012154 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012155 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012156 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012157
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012158 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012159 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012160 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012161
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012162 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12163 pipe_config);
12164 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012165 goto fail;
12166
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012167 /*
12168 * Determine the real pipe dimensions. Note that stereo modes can
12169 * increase the actual pipe size due to the frame doubling and
12170 * insertion of additional space for blanks between the frame. This
12171 * is stored in the crtc timings. We use the requested mode to do this
12172 * computation to clearly distinguish it from the adjusted mode, which
12173 * can be changed by the connectors in the below retry loop.
12174 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012175 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012176 &pipe_config->pipe_src_w,
12177 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012178
Daniel Vettere29c22c2013-02-21 00:00:16 +010012179encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012180 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012181 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012182 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012183
Daniel Vetter135c81b2013-07-21 21:37:09 +020012184 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012185 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12186 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012187
Daniel Vetter7758a112012-07-08 19:40:39 +020012188 /* Pass our mode to the connectors and the CRTC to give them a chance to
12189 * adjust it according to limitations or connector properties, and also
12190 * a chance to reject the mode entirely.
12191 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012192 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012193 if (connector_state->crtc != crtc)
12194 continue;
12195
12196 encoder = to_intel_encoder(connector_state->best_encoder);
12197
Daniel Vetterefea6e82013-07-21 21:36:59 +020012198 if (!(encoder->compute_config(encoder, pipe_config))) {
12199 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012200 goto fail;
12201 }
12202 }
12203
Daniel Vetterff9a6752013-06-01 17:16:21 +020012204 /* Set default port clock if not overwritten by the encoder. Needs to be
12205 * done afterwards in case the encoder adjusts the mode. */
12206 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012207 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012208 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012209
Daniel Vettera43f6e02013-06-07 23:10:32 +020012210 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012211 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012212 DRM_DEBUG_KMS("CRTC fixup failed\n");
12213 goto fail;
12214 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012215
12216 if (ret == RETRY) {
12217 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12218 ret = -EINVAL;
12219 goto fail;
12220 }
12221
12222 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12223 retry = false;
12224 goto encoder_retry;
12225 }
12226
Daniel Vettere8fa4272015-08-12 11:43:34 +020012227 /* Dithering seems to not pass-through bits correctly when it should, so
12228 * only enable it on 6bpc panels. */
12229 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012230 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012231 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012232
Daniel Vetter7758a112012-07-08 19:40:39 +020012233fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012234 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012235}
12236
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012237static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012238intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012239{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012240 struct drm_crtc *crtc;
12241 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012242 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012243
Ville Syrjälä76688512014-01-10 11:28:06 +020012244 /* Double check state. */
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012245 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012246 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012247
12248 /* Update hwmode for vblank functions */
12249 if (crtc->state->active)
12250 crtc->hwmode = crtc->state->adjusted_mode;
12251 else
12252 crtc->hwmode.crtc_clock = 0;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012253 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012254}
12255
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012256static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012257{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012258 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012259
12260 if (clock1 == clock2)
12261 return true;
12262
12263 if (!clock1 || !clock2)
12264 return false;
12265
12266 diff = abs(clock1 - clock2);
12267
12268 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12269 return true;
12270
12271 return false;
12272}
12273
Daniel Vetter25c5b262012-07-08 22:08:04 +020012274#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12275 list_for_each_entry((intel_crtc), \
12276 &(dev)->mode_config.crtc_list, \
12277 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012278 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012279
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012280static bool
12281intel_compare_m_n(unsigned int m, unsigned int n,
12282 unsigned int m2, unsigned int n2,
12283 bool exact)
12284{
12285 if (m == m2 && n == n2)
12286 return true;
12287
12288 if (exact || !m || !n || !m2 || !n2)
12289 return false;
12290
12291 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12292
12293 if (m > m2) {
12294 while (m > m2) {
12295 m2 <<= 1;
12296 n2 <<= 1;
12297 }
12298 } else if (m < m2) {
12299 while (m < m2) {
12300 m <<= 1;
12301 n <<= 1;
12302 }
12303 }
12304
12305 return m == m2 && n == n2;
12306}
12307
12308static bool
12309intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12310 struct intel_link_m_n *m2_n2,
12311 bool adjust)
12312{
12313 if (m_n->tu == m2_n2->tu &&
12314 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12315 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12316 intel_compare_m_n(m_n->link_m, m_n->link_n,
12317 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12318 if (adjust)
12319 *m2_n2 = *m_n;
12320
12321 return true;
12322 }
12323
12324 return false;
12325}
12326
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012327static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012328intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012329 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012330 struct intel_crtc_state *pipe_config,
12331 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012332{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012333 bool ret = true;
12334
12335#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12336 do { \
12337 if (!adjust) \
12338 DRM_ERROR(fmt, ##__VA_ARGS__); \
12339 else \
12340 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12341 } while (0)
12342
Daniel Vetter66e985c2013-06-05 13:34:20 +020012343#define PIPE_CONF_CHECK_X(name) \
12344 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012345 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012346 "(expected 0x%08x, found 0x%08x)\n", \
12347 current_config->name, \
12348 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012349 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012350 }
12351
Daniel Vetter08a24032013-04-19 11:25:34 +020012352#define PIPE_CONF_CHECK_I(name) \
12353 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012354 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012355 "(expected %i, found %i)\n", \
12356 current_config->name, \
12357 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012358 ret = false; \
12359 }
12360
12361#define PIPE_CONF_CHECK_M_N(name) \
12362 if (!intel_compare_link_m_n(&current_config->name, \
12363 &pipe_config->name,\
12364 adjust)) { \
12365 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12366 "(expected tu %i gmch %i/%i link %i/%i, " \
12367 "found tu %i, gmch %i/%i link %i/%i)\n", \
12368 current_config->name.tu, \
12369 current_config->name.gmch_m, \
12370 current_config->name.gmch_n, \
12371 current_config->name.link_m, \
12372 current_config->name.link_n, \
12373 pipe_config->name.tu, \
12374 pipe_config->name.gmch_m, \
12375 pipe_config->name.gmch_n, \
12376 pipe_config->name.link_m, \
12377 pipe_config->name.link_n); \
12378 ret = false; \
12379 }
12380
12381#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12382 if (!intel_compare_link_m_n(&current_config->name, \
12383 &pipe_config->name, adjust) && \
12384 !intel_compare_link_m_n(&current_config->alt_name, \
12385 &pipe_config->name, adjust)) { \
12386 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12387 "(expected tu %i gmch %i/%i link %i/%i, " \
12388 "or tu %i gmch %i/%i link %i/%i, " \
12389 "found tu %i, gmch %i/%i link %i/%i)\n", \
12390 current_config->name.tu, \
12391 current_config->name.gmch_m, \
12392 current_config->name.gmch_n, \
12393 current_config->name.link_m, \
12394 current_config->name.link_n, \
12395 current_config->alt_name.tu, \
12396 current_config->alt_name.gmch_m, \
12397 current_config->alt_name.gmch_n, \
12398 current_config->alt_name.link_m, \
12399 current_config->alt_name.link_n, \
12400 pipe_config->name.tu, \
12401 pipe_config->name.gmch_m, \
12402 pipe_config->name.gmch_n, \
12403 pipe_config->name.link_m, \
12404 pipe_config->name.link_n); \
12405 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012406 }
12407
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012408/* This is required for BDW+ where there is only one set of registers for
12409 * switching between high and low RR.
12410 * This macro can be used whenever a comparison has to be made between one
12411 * hw state and multiple sw state variables.
12412 */
12413#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12414 if ((current_config->name != pipe_config->name) && \
12415 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012416 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012417 "(expected %i or %i, found %i)\n", \
12418 current_config->name, \
12419 current_config->alt_name, \
12420 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012421 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012422 }
12423
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012424#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12425 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012426 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012427 "(expected %i, found %i)\n", \
12428 current_config->name & (mask), \
12429 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012430 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012431 }
12432
Ville Syrjälä5e550652013-09-06 23:29:07 +030012433#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12434 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012435 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012436 "(expected %i, found %i)\n", \
12437 current_config->name, \
12438 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012439 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012440 }
12441
Daniel Vetterbb760062013-06-06 14:55:52 +020012442#define PIPE_CONF_QUIRK(quirk) \
12443 ((current_config->quirks | pipe_config->quirks) & (quirk))
12444
Daniel Vettereccb1402013-05-22 00:50:22 +020012445 PIPE_CONF_CHECK_I(cpu_transcoder);
12446
Daniel Vetter08a24032013-04-19 11:25:34 +020012447 PIPE_CONF_CHECK_I(has_pch_encoder);
12448 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012449 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012450
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012451 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012452 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012453
12454 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012455 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012456
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012457 PIPE_CONF_CHECK_I(has_drrs);
12458 if (current_config->has_drrs)
12459 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12460 } else
12461 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012462
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012463 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12464 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12465 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12466 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12467 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12468 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012469
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012470 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12471 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12472 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12473 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12474 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12475 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012476
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012477 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012478 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012479 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12480 IS_VALLEYVIEW(dev))
12481 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012482 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012483
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012484 PIPE_CONF_CHECK_I(has_audio);
12485
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012486 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012487 DRM_MODE_FLAG_INTERLACE);
12488
Daniel Vetterbb760062013-06-06 14:55:52 +020012489 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012490 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012491 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012492 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012493 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012494 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012495 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012496 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012497 DRM_MODE_FLAG_NVSYNC);
12498 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012499
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012500 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012501 /* pfit ratios are autocomputed by the hw on gen4+ */
12502 if (INTEL_INFO(dev)->gen < 4)
12503 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012504 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012505
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012506 if (!adjust) {
12507 PIPE_CONF_CHECK_I(pipe_src_w);
12508 PIPE_CONF_CHECK_I(pipe_src_h);
12509
12510 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12511 if (current_config->pch_pfit.enabled) {
12512 PIPE_CONF_CHECK_X(pch_pfit.pos);
12513 PIPE_CONF_CHECK_X(pch_pfit.size);
12514 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012515
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012516 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12517 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012518
Jesse Barnese59150d2014-01-07 13:30:45 -080012519 /* BDW+ don't expose a synchronous way to read the state */
12520 if (IS_HASWELL(dev))
12521 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012522
Ville Syrjälä282740f2013-09-04 18:30:03 +030012523 PIPE_CONF_CHECK_I(double_wide);
12524
Daniel Vetter26804af2014-06-25 22:01:55 +030012525 PIPE_CONF_CHECK_X(ddi_pll_sel);
12526
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012527 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012528 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012529 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012530 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12531 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012532 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012533 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12534 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12535 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012536
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012537 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12538 PIPE_CONF_CHECK_I(pipe_bpp);
12539
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012540 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012541 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012542
Daniel Vetter66e985c2013-06-05 13:34:20 +020012543#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012544#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012545#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012546#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012547#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012548#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012549#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012550
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012551 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012552}
12553
Damien Lespiau08db6652014-11-04 17:06:52 +000012554static void check_wm_state(struct drm_device *dev)
12555{
12556 struct drm_i915_private *dev_priv = dev->dev_private;
12557 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12558 struct intel_crtc *intel_crtc;
12559 int plane;
12560
12561 if (INTEL_INFO(dev)->gen < 9)
12562 return;
12563
12564 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12565 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12566
12567 for_each_intel_crtc(dev, intel_crtc) {
12568 struct skl_ddb_entry *hw_entry, *sw_entry;
12569 const enum pipe pipe = intel_crtc->pipe;
12570
12571 if (!intel_crtc->active)
12572 continue;
12573
12574 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012575 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012576 hw_entry = &hw_ddb.plane[pipe][plane];
12577 sw_entry = &sw_ddb->plane[pipe][plane];
12578
12579 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12580 continue;
12581
12582 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12583 "(expected (%u,%u), found (%u,%u))\n",
12584 pipe_name(pipe), plane + 1,
12585 sw_entry->start, sw_entry->end,
12586 hw_entry->start, hw_entry->end);
12587 }
12588
12589 /* cursor */
Matt Roper4969d332015-09-24 15:53:10 -070012590 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12591 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
Damien Lespiau08db6652014-11-04 17:06:52 +000012592
12593 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12594 continue;
12595
12596 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12597 "(expected (%u,%u), found (%u,%u))\n",
12598 pipe_name(pipe),
12599 sw_entry->start, sw_entry->end,
12600 hw_entry->start, hw_entry->end);
12601 }
12602}
12603
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012604static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012605check_connector_state(struct drm_device *dev,
12606 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012607{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012608 struct drm_connector_state *old_conn_state;
12609 struct drm_connector *connector;
12610 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012611
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012612 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12613 struct drm_encoder *encoder = connector->encoder;
12614 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012615
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012616 /* This also checks the encoder/connector hw state with the
12617 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012618 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012619
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012620 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012621 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012622 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012623}
12624
12625static void
12626check_encoder_state(struct drm_device *dev)
12627{
12628 struct intel_encoder *encoder;
12629 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012630
Damien Lespiaub2784e12014-08-05 11:29:37 +010012631 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012632 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012633 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012634
12635 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12636 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012637 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012638
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012639 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012640 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012641 continue;
12642 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012643
12644 I915_STATE_WARN(connector->base.state->crtc !=
12645 encoder->base.crtc,
12646 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012647 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012648
Rob Clarke2c719b2014-12-15 13:56:32 -050012649 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012650 "encoder's enabled state mismatch "
12651 "(expected %i, found %i)\n",
12652 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012653
12654 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012655 bool active;
12656
12657 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012658 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012659 "encoder detached but still enabled on pipe %c.\n",
12660 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012661 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012662 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012663}
12664
12665static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012666check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012667{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012668 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012669 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012670 struct drm_crtc_state *old_crtc_state;
12671 struct drm_crtc *crtc;
12672 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012673
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012674 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12675 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12676 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020012677 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012678
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012679 if (!needs_modeset(crtc->state) &&
12680 !to_intel_crtc_state(crtc->state)->update_pipe)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012681 continue;
12682
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012683 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12684 pipe_config = to_intel_crtc_state(old_crtc_state);
12685 memset(pipe_config, 0, sizeof(*pipe_config));
12686 pipe_config->base.crtc = crtc;
12687 pipe_config->base.state = old_state;
12688
12689 DRM_DEBUG_KMS("[CRTC:%d]\n",
12690 crtc->base.id);
12691
12692 active = dev_priv->display.get_pipe_config(intel_crtc,
12693 pipe_config);
12694
12695 /* hw state is inconsistent with the pipe quirk */
12696 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12697 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12698 active = crtc->state->active;
12699
12700 I915_STATE_WARN(crtc->state->active != active,
12701 "crtc active state doesn't match with hw state "
12702 "(expected %i, found %i)\n", crtc->state->active, active);
12703
12704 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
12705 "transitional active state does not match atomic hw state "
12706 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12707
12708 for_each_encoder_on_crtc(dev, crtc, encoder) {
12709 enum pipe pipe;
12710
12711 active = encoder->get_hw_state(encoder, &pipe);
12712 I915_STATE_WARN(active != crtc->state->active,
12713 "[ENCODER:%i] active %i with crtc active %i\n",
12714 encoder->base.base.id, active, crtc->state->active);
12715
12716 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12717 "Encoder connected to wrong pipe %c\n",
12718 pipe_name(pipe));
12719
12720 if (active)
12721 encoder->get_config(encoder, pipe_config);
12722 }
12723
12724 if (!crtc->state->active)
12725 continue;
12726
12727 sw_config = to_intel_crtc_state(crtc->state);
12728 if (!intel_pipe_config_compare(dev, sw_config,
12729 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012730 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012731 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012732 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012733 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012734 "[sw state]");
12735 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012736 }
12737}
12738
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012739static void
12740check_shared_dpll_state(struct drm_device *dev)
12741{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012742 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012743 struct intel_crtc *crtc;
12744 struct intel_dpll_hw_state dpll_hw_state;
12745 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012746
12747 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12748 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12749 int enabled_crtcs = 0, active_crtcs = 0;
12750 bool active;
12751
12752 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12753
12754 DRM_DEBUG_KMS("%s\n", pll->name);
12755
12756 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12757
Rob Clarke2c719b2014-12-15 13:56:32 -050012758 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012759 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012760 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012761 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012762 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012763 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012764 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012765 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012766 "pll on state mismatch (expected %i, found %i)\n",
12767 pll->on, active);
12768
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012769 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012770 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012771 enabled_crtcs++;
12772 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12773 active_crtcs++;
12774 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012775 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012776 "pll active crtcs mismatch (expected %i, found %i)\n",
12777 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012778 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012779 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012780 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012781
Rob Clarke2c719b2014-12-15 13:56:32 -050012782 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012783 sizeof(dpll_hw_state)),
12784 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012785 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012786}
12787
Maarten Lankhorstee165b12015-08-05 12:37:00 +020012788static void
12789intel_modeset_check_state(struct drm_device *dev,
12790 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012791{
Damien Lespiau08db6652014-11-04 17:06:52 +000012792 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012793 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012794 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012795 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012796 check_shared_dpll_state(dev);
12797}
12798
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012799void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012800 int dotclock)
12801{
12802 /*
12803 * FDI already provided one idea for the dotclock.
12804 * Yell if the encoder disagrees.
12805 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012806 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012807 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012808 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012809}
12810
Ville Syrjälä80715b22014-05-15 20:23:23 +030012811static void update_scanline_offset(struct intel_crtc *crtc)
12812{
12813 struct drm_device *dev = crtc->base.dev;
12814
12815 /*
12816 * The scanline counter increments at the leading edge of hsync.
12817 *
12818 * On most platforms it starts counting from vtotal-1 on the
12819 * first active line. That means the scanline counter value is
12820 * always one less than what we would expect. Ie. just after
12821 * start of vblank, which also occurs at start of hsync (on the
12822 * last active line), the scanline counter will read vblank_start-1.
12823 *
12824 * On gen2 the scanline counter starts counting from 1 instead
12825 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12826 * to keep the value positive), instead of adding one.
12827 *
12828 * On HSW+ the behaviour of the scanline counter depends on the output
12829 * type. For DP ports it behaves like most other platforms, but on HDMI
12830 * there's an extra 1 line difference. So we need to add two instead of
12831 * one to the value.
12832 */
12833 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030012834 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012835 int vtotal;
12836
Ville Syrjälä124abe02015-09-08 13:40:45 +030012837 vtotal = adjusted_mode->crtc_vtotal;
12838 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030012839 vtotal /= 2;
12840
12841 crtc->scanline_offset = vtotal - 1;
12842 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012843 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012844 crtc->scanline_offset = 2;
12845 } else
12846 crtc->scanline_offset = 1;
12847}
12848
Maarten Lankhorstad421372015-06-15 12:33:42 +020012849static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012850{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012851 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012852 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012853 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012854 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012855 struct intel_crtc_state *intel_crtc_state;
12856 struct drm_crtc *crtc;
12857 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012858 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012859
12860 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012861 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012862
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012863 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012864 int dpll;
12865
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012866 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012867 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012868 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012869
Maarten Lankhorstad421372015-06-15 12:33:42 +020012870 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012871 continue;
12872
Maarten Lankhorstad421372015-06-15 12:33:42 +020012873 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012874
Maarten Lankhorstad421372015-06-15 12:33:42 +020012875 if (!shared_dpll)
12876 shared_dpll = intel_atomic_get_shared_dpll_state(state);
12877
12878 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012879 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012880}
12881
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012882/*
12883 * This implements the workaround described in the "notes" section of the mode
12884 * set sequence documentation. When going from no pipes or single pipe to
12885 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12886 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12887 */
12888static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12889{
12890 struct drm_crtc_state *crtc_state;
12891 struct intel_crtc *intel_crtc;
12892 struct drm_crtc *crtc;
12893 struct intel_crtc_state *first_crtc_state = NULL;
12894 struct intel_crtc_state *other_crtc_state = NULL;
12895 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12896 int i;
12897
12898 /* look at all crtc's that are going to be enabled in during modeset */
12899 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12900 intel_crtc = to_intel_crtc(crtc);
12901
12902 if (!crtc_state->active || !needs_modeset(crtc_state))
12903 continue;
12904
12905 if (first_crtc_state) {
12906 other_crtc_state = to_intel_crtc_state(crtc_state);
12907 break;
12908 } else {
12909 first_crtc_state = to_intel_crtc_state(crtc_state);
12910 first_pipe = intel_crtc->pipe;
12911 }
12912 }
12913
12914 /* No workaround needed? */
12915 if (!first_crtc_state)
12916 return 0;
12917
12918 /* w/a possibly needed, check how many crtc's are already enabled. */
12919 for_each_intel_crtc(state->dev, intel_crtc) {
12920 struct intel_crtc_state *pipe_config;
12921
12922 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12923 if (IS_ERR(pipe_config))
12924 return PTR_ERR(pipe_config);
12925
12926 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12927
12928 if (!pipe_config->base.active ||
12929 needs_modeset(&pipe_config->base))
12930 continue;
12931
12932 /* 2 or more enabled crtcs means no need for w/a */
12933 if (enabled_pipe != INVALID_PIPE)
12934 return 0;
12935
12936 enabled_pipe = intel_crtc->pipe;
12937 }
12938
12939 if (enabled_pipe != INVALID_PIPE)
12940 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12941 else if (other_crtc_state)
12942 other_crtc_state->hsw_workaround_pipe = first_pipe;
12943
12944 return 0;
12945}
12946
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012947static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12948{
12949 struct drm_crtc *crtc;
12950 struct drm_crtc_state *crtc_state;
12951 int ret = 0;
12952
12953 /* add all active pipes to the state */
12954 for_each_crtc(state->dev, crtc) {
12955 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12956 if (IS_ERR(crtc_state))
12957 return PTR_ERR(crtc_state);
12958
12959 if (!crtc_state->active || needs_modeset(crtc_state))
12960 continue;
12961
12962 crtc_state->mode_changed = true;
12963
12964 ret = drm_atomic_add_affected_connectors(state, crtc);
12965 if (ret)
12966 break;
12967
12968 ret = drm_atomic_add_affected_planes(state, crtc);
12969 if (ret)
12970 break;
12971 }
12972
12973 return ret;
12974}
12975
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012976static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012977{
12978 struct drm_device *dev = state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012979 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012980 int ret;
12981
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012982 if (!check_digital_port_conflicts(state)) {
12983 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12984 return -EINVAL;
12985 }
12986
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012987 /*
12988 * See if the config requires any additional preparation, e.g.
12989 * to adjust global state with pipes off. We need to do this
12990 * here so we can get the modeset_pipe updated config for the new
12991 * mode set on this crtc. For other crtcs we need to use the
12992 * adjusted_mode bits in the crtc directly.
12993 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012994 if (dev_priv->display.modeset_calc_cdclk) {
12995 unsigned int cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030012996
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012997 ret = dev_priv->display.modeset_calc_cdclk(state);
12998
12999 cdclk = to_intel_atomic_state(state)->cdclk;
13000 if (!ret && cdclk != dev_priv->cdclk_freq)
13001 ret = intel_modeset_all_pipes(state);
13002
13003 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013004 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013005 } else
13006 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013007
Maarten Lankhorstad421372015-06-15 12:33:42 +020013008 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013009
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013010 if (IS_HASWELL(dev))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013011 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013012
Maarten Lankhorstad421372015-06-15 12:33:42 +020013013 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013014}
13015
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013016/**
13017 * intel_atomic_check - validate state object
13018 * @dev: drm device
13019 * @state: state to validate
13020 */
13021static int intel_atomic_check(struct drm_device *dev,
13022 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013023{
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013024 struct drm_crtc *crtc;
13025 struct drm_crtc_state *crtc_state;
13026 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013027 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013028
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013029 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013030 if (ret)
13031 return ret;
13032
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013033 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013034 struct intel_crtc_state *pipe_config =
13035 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013036
13037 /* Catch I915_MODE_FLAG_INHERITED */
13038 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13039 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013040
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013041 if (!crtc_state->enable) {
13042 if (needs_modeset(crtc_state))
13043 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013044 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013045 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013046
Daniel Vetter26495482015-07-15 14:15:52 +020013047 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013048 continue;
13049
Daniel Vetter26495482015-07-15 14:15:52 +020013050 /* FIXME: For only active_changed we shouldn't need to do any
13051 * state recomputation at all. */
13052
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013053 ret = drm_atomic_add_affected_connectors(state, crtc);
13054 if (ret)
13055 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013056
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013057 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013058 if (ret)
13059 return ret;
13060
Maarten Lankhorst6764e9f2015-08-27 15:44:06 +020013061 if (intel_pipe_config_compare(state->dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013062 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013063 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013064 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013065 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013066 }
13067
13068 if (needs_modeset(crtc_state)) {
13069 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013070
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013071 ret = drm_atomic_add_affected_planes(state, crtc);
13072 if (ret)
13073 return ret;
13074 }
13075
Daniel Vetter26495482015-07-15 14:15:52 +020013076 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13077 needs_modeset(crtc_state) ?
13078 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013079 }
13080
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013081 if (any_ms) {
13082 ret = intel_modeset_checks(state);
13083
13084 if (ret)
13085 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013086 } else
13087 to_intel_atomic_state(state)->cdclk =
13088 to_i915(state->dev)->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013089
13090 return drm_atomic_helper_check_planes(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013091}
13092
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013093/**
13094 * intel_atomic_commit - commit validated state object
13095 * @dev: DRM device
13096 * @state: the top-level driver state object
13097 * @async: asynchronous commit
13098 *
13099 * This function commits a top-level state object that has been validated
13100 * with drm_atomic_helper_check().
13101 *
13102 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13103 * we can only handle plane-related operations and do not yet support
13104 * asynchronous commit.
13105 *
13106 * RETURNS
13107 * Zero for success or -errno.
13108 */
13109static int intel_atomic_commit(struct drm_device *dev,
13110 struct drm_atomic_state *state,
13111 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013112{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013113 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013114 struct drm_crtc *crtc;
13115 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013116 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013117 int i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013118 bool any_ms = false;
Daniel Vettera6778b32012-07-02 09:56:42 +020013119
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013120 if (async) {
13121 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13122 return -EINVAL;
13123 }
13124
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013125 ret = drm_atomic_helper_prepare_planes(dev, state);
13126 if (ret)
13127 return ret;
13128
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013129 drm_atomic_helper_swap_state(dev, state);
13130
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013131 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013132 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13133
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013134 if (!needs_modeset(crtc->state))
13135 continue;
13136
13137 any_ms = true;
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013138 intel_pre_plane_update(intel_crtc);
Daniel Vetter460da9162013-03-27 00:44:51 +010013139
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013140 if (crtc_state->active) {
13141 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13142 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013143 intel_crtc->active = false;
13144 intel_disable_shared_dpll(intel_crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013145 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013146 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013147
Daniel Vetterea9d7582012-07-10 10:42:52 +020013148 /* Only after disabling all output pipelines that will be changed can we
13149 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013150 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013151
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013152 if (any_ms) {
13153 intel_shared_dpll_commit(state);
13154
13155 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013156 modeset_update_crtc_power_domains(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013157 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013158
Daniel Vettera6778b32012-07-02 09:56:42 +020013159 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013160 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013161 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13162 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013163 bool update_pipe = !modeset &&
13164 to_intel_crtc_state(crtc->state)->update_pipe;
13165 unsigned long put_domains = 0;
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013166
13167 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013168 update_scanline_offset(to_intel_crtc(crtc));
13169 dev_priv->display.crtc_enable(crtc);
13170 }
13171
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013172 if (update_pipe) {
13173 put_domains = modeset_get_crtc_power_domains(crtc);
13174
13175 /* make sure intel_modeset_check_state runs */
13176 any_ms = true;
13177 }
13178
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013179 if (!modeset)
13180 intel_pre_plane_update(intel_crtc);
13181
Maarten Lankhorst5ac1c4b2015-06-01 12:50:01 +020013182 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013183
13184 if (put_domains)
13185 modeset_put_power_domains(dev_priv, put_domains);
13186
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013187 intel_post_plane_update(intel_crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013188 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013189
Daniel Vettera6778b32012-07-02 09:56:42 +020013190 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013191
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013192 drm_atomic_helper_wait_for_vblanks(dev, state);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013193 drm_atomic_helper_cleanup_planes(dev, state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013194
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013195 if (any_ms)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013196 intel_modeset_check_state(dev, state);
13197
13198 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013199
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013200 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013201}
13202
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013203void intel_crtc_restore_mode(struct drm_crtc *crtc)
13204{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013205 struct drm_device *dev = crtc->dev;
13206 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013207 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013208 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013209
13210 state = drm_atomic_state_alloc(dev);
13211 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013212 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013213 crtc->base.id);
13214 return;
13215 }
13216
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013217 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013218
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013219retry:
13220 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13221 ret = PTR_ERR_OR_ZERO(crtc_state);
13222 if (!ret) {
13223 if (!crtc_state->active)
13224 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013225
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013226 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013227 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013228 }
13229
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013230 if (ret == -EDEADLK) {
13231 drm_atomic_state_clear(state);
13232 drm_modeset_backoff(state->acquire_ctx);
13233 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013234 }
13235
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013236 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013237out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013238 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013239}
13240
Daniel Vetter25c5b262012-07-08 22:08:04 +020013241#undef for_each_intel_crtc_masked
13242
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013243static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013244 .gamma_set = intel_crtc_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013245 .set_config = drm_atomic_helper_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013246 .destroy = intel_crtc_destroy,
13247 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013248 .atomic_duplicate_state = intel_crtc_duplicate_state,
13249 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013250};
13251
Daniel Vetter53589012013-06-05 13:34:16 +020013252static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13253 struct intel_shared_dpll *pll,
13254 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013255{
Daniel Vetter53589012013-06-05 13:34:16 +020013256 uint32_t val;
13257
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013258 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013259 return false;
13260
Daniel Vetter53589012013-06-05 13:34:16 +020013261 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013262 hw_state->dpll = val;
13263 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13264 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013265
13266 return val & DPLL_VCO_ENABLE;
13267}
13268
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013269static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13270 struct intel_shared_dpll *pll)
13271{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013272 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13273 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013274}
13275
Daniel Vettere7b903d2013-06-05 13:34:14 +020013276static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13277 struct intel_shared_dpll *pll)
13278{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013279 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013280 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013281
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013282 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013283
13284 /* Wait for the clocks to stabilize. */
13285 POSTING_READ(PCH_DPLL(pll->id));
13286 udelay(150);
13287
13288 /* The pixel multiplier can only be updated once the
13289 * DPLL is enabled and the clocks are stable.
13290 *
13291 * So write it again.
13292 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013293 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013294 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013295 udelay(200);
13296}
13297
13298static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13299 struct intel_shared_dpll *pll)
13300{
13301 struct drm_device *dev = dev_priv->dev;
13302 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013303
13304 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013305 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013306 if (intel_crtc_to_shared_dpll(crtc) == pll)
13307 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13308 }
13309
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013310 I915_WRITE(PCH_DPLL(pll->id), 0);
13311 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013312 udelay(200);
13313}
13314
Daniel Vetter46edb022013-06-05 13:34:12 +020013315static char *ibx_pch_dpll_names[] = {
13316 "PCH DPLL A",
13317 "PCH DPLL B",
13318};
13319
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013320static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013321{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013322 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013323 int i;
13324
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013325 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013326
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013327 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013328 dev_priv->shared_dplls[i].id = i;
13329 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013330 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013331 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13332 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013333 dev_priv->shared_dplls[i].get_hw_state =
13334 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013335 }
13336}
13337
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013338static void intel_shared_dpll_init(struct drm_device *dev)
13339{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013340 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013341
Daniel Vetter9cd86932014-06-25 22:01:57 +030013342 if (HAS_DDI(dev))
13343 intel_ddi_pll_init(dev);
13344 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013345 ibx_pch_dpll_init(dev);
13346 else
13347 dev_priv->num_shared_dpll = 0;
13348
13349 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013350}
13351
Matt Roper6beb8c232014-12-01 15:40:14 -080013352/**
13353 * intel_prepare_plane_fb - Prepare fb for usage on plane
13354 * @plane: drm plane to prepare for
13355 * @fb: framebuffer to prepare for presentation
13356 *
13357 * Prepares a framebuffer for usage on a display plane. Generally this
13358 * involves pinning the underlying object and updating the frontbuffer tracking
13359 * bits. Some older platforms need special physical address handling for
13360 * cursor planes.
13361 *
13362 * Returns 0 on success, negative error code on failure.
13363 */
13364int
13365intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013366 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013367{
13368 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013369 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013370 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013371 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13372 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013373 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013374
Matt Roperea2c67b2014-12-23 10:41:52 -080013375 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013376 return 0;
13377
Matt Roper4c345742014-07-09 16:22:10 -070013378 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013379
Matt Roper6beb8c232014-12-01 15:40:14 -080013380 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13381 INTEL_INFO(dev)->cursor_needs_physical) {
13382 int align = IS_I830(dev) ? 16 * 1024 : 256;
13383 ret = i915_gem_object_attach_phys(obj, align);
13384 if (ret)
13385 DRM_DEBUG_KMS("failed to attach phys object\n");
13386 } else {
John Harrison91af1272015-06-18 13:14:56 +010013387 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013388 }
13389
13390 if (ret == 0)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013391 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Matt Roper6beb8c232014-12-01 15:40:14 -080013392
13393 mutex_unlock(&dev->struct_mutex);
13394
13395 return ret;
13396}
13397
Matt Roper38f3ce32014-12-02 07:45:25 -080013398/**
13399 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13400 * @plane: drm plane to clean up for
13401 * @fb: old framebuffer that was on plane
13402 *
13403 * Cleans up a framebuffer that has just been removed from a plane.
13404 */
13405void
13406intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013407 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013408{
13409 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013410 struct drm_i915_gem_object *obj = intel_fb_obj(old_state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013411
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013412 if (!obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013413 return;
13414
13415 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13416 !INTEL_INFO(dev)->cursor_needs_physical) {
13417 mutex_lock(&dev->struct_mutex);
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013418 intel_unpin_fb_obj(old_state->fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013419 mutex_unlock(&dev->struct_mutex);
13420 }
Matt Roper465c1202014-05-29 08:06:54 -070013421}
13422
Chandra Konduru6156a452015-04-27 13:48:39 -070013423int
13424skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13425{
13426 int max_scale;
13427 struct drm_device *dev;
13428 struct drm_i915_private *dev_priv;
13429 int crtc_clock, cdclk;
13430
13431 if (!intel_crtc || !crtc_state)
13432 return DRM_PLANE_HELPER_NO_SCALING;
13433
13434 dev = intel_crtc->base.dev;
13435 dev_priv = dev->dev_private;
13436 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013437 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013438
13439 if (!crtc_clock || !cdclk)
13440 return DRM_PLANE_HELPER_NO_SCALING;
13441
13442 /*
13443 * skl max scale is lower of:
13444 * close to 3 but not 3, -1 is for that purpose
13445 * or
13446 * cdclk/crtc_clock
13447 */
13448 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13449
13450 return max_scale;
13451}
13452
Matt Roper465c1202014-05-29 08:06:54 -070013453static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013454intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013455 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013456 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013457{
Matt Roper2b875c22014-12-01 15:40:13 -080013458 struct drm_crtc *crtc = state->base.crtc;
13459 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013460 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013461 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13462 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013463
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013464 /* use scaler when colorkey is not required */
13465 if (INTEL_INFO(plane->dev)->gen >= 9 &&
Maarten Lankhorst818ed962015-06-15 12:33:54 +020013466 state->ckey.flags == I915_SET_COLORKEY_NONE) {
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013467 min_scale = 1;
13468 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013469 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013470 }
Sonika Jindald8106362015-04-10 14:37:28 +053013471
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013472 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13473 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013474 min_scale, max_scale,
13475 can_position, true,
13476 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013477}
13478
Gustavo Padovan14af2932014-10-24 14:51:31 +010013479static void
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013480intel_commit_primary_plane(struct drm_plane *plane,
13481 struct intel_plane_state *state)
13482{
Matt Roper2b875c22014-12-01 15:40:13 -080013483 struct drm_crtc *crtc = state->base.crtc;
13484 struct drm_framebuffer *fb = state->base.fb;
13485 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013486 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013487 struct intel_crtc *intel_crtc;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013488 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013489
Matt Roperea2c67b2014-12-23 10:41:52 -080013490 crtc = crtc ? crtc : plane->crtc;
13491 intel_crtc = to_intel_crtc(crtc);
13492
Matt Ropercf4c7c12014-12-04 10:27:42 -080013493 plane->fb = fb;
Matt Roper9dc806f2014-11-17 18:10:38 -080013494 crtc->x = src->x1 >> 16;
13495 crtc->y = src->y1 >> 16;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013496
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013497 if (!crtc->state->active)
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013498 return;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013499
Maarten Lankhorstd4b08632015-09-10 16:07:56 +020013500 dev_priv->display.update_primary_plane(crtc, fb,
13501 state->src.x1 >> 16,
13502 state->src.y1 >> 16);
Matt Roper32b7eee2014-12-24 07:59:06 -080013503}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013504
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013505static void
13506intel_disable_primary_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013507 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013508{
13509 struct drm_device *dev = plane->dev;
13510 struct drm_i915_private *dev_priv = dev->dev_private;
13511
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013512 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13513}
13514
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013515static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13516 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013517{
13518 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013519 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013520 struct intel_crtc_state *old_intel_state =
13521 to_intel_crtc_state(old_crtc_state);
13522 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013523
Ville Syrjäläf015c552015-06-24 22:00:02 +030013524 if (intel_crtc->atomic.update_wm_pre)
Matt Roper32b7eee2014-12-24 07:59:06 -080013525 intel_update_watermarks(crtc);
13526
Matt Roperc34c9ee2014-12-23 10:41:50 -080013527 /* Perform vblank evasion around commit operation */
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013528 if (crtc->state->active)
Maarten Lankhorst34e0adb2015-08-31 13:04:25 +020013529 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013530
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013531 if (modeset)
13532 return;
13533
13534 if (to_intel_crtc_state(crtc->state)->update_pipe)
13535 intel_update_pipe_config(intel_crtc, old_intel_state);
13536 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020013537 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013538}
13539
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013540static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13541 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013542{
Matt Roper32b7eee2014-12-24 07:59:06 -080013543 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013544
Maarten Lankhorst8f539a82015-07-13 16:30:32 +020013545 if (crtc->state->active)
Maarten Lankhorst34e0adb2015-08-31 13:04:25 +020013546 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013547}
13548
Matt Ropercf4c7c12014-12-04 10:27:42 -080013549/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013550 * intel_plane_destroy - destroy a plane
13551 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013552 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013553 * Common destruction function for all types of planes (primary, cursor,
13554 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013555 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013556void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013557{
13558 struct intel_plane *intel_plane = to_intel_plane(plane);
13559 drm_plane_cleanup(plane);
13560 kfree(intel_plane);
13561}
13562
Matt Roper65a3fea2015-01-21 16:35:42 -080013563const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013564 .update_plane = drm_atomic_helper_update_plane,
13565 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013566 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013567 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013568 .atomic_get_property = intel_plane_atomic_get_property,
13569 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013570 .atomic_duplicate_state = intel_plane_duplicate_state,
13571 .atomic_destroy_state = intel_plane_destroy_state,
13572
Matt Roper465c1202014-05-29 08:06:54 -070013573};
13574
13575static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13576 int pipe)
13577{
13578 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013579 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013580 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013581 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070013582
13583 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13584 if (primary == NULL)
13585 return NULL;
13586
Matt Roper8e7d6882015-01-21 16:35:41 -080013587 state = intel_create_plane_state(&primary->base);
13588 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013589 kfree(primary);
13590 return NULL;
13591 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013592 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013593
Matt Roper465c1202014-05-29 08:06:54 -070013594 primary->can_scale = false;
13595 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013596 if (INTEL_INFO(dev)->gen >= 9) {
13597 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013598 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013599 }
Matt Roper465c1202014-05-29 08:06:54 -070013600 primary->pipe = pipe;
13601 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013602 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013603 primary->check_plane = intel_check_primary_plane;
13604 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013605 primary->disable_plane = intel_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013606 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13607 primary->plane = !pipe;
13608
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013609 if (INTEL_INFO(dev)->gen >= 9) {
13610 intel_primary_formats = skl_primary_formats;
13611 num_formats = ARRAY_SIZE(skl_primary_formats);
13612 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013613 intel_primary_formats = i965_primary_formats;
13614 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013615 } else {
13616 intel_primary_formats = i8xx_primary_formats;
13617 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013618 }
13619
13620 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013621 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013622 intel_primary_formats, num_formats,
13623 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013624
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013625 if (INTEL_INFO(dev)->gen >= 4)
13626 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013627
Matt Roperea2c67b2014-12-23 10:41:52 -080013628 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13629
Matt Roper465c1202014-05-29 08:06:54 -070013630 return &primary->base;
13631}
13632
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013633void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13634{
13635 if (!dev->mode_config.rotation_property) {
13636 unsigned long flags = BIT(DRM_ROTATE_0) |
13637 BIT(DRM_ROTATE_180);
13638
13639 if (INTEL_INFO(dev)->gen >= 9)
13640 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13641
13642 dev->mode_config.rotation_property =
13643 drm_mode_create_rotation_property(dev, flags);
13644 }
13645 if (dev->mode_config.rotation_property)
13646 drm_object_attach_property(&plane->base.base,
13647 dev->mode_config.rotation_property,
13648 plane->base.state->rotation);
13649}
13650
Matt Roper3d7d6512014-06-10 08:28:13 -070013651static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013652intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013653 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013654 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013655{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013656 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013657 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013658 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013659 unsigned stride;
13660 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013661
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013662 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13663 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013664 DRM_PLANE_HELPER_NO_SCALING,
13665 DRM_PLANE_HELPER_NO_SCALING,
13666 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013667 if (ret)
13668 return ret;
13669
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013670 /* if we want to turn off the cursor ignore width and height */
13671 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013672 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013673
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013674 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013675 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013676 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13677 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013678 return -EINVAL;
13679 }
13680
Matt Roperea2c67b2014-12-23 10:41:52 -080013681 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13682 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013683 DRM_DEBUG_KMS("buffer is too small\n");
13684 return -ENOMEM;
13685 }
13686
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013687 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013688 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013689 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013690 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013691
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013692 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013693}
13694
Matt Roperf4a2cf22014-12-01 15:40:12 -080013695static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013696intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013697 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013698{
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013699 intel_crtc_update_cursor(crtc, false);
13700}
13701
13702static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013703intel_commit_cursor_plane(struct drm_plane *plane,
13704 struct intel_plane_state *state)
13705{
Matt Roper2b875c22014-12-01 15:40:13 -080013706 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013707 struct drm_device *dev = plane->dev;
13708 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013709 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013710 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013711
Matt Roperea2c67b2014-12-23 10:41:52 -080013712 crtc = crtc ? crtc : plane->crtc;
13713 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013714
Gustavo Padovana912f122014-12-01 15:40:10 -080013715 if (intel_crtc->cursor_bo == obj)
13716 goto update;
13717
Matt Roperf4a2cf22014-12-01 15:40:12 -080013718 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013719 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013720 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013721 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013722 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013723 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013724
Gustavo Padovana912f122014-12-01 15:40:10 -080013725 intel_crtc->cursor_addr = addr;
13726 intel_crtc->cursor_bo = obj;
Gustavo Padovana912f122014-12-01 15:40:10 -080013727
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013728update:
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013729 if (crtc->state->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013730 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013731}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013732
Matt Roper3d7d6512014-06-10 08:28:13 -070013733static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13734 int pipe)
13735{
13736 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013737 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013738
13739 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13740 if (cursor == NULL)
13741 return NULL;
13742
Matt Roper8e7d6882015-01-21 16:35:41 -080013743 state = intel_create_plane_state(&cursor->base);
13744 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013745 kfree(cursor);
13746 return NULL;
13747 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013748 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013749
Matt Roper3d7d6512014-06-10 08:28:13 -070013750 cursor->can_scale = false;
13751 cursor->max_downscale = 1;
13752 cursor->pipe = pipe;
13753 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013754 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013755 cursor->check_plane = intel_check_cursor_plane;
13756 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013757 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013758
13759 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013760 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013761 intel_cursor_formats,
13762 ARRAY_SIZE(intel_cursor_formats),
13763 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013764
13765 if (INTEL_INFO(dev)->gen >= 4) {
13766 if (!dev->mode_config.rotation_property)
13767 dev->mode_config.rotation_property =
13768 drm_mode_create_rotation_property(dev,
13769 BIT(DRM_ROTATE_0) |
13770 BIT(DRM_ROTATE_180));
13771 if (dev->mode_config.rotation_property)
13772 drm_object_attach_property(&cursor->base.base,
13773 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013774 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013775 }
13776
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013777 if (INTEL_INFO(dev)->gen >=9)
13778 state->scaler_id = -1;
13779
Matt Roperea2c67b2014-12-23 10:41:52 -080013780 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13781
Matt Roper3d7d6512014-06-10 08:28:13 -070013782 return &cursor->base;
13783}
13784
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013785static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13786 struct intel_crtc_state *crtc_state)
13787{
13788 int i;
13789 struct intel_scaler *intel_scaler;
13790 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13791
13792 for (i = 0; i < intel_crtc->num_scalers; i++) {
13793 intel_scaler = &scaler_state->scalers[i];
13794 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013795 intel_scaler->mode = PS_SCALER_MODE_DYN;
13796 }
13797
13798 scaler_state->scaler_id = -1;
13799}
13800
Hannes Ederb358d0a2008-12-18 21:18:47 +010013801static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013802{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013803 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013804 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013805 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013806 struct drm_plane *primary = NULL;
13807 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013808 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013809
Daniel Vetter955382f2013-09-19 14:05:45 +020013810 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013811 if (intel_crtc == NULL)
13812 return;
13813
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013814 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13815 if (!crtc_state)
13816 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013817 intel_crtc->config = crtc_state;
13818 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013819 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013820
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013821 /* initialize shared scalers */
13822 if (INTEL_INFO(dev)->gen >= 9) {
13823 if (pipe == PIPE_C)
13824 intel_crtc->num_scalers = 1;
13825 else
13826 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13827
13828 skl_init_scalers(dev, intel_crtc, crtc_state);
13829 }
13830
Matt Roper465c1202014-05-29 08:06:54 -070013831 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013832 if (!primary)
13833 goto fail;
13834
13835 cursor = intel_cursor_plane_create(dev, pipe);
13836 if (!cursor)
13837 goto fail;
13838
Matt Roper465c1202014-05-29 08:06:54 -070013839 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013840 cursor, &intel_crtc_funcs);
13841 if (ret)
13842 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013843
13844 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013845 for (i = 0; i < 256; i++) {
13846 intel_crtc->lut_r[i] = i;
13847 intel_crtc->lut_g[i] = i;
13848 intel_crtc->lut_b[i] = i;
13849 }
13850
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013851 /*
13852 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013853 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013854 */
Jesse Barnes80824002009-09-10 15:28:06 -070013855 intel_crtc->pipe = pipe;
13856 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013857 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013858 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013859 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013860 }
13861
Chris Wilson4b0e3332014-05-30 16:35:26 +030013862 intel_crtc->cursor_base = ~0;
13863 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013864 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013865
Ville Syrjälä852eb002015-06-24 22:00:07 +030013866 intel_crtc->wm.cxsr_allowed = true;
13867
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013868 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13869 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13870 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13871 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13872
Jesse Barnes79e53942008-11-07 14:24:08 -080013873 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013874
13875 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013876 return;
13877
13878fail:
13879 if (primary)
13880 drm_plane_cleanup(primary);
13881 if (cursor)
13882 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013883 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013884 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013885}
13886
Jesse Barnes752aa882013-10-31 18:55:49 +020013887enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13888{
13889 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013890 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013891
Rob Clark51fd3712013-11-19 12:10:12 -050013892 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013893
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013894 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013895 return INVALID_PIPE;
13896
13897 return to_intel_crtc(encoder->crtc)->pipe;
13898}
13899
Carl Worth08d7b3d2009-04-29 14:43:54 -070013900int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013901 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013902{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013903 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013904 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013905 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013906
Rob Clark7707e652014-07-17 23:30:04 -040013907 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013908
Rob Clark7707e652014-07-17 23:30:04 -040013909 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013910 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013911 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013912 }
13913
Rob Clark7707e652014-07-17 23:30:04 -040013914 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013915 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013916
Daniel Vetterc05422d2009-08-11 16:05:30 +020013917 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013918}
13919
Daniel Vetter66a92782012-07-12 20:08:18 +020013920static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013921{
Daniel Vetter66a92782012-07-12 20:08:18 +020013922 struct drm_device *dev = encoder->base.dev;
13923 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013924 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013925 int entry = 0;
13926
Damien Lespiaub2784e12014-08-05 11:29:37 +010013927 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013928 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013929 index_mask |= (1 << entry);
13930
Jesse Barnes79e53942008-11-07 14:24:08 -080013931 entry++;
13932 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013933
Jesse Barnes79e53942008-11-07 14:24:08 -080013934 return index_mask;
13935}
13936
Chris Wilson4d302442010-12-14 19:21:29 +000013937static bool has_edp_a(struct drm_device *dev)
13938{
13939 struct drm_i915_private *dev_priv = dev->dev_private;
13940
13941 if (!IS_MOBILE(dev))
13942 return false;
13943
13944 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13945 return false;
13946
Damien Lespiaue3589902014-02-07 19:12:50 +000013947 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013948 return false;
13949
13950 return true;
13951}
13952
Jesse Barnes84b4e042014-06-25 08:24:29 -070013953static bool intel_crt_present(struct drm_device *dev)
13954{
13955 struct drm_i915_private *dev_priv = dev->dev_private;
13956
Damien Lespiau884497e2013-12-03 13:56:23 +000013957 if (INTEL_INFO(dev)->gen >= 9)
13958 return false;
13959
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013960 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013961 return false;
13962
13963 if (IS_CHERRYVIEW(dev))
13964 return false;
13965
13966 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13967 return false;
13968
13969 return true;
13970}
13971
Jesse Barnes79e53942008-11-07 14:24:08 -080013972static void intel_setup_outputs(struct drm_device *dev)
13973{
Eric Anholt725e30a2009-01-22 13:01:02 -080013974 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013975 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013976 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013977
Daniel Vetterc9093352013-06-06 22:22:47 +020013978 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013979
Jesse Barnes84b4e042014-06-25 08:24:29 -070013980 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013981 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013982
Vandana Kannanc776eb22014-08-19 12:05:01 +053013983 if (IS_BROXTON(dev)) {
13984 /*
13985 * FIXME: Broxton doesn't support port detection via the
13986 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13987 * detect the ports.
13988 */
13989 intel_ddi_init(dev, PORT_A);
13990 intel_ddi_init(dev, PORT_B);
13991 intel_ddi_init(dev, PORT_C);
13992 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013993 int found;
13994
Jesse Barnesde31fac2015-03-06 15:53:32 -080013995 /*
13996 * Haswell uses DDI functions to detect digital outputs.
13997 * On SKL pre-D0 the strap isn't connected, so we assume
13998 * it's there.
13999 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014000 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014001 /* WaIgnoreDDIAStrap: skl */
Jani Nikula5a2376d2015-08-14 10:53:17 +030014002 if (found || IS_SKYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014003 intel_ddi_init(dev, PORT_A);
14004
14005 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14006 * register */
14007 found = I915_READ(SFUSE_STRAP);
14008
14009 if (found & SFUSE_STRAP_DDIB_DETECTED)
14010 intel_ddi_init(dev, PORT_B);
14011 if (found & SFUSE_STRAP_DDIC_DETECTED)
14012 intel_ddi_init(dev, PORT_C);
14013 if (found & SFUSE_STRAP_DDID_DETECTED)
14014 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014015 /*
14016 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14017 */
14018 if (IS_SKYLAKE(dev) &&
14019 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14020 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14021 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14022 intel_ddi_init(dev, PORT_E);
14023
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014024 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014025 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014026 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014027
14028 if (has_edp_a(dev))
14029 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014030
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014031 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014032 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010014033 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014034 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014035 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014036 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014037 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014038 }
14039
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014040 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014041 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014042
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014043 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014044 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014045
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014046 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014047 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014048
Daniel Vetter270b3042012-10-27 15:52:05 +020014049 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014050 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014051 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014052 /*
14053 * The DP_DETECTED bit is the latched state of the DDC
14054 * SDA pin at boot. However since eDP doesn't require DDC
14055 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14056 * eDP ports may have been muxed to an alternate function.
14057 * Thus we can't rely on the DP_DETECTED bit alone to detect
14058 * eDP ports. Consult the VBT as well as DP_DETECTED to
14059 * detect eDP ports.
14060 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014061 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014062 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014063 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14064 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014065 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014066 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014067
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014068 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014069 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014070 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14071 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014072 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014073 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014074
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014075 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014076 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014077 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14078 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14079 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14080 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014081 }
14082
Jani Nikula3cfca972013-08-27 15:12:26 +030014083 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014084 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014085 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014086
Paulo Zanonie2debe92013-02-18 19:00:27 -030014087 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014088 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014089 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014090 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014091 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014092 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014093 }
Ma Ling27185ae2009-08-24 13:50:23 +080014094
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014095 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014096 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014097 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014098
14099 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014100
Paulo Zanonie2debe92013-02-18 19:00:27 -030014101 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014102 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014103 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014104 }
Ma Ling27185ae2009-08-24 13:50:23 +080014105
Paulo Zanonie2debe92013-02-18 19:00:27 -030014106 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014107
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014108 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014109 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014110 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014111 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014112 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014113 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014114 }
Ma Ling27185ae2009-08-24 13:50:23 +080014115
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014116 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014117 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014118 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014119 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014120 intel_dvo_init(dev);
14121
Zhenyu Wang103a1962009-11-27 11:44:36 +080014122 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014123 intel_tv_init(dev);
14124
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014125 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014126
Damien Lespiaub2784e12014-08-05 11:29:37 +010014127 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014128 encoder->base.possible_crtcs = encoder->crtc_mask;
14129 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014130 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014131 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014132
Paulo Zanonidde86e22012-12-01 12:04:25 -020014133 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014134
14135 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014136}
14137
14138static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14139{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014140 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014141 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014142
Daniel Vetteref2d6332014-02-10 18:00:38 +010014143 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014144 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014145 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014146 drm_gem_object_unreference(&intel_fb->obj->base);
14147 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014148 kfree(intel_fb);
14149}
14150
14151static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014152 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014153 unsigned int *handle)
14154{
14155 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014156 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014157
Chris Wilson05394f32010-11-08 19:18:58 +000014158 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014159}
14160
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014161static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14162 struct drm_file *file,
14163 unsigned flags, unsigned color,
14164 struct drm_clip_rect *clips,
14165 unsigned num_clips)
14166{
14167 struct drm_device *dev = fb->dev;
14168 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14169 struct drm_i915_gem_object *obj = intel_fb->obj;
14170
14171 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014172 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014173 mutex_unlock(&dev->struct_mutex);
14174
14175 return 0;
14176}
14177
Jesse Barnes79e53942008-11-07 14:24:08 -080014178static const struct drm_framebuffer_funcs intel_fb_funcs = {
14179 .destroy = intel_user_framebuffer_destroy,
14180 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014181 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014182};
14183
Damien Lespiaub3218032015-02-27 11:15:18 +000014184static
14185u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14186 uint32_t pixel_format)
14187{
14188 u32 gen = INTEL_INFO(dev)->gen;
14189
14190 if (gen >= 9) {
14191 /* "The stride in bytes must not exceed the of the size of 8K
14192 * pixels and 32K bytes."
14193 */
14194 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14195 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14196 return 32*1024;
14197 } else if (gen >= 4) {
14198 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14199 return 16*1024;
14200 else
14201 return 32*1024;
14202 } else if (gen >= 3) {
14203 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14204 return 8*1024;
14205 else
14206 return 16*1024;
14207 } else {
14208 /* XXX DSPC is limited to 4k tiled */
14209 return 8*1024;
14210 }
14211}
14212
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014213static int intel_framebuffer_init(struct drm_device *dev,
14214 struct intel_framebuffer *intel_fb,
14215 struct drm_mode_fb_cmd2 *mode_cmd,
14216 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014217{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014218 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014219 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014220 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014221
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014222 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14223
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014224 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14225 /* Enforce that fb modifier and tiling mode match, but only for
14226 * X-tiled. This is needed for FBC. */
14227 if (!!(obj->tiling_mode == I915_TILING_X) !=
14228 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14229 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14230 return -EINVAL;
14231 }
14232 } else {
14233 if (obj->tiling_mode == I915_TILING_X)
14234 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14235 else if (obj->tiling_mode == I915_TILING_Y) {
14236 DRM_DEBUG("No Y tiling for legacy addfb\n");
14237 return -EINVAL;
14238 }
14239 }
14240
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014241 /* Passed in modifier sanity checking. */
14242 switch (mode_cmd->modifier[0]) {
14243 case I915_FORMAT_MOD_Y_TILED:
14244 case I915_FORMAT_MOD_Yf_TILED:
14245 if (INTEL_INFO(dev)->gen < 9) {
14246 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14247 mode_cmd->modifier[0]);
14248 return -EINVAL;
14249 }
14250 case DRM_FORMAT_MOD_NONE:
14251 case I915_FORMAT_MOD_X_TILED:
14252 break;
14253 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014254 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14255 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014256 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014257 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014258
Damien Lespiaub3218032015-02-27 11:15:18 +000014259 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14260 mode_cmd->pixel_format);
14261 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14262 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14263 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014264 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014265 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014266
Damien Lespiaub3218032015-02-27 11:15:18 +000014267 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14268 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014269 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014270 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14271 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014272 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014273 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014274 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014275 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014276
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014277 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014278 mode_cmd->pitches[0] != obj->stride) {
14279 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14280 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014281 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014282 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014283
Ville Syrjälä57779d02012-10-31 17:50:14 +020014284 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014285 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014286 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014287 case DRM_FORMAT_RGB565:
14288 case DRM_FORMAT_XRGB8888:
14289 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014290 break;
14291 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014292 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014293 DRM_DEBUG("unsupported pixel format: %s\n",
14294 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014295 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014296 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014297 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014298 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014299 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14300 DRM_DEBUG("unsupported pixel format: %s\n",
14301 drm_get_format_name(mode_cmd->pixel_format));
14302 return -EINVAL;
14303 }
14304 break;
14305 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014306 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014307 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014308 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014309 DRM_DEBUG("unsupported pixel format: %s\n",
14310 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014311 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014312 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014313 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014314 case DRM_FORMAT_ABGR2101010:
14315 if (!IS_VALLEYVIEW(dev)) {
14316 DRM_DEBUG("unsupported pixel format: %s\n",
14317 drm_get_format_name(mode_cmd->pixel_format));
14318 return -EINVAL;
14319 }
14320 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014321 case DRM_FORMAT_YUYV:
14322 case DRM_FORMAT_UYVY:
14323 case DRM_FORMAT_YVYU:
14324 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014325 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014326 DRM_DEBUG("unsupported pixel format: %s\n",
14327 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014328 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014329 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014330 break;
14331 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014332 DRM_DEBUG("unsupported pixel format: %s\n",
14333 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014334 return -EINVAL;
14335 }
14336
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014337 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14338 if (mode_cmd->offsets[0] != 0)
14339 return -EINVAL;
14340
Damien Lespiauec2c9812015-01-20 12:51:45 +000014341 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014342 mode_cmd->pixel_format,
14343 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014344 /* FIXME drm helper for size checks (especially planar formats)? */
14345 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14346 return -EINVAL;
14347
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014348 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14349 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014350 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014351
Jesse Barnes79e53942008-11-07 14:24:08 -080014352 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14353 if (ret) {
14354 DRM_ERROR("framebuffer init failed %d\n", ret);
14355 return ret;
14356 }
14357
Jesse Barnes79e53942008-11-07 14:24:08 -080014358 return 0;
14359}
14360
Jesse Barnes79e53942008-11-07 14:24:08 -080014361static struct drm_framebuffer *
14362intel_user_framebuffer_create(struct drm_device *dev,
14363 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014364 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014365{
Chris Wilson05394f32010-11-08 19:18:58 +000014366 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014367
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014368 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14369 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014370 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014371 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014372
Chris Wilsond2dff872011-04-19 08:36:26 +010014373 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014374}
14375
Daniel Vetter06957262015-08-10 13:34:08 +020014376#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014377static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014378{
14379}
14380#endif
14381
Jesse Barnes79e53942008-11-07 14:24:08 -080014382static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014383 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014384 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014385 .atomic_check = intel_atomic_check,
14386 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014387 .atomic_state_alloc = intel_atomic_state_alloc,
14388 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014389};
14390
Jesse Barnese70236a2009-09-21 10:42:27 -070014391/* Set up chip specific display functions */
14392static void intel_init_display(struct drm_device *dev)
14393{
14394 struct drm_i915_private *dev_priv = dev->dev_private;
14395
Daniel Vetteree9300b2013-06-03 22:40:22 +020014396 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14397 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014398 else if (IS_CHERRYVIEW(dev))
14399 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014400 else if (IS_VALLEYVIEW(dev))
14401 dev_priv->display.find_dpll = vlv_find_best_dpll;
14402 else if (IS_PINEVIEW(dev))
14403 dev_priv->display.find_dpll = pnv_find_best_dpll;
14404 else
14405 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14406
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014407 if (INTEL_INFO(dev)->gen >= 9) {
14408 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014409 dev_priv->display.get_initial_plane_config =
14410 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014411 dev_priv->display.crtc_compute_clock =
14412 haswell_crtc_compute_clock;
14413 dev_priv->display.crtc_enable = haswell_crtc_enable;
14414 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014415 dev_priv->display.update_primary_plane =
14416 skylake_update_primary_plane;
14417 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014418 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014419 dev_priv->display.get_initial_plane_config =
14420 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014421 dev_priv->display.crtc_compute_clock =
14422 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014423 dev_priv->display.crtc_enable = haswell_crtc_enable;
14424 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014425 dev_priv->display.update_primary_plane =
14426 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014427 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014428 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014429 dev_priv->display.get_initial_plane_config =
14430 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014431 dev_priv->display.crtc_compute_clock =
14432 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014433 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14434 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014435 dev_priv->display.update_primary_plane =
14436 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014437 } else if (IS_VALLEYVIEW(dev)) {
14438 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014439 dev_priv->display.get_initial_plane_config =
14440 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014441 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014442 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14443 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014444 dev_priv->display.update_primary_plane =
14445 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014446 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014447 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014448 dev_priv->display.get_initial_plane_config =
14449 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014450 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014451 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14452 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014453 dev_priv->display.update_primary_plane =
14454 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014455 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014456
Jesse Barnese70236a2009-09-21 10:42:27 -070014457 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014458 if (IS_SKYLAKE(dev))
14459 dev_priv->display.get_display_clock_speed =
14460 skylake_get_display_clock_speed;
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014461 else if (IS_BROXTON(dev))
14462 dev_priv->display.get_display_clock_speed =
14463 broxton_get_display_clock_speed;
Ville Syrjälä1652d192015-03-31 14:12:01 +030014464 else if (IS_BROADWELL(dev))
14465 dev_priv->display.get_display_clock_speed =
14466 broadwell_get_display_clock_speed;
14467 else if (IS_HASWELL(dev))
14468 dev_priv->display.get_display_clock_speed =
14469 haswell_get_display_clock_speed;
14470 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014471 dev_priv->display.get_display_clock_speed =
14472 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014473 else if (IS_GEN5(dev))
14474 dev_priv->display.get_display_clock_speed =
14475 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014476 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014477 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014478 dev_priv->display.get_display_clock_speed =
14479 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014480 else if (IS_GM45(dev))
14481 dev_priv->display.get_display_clock_speed =
14482 gm45_get_display_clock_speed;
14483 else if (IS_CRESTLINE(dev))
14484 dev_priv->display.get_display_clock_speed =
14485 i965gm_get_display_clock_speed;
14486 else if (IS_PINEVIEW(dev))
14487 dev_priv->display.get_display_clock_speed =
14488 pnv_get_display_clock_speed;
14489 else if (IS_G33(dev) || IS_G4X(dev))
14490 dev_priv->display.get_display_clock_speed =
14491 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014492 else if (IS_I915G(dev))
14493 dev_priv->display.get_display_clock_speed =
14494 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014495 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014496 dev_priv->display.get_display_clock_speed =
14497 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014498 else if (IS_PINEVIEW(dev))
14499 dev_priv->display.get_display_clock_speed =
14500 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014501 else if (IS_I915GM(dev))
14502 dev_priv->display.get_display_clock_speed =
14503 i915gm_get_display_clock_speed;
14504 else if (IS_I865G(dev))
14505 dev_priv->display.get_display_clock_speed =
14506 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014507 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014508 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014509 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014510 else { /* 830 */
14511 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014512 dev_priv->display.get_display_clock_speed =
14513 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014514 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014515
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014516 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014517 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014518 } else if (IS_GEN6(dev)) {
14519 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014520 } else if (IS_IVYBRIDGE(dev)) {
14521 /* FIXME: detect B0+ stepping and use auto training */
14522 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014523 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014524 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014525 if (IS_BROADWELL(dev)) {
14526 dev_priv->display.modeset_commit_cdclk =
14527 broadwell_modeset_commit_cdclk;
14528 dev_priv->display.modeset_calc_cdclk =
14529 broadwell_modeset_calc_cdclk;
14530 }
Jesse Barnes30a970c2013-11-04 13:48:12 -080014531 } else if (IS_VALLEYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014532 dev_priv->display.modeset_commit_cdclk =
14533 valleyview_modeset_commit_cdclk;
14534 dev_priv->display.modeset_calc_cdclk =
14535 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014536 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014537 dev_priv->display.modeset_commit_cdclk =
14538 broxton_modeset_commit_cdclk;
14539 dev_priv->display.modeset_calc_cdclk =
14540 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014541 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014542
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014543 switch (INTEL_INFO(dev)->gen) {
14544 case 2:
14545 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14546 break;
14547
14548 case 3:
14549 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14550 break;
14551
14552 case 4:
14553 case 5:
14554 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14555 break;
14556
14557 case 6:
14558 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14559 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014560 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014561 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014562 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14563 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014564 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014565 /* Drop through - unsupported since execlist only. */
14566 default:
14567 /* Default just returns -ENODEV to indicate unsupported */
14568 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014569 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014570
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014571 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014572}
14573
Jesse Barnesb690e962010-07-19 13:53:12 -070014574/*
14575 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14576 * resume, or other times. This quirk makes sure that's the case for
14577 * affected systems.
14578 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014579static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014580{
14581 struct drm_i915_private *dev_priv = dev->dev_private;
14582
14583 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014584 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014585}
14586
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014587static void quirk_pipeb_force(struct drm_device *dev)
14588{
14589 struct drm_i915_private *dev_priv = dev->dev_private;
14590
14591 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14592 DRM_INFO("applying pipe b force quirk\n");
14593}
14594
Keith Packard435793d2011-07-12 14:56:22 -070014595/*
14596 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14597 */
14598static void quirk_ssc_force_disable(struct drm_device *dev)
14599{
14600 struct drm_i915_private *dev_priv = dev->dev_private;
14601 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014602 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014603}
14604
Carsten Emde4dca20e2012-03-15 15:56:26 +010014605/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014606 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14607 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014608 */
14609static void quirk_invert_brightness(struct drm_device *dev)
14610{
14611 struct drm_i915_private *dev_priv = dev->dev_private;
14612 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014613 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014614}
14615
Scot Doyle9c72cc62014-07-03 23:27:50 +000014616/* Some VBT's incorrectly indicate no backlight is present */
14617static void quirk_backlight_present(struct drm_device *dev)
14618{
14619 struct drm_i915_private *dev_priv = dev->dev_private;
14620 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14621 DRM_INFO("applying backlight present quirk\n");
14622}
14623
Jesse Barnesb690e962010-07-19 13:53:12 -070014624struct intel_quirk {
14625 int device;
14626 int subsystem_vendor;
14627 int subsystem_device;
14628 void (*hook)(struct drm_device *dev);
14629};
14630
Egbert Eich5f85f172012-10-14 15:46:38 +020014631/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14632struct intel_dmi_quirk {
14633 void (*hook)(struct drm_device *dev);
14634 const struct dmi_system_id (*dmi_id_list)[];
14635};
14636
14637static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14638{
14639 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14640 return 1;
14641}
14642
14643static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14644 {
14645 .dmi_id_list = &(const struct dmi_system_id[]) {
14646 {
14647 .callback = intel_dmi_reverse_brightness,
14648 .ident = "NCR Corporation",
14649 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14650 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14651 },
14652 },
14653 { } /* terminating entry */
14654 },
14655 .hook = quirk_invert_brightness,
14656 },
14657};
14658
Ben Widawskyc43b5632012-04-16 14:07:40 -070014659static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014660 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14661 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14662
Jesse Barnesb690e962010-07-19 13:53:12 -070014663 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14664 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14665
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014666 /* 830 needs to leave pipe A & dpll A up */
14667 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14668
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014669 /* 830 needs to leave pipe B & dpll B up */
14670 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14671
Keith Packard435793d2011-07-12 14:56:22 -070014672 /* Lenovo U160 cannot use SSC on LVDS */
14673 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014674
14675 /* Sony Vaio Y cannot use SSC on LVDS */
14676 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014677
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014678 /* Acer Aspire 5734Z must invert backlight brightness */
14679 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14680
14681 /* Acer/eMachines G725 */
14682 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14683
14684 /* Acer/eMachines e725 */
14685 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14686
14687 /* Acer/Packard Bell NCL20 */
14688 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14689
14690 /* Acer Aspire 4736Z */
14691 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014692
14693 /* Acer Aspire 5336 */
14694 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014695
14696 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14697 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014698
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014699 /* Acer C720 Chromebook (Core i3 4005U) */
14700 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14701
jens steinb2a96012014-10-28 20:25:53 +010014702 /* Apple Macbook 2,1 (Core 2 T7400) */
14703 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14704
Scot Doyled4967d82014-07-03 23:27:52 +000014705 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14706 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014707
14708 /* HP Chromebook 14 (Celeron 2955U) */
14709 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014710
14711 /* Dell Chromebook 11 */
14712 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014713};
14714
14715static void intel_init_quirks(struct drm_device *dev)
14716{
14717 struct pci_dev *d = dev->pdev;
14718 int i;
14719
14720 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14721 struct intel_quirk *q = &intel_quirks[i];
14722
14723 if (d->device == q->device &&
14724 (d->subsystem_vendor == q->subsystem_vendor ||
14725 q->subsystem_vendor == PCI_ANY_ID) &&
14726 (d->subsystem_device == q->subsystem_device ||
14727 q->subsystem_device == PCI_ANY_ID))
14728 q->hook(dev);
14729 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014730 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14731 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14732 intel_dmi_quirks[i].hook(dev);
14733 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014734}
14735
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014736/* Disable the VGA plane that we never use */
14737static void i915_disable_vga(struct drm_device *dev)
14738{
14739 struct drm_i915_private *dev_priv = dev->dev_private;
14740 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014741 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014742
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014743 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014744 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014745 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014746 sr1 = inb(VGA_SR_DATA);
14747 outb(sr1 | 1<<5, VGA_SR_DATA);
14748 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14749 udelay(300);
14750
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014751 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014752 POSTING_READ(vga_reg);
14753}
14754
Daniel Vetterf8175862012-04-10 15:50:11 +020014755void intel_modeset_init_hw(struct drm_device *dev)
14756{
Ville Syrjäläb6283052015-06-03 15:45:07 +030014757 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014758 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014759 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014760 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014761}
14762
Jesse Barnes79e53942008-11-07 14:24:08 -080014763void intel_modeset_init(struct drm_device *dev)
14764{
Jesse Barnes652c3932009-08-17 13:31:43 -070014765 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014766 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014767 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014768 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014769
14770 drm_mode_config_init(dev);
14771
14772 dev->mode_config.min_width = 0;
14773 dev->mode_config.min_height = 0;
14774
Dave Airlie019d96c2011-09-29 16:20:42 +010014775 dev->mode_config.preferred_depth = 24;
14776 dev->mode_config.prefer_shadow = 1;
14777
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014778 dev->mode_config.allow_fb_modifiers = true;
14779
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014780 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014781
Jesse Barnesb690e962010-07-19 13:53:12 -070014782 intel_init_quirks(dev);
14783
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014784 intel_init_pm(dev);
14785
Ben Widawskye3c74752013-04-05 13:12:39 -070014786 if (INTEL_INFO(dev)->num_pipes == 0)
14787 return;
14788
Lukas Wunner69f92f62015-07-15 13:57:35 +020014789 /*
14790 * There may be no VBT; and if the BIOS enabled SSC we can
14791 * just keep using it to avoid unnecessary flicker. Whereas if the
14792 * BIOS isn't using it, don't assume it will work even if the VBT
14793 * indicates as much.
14794 */
14795 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
14796 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14797 DREF_SSC1_ENABLE);
14798
14799 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
14800 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
14801 bios_lvds_use_ssc ? "en" : "dis",
14802 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
14803 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
14804 }
14805 }
14806
Jesse Barnese70236a2009-09-21 10:42:27 -070014807 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014808 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014809
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014810 if (IS_GEN2(dev)) {
14811 dev->mode_config.max_width = 2048;
14812 dev->mode_config.max_height = 2048;
14813 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014814 dev->mode_config.max_width = 4096;
14815 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014816 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014817 dev->mode_config.max_width = 8192;
14818 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014819 }
Damien Lespiau068be562014-03-28 14:17:49 +000014820
Ville Syrjälädc41c152014-08-13 11:57:05 +030014821 if (IS_845G(dev) || IS_I865G(dev)) {
14822 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14823 dev->mode_config.cursor_height = 1023;
14824 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014825 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14826 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14827 } else {
14828 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14829 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14830 }
14831
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014832 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014833
Zhao Yakui28c97732009-10-09 11:39:41 +080014834 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014835 INTEL_INFO(dev)->num_pipes,
14836 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014837
Damien Lespiau055e3932014-08-18 13:49:10 +010014838 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014839 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014840 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014841 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014842 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014843 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014844 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014845 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014846 }
14847
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030014848 intel_update_czclk(dev_priv);
14849 intel_update_cdclk(dev);
14850
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014851 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014852
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014853 /* Just disable it once at startup */
14854 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014855 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014856
14857 /* Just in case the BIOS is doing something questionable. */
Paulo Zanoni7733b492015-07-07 15:26:04 -030014858 intel_fbc_disable(dev_priv);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014859
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014860 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020014861 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014862 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014863
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014864 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014865 struct intel_initial_plane_config plane_config = {};
14866
Jesse Barnes46f297f2014-03-07 08:57:48 -080014867 if (!crtc->active)
14868 continue;
14869
Jesse Barnes46f297f2014-03-07 08:57:48 -080014870 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014871 * Note that reserving the BIOS fb up front prevents us
14872 * from stuffing other stolen allocations like the ring
14873 * on top. This prevents some ugliness at boot time, and
14874 * can even allow for smooth boot transitions if the BIOS
14875 * fb is large enough for the active pipe configuration.
14876 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014877 dev_priv->display.get_initial_plane_config(crtc,
14878 &plane_config);
14879
14880 /*
14881 * If the fb is shared between multiple heads, we'll
14882 * just get the first one.
14883 */
14884 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014885 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014886}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014887
Daniel Vetter7fad7982012-07-04 17:51:47 +020014888static void intel_enable_pipe_a(struct drm_device *dev)
14889{
14890 struct intel_connector *connector;
14891 struct drm_connector *crt = NULL;
14892 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014893 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014894
14895 /* We can't just switch on the pipe A, we need to set things up with a
14896 * proper mode and output configuration. As a gross hack, enable pipe A
14897 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014898 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014899 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14900 crt = &connector->base;
14901 break;
14902 }
14903 }
14904
14905 if (!crt)
14906 return;
14907
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014908 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014909 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014910}
14911
Daniel Vetterfa555832012-10-10 23:14:00 +020014912static bool
14913intel_check_plane_mapping(struct intel_crtc *crtc)
14914{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014915 struct drm_device *dev = crtc->base.dev;
14916 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014917 u32 reg, val;
14918
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014919 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014920 return true;
14921
14922 reg = DSPCNTR(!crtc->plane);
14923 val = I915_READ(reg);
14924
14925 if ((val & DISPLAY_PLANE_ENABLE) &&
14926 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14927 return false;
14928
14929 return true;
14930}
14931
Ville Syrjälä02e93c32015-08-26 19:39:19 +030014932static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
14933{
14934 struct drm_device *dev = crtc->base.dev;
14935 struct intel_encoder *encoder;
14936
14937 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
14938 return true;
14939
14940 return false;
14941}
14942
Daniel Vetter24929352012-07-02 20:28:59 +020014943static void intel_sanitize_crtc(struct intel_crtc *crtc)
14944{
14945 struct drm_device *dev = crtc->base.dev;
14946 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014947 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014948
Daniel Vetter24929352012-07-02 20:28:59 +020014949 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014950 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014951 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14952
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014953 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014954 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014955 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030014956 struct intel_plane *plane;
14957
Daniel Vetter96256042015-02-13 21:03:42 +010014958 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030014959
14960 /* Disable everything but the primary plane */
14961 for_each_intel_plane_on_crtc(dev, crtc, plane) {
14962 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
14963 continue;
14964
14965 plane->disable_plane(&plane->base, &crtc->base);
14966 }
Daniel Vetter96256042015-02-13 21:03:42 +010014967 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014968
Daniel Vetter24929352012-07-02 20:28:59 +020014969 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014970 * disable the crtc (and hence change the state) if it is wrong. Note
14971 * that gen4+ has a fixed plane -> pipe mapping. */
14972 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014973 bool plane;
14974
Daniel Vetter24929352012-07-02 20:28:59 +020014975 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14976 crtc->base.base.id);
14977
14978 /* Pipe has the wrong plane attached and the plane is active.
14979 * Temporarily change the plane mapping and disable everything
14980 * ... */
14981 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014982 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014983 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020014984 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014985 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020014986 }
Daniel Vetter24929352012-07-02 20:28:59 +020014987
Daniel Vetter7fad7982012-07-04 17:51:47 +020014988 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14989 crtc->pipe == PIPE_A && !crtc->active) {
14990 /* BIOS forgot to enable pipe A, this mostly happens after
14991 * resume. Force-enable the pipe to fix this, the update_dpms
14992 * call below we restore the pipe to the right state, but leave
14993 * the required bits on. */
14994 intel_enable_pipe_a(dev);
14995 }
14996
Daniel Vetter24929352012-07-02 20:28:59 +020014997 /* Adjust the state of the output pipe according to whether we
14998 * have active connectors/encoders. */
Ville Syrjälä02e93c32015-08-26 19:39:19 +030014999 if (!intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015000 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015001
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015002 if (crtc->active != crtc->base.state->active) {
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015003 struct intel_encoder *encoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015004
15005 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015006 * functions or because of calls to intel_crtc_disable_noatomic,
15007 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015008 * pipe A quirk. */
15009 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15010 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015011 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015012 crtc->active ? "enabled" : "disabled");
15013
Maarten Lankhorst4be40c92015-07-14 13:45:32 +020015014 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015015 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015016 crtc->base.enabled = crtc->active;
15017
15018 /* Because we only establish the connector -> encoder ->
15019 * crtc links if something is active, this means the
15020 * crtc is now deactivated. Break the links. connector
15021 * -> encoder links are only establish when things are
15022 * actually up, hence no need to break them. */
15023 WARN_ON(crtc->active);
15024
Maarten Lankhorst2d406bb2015-08-05 12:37:09 +020015025 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetter24929352012-07-02 20:28:59 +020015026 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015027 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015028
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015029 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015030 /*
15031 * We start out with underrun reporting disabled to avoid races.
15032 * For correct bookkeeping mark this on active crtcs.
15033 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015034 * Also on gmch platforms we dont have any hardware bits to
15035 * disable the underrun reporting. Which means we need to start
15036 * out with underrun reporting disabled also on inactive pipes,
15037 * since otherwise we'll complain about the garbage we read when
15038 * e.g. coming up after runtime pm.
15039 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015040 * No protection against concurrent access is required - at
15041 * worst a fifo underrun happens which also sets this to false.
15042 */
15043 crtc->cpu_fifo_underrun_disabled = true;
15044 crtc->pch_fifo_underrun_disabled = true;
15045 }
Daniel Vetter24929352012-07-02 20:28:59 +020015046}
15047
15048static void intel_sanitize_encoder(struct intel_encoder *encoder)
15049{
15050 struct intel_connector *connector;
15051 struct drm_device *dev = encoder->base.dev;
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020015052 bool active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015053
15054 /* We need to check both for a crtc link (meaning that the
15055 * encoder is active and trying to read from a pipe) and the
15056 * pipe itself being active. */
15057 bool has_active_crtc = encoder->base.crtc &&
15058 to_intel_crtc(encoder->base.crtc)->active;
15059
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020015060 for_each_intel_connector(dev, connector) {
15061 if (connector->base.encoder != &encoder->base)
15062 continue;
15063
15064 active = true;
15065 break;
15066 }
15067
15068 if (active && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015069 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15070 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015071 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015072
15073 /* Connector is active, but has no active pipe. This is
15074 * fallout from our resume register restoring. Disable
15075 * the encoder manually again. */
15076 if (encoder->base.crtc) {
15077 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15078 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015079 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015080 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015081 if (encoder->post_disable)
15082 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015083 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015084 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015085
15086 /* Inconsistent output/port/pipe state happens presumably due to
15087 * a bug in one of the get_hw_state functions. Or someplace else
15088 * in our code, like the register restore mess on resume. Clamp
15089 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015090 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015091 if (connector->encoder != encoder)
15092 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015093 connector->base.dpms = DRM_MODE_DPMS_OFF;
15094 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015095 }
15096 }
15097 /* Enabled encoders without active connectors will be fixed in
15098 * the crtc fixup. */
15099}
15100
Imre Deak04098752014-02-18 00:02:16 +020015101void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015102{
15103 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015104 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015105
Imre Deak04098752014-02-18 00:02:16 +020015106 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15107 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15108 i915_disable_vga(dev);
15109 }
15110}
15111
15112void i915_redisable_vga(struct drm_device *dev)
15113{
15114 struct drm_i915_private *dev_priv = dev->dev_private;
15115
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015116 /* This function can be called both from intel_modeset_setup_hw_state or
15117 * at a very early point in our resume sequence, where the power well
15118 * structures are not yet restored. Since this function is at a very
15119 * paranoid "someone might have enabled VGA while we were not looking"
15120 * level, just check if the power well is enabled instead of trying to
15121 * follow the "don't touch the power well if we don't need it" policy
15122 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015123 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015124 return;
15125
Imre Deak04098752014-02-18 00:02:16 +020015126 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015127}
15128
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015129static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015130{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015131 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015132
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015133 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015134}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015135
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015136/* FIXME read out full plane state for all planes */
15137static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015138{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015139 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015140 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015141 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015142
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015143 plane_state->visible =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015144 primary_get_hw_state(to_intel_plane(primary));
15145
15146 if (plane_state->visible)
15147 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015148}
15149
Daniel Vetter30e984d2013-06-05 13:34:17 +020015150static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015151{
15152 struct drm_i915_private *dev_priv = dev->dev_private;
15153 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015154 struct intel_crtc *crtc;
15155 struct intel_encoder *encoder;
15156 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015157 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015158
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015159 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb06f8b02015-07-14 13:42:49 +020015160 __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015161 memset(crtc->config, 0, sizeof(*crtc->config));
Maarten Lankhorstf7217902015-06-10 10:24:20 +020015162 crtc->config->base.crtc = &crtc->base;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015163
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015164 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015165 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015166
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015167 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015168 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015169
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015170 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015171
15172 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15173 crtc->base.base.id,
15174 crtc->active ? "enabled" : "disabled");
15175 }
15176
Daniel Vetter53589012013-06-05 13:34:16 +020015177 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15178 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15179
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015180 pll->on = pll->get_hw_state(dev_priv, pll,
15181 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015182 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015183 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015184 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015185 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015186 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015187 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015188 }
Daniel Vetter53589012013-06-05 13:34:16 +020015189 }
Daniel Vetter53589012013-06-05 13:34:16 +020015190
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015191 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015192 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015193
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015194 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015195 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015196 }
15197
Damien Lespiaub2784e12014-08-05 11:29:37 +010015198 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015199 pipe = 0;
15200
15201 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015202 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15203 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015204 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015205 } else {
15206 encoder->base.crtc = NULL;
15207 }
15208
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015209 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015210 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015211 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015212 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015213 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015214 }
15215
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015216 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015217 if (connector->get_hw_state(connector)) {
15218 connector->base.dpms = DRM_MODE_DPMS_ON;
Daniel Vetter24929352012-07-02 20:28:59 +020015219 connector->base.encoder = &connector->encoder->base;
15220 } else {
15221 connector->base.dpms = DRM_MODE_DPMS_OFF;
15222 connector->base.encoder = NULL;
15223 }
15224 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15225 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015226 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015227 connector->base.encoder ? "enabled" : "disabled");
15228 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015229
15230 for_each_intel_crtc(dev, crtc) {
15231 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15232
15233 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15234 if (crtc->base.state->active) {
15235 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15236 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15237 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15238
15239 /*
15240 * The initial mode needs to be set in order to keep
15241 * the atomic core happy. It wants a valid mode if the
15242 * crtc's enabled, so we do the above call.
15243 *
15244 * At this point some state updated by the connectors
15245 * in their ->detect() callback has not run yet, so
15246 * no recalculation can be done yet.
15247 *
15248 * Even if we could do a recalculation and modeset
15249 * right now it would cause a double modeset if
15250 * fbdev or userspace chooses a different initial mode.
15251 *
15252 * If that happens, someone indicated they wanted a
15253 * mode change, which means it's safe to do a full
15254 * recalculation.
15255 */
15256 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015257
15258 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15259 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015260 }
15261 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015262}
15263
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015264/* Scan out the current hw modeset state,
15265 * and sanitizes it to the current state
15266 */
15267static void
15268intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015269{
15270 struct drm_i915_private *dev_priv = dev->dev_private;
15271 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015272 struct intel_crtc *crtc;
15273 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015274 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015275
15276 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015277
15278 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015279 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015280 intel_sanitize_encoder(encoder);
15281 }
15282
Damien Lespiau055e3932014-08-18 13:49:10 +010015283 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015284 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15285 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015286 intel_dump_pipe_config(crtc, crtc->config,
15287 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015288 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015289
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015290 intel_modeset_update_connector_atomic_state(dev);
15291
Daniel Vetter35c95372013-07-17 06:55:04 +020015292 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15293 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15294
15295 if (!pll->on || pll->active)
15296 continue;
15297
15298 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15299
15300 pll->disable(dev_priv, pll);
15301 pll->on = false;
15302 }
15303
Ville Syrjälä26e1fe42015-06-24 22:00:06 +030015304 if (IS_VALLEYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015305 vlv_wm_get_hw_state(dev);
15306 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015307 skl_wm_get_hw_state(dev);
15308 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015309 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015310
15311 for_each_intel_crtc(dev, crtc) {
15312 unsigned long put_domains;
15313
15314 put_domains = modeset_get_crtc_power_domains(&crtc->base);
15315 if (WARN_ON(put_domains))
15316 modeset_put_power_domains(dev_priv, put_domains);
15317 }
15318 intel_display_set_init_power(dev_priv, false);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015319}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015320
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015321void intel_display_resume(struct drm_device *dev)
15322{
15323 struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
15324 struct intel_connector *conn;
15325 struct intel_plane *plane;
15326 struct drm_crtc *crtc;
15327 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015328
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015329 if (!state)
15330 return;
15331
15332 state->acquire_ctx = dev->mode_config.acquire_ctx;
15333
15334 /* preserve complete old state, including dpll */
15335 intel_atomic_get_shared_dpll_state(state);
15336
15337 for_each_crtc(dev, crtc) {
15338 struct drm_crtc_state *crtc_state =
15339 drm_atomic_get_crtc_state(state, crtc);
15340
15341 ret = PTR_ERR_OR_ZERO(crtc_state);
15342 if (ret)
15343 goto err;
15344
15345 /* force a restore */
15346 crtc_state->mode_changed = true;
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015347 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015348
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015349 for_each_intel_plane(dev, plane) {
15350 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
15351 if (ret)
15352 goto err;
15353 }
15354
15355 for_each_intel_connector(dev, conn) {
15356 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
15357 if (ret)
15358 goto err;
15359 }
15360
15361 intel_modeset_setup_hw_state(dev);
15362
15363 i915_redisable_vga(dev);
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020015364 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015365 if (!ret)
15366 return;
15367
15368err:
15369 DRM_ERROR("Restoring old state failed with %i\n", ret);
15370 drm_atomic_state_free(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015371}
15372
15373void intel_modeset_gem_init(struct drm_device *dev)
15374{
Jesse Barnes484b41d2014-03-07 08:57:55 -080015375 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015376 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015377 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015378
Imre Deakae484342014-03-31 15:10:44 +030015379 mutex_lock(&dev->struct_mutex);
15380 intel_init_gt_powersave(dev);
15381 mutex_unlock(&dev->struct_mutex);
15382
Chris Wilson1833b132012-05-09 11:56:28 +010015383 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015384
15385 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015386
15387 /*
15388 * Make sure any fbs we allocated at startup are properly
15389 * pinned & fenced. When we do the allocation it's too early
15390 * for this.
15391 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015392 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015393 obj = intel_fb_obj(c->primary->fb);
15394 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015395 continue;
15396
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015397 mutex_lock(&dev->struct_mutex);
15398 ret = intel_pin_and_fence_fb_obj(c->primary,
15399 c->primary->fb,
15400 c->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010015401 NULL, NULL);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015402 mutex_unlock(&dev->struct_mutex);
15403 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015404 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15405 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015406 drm_framebuffer_unreference(c->primary->fb);
15407 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015408 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015409 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015410 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015411 }
15412 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015413
15414 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015415}
15416
Imre Deak4932e2c2014-02-11 17:12:48 +020015417void intel_connector_unregister(struct intel_connector *intel_connector)
15418{
15419 struct drm_connector *connector = &intel_connector->base;
15420
15421 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015422 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015423}
15424
Jesse Barnes79e53942008-11-07 14:24:08 -080015425void intel_modeset_cleanup(struct drm_device *dev)
15426{
Jesse Barnes652c3932009-08-17 13:31:43 -070015427 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015428 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015429
Imre Deak2eb52522014-11-19 15:30:05 +020015430 intel_disable_gt_powersave(dev);
15431
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015432 intel_backlight_unregister(dev);
15433
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015434 /*
15435 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015436 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015437 * experience fancy races otherwise.
15438 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015439 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015440
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015441 /*
15442 * Due to the hpd irq storm handling the hotplug work can re-arm the
15443 * poll handlers. Hence disable polling after hpd handling is shut down.
15444 */
Keith Packardf87ea762010-10-03 19:36:26 -070015445 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015446
Jesse Barnes723bfd72010-10-07 16:01:13 -070015447 intel_unregister_dsm_handler();
15448
Paulo Zanoni7733b492015-07-07 15:26:04 -030015449 intel_fbc_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015450
Chris Wilson1630fe72011-07-08 12:22:42 +010015451 /* flush any delayed tasks or pending work */
15452 flush_scheduled_work();
15453
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015454 /* destroy the backlight and sysfs files before encoders/connectors */
15455 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015456 struct intel_connector *intel_connector;
15457
15458 intel_connector = to_intel_connector(connector);
15459 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015460 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015461
Jesse Barnes79e53942008-11-07 14:24:08 -080015462 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015463
15464 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015465
15466 mutex_lock(&dev->struct_mutex);
15467 intel_cleanup_gt_powersave(dev);
15468 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015469}
15470
Dave Airlie28d52042009-09-21 14:33:58 +100015471/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015472 * Return which encoder is currently attached for connector.
15473 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015474struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015475{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015476 return &intel_attached_encoder(connector)->base;
15477}
Jesse Barnes79e53942008-11-07 14:24:08 -080015478
Chris Wilsondf0e9242010-09-09 16:20:55 +010015479void intel_connector_attach_encoder(struct intel_connector *connector,
15480 struct intel_encoder *encoder)
15481{
15482 connector->encoder = encoder;
15483 drm_mode_connector_attach_encoder(&connector->base,
15484 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015485}
Dave Airlie28d52042009-09-21 14:33:58 +100015486
15487/*
15488 * set vga decode state - true == enable VGA decode
15489 */
15490int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15491{
15492 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015493 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015494 u16 gmch_ctrl;
15495
Chris Wilson75fa0412014-02-07 18:37:02 -020015496 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15497 DRM_ERROR("failed to read control word\n");
15498 return -EIO;
15499 }
15500
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015501 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15502 return 0;
15503
Dave Airlie28d52042009-09-21 14:33:58 +100015504 if (state)
15505 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15506 else
15507 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015508
15509 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15510 DRM_ERROR("failed to write control word\n");
15511 return -EIO;
15512 }
15513
Dave Airlie28d52042009-09-21 14:33:58 +100015514 return 0;
15515}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015516
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015517struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015518
15519 u32 power_well_driver;
15520
Chris Wilson63b66e52013-08-08 15:12:06 +020015521 int num_transcoders;
15522
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015523 struct intel_cursor_error_state {
15524 u32 control;
15525 u32 position;
15526 u32 base;
15527 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015528 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015529
15530 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015531 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015532 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015533 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015534 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015535
15536 struct intel_plane_error_state {
15537 u32 control;
15538 u32 stride;
15539 u32 size;
15540 u32 pos;
15541 u32 addr;
15542 u32 surface;
15543 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015544 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015545
15546 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015547 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015548 enum transcoder cpu_transcoder;
15549
15550 u32 conf;
15551
15552 u32 htotal;
15553 u32 hblank;
15554 u32 hsync;
15555 u32 vtotal;
15556 u32 vblank;
15557 u32 vsync;
15558 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015559};
15560
15561struct intel_display_error_state *
15562intel_display_capture_error_state(struct drm_device *dev)
15563{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015564 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015565 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015566 int transcoders[] = {
15567 TRANSCODER_A,
15568 TRANSCODER_B,
15569 TRANSCODER_C,
15570 TRANSCODER_EDP,
15571 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015572 int i;
15573
Chris Wilson63b66e52013-08-08 15:12:06 +020015574 if (INTEL_INFO(dev)->num_pipes == 0)
15575 return NULL;
15576
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015577 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015578 if (error == NULL)
15579 return NULL;
15580
Imre Deak190be112013-11-25 17:15:31 +020015581 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015582 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15583
Damien Lespiau055e3932014-08-18 13:49:10 +010015584 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015585 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015586 __intel_display_power_is_enabled(dev_priv,
15587 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015588 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015589 continue;
15590
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015591 error->cursor[i].control = I915_READ(CURCNTR(i));
15592 error->cursor[i].position = I915_READ(CURPOS(i));
15593 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015594
15595 error->plane[i].control = I915_READ(DSPCNTR(i));
15596 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015597 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015598 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015599 error->plane[i].pos = I915_READ(DSPPOS(i));
15600 }
Paulo Zanonica291362013-03-06 20:03:14 -030015601 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15602 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015603 if (INTEL_INFO(dev)->gen >= 4) {
15604 error->plane[i].surface = I915_READ(DSPSURF(i));
15605 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15606 }
15607
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015608 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015609
Sonika Jindal3abfce72014-07-21 15:23:43 +053015610 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015611 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015612 }
15613
15614 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15615 if (HAS_DDI(dev_priv->dev))
15616 error->num_transcoders++; /* Account for eDP. */
15617
15618 for (i = 0; i < error->num_transcoders; i++) {
15619 enum transcoder cpu_transcoder = transcoders[i];
15620
Imre Deakddf9c532013-11-27 22:02:02 +020015621 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015622 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015623 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015624 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015625 continue;
15626
Chris Wilson63b66e52013-08-08 15:12:06 +020015627 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15628
15629 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15630 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15631 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15632 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15633 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15634 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15635 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015636 }
15637
15638 return error;
15639}
15640
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015641#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15642
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015643void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015644intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015645 struct drm_device *dev,
15646 struct intel_display_error_state *error)
15647{
Damien Lespiau055e3932014-08-18 13:49:10 +010015648 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015649 int i;
15650
Chris Wilson63b66e52013-08-08 15:12:06 +020015651 if (!error)
15652 return;
15653
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015654 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015655 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015656 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015657 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015658 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015659 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015660 err_printf(m, " Power: %s\n",
15661 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015662 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015663 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015664
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015665 err_printf(m, "Plane [%d]:\n", i);
15666 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15667 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015668 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015669 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15670 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015671 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015672 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015673 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015674 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015675 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15676 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015677 }
15678
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015679 err_printf(m, "Cursor [%d]:\n", i);
15680 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15681 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15682 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015683 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015684
15685 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015686 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015687 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015688 err_printf(m, " Power: %s\n",
15689 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015690 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15691 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15692 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15693 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15694 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15695 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15696 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15697 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015698}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015699
15700void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15701{
15702 struct intel_crtc *crtc;
15703
15704 for_each_intel_crtc(dev, crtc) {
15705 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015706
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015707 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015708
15709 work = crtc->unpin_work;
15710
15711 if (work && work->event &&
15712 work->event->base.file_priv == file) {
15713 kfree(work->event);
15714 work->event = NULL;
15715 }
15716
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015717 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015718 }
15719}