blob: bcf227356f78122f56e0bd3bcdeaff945bd86393 [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{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001160 u32 val;
1161 bool cur_state;
1162
Ville Syrjälä649636e2015-09-22 19:50:01 +03001163 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001164 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001165 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001166 "PLL state assertion failure (expected %s, current %s)\n",
1167 state_string(state), state_string(cur_state));
1168}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001169
Jani Nikula23538ef2013-08-27 15:12:22 +03001170/* XXX: the dsi pll is shared between MIPI DSI ports */
1171static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1172{
1173 u32 val;
1174 bool cur_state;
1175
Ville Syrjäläa5805162015-05-26 20:42:30 +03001176 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001177 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001178 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001179
1180 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001181 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001182 "DSI PLL state assertion failure (expected %s, current %s)\n",
1183 state_string(state), state_string(cur_state));
1184}
1185#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1186#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1187
Daniel Vetter55607e82013-06-16 21:42:39 +02001188struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001189intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001190{
Daniel Vettere2b78262013-06-07 23:10:03 +02001191 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1192
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001193 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001194 return NULL;
1195
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001196 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001197}
1198
Jesse Barnesb24e7172011-01-04 15:09:30 -08001199/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001200void assert_shared_dpll(struct drm_i915_private *dev_priv,
1201 struct intel_shared_dpll *pll,
1202 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001203{
Jesse Barnes040484a2011-01-03 12:14:26 -08001204 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001205 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001206
Chris Wilson92b27b02012-05-20 18:10:50 +01001207 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001208 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001209 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001210
Daniel Vetter53589012013-06-05 13:34:16 +02001211 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001212 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001213 "%s assertion failure (expected %s, current %s)\n",
1214 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001215}
Jesse Barnes040484a2011-01-03 12:14:26 -08001216
1217static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1218 enum pipe pipe, bool state)
1219{
Jesse Barnes040484a2011-01-03 12:14:26 -08001220 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001221 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1222 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001223
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001224 if (HAS_DDI(dev_priv->dev)) {
1225 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001226 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001227 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001228 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001229 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001230 cur_state = !!(val & FDI_TX_ENABLE);
1231 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001232 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001233 "FDI TX state assertion failure (expected %s, current %s)\n",
1234 state_string(state), state_string(cur_state));
1235}
1236#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1237#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1238
1239static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1240 enum pipe pipe, bool state)
1241{
Jesse Barnes040484a2011-01-03 12:14:26 -08001242 u32 val;
1243 bool cur_state;
1244
Ville Syrjälä649636e2015-09-22 19:50:01 +03001245 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001246 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001247 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001248 "FDI RX state assertion failure (expected %s, current %s)\n",
1249 state_string(state), state_string(cur_state));
1250}
1251#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1252#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1253
1254static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1255 enum pipe pipe)
1256{
Jesse Barnes040484a2011-01-03 12:14:26 -08001257 u32 val;
1258
1259 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001260 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001261 return;
1262
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001263 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001264 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001265 return;
1266
Ville Syrjälä649636e2015-09-22 19:50:01 +03001267 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001268 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 -08001269}
1270
Daniel Vetter55607e82013-06-16 21:42:39 +02001271void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1272 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001273{
Jesse Barnes040484a2011-01-03 12:14:26 -08001274 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001275 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001276
Ville Syrjälä649636e2015-09-22 19:50:01 +03001277 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001278 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001279 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001280 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1281 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001282}
1283
Daniel Vetterb680c372014-09-19 18:27:27 +02001284void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1285 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001286{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001287 struct drm_device *dev = dev_priv->dev;
1288 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001289 u32 val;
1290 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001291 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001292
Jani Nikulabedd4db2014-08-22 15:04:13 +03001293 if (WARN_ON(HAS_DDI(dev)))
1294 return;
1295
1296 if (HAS_PCH_SPLIT(dev)) {
1297 u32 port_sel;
1298
Jesse Barnesea0760c2011-01-04 15:09:32 -08001299 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001300 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1301
1302 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1303 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1304 panel_pipe = PIPE_B;
1305 /* XXX: else fix for eDP */
1306 } else if (IS_VALLEYVIEW(dev)) {
1307 /* presumably write lock depends on pipe, not port select */
1308 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1309 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001310 } else {
1311 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001312 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1313 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001314 }
1315
1316 val = I915_READ(pp_reg);
1317 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001318 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001319 locked = false;
1320
Rob Clarke2c719b2014-12-15 13:56:32 -05001321 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001322 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001323 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001324}
1325
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001326static void assert_cursor(struct drm_i915_private *dev_priv,
1327 enum pipe pipe, bool state)
1328{
1329 struct drm_device *dev = dev_priv->dev;
1330 bool cur_state;
1331
Paulo Zanonid9d82082014-02-27 16:30:56 -03001332 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001333 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001334 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001335 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001336
Rob Clarke2c719b2014-12-15 13:56:32 -05001337 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001338 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1339 pipe_name(pipe), state_string(state), state_string(cur_state));
1340}
1341#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1342#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1343
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001344void assert_pipe(struct drm_i915_private *dev_priv,
1345 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001346{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001347 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001348 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1349 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001350
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001351 /* if we need the pipe quirk it must be always on */
1352 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1353 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001354 state = true;
1355
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001356 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001357 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001358 cur_state = false;
1359 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001360 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001361 cur_state = !!(val & PIPECONF_ENABLE);
1362 }
1363
Rob Clarke2c719b2014-12-15 13:56:32 -05001364 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001365 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001366 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001367}
1368
Chris Wilson931872f2012-01-16 23:01:13 +00001369static void assert_plane(struct drm_i915_private *dev_priv,
1370 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001371{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001372 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001373 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001374
Ville Syrjälä649636e2015-09-22 19:50:01 +03001375 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001376 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001377 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001378 "plane %c assertion failure (expected %s, current %s)\n",
1379 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001380}
1381
Chris Wilson931872f2012-01-16 23:01:13 +00001382#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1383#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1384
Jesse Barnesb24e7172011-01-04 15:09:30 -08001385static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1386 enum pipe pipe)
1387{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001388 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001389 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001390
Ville Syrjälä653e1022013-06-04 13:49:05 +03001391 /* Primary planes are fixed to pipes on gen4+ */
1392 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001393 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001394 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001395 "plane %c assertion failure, should be disabled but not\n",
1396 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001397 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001398 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001399
Jesse Barnesb24e7172011-01-04 15:09:30 -08001400 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001401 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001402 u32 val = I915_READ(DSPCNTR(i));
1403 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001404 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001405 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001406 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1407 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001408 }
1409}
1410
Jesse Barnes19332d72013-03-28 09:55:38 -07001411static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1412 enum pipe pipe)
1413{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001414 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001415 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001416
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001417 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001418 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001419 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001420 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001421 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1422 sprite, pipe_name(pipe));
1423 }
1424 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001425 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001426 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001427 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001428 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001429 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001430 }
1431 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001432 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001433 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001434 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001435 plane_name(pipe), pipe_name(pipe));
1436 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001437 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001438 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001439 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1440 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001441 }
1442}
1443
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001444static void assert_vblank_disabled(struct drm_crtc *crtc)
1445{
Rob Clarke2c719b2014-12-15 13:56:32 -05001446 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001447 drm_crtc_vblank_put(crtc);
1448}
1449
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001450static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001451{
1452 u32 val;
1453 bool enabled;
1454
Rob Clarke2c719b2014-12-15 13:56:32 -05001455 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001456
Jesse Barnes92f25842011-01-04 15:09:34 -08001457 val = I915_READ(PCH_DREF_CONTROL);
1458 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1459 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001460 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001461}
1462
Daniel Vetterab9412b2013-05-03 11:49:46 +02001463static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1464 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001465{
Jesse Barnes92f25842011-01-04 15:09:34 -08001466 u32 val;
1467 bool enabled;
1468
Ville Syrjälä649636e2015-09-22 19:50:01 +03001469 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001470 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001471 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001472 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1473 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001474}
1475
Keith Packard4e634382011-08-06 10:39:45 -07001476static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1477 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001478{
1479 if ((val & DP_PORT_EN) == 0)
1480 return false;
1481
1482 if (HAS_PCH_CPT(dev_priv->dev)) {
1483 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1484 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1485 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1486 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001487 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1488 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1489 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001490 } else {
1491 if ((val & DP_PIPE_MASK) != (pipe << 30))
1492 return false;
1493 }
1494 return true;
1495}
1496
Keith Packard1519b992011-08-06 10:35:34 -07001497static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1498 enum pipe pipe, u32 val)
1499{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001500 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001501 return false;
1502
1503 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001504 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001505 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001506 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1507 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1508 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001509 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001510 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001511 return false;
1512 }
1513 return true;
1514}
1515
1516static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1517 enum pipe pipe, u32 val)
1518{
1519 if ((val & LVDS_PORT_EN) == 0)
1520 return false;
1521
1522 if (HAS_PCH_CPT(dev_priv->dev)) {
1523 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1524 return false;
1525 } else {
1526 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1527 return false;
1528 }
1529 return true;
1530}
1531
1532static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1533 enum pipe pipe, u32 val)
1534{
1535 if ((val & ADPA_DAC_ENABLE) == 0)
1536 return false;
1537 if (HAS_PCH_CPT(dev_priv->dev)) {
1538 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1539 return false;
1540 } else {
1541 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1542 return false;
1543 }
1544 return true;
1545}
1546
Jesse Barnes291906f2011-02-02 12:28:03 -08001547static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001548 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001549{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001550 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001551 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001552 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001553 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001554
Rob Clarke2c719b2014-12-15 13:56:32 -05001555 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001556 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001557 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001558}
1559
1560static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1561 enum pipe pipe, int reg)
1562{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001563 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001564 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001565 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001566 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001567
Rob Clarke2c719b2014-12-15 13:56:32 -05001568 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001569 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001570 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001571}
1572
1573static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1574 enum pipe pipe)
1575{
Jesse Barnes291906f2011-02-02 12:28:03 -08001576 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001577
Keith Packardf0575e92011-07-25 22:12:43 -07001578 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1579 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1580 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001581
Ville Syrjälä649636e2015-09-22 19:50:01 +03001582 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001583 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001584 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001585 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001586
Ville Syrjälä649636e2015-09-22 19:50:01 +03001587 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001588 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001589 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001590 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001591
Paulo Zanonie2debe92013-02-18 19:00:27 -03001592 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1593 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1594 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001595}
1596
Ville Syrjäläd288f652014-10-28 13:20:22 +02001597static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001598 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001599{
Daniel Vetter426115c2013-07-11 22:13:42 +02001600 struct drm_device *dev = crtc->base.dev;
1601 struct drm_i915_private *dev_priv = dev->dev_private;
1602 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001603 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001604
Daniel Vetter426115c2013-07-11 22:13:42 +02001605 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001606
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001607 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001608 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1609
1610 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001611 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001612 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001613
Daniel Vetter426115c2013-07-11 22:13:42 +02001614 I915_WRITE(reg, dpll);
1615 POSTING_READ(reg);
1616 udelay(150);
1617
1618 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1619 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1620
Ville Syrjäläd288f652014-10-28 13:20:22 +02001621 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001622 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001623
1624 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001625 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001626 POSTING_READ(reg);
1627 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001628 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001629 POSTING_READ(reg);
1630 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001631 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001632 POSTING_READ(reg);
1633 udelay(150); /* wait for warmup */
1634}
1635
Ville Syrjäläd288f652014-10-28 13:20:22 +02001636static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001637 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001638{
1639 struct drm_device *dev = crtc->base.dev;
1640 struct drm_i915_private *dev_priv = dev->dev_private;
1641 int pipe = crtc->pipe;
1642 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001643 u32 tmp;
1644
1645 assert_pipe_disabled(dev_priv, crtc->pipe);
1646
1647 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1648
Ville Syrjäläa5805162015-05-26 20:42:30 +03001649 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001650
1651 /* Enable back the 10bit clock to display controller */
1652 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1653 tmp |= DPIO_DCLKP_EN;
1654 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1655
Ville Syrjälä54433e92015-05-26 20:42:31 +03001656 mutex_unlock(&dev_priv->sb_lock);
1657
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001658 /*
1659 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1660 */
1661 udelay(1);
1662
1663 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001664 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001665
1666 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001667 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001668 DRM_ERROR("PLL %d failed to lock\n", pipe);
1669
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001670 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001671 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001672 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001673}
1674
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001675static int intel_num_dvo_pipes(struct drm_device *dev)
1676{
1677 struct intel_crtc *crtc;
1678 int count = 0;
1679
1680 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001681 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001682 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001683
1684 return count;
1685}
1686
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001687static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001688{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001689 struct drm_device *dev = crtc->base.dev;
1690 struct drm_i915_private *dev_priv = dev->dev_private;
1691 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001692 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001693
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001694 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001695
1696 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001697 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001698
1699 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001700 if (IS_MOBILE(dev) && !IS_I830(dev))
1701 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001702
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001703 /* Enable DVO 2x clock on both PLLs if necessary */
1704 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1705 /*
1706 * It appears to be important that we don't enable this
1707 * for the current pipe before otherwise configuring the
1708 * PLL. No idea how this should be handled if multiple
1709 * DVO outputs are enabled simultaneosly.
1710 */
1711 dpll |= DPLL_DVO_2X_MODE;
1712 I915_WRITE(DPLL(!crtc->pipe),
1713 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1714 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001715
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001716 /*
1717 * Apparently we need to have VGA mode enabled prior to changing
1718 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1719 * dividers, even though the register value does change.
1720 */
1721 I915_WRITE(reg, 0);
1722
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001723 I915_WRITE(reg, dpll);
1724
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001725 /* Wait for the clocks to stabilize. */
1726 POSTING_READ(reg);
1727 udelay(150);
1728
1729 if (INTEL_INFO(dev)->gen >= 4) {
1730 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001731 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001732 } else {
1733 /* The pixel multiplier can only be updated once the
1734 * DPLL is enabled and the clocks are stable.
1735 *
1736 * So write it again.
1737 */
1738 I915_WRITE(reg, dpll);
1739 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001740
1741 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001742 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001743 POSTING_READ(reg);
1744 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001745 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001746 POSTING_READ(reg);
1747 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001748 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001749 POSTING_READ(reg);
1750 udelay(150); /* wait for warmup */
1751}
1752
1753/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001754 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001755 * @dev_priv: i915 private structure
1756 * @pipe: pipe PLL to disable
1757 *
1758 * Disable the PLL for @pipe, making sure the pipe is off first.
1759 *
1760 * Note! This is for pre-ILK only.
1761 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001762static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001763{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001764 struct drm_device *dev = crtc->base.dev;
1765 struct drm_i915_private *dev_priv = dev->dev_private;
1766 enum pipe pipe = crtc->pipe;
1767
1768 /* Disable DVO 2x clock on both PLLs if necessary */
1769 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001770 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001771 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001772 I915_WRITE(DPLL(PIPE_B),
1773 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1774 I915_WRITE(DPLL(PIPE_A),
1775 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1776 }
1777
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001778 /* Don't disable pipe or pipe PLLs if needed */
1779 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1780 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001781 return;
1782
1783 /* Make sure the pipe isn't still relying on us */
1784 assert_pipe_disabled(dev_priv, pipe);
1785
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001786 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001787 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001788}
1789
Jesse Barnesf6071162013-10-01 10:41:38 -07001790static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1791{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001792 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001793
1794 /* Make sure the pipe isn't still relying on us */
1795 assert_pipe_disabled(dev_priv, pipe);
1796
Imre Deake5cbfbf2014-01-09 17:08:16 +02001797 /*
1798 * Leave integrated clock source and reference clock enabled for pipe B.
1799 * The latter is needed for VGA hotplug / manual detection.
1800 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001801 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001802 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001803 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001804 I915_WRITE(DPLL(pipe), val);
1805 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001806
1807}
1808
1809static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1810{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001811 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001812 u32 val;
1813
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001814 /* Make sure the pipe isn't still relying on us */
1815 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001816
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001817 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001818 val = DPLL_SSC_REF_CLK_CHV |
1819 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001820 if (pipe != PIPE_A)
1821 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1822 I915_WRITE(DPLL(pipe), val);
1823 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001824
Ville Syrjäläa5805162015-05-26 20:42:30 +03001825 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001826
1827 /* Disable 10bit clock to display controller */
1828 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1829 val &= ~DPIO_DCLKP_EN;
1830 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1831
Ville Syrjäläa5805162015-05-26 20:42:30 +03001832 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001833}
1834
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001835void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001836 struct intel_digital_port *dport,
1837 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001838{
1839 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001840 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001841
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001842 switch (dport->port) {
1843 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001844 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001845 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001846 break;
1847 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001848 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001849 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001850 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001851 break;
1852 case PORT_D:
1853 port_mask = DPLL_PORTD_READY_MASK;
1854 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001855 break;
1856 default:
1857 BUG();
1858 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001859
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001860 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1861 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1862 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001863}
1864
Daniel Vetterb14b1052014-04-24 23:55:13 +02001865static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1866{
1867 struct drm_device *dev = crtc->base.dev;
1868 struct drm_i915_private *dev_priv = dev->dev_private;
1869 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1870
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001871 if (WARN_ON(pll == NULL))
1872 return;
1873
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001874 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001875 if (pll->active == 0) {
1876 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1877 WARN_ON(pll->on);
1878 assert_shared_dpll_disabled(dev_priv, pll);
1879
1880 pll->mode_set(dev_priv, pll);
1881 }
1882}
1883
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001884/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001885 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001886 * @dev_priv: i915 private structure
1887 * @pipe: pipe PLL to enable
1888 *
1889 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1890 * drives the transcoder clock.
1891 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001892static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001893{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001894 struct drm_device *dev = crtc->base.dev;
1895 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001896 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001897
Daniel Vetter87a875b2013-06-05 13:34:19 +02001898 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001899 return;
1900
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001901 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001902 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001903
Damien Lespiau74dd6922014-07-29 18:06:17 +01001904 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001905 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001906 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001907
Daniel Vettercdbd2312013-06-05 13:34:03 +02001908 if (pll->active++) {
1909 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001910 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001911 return;
1912 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001913 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001914
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001915 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1916
Daniel Vetter46edb022013-06-05 13:34:12 +02001917 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001918 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001919 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001920}
1921
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001922static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001923{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001924 struct drm_device *dev = crtc->base.dev;
1925 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001926 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001927
Jesse Barnes92f25842011-01-04 15:09:34 -08001928 /* PCH only available on ILK+ */
Jesse Barnes80aa9312015-08-03 13:09:11 -07001929 if (INTEL_INFO(dev)->gen < 5)
1930 return;
1931
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001932 if (pll == NULL)
1933 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001934
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001935 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
Chris Wilson48da64a2012-05-13 20:16:12 +01001936 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001937
Daniel Vetter46edb022013-06-05 13:34:12 +02001938 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1939 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001940 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001941
Chris Wilson48da64a2012-05-13 20:16:12 +01001942 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001943 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001944 return;
1945 }
1946
Daniel Vettere9d69442013-06-05 13:34:15 +02001947 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001948 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001949 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001950 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001951
Daniel Vetter46edb022013-06-05 13:34:12 +02001952 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001953 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001954 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001955
1956 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001957}
1958
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001959static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1960 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001961{
Daniel Vetter23670b322012-11-01 09:15:30 +01001962 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001963 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001964 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001965 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001966
1967 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001968 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001969
1970 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001971 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001972 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001973
1974 /* FDI must be feeding us bits for PCH ports */
1975 assert_fdi_tx_enabled(dev_priv, pipe);
1976 assert_fdi_rx_enabled(dev_priv, pipe);
1977
Daniel Vetter23670b322012-11-01 09:15:30 +01001978 if (HAS_PCH_CPT(dev)) {
1979 /* Workaround: Set the timing override bit before enabling the
1980 * pch transcoder. */
1981 reg = TRANS_CHICKEN2(pipe);
1982 val = I915_READ(reg);
1983 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1984 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001985 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001986
Daniel Vetterab9412b2013-05-03 11:49:46 +02001987 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001988 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001989 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001990
1991 if (HAS_PCH_IBX(dev_priv->dev)) {
1992 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001993 * Make the BPC in transcoder be consistent with
1994 * that in pipeconf reg. For HDMI we must use 8bpc
1995 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001996 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001997 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001998 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1999 val |= PIPECONF_8BPC;
2000 else
2001 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002002 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002003
2004 val &= ~TRANS_INTERLACE_MASK;
2005 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002006 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002007 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002008 val |= TRANS_LEGACY_INTERLACED_ILK;
2009 else
2010 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002011 else
2012 val |= TRANS_PROGRESSIVE;
2013
Jesse Barnes040484a2011-01-03 12:14:26 -08002014 I915_WRITE(reg, val | TRANS_ENABLE);
2015 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002016 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002017}
2018
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002019static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002020 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002021{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002022 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002023
2024 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002025 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002026
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002027 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002028 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002029 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002030
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002031 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002032 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01002033 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002034 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002035
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002036 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002037 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002038
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002039 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2040 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002041 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002042 else
2043 val |= TRANS_PROGRESSIVE;
2044
Daniel Vetterab9412b2013-05-03 11:49:46 +02002045 I915_WRITE(LPT_TRANSCONF, val);
2046 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002047 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002048}
2049
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002050static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2051 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002052{
Daniel Vetter23670b322012-11-01 09:15:30 +01002053 struct drm_device *dev = dev_priv->dev;
2054 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002055
2056 /* FDI relies on the transcoder */
2057 assert_fdi_tx_disabled(dev_priv, pipe);
2058 assert_fdi_rx_disabled(dev_priv, pipe);
2059
Jesse Barnes291906f2011-02-02 12:28:03 -08002060 /* Ports must be off as well */
2061 assert_pch_ports_disabled(dev_priv, pipe);
2062
Daniel Vetterab9412b2013-05-03 11:49:46 +02002063 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002064 val = I915_READ(reg);
2065 val &= ~TRANS_ENABLE;
2066 I915_WRITE(reg, val);
2067 /* wait for PCH transcoder off, transcoder state */
2068 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002069 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002070
2071 if (!HAS_PCH_IBX(dev)) {
2072 /* Workaround: Clear the timing override chicken bit again. */
2073 reg = TRANS_CHICKEN2(pipe);
2074 val = I915_READ(reg);
2075 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2076 I915_WRITE(reg, val);
2077 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002078}
2079
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002080static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002081{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002082 u32 val;
2083
Daniel Vetterab9412b2013-05-03 11:49:46 +02002084 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002085 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002086 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002087 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002088 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002089 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002090
2091 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002092 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01002093 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03002094 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002095}
2096
2097/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002098 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002099 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002100 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002101 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002102 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002103 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002104static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002105{
Paulo Zanoni03722642014-01-17 13:51:09 -02002106 struct drm_device *dev = crtc->base.dev;
2107 struct drm_i915_private *dev_priv = dev->dev_private;
2108 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002109 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2110 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002111 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002112 int reg;
2113 u32 val;
2114
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002115 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
2116
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002117 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002118 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002119 assert_sprites_disabled(dev_priv, pipe);
2120
Paulo Zanoni681e5812012-12-06 11:12:38 -02002121 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002122 pch_transcoder = TRANSCODER_A;
2123 else
2124 pch_transcoder = pipe;
2125
Jesse Barnesb24e7172011-01-04 15:09:30 -08002126 /*
2127 * A pipe without a PLL won't actually be able to drive bits from
2128 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2129 * need the check.
2130 */
Imre Deak50360402015-01-16 00:55:16 -08002131 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002132 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002133 assert_dsi_pll_enabled(dev_priv);
2134 else
2135 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002136 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002137 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002138 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002139 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002140 assert_fdi_tx_pll_enabled(dev_priv,
2141 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002142 }
2143 /* FIXME: assert CPU port conditions for SNB+ */
2144 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002145
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002146 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002147 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002148 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002149 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2150 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002151 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002152 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002153
2154 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002155 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002156}
2157
2158/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002159 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002160 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002161 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002162 * Disable the pipe of @crtc, making sure that various hardware
2163 * specific requirements are met, if applicable, e.g. plane
2164 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002165 *
2166 * Will wait until the pipe has shut down before returning.
2167 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002168static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002169{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002170 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002171 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002172 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002173 int reg;
2174 u32 val;
2175
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002176 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2177
Jesse Barnesb24e7172011-01-04 15:09:30 -08002178 /*
2179 * Make sure planes won't keep trying to pump pixels to us,
2180 * or we might hang the display.
2181 */
2182 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002183 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002184 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002185
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002186 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002187 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002188 if ((val & PIPECONF_ENABLE) == 0)
2189 return;
2190
Ville Syrjälä67adc642014-08-15 01:21:57 +03002191 /*
2192 * Double wide has implications for planes
2193 * so best keep it disabled when not needed.
2194 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002195 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002196 val &= ~PIPECONF_DOUBLE_WIDE;
2197
2198 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002199 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2200 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002201 val &= ~PIPECONF_ENABLE;
2202
2203 I915_WRITE(reg, val);
2204 if ((val & PIPECONF_ENABLE) == 0)
2205 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002206}
2207
Chris Wilson693db182013-03-05 14:52:39 +00002208static bool need_vtd_wa(struct drm_device *dev)
2209{
2210#ifdef CONFIG_INTEL_IOMMU
2211 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2212 return true;
2213#endif
2214 return false;
2215}
2216
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002217unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002218intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01002219 uint64_t fb_format_modifier, unsigned int plane)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002220{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002221 unsigned int tile_height;
2222 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002223
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002224 switch (fb_format_modifier) {
2225 case DRM_FORMAT_MOD_NONE:
2226 tile_height = 1;
2227 break;
2228 case I915_FORMAT_MOD_X_TILED:
2229 tile_height = IS_GEN2(dev) ? 16 : 8;
2230 break;
2231 case I915_FORMAT_MOD_Y_TILED:
2232 tile_height = 32;
2233 break;
2234 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01002235 pixel_bytes = drm_format_plane_cpp(pixel_format, plane);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002236 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002237 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002238 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002239 tile_height = 64;
2240 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002241 case 2:
2242 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002243 tile_height = 32;
2244 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002245 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002246 tile_height = 16;
2247 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002248 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002249 WARN_ONCE(1,
2250 "128-bit pixels are not supported for display!");
2251 tile_height = 16;
2252 break;
2253 }
2254 break;
2255 default:
2256 MISSING_CASE(fb_format_modifier);
2257 tile_height = 1;
2258 break;
2259 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002260
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002261 return tile_height;
2262}
2263
2264unsigned int
2265intel_fb_align_height(struct drm_device *dev, unsigned int height,
2266 uint32_t pixel_format, uint64_t fb_format_modifier)
2267{
2268 return ALIGN(height, intel_tile_height(dev, pixel_format,
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01002269 fb_format_modifier, 0));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002270}
2271
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002272static int
2273intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2274 const struct drm_plane_state *plane_state)
2275{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002276 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002277 unsigned int tile_height, tile_pitch;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002278
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002279 *view = i915_ggtt_view_normal;
2280
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002281 if (!plane_state)
2282 return 0;
2283
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002284 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002285 return 0;
2286
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002287 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002288
2289 info->height = fb->height;
2290 info->pixel_format = fb->pixel_format;
2291 info->pitch = fb->pitches[0];
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002292 info->uv_offset = fb->offsets[1];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002293 info->fb_modifier = fb->modifier[0];
2294
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002295 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01002296 fb->modifier[0], 0);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002297 tile_pitch = PAGE_SIZE / tile_height;
2298 info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2299 info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
2300 info->size = info->width_pages * info->height_pages * PAGE_SIZE;
2301
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002302 if (info->pixel_format == DRM_FORMAT_NV12) {
2303 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
2304 fb->modifier[0], 1);
2305 tile_pitch = PAGE_SIZE / tile_height;
2306 info->width_pages_uv = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2307 info->height_pages_uv = DIV_ROUND_UP(fb->height / 2,
2308 tile_height);
2309 info->size_uv = info->width_pages_uv * info->height_pages_uv *
2310 PAGE_SIZE;
2311 }
2312
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002313 return 0;
2314}
2315
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002316static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2317{
2318 if (INTEL_INFO(dev_priv)->gen >= 9)
2319 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002320 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2321 IS_VALLEYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002322 return 128 * 1024;
2323 else if (INTEL_INFO(dev_priv)->gen >= 4)
2324 return 4 * 1024;
2325 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002326 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002327}
2328
Chris Wilson127bd2a2010-07-23 23:32:05 +01002329int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002330intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2331 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002332 const struct drm_plane_state *plane_state,
John Harrison91af1272015-06-18 13:14:56 +01002333 struct intel_engine_cs *pipelined,
2334 struct drm_i915_gem_request **pipelined_request)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002335{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002336 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002337 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002338 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002339 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002340 u32 alignment;
2341 int ret;
2342
Matt Roperebcdd392014-07-09 16:22:11 -07002343 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2344
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002345 switch (fb->modifier[0]) {
2346 case DRM_FORMAT_MOD_NONE:
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002347 alignment = intel_linear_alignment(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002348 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002349 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002350 if (INTEL_INFO(dev)->gen >= 9)
2351 alignment = 256 * 1024;
2352 else {
2353 /* pin() will align the object as required by fence */
2354 alignment = 0;
2355 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002356 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002357 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002358 case I915_FORMAT_MOD_Yf_TILED:
2359 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2360 "Y tiling bo slipped through, driver bug!\n"))
2361 return -EINVAL;
2362 alignment = 1 * 1024 * 1024;
2363 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002364 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002365 MISSING_CASE(fb->modifier[0]);
2366 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002367 }
2368
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002369 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2370 if (ret)
2371 return ret;
2372
Chris Wilson693db182013-03-05 14:52:39 +00002373 /* Note that the w/a also requires 64 PTE of padding following the
2374 * bo. We currently fill all unused PTE with the shadow page and so
2375 * we should always have valid PTE following the scanout preventing
2376 * the VT-d warning.
2377 */
2378 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2379 alignment = 256 * 1024;
2380
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002381 /*
2382 * Global gtt pte registers are special registers which actually forward
2383 * writes to a chunk of system memory. Which means that there is no risk
2384 * that the register values disappear as soon as we call
2385 * intel_runtime_pm_put(), so it is correct to wrap only the
2386 * pin/unpin/fence and not more.
2387 */
2388 intel_runtime_pm_get(dev_priv);
2389
Chris Wilsonce453d82011-02-21 14:43:56 +00002390 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002391 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
John Harrison91af1272015-06-18 13:14:56 +01002392 pipelined_request, &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002393 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002394 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002395
2396 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2397 * fence, whereas 965+ only requires a fence if using
2398 * framebuffer compression. For simplicity, we always install
2399 * a fence as the cost is not that onerous.
2400 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002401 if (view.type == I915_GGTT_VIEW_NORMAL) {
2402 ret = i915_gem_object_get_fence(obj);
2403 if (ret == -EDEADLK) {
2404 /*
2405 * -EDEADLK means there are no free fences
2406 * no pending flips.
2407 *
2408 * This is propagated to atomic, but it uses
2409 * -EDEADLK to force a locking recovery, so
2410 * change the returned error to -EBUSY.
2411 */
2412 ret = -EBUSY;
2413 goto err_unpin;
2414 } else if (ret)
2415 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002416
Vivek Kasireddy98072162015-10-29 18:54:38 -07002417 i915_gem_object_pin_fence(obj);
2418 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002419
Chris Wilsonce453d82011-02-21 14:43:56 +00002420 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002421 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002422 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002423
2424err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002425 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002426err_interruptible:
2427 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002428 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002429 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002430}
2431
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002432static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2433 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002434{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002435 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002436 struct i915_ggtt_view view;
2437 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002438
Matt Roperebcdd392014-07-09 16:22:11 -07002439 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2440
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002441 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2442 WARN_ONCE(ret, "Couldn't get view from plane state!");
2443
Vivek Kasireddy98072162015-10-29 18:54:38 -07002444 if (view.type == I915_GGTT_VIEW_NORMAL)
2445 i915_gem_object_unpin_fence(obj);
2446
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002447 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002448}
2449
Daniel Vetterc2c75132012-07-05 12:17:30 +02002450/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2451 * is assumed to be a power-of-two. */
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002452unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2453 int *x, int *y,
Chris Wilsonbc752862013-02-21 20:04:31 +00002454 unsigned int tiling_mode,
2455 unsigned int cpp,
2456 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002457{
Chris Wilsonbc752862013-02-21 20:04:31 +00002458 if (tiling_mode != I915_TILING_NONE) {
2459 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002460
Chris Wilsonbc752862013-02-21 20:04:31 +00002461 tile_rows = *y / 8;
2462 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002463
Chris Wilsonbc752862013-02-21 20:04:31 +00002464 tiles = *x / (512/cpp);
2465 *x %= 512/cpp;
2466
2467 return tile_rows * pitch * 8 + tiles * 4096;
2468 } else {
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002469 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
Chris Wilsonbc752862013-02-21 20:04:31 +00002470 unsigned int offset;
2471
2472 offset = *y * pitch + *x * cpp;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002473 *y = (offset & alignment) / pitch;
2474 *x = ((offset & alignment) - *y * pitch) / cpp;
2475 return offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002476 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002477}
2478
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002479static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002480{
2481 switch (format) {
2482 case DISPPLANE_8BPP:
2483 return DRM_FORMAT_C8;
2484 case DISPPLANE_BGRX555:
2485 return DRM_FORMAT_XRGB1555;
2486 case DISPPLANE_BGRX565:
2487 return DRM_FORMAT_RGB565;
2488 default:
2489 case DISPPLANE_BGRX888:
2490 return DRM_FORMAT_XRGB8888;
2491 case DISPPLANE_RGBX888:
2492 return DRM_FORMAT_XBGR8888;
2493 case DISPPLANE_BGRX101010:
2494 return DRM_FORMAT_XRGB2101010;
2495 case DISPPLANE_RGBX101010:
2496 return DRM_FORMAT_XBGR2101010;
2497 }
2498}
2499
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002500static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2501{
2502 switch (format) {
2503 case PLANE_CTL_FORMAT_RGB_565:
2504 return DRM_FORMAT_RGB565;
2505 default:
2506 case PLANE_CTL_FORMAT_XRGB_8888:
2507 if (rgb_order) {
2508 if (alpha)
2509 return DRM_FORMAT_ABGR8888;
2510 else
2511 return DRM_FORMAT_XBGR8888;
2512 } else {
2513 if (alpha)
2514 return DRM_FORMAT_ARGB8888;
2515 else
2516 return DRM_FORMAT_XRGB8888;
2517 }
2518 case PLANE_CTL_FORMAT_XRGB_2101010:
2519 if (rgb_order)
2520 return DRM_FORMAT_XBGR2101010;
2521 else
2522 return DRM_FORMAT_XRGB2101010;
2523 }
2524}
2525
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002526static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002527intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2528 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002529{
2530 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002531 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002532 struct drm_i915_gem_object *obj = NULL;
2533 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002534 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002535 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2536 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2537 PAGE_SIZE);
2538
2539 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002540
Chris Wilsonff2652e2014-03-10 08:07:02 +00002541 if (plane_config->size == 0)
2542 return false;
2543
Paulo Zanoni3badb492015-09-23 12:52:23 -03002544 /* If the FB is too big, just don't use it since fbdev is not very
2545 * important and we should probably use that space with FBC or other
2546 * features. */
2547 if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
2548 return false;
2549
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002550 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2551 base_aligned,
2552 base_aligned,
2553 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002554 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002555 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002556
Damien Lespiau49af4492015-01-20 12:51:44 +00002557 obj->tiling_mode = plane_config->tiling;
2558 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002559 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002560
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002561 mode_cmd.pixel_format = fb->pixel_format;
2562 mode_cmd.width = fb->width;
2563 mode_cmd.height = fb->height;
2564 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002565 mode_cmd.modifier[0] = fb->modifier[0];
2566 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002567
2568 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002569 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002570 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002571 DRM_DEBUG_KMS("intel fb init failed\n");
2572 goto out_unref_obj;
2573 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002574 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002575
Daniel Vetterf6936e22015-03-26 12:17:05 +01002576 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002577 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002578
2579out_unref_obj:
2580 drm_gem_object_unreference(&obj->base);
2581 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002582 return false;
2583}
2584
Matt Roperafd65eb2015-02-03 13:10:04 -08002585/* Update plane->state->fb to match plane->fb after driver-internal updates */
2586static void
2587update_state_fb(struct drm_plane *plane)
2588{
2589 if (plane->fb == plane->state->fb)
2590 return;
2591
2592 if (plane->state->fb)
2593 drm_framebuffer_unreference(plane->state->fb);
2594 plane->state->fb = plane->fb;
2595 if (plane->state->fb)
2596 drm_framebuffer_reference(plane->state->fb);
2597}
2598
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002599static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002600intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2601 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002602{
2603 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002604 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002605 struct drm_crtc *c;
2606 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002607 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002608 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002609 struct drm_plane_state *plane_state = primary->state;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002610 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002611
Damien Lespiau2d140302015-02-05 17:22:18 +00002612 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002613 return;
2614
Daniel Vetterf6936e22015-03-26 12:17:05 +01002615 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002616 fb = &plane_config->fb->base;
2617 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002618 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002619
Damien Lespiau2d140302015-02-05 17:22:18 +00002620 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002621
2622 /*
2623 * Failed to alloc the obj, check to see if we should share
2624 * an fb with another CRTC instead
2625 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002626 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002627 i = to_intel_crtc(c);
2628
2629 if (c == &intel_crtc->base)
2630 continue;
2631
Matt Roper2ff8fde2014-07-08 07:50:07 -07002632 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002633 continue;
2634
Daniel Vetter88595ac2015-03-26 12:42:24 +01002635 fb = c->primary->fb;
2636 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002637 continue;
2638
Daniel Vetter88595ac2015-03-26 12:42:24 +01002639 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002640 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002641 drm_framebuffer_reference(fb);
2642 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002643 }
2644 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002645
2646 return;
2647
2648valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002649 plane_state->src_x = 0;
2650 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002651 plane_state->src_w = fb->width << 16;
2652 plane_state->src_h = fb->height << 16;
2653
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002654 plane_state->crtc_x = 0;
2655 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002656 plane_state->crtc_w = fb->width;
2657 plane_state->crtc_h = fb->height;
2658
Daniel Vetter88595ac2015-03-26 12:42:24 +01002659 obj = intel_fb_obj(fb);
2660 if (obj->tiling_mode != I915_TILING_NONE)
2661 dev_priv->preserve_bios_swizzle = true;
2662
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002663 drm_framebuffer_reference(fb);
2664 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002665 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002666 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002667 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002668}
2669
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002670static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2671 struct drm_framebuffer *fb,
2672 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002673{
2674 struct drm_device *dev = crtc->dev;
2675 struct drm_i915_private *dev_priv = dev->dev_private;
2676 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002677 struct drm_plane *primary = crtc->primary;
2678 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002679 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002680 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002681 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002682 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002683 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302684 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002685
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002686 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002687 I915_WRITE(reg, 0);
2688 if (INTEL_INFO(dev)->gen >= 4)
2689 I915_WRITE(DSPSURF(plane), 0);
2690 else
2691 I915_WRITE(DSPADDR(plane), 0);
2692 POSTING_READ(reg);
2693 return;
2694 }
2695
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002696 obj = intel_fb_obj(fb);
2697 if (WARN_ON(obj == NULL))
2698 return;
2699
2700 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2701
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002702 dspcntr = DISPPLANE_GAMMA_ENABLE;
2703
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002704 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002705
2706 if (INTEL_INFO(dev)->gen < 4) {
2707 if (intel_crtc->pipe == PIPE_B)
2708 dspcntr |= DISPPLANE_SEL_PIPE_B;
2709
2710 /* pipesrc and dspsize control the size that is scaled from,
2711 * which should always be the user's requested size.
2712 */
2713 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002714 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2715 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002716 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002717 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2718 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002719 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2720 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002721 I915_WRITE(PRIMPOS(plane), 0);
2722 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002723 }
2724
Ville Syrjälä57779d02012-10-31 17:50:14 +02002725 switch (fb->pixel_format) {
2726 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002727 dspcntr |= DISPPLANE_8BPP;
2728 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002729 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002730 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002731 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002732 case DRM_FORMAT_RGB565:
2733 dspcntr |= DISPPLANE_BGRX565;
2734 break;
2735 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002736 dspcntr |= DISPPLANE_BGRX888;
2737 break;
2738 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002739 dspcntr |= DISPPLANE_RGBX888;
2740 break;
2741 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002742 dspcntr |= DISPPLANE_BGRX101010;
2743 break;
2744 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002745 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002746 break;
2747 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002748 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002749 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002750
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002751 if (INTEL_INFO(dev)->gen >= 4 &&
2752 obj->tiling_mode != I915_TILING_NONE)
2753 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002754
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002755 if (IS_G4X(dev))
2756 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2757
Ville Syrjäläb98971272014-08-27 16:51:22 +03002758 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002759
Daniel Vetterc2c75132012-07-05 12:17:30 +02002760 if (INTEL_INFO(dev)->gen >= 4) {
2761 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002762 intel_gen4_compute_page_offset(dev_priv,
2763 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002764 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002765 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002766 linear_offset -= intel_crtc->dspaddr_offset;
2767 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002768 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002769 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002770
Matt Roper8e7d6882015-01-21 16:35:41 -08002771 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302772 dspcntr |= DISPPLANE_ROTATE_180;
2773
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002774 x += (intel_crtc->config->pipe_src_w - 1);
2775 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302776
2777 /* Finding the last pixel of the last line of the display
2778 data and adding to linear_offset*/
2779 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002780 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2781 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302782 }
2783
Paulo Zanoni2db33662015-09-14 15:20:03 -03002784 intel_crtc->adjusted_x = x;
2785 intel_crtc->adjusted_y = y;
2786
Sonika Jindal48404c12014-08-22 14:06:04 +05302787 I915_WRITE(reg, dspcntr);
2788
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002789 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002790 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002791 I915_WRITE(DSPSURF(plane),
2792 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002793 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002794 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002795 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002796 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002797 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002798}
2799
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002800static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2801 struct drm_framebuffer *fb,
2802 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002803{
2804 struct drm_device *dev = crtc->dev;
2805 struct drm_i915_private *dev_priv = dev->dev_private;
2806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002807 struct drm_plane *primary = crtc->primary;
2808 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002809 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002810 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002811 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002812 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002813 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302814 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002815
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002816 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002817 I915_WRITE(reg, 0);
2818 I915_WRITE(DSPSURF(plane), 0);
2819 POSTING_READ(reg);
2820 return;
2821 }
2822
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002823 obj = intel_fb_obj(fb);
2824 if (WARN_ON(obj == NULL))
2825 return;
2826
2827 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2828
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002829 dspcntr = DISPPLANE_GAMMA_ENABLE;
2830
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002831 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002832
2833 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2834 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2835
Ville Syrjälä57779d02012-10-31 17:50:14 +02002836 switch (fb->pixel_format) {
2837 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002838 dspcntr |= DISPPLANE_8BPP;
2839 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002840 case DRM_FORMAT_RGB565:
2841 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002842 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002843 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002844 dspcntr |= DISPPLANE_BGRX888;
2845 break;
2846 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002847 dspcntr |= DISPPLANE_RGBX888;
2848 break;
2849 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002850 dspcntr |= DISPPLANE_BGRX101010;
2851 break;
2852 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002853 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002854 break;
2855 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002856 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002857 }
2858
2859 if (obj->tiling_mode != I915_TILING_NONE)
2860 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002861
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002862 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002863 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002864
Ville Syrjäläb98971272014-08-27 16:51:22 +03002865 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002866 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002867 intel_gen4_compute_page_offset(dev_priv,
2868 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002869 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002870 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002871 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002872 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302873 dspcntr |= DISPPLANE_ROTATE_180;
2874
2875 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002876 x += (intel_crtc->config->pipe_src_w - 1);
2877 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302878
2879 /* Finding the last pixel of the last line of the display
2880 data and adding to linear_offset*/
2881 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002882 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2883 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302884 }
2885 }
2886
Paulo Zanoni2db33662015-09-14 15:20:03 -03002887 intel_crtc->adjusted_x = x;
2888 intel_crtc->adjusted_y = y;
2889
Sonika Jindal48404c12014-08-22 14:06:04 +05302890 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002891
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002892 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002893 I915_WRITE(DSPSURF(plane),
2894 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002895 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002896 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2897 } else {
2898 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2899 I915_WRITE(DSPLINOFF(plane), linear_offset);
2900 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002901 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002902}
2903
Damien Lespiaub3218032015-02-27 11:15:18 +00002904u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2905 uint32_t pixel_format)
2906{
2907 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2908
2909 /*
2910 * The stride is either expressed as a multiple of 64 bytes
2911 * chunks for linear buffers or in number of tiles for tiled
2912 * buffers.
2913 */
2914 switch (fb_modifier) {
2915 case DRM_FORMAT_MOD_NONE:
2916 return 64;
2917 case I915_FORMAT_MOD_X_TILED:
2918 if (INTEL_INFO(dev)->gen == 2)
2919 return 128;
2920 return 512;
2921 case I915_FORMAT_MOD_Y_TILED:
2922 /* No need to check for old gens and Y tiling since this is
2923 * about the display engine and those will be blocked before
2924 * we get here.
2925 */
2926 return 128;
2927 case I915_FORMAT_MOD_Yf_TILED:
2928 if (bits_per_pixel == 8)
2929 return 64;
2930 else
2931 return 128;
2932 default:
2933 MISSING_CASE(fb_modifier);
2934 return 64;
2935 }
2936}
2937
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002938unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002939 struct drm_i915_gem_object *obj,
2940 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002941{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002942 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002943 struct i915_vma *vma;
2944 unsigned char *offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002945
2946 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002947 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002948
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002949 vma = i915_gem_obj_to_ggtt_view(obj, view);
2950 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
2951 view->type))
2952 return -1;
2953
2954 offset = (unsigned char *)vma->node.start;
2955
2956 if (plane == 1) {
2957 offset += vma->ggtt_view.rotation_info.uv_start_page *
2958 PAGE_SIZE;
2959 }
2960
2961 return (unsigned long)offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002962}
2963
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002964static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2965{
2966 struct drm_device *dev = intel_crtc->base.dev;
2967 struct drm_i915_private *dev_priv = dev->dev_private;
2968
2969 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2970 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2971 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002972}
2973
Chandra Kondurua1b22782015-04-07 15:28:45 -07002974/*
2975 * This function detaches (aka. unbinds) unused scalers in hardware
2976 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002977static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002978{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002979 struct intel_crtc_scaler_state *scaler_state;
2980 int i;
2981
Chandra Kondurua1b22782015-04-07 15:28:45 -07002982 scaler_state = &intel_crtc->config->scaler_state;
2983
2984 /* loop through and disable scalers that aren't in use */
2985 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002986 if (!scaler_state->scalers[i].in_use)
2987 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002988 }
2989}
2990
Chandra Konduru6156a452015-04-27 13:48:39 -07002991u32 skl_plane_ctl_format(uint32_t pixel_format)
2992{
Chandra Konduru6156a452015-04-27 13:48:39 -07002993 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002994 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002995 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002996 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002997 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002998 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002999 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003000 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003001 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003002 /*
3003 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3004 * to be already pre-multiplied. We need to add a knob (or a different
3005 * DRM_FORMAT) for user-space to configure that.
3006 */
3007 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003008 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07003009 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003010 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003011 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07003012 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003013 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003014 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003015 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003016 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003017 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003018 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003019 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003020 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003021 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003022 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003023 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003024 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003025 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003026 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003027 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003028
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003029 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003030}
3031
3032u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3033{
Chandra Konduru6156a452015-04-27 13:48:39 -07003034 switch (fb_modifier) {
3035 case DRM_FORMAT_MOD_NONE:
3036 break;
3037 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003038 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003039 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003040 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003041 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003042 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003043 default:
3044 MISSING_CASE(fb_modifier);
3045 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003046
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003047 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003048}
3049
3050u32 skl_plane_ctl_rotation(unsigned int rotation)
3051{
Chandra Konduru6156a452015-04-27 13:48:39 -07003052 switch (rotation) {
3053 case BIT(DRM_ROTATE_0):
3054 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303055 /*
3056 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3057 * while i915 HW rotation is clockwise, thats why this swapping.
3058 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003059 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303060 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003061 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003062 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003063 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303064 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003065 default:
3066 MISSING_CASE(rotation);
3067 }
3068
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003069 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003070}
3071
Damien Lespiau70d21f02013-07-03 21:06:04 +01003072static void skylake_update_primary_plane(struct drm_crtc *crtc,
3073 struct drm_framebuffer *fb,
3074 int x, int y)
3075{
3076 struct drm_device *dev = crtc->dev;
3077 struct drm_i915_private *dev_priv = dev->dev_private;
3078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003079 struct drm_plane *plane = crtc->primary;
3080 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003081 struct drm_i915_gem_object *obj;
3082 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303083 u32 plane_ctl, stride_div, stride;
3084 u32 tile_height, plane_offset, plane_size;
3085 unsigned int rotation;
3086 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003087 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003088 struct intel_crtc_state *crtc_state = intel_crtc->config;
3089 struct intel_plane_state *plane_state;
3090 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3091 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3092 int scaler_id = -1;
3093
Chandra Konduru6156a452015-04-27 13:48:39 -07003094 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003095
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003096 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003097 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3098 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3099 POSTING_READ(PLANE_CTL(pipe, 0));
3100 return;
3101 }
3102
3103 plane_ctl = PLANE_CTL_ENABLE |
3104 PLANE_CTL_PIPE_GAMMA_ENABLE |
3105 PLANE_CTL_PIPE_CSC_ENABLE;
3106
Chandra Konduru6156a452015-04-27 13:48:39 -07003107 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3108 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003109 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303110
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303111 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003112 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003113
Damien Lespiaub3218032015-02-27 11:15:18 +00003114 obj = intel_fb_obj(fb);
3115 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3116 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003117 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303118
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003119 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003120
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003121 scaler_id = plane_state->scaler_id;
3122 src_x = plane_state->src.x1 >> 16;
3123 src_y = plane_state->src.y1 >> 16;
3124 src_w = drm_rect_width(&plane_state->src) >> 16;
3125 src_h = drm_rect_height(&plane_state->src) >> 16;
3126 dst_x = plane_state->dst.x1;
3127 dst_y = plane_state->dst.y1;
3128 dst_w = drm_rect_width(&plane_state->dst);
3129 dst_h = drm_rect_height(&plane_state->dst);
3130
3131 WARN_ON(x != src_x || y != src_y);
Chandra Konduru6156a452015-04-27 13:48:39 -07003132
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303133 if (intel_rotation_90_or_270(rotation)) {
3134 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003135 tile_height = intel_tile_height(dev, fb->pixel_format,
Tvrtko Ursulinfe47ea02015-09-21 10:45:32 +01003136 fb->modifier[0], 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303137 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003138 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303139 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003140 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303141 } else {
3142 stride = fb->pitches[0] / stride_div;
3143 x_offset = x;
3144 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003145 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303146 }
3147 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003148
Paulo Zanoni2db33662015-09-14 15:20:03 -03003149 intel_crtc->adjusted_x = x_offset;
3150 intel_crtc->adjusted_y = y_offset;
3151
Damien Lespiau70d21f02013-07-03 21:06:04 +01003152 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303153 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3154 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3155 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003156
3157 if (scaler_id >= 0) {
3158 uint32_t ps_ctrl = 0;
3159
3160 WARN_ON(!dst_w || !dst_h);
3161 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3162 crtc_state->scaler_state.scalers[scaler_id].mode;
3163 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3164 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3165 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3166 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3167 I915_WRITE(PLANE_POS(pipe, 0), 0);
3168 } else {
3169 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3170 }
3171
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003172 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003173
3174 POSTING_READ(PLANE_SURF(pipe, 0));
3175}
3176
Jesse Barnes17638cd2011-06-24 12:19:23 -07003177/* Assume fb object is pinned & idle & fenced and just update base pointers */
3178static int
3179intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3180 int x, int y, enum mode_set_atomic state)
3181{
3182 struct drm_device *dev = crtc->dev;
3183 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003184
Paulo Zanoniff2a3112015-07-07 15:26:03 -03003185 if (dev_priv->fbc.disable_fbc)
Paulo Zanoni7733b492015-07-07 15:26:04 -03003186 dev_priv->fbc.disable_fbc(dev_priv);
Jesse Barnes81255562010-08-02 12:07:50 -07003187
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003188 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3189
3190 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003191}
3192
Ville Syrjälä75147472014-11-24 18:28:11 +02003193static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003194{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003195 struct drm_crtc *crtc;
3196
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003197 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003198 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3199 enum plane plane = intel_crtc->plane;
3200
3201 intel_prepare_page_flip(dev, plane);
3202 intel_finish_page_flip_plane(dev, plane);
3203 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003204}
3205
3206static void intel_update_primary_planes(struct drm_device *dev)
3207{
Ville Syrjälä75147472014-11-24 18:28:11 +02003208 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003209
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003210 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003211 struct intel_plane *plane = to_intel_plane(crtc->primary);
3212 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003213
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003214 drm_modeset_lock_crtc(crtc, &plane->base);
3215
3216 plane_state = to_intel_plane_state(plane->base.state);
3217
3218 if (plane_state->base.fb)
3219 plane->commit_plane(&plane->base, plane_state);
3220
3221 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003222 }
3223}
3224
Ville Syrjälä75147472014-11-24 18:28:11 +02003225void intel_prepare_reset(struct drm_device *dev)
3226{
3227 /* no reset support for gen2 */
3228 if (IS_GEN2(dev))
3229 return;
3230
3231 /* reset doesn't touch the display */
3232 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3233 return;
3234
3235 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003236 /*
3237 * Disabling the crtcs gracefully seems nicer. Also the
3238 * g33 docs say we should at least disable all the planes.
3239 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003240 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003241}
3242
3243void intel_finish_reset(struct drm_device *dev)
3244{
3245 struct drm_i915_private *dev_priv = to_i915(dev);
3246
3247 /*
3248 * Flips in the rings will be nuked by the reset,
3249 * so complete all pending flips so that user space
3250 * will get its events and not get stuck.
3251 */
3252 intel_complete_page_flips(dev);
3253
3254 /* no reset support for gen2 */
3255 if (IS_GEN2(dev))
3256 return;
3257
3258 /* reset doesn't touch the display */
3259 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3260 /*
3261 * Flips in the rings have been nuked by the reset,
3262 * so update the base address of all primary
3263 * planes to the the last fb to make sure we're
3264 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003265 *
3266 * FIXME: Atomic will make this obsolete since we won't schedule
3267 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003268 */
3269 intel_update_primary_planes(dev);
3270 return;
3271 }
3272
3273 /*
3274 * The display has been reset as well,
3275 * so need a full re-initialization.
3276 */
3277 intel_runtime_pm_disable_interrupts(dev_priv);
3278 intel_runtime_pm_enable_interrupts(dev_priv);
3279
3280 intel_modeset_init_hw(dev);
3281
3282 spin_lock_irq(&dev_priv->irq_lock);
3283 if (dev_priv->display.hpd_irq_setup)
3284 dev_priv->display.hpd_irq_setup(dev);
3285 spin_unlock_irq(&dev_priv->irq_lock);
3286
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003287 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003288
3289 intel_hpd_init(dev_priv);
3290
3291 drm_modeset_unlock_all(dev);
3292}
3293
Chris Wilson2e2f3512015-04-27 13:41:14 +01003294static void
Chris Wilson14667a42012-04-03 17:58:35 +01003295intel_finish_fb(struct drm_framebuffer *old_fb)
3296{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003297 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003298 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003299 bool was_interruptible = dev_priv->mm.interruptible;
3300 int ret;
3301
Chris Wilson14667a42012-04-03 17:58:35 +01003302 /* Big Hammer, we also need to ensure that any pending
3303 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3304 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003305 * framebuffer. Note that we rely on userspace rendering
3306 * into the buffer attached to the pipe they are waiting
3307 * on. If not, userspace generates a GPU hang with IPEHR
3308 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003309 *
3310 * This should only fail upon a hung GPU, in which case we
3311 * can safely continue.
3312 */
3313 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003314 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003315 dev_priv->mm.interruptible = was_interruptible;
3316
Chris Wilson2e2f3512015-04-27 13:41:14 +01003317 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003318}
3319
Chris Wilson7d5e3792014-03-04 13:15:08 +00003320static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3321{
3322 struct drm_device *dev = crtc->dev;
3323 struct drm_i915_private *dev_priv = dev->dev_private;
3324 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003325 bool pending;
3326
3327 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3328 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3329 return false;
3330
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003331 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003332 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003333 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003334
3335 return pending;
3336}
3337
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003338static void intel_update_pipe_config(struct intel_crtc *crtc,
3339 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003340{
3341 struct drm_device *dev = crtc->base.dev;
3342 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003343 struct intel_crtc_state *pipe_config =
3344 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003345
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003346 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3347 crtc->base.mode = crtc->base.state->mode;
3348
3349 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3350 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3351 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003352
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003353 if (HAS_DDI(dev))
3354 intel_set_pipe_csc(&crtc->base);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003355
3356 /*
3357 * Update pipe size and adjust fitter if needed: the reason for this is
3358 * that in compute_mode_changes we check the native mode (not the pfit
3359 * mode) to see if we can flip rather than do a full mode set. In the
3360 * fastboot case, we'll flip, but if we don't update the pipesrc and
3361 * pfit state, we'll end up with a big fb scanned out into the wrong
3362 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003363 */
3364
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003365 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003366 ((pipe_config->pipe_src_w - 1) << 16) |
3367 (pipe_config->pipe_src_h - 1));
3368
3369 /* on skylake this is done by detaching scalers */
3370 if (INTEL_INFO(dev)->gen >= 9) {
3371 skl_detach_scalers(crtc);
3372
3373 if (pipe_config->pch_pfit.enabled)
3374 skylake_pfit_enable(crtc);
3375 } else if (HAS_PCH_SPLIT(dev)) {
3376 if (pipe_config->pch_pfit.enabled)
3377 ironlake_pfit_enable(crtc);
3378 else if (old_crtc_state->pch_pfit.enabled)
3379 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003380 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003381}
3382
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003383static void intel_fdi_normal_train(struct drm_crtc *crtc)
3384{
3385 struct drm_device *dev = crtc->dev;
3386 struct drm_i915_private *dev_priv = dev->dev_private;
3387 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3388 int pipe = intel_crtc->pipe;
3389 u32 reg, temp;
3390
3391 /* enable normal train */
3392 reg = FDI_TX_CTL(pipe);
3393 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003394 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003395 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3396 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003397 } else {
3398 temp &= ~FDI_LINK_TRAIN_NONE;
3399 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003400 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003401 I915_WRITE(reg, temp);
3402
3403 reg = FDI_RX_CTL(pipe);
3404 temp = I915_READ(reg);
3405 if (HAS_PCH_CPT(dev)) {
3406 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3407 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3408 } else {
3409 temp &= ~FDI_LINK_TRAIN_NONE;
3410 temp |= FDI_LINK_TRAIN_NONE;
3411 }
3412 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3413
3414 /* wait one idle pattern time */
3415 POSTING_READ(reg);
3416 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003417
3418 /* IVB wants error correction enabled */
3419 if (IS_IVYBRIDGE(dev))
3420 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3421 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003422}
3423
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003424/* The FDI link training functions for ILK/Ibexpeak. */
3425static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3426{
3427 struct drm_device *dev = crtc->dev;
3428 struct drm_i915_private *dev_priv = dev->dev_private;
3429 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3430 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003431 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003432
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003433 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003434 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003435
Adam Jacksone1a44742010-06-25 15:32:14 -04003436 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3437 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003438 reg = FDI_RX_IMR(pipe);
3439 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003440 temp &= ~FDI_RX_SYMBOL_LOCK;
3441 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003442 I915_WRITE(reg, temp);
3443 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003444 udelay(150);
3445
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003446 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003447 reg = FDI_TX_CTL(pipe);
3448 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003449 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003450 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003451 temp &= ~FDI_LINK_TRAIN_NONE;
3452 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003453 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003454
Chris Wilson5eddb702010-09-11 13:48:45 +01003455 reg = FDI_RX_CTL(pipe);
3456 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003457 temp &= ~FDI_LINK_TRAIN_NONE;
3458 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003459 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3460
3461 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003462 udelay(150);
3463
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003464 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003465 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3466 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3467 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003468
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003470 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003471 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003472 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3473
3474 if ((temp & FDI_RX_BIT_LOCK)) {
3475 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003476 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003477 break;
3478 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003479 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003480 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003481 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003482
3483 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003484 reg = FDI_TX_CTL(pipe);
3485 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003486 temp &= ~FDI_LINK_TRAIN_NONE;
3487 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003488 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003489
Chris Wilson5eddb702010-09-11 13:48:45 +01003490 reg = FDI_RX_CTL(pipe);
3491 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003492 temp &= ~FDI_LINK_TRAIN_NONE;
3493 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003494 I915_WRITE(reg, temp);
3495
3496 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003497 udelay(150);
3498
Chris Wilson5eddb702010-09-11 13:48:45 +01003499 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003500 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003501 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003502 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3503
3504 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003505 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003506 DRM_DEBUG_KMS("FDI train 2 done.\n");
3507 break;
3508 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003509 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003510 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003511 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003512
3513 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003514
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003515}
3516
Akshay Joshi0206e352011-08-16 15:34:10 -04003517static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003518 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3519 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3520 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3521 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3522};
3523
3524/* The FDI link training functions for SNB/Cougarpoint. */
3525static void gen6_fdi_link_train(struct drm_crtc *crtc)
3526{
3527 struct drm_device *dev = crtc->dev;
3528 struct drm_i915_private *dev_priv = dev->dev_private;
3529 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3530 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003531 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003532
Adam Jacksone1a44742010-06-25 15:32:14 -04003533 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3534 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003535 reg = FDI_RX_IMR(pipe);
3536 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003537 temp &= ~FDI_RX_SYMBOL_LOCK;
3538 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003539 I915_WRITE(reg, temp);
3540
3541 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003542 udelay(150);
3543
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003544 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003545 reg = FDI_TX_CTL(pipe);
3546 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003547 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003548 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003549 temp &= ~FDI_LINK_TRAIN_NONE;
3550 temp |= FDI_LINK_TRAIN_PATTERN_1;
3551 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3552 /* SNB-B */
3553 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003554 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003555
Daniel Vetterd74cf322012-10-26 10:58:13 +02003556 I915_WRITE(FDI_RX_MISC(pipe),
3557 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3558
Chris Wilson5eddb702010-09-11 13:48:45 +01003559 reg = FDI_RX_CTL(pipe);
3560 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003561 if (HAS_PCH_CPT(dev)) {
3562 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3563 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3564 } else {
3565 temp &= ~FDI_LINK_TRAIN_NONE;
3566 temp |= FDI_LINK_TRAIN_PATTERN_1;
3567 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003568 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3569
3570 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003571 udelay(150);
3572
Akshay Joshi0206e352011-08-16 15:34:10 -04003573 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003574 reg = FDI_TX_CTL(pipe);
3575 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003576 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3577 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003578 I915_WRITE(reg, temp);
3579
3580 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003581 udelay(500);
3582
Sean Paulfa37d392012-03-02 12:53:39 -05003583 for (retry = 0; retry < 5; retry++) {
3584 reg = FDI_RX_IIR(pipe);
3585 temp = I915_READ(reg);
3586 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3587 if (temp & FDI_RX_BIT_LOCK) {
3588 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3589 DRM_DEBUG_KMS("FDI train 1 done.\n");
3590 break;
3591 }
3592 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003593 }
Sean Paulfa37d392012-03-02 12:53:39 -05003594 if (retry < 5)
3595 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003596 }
3597 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003598 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003599
3600 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003601 reg = FDI_TX_CTL(pipe);
3602 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003603 temp &= ~FDI_LINK_TRAIN_NONE;
3604 temp |= FDI_LINK_TRAIN_PATTERN_2;
3605 if (IS_GEN6(dev)) {
3606 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3607 /* SNB-B */
3608 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3609 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003610 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003611
Chris Wilson5eddb702010-09-11 13:48:45 +01003612 reg = FDI_RX_CTL(pipe);
3613 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003614 if (HAS_PCH_CPT(dev)) {
3615 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3616 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3617 } else {
3618 temp &= ~FDI_LINK_TRAIN_NONE;
3619 temp |= FDI_LINK_TRAIN_PATTERN_2;
3620 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003621 I915_WRITE(reg, temp);
3622
3623 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003624 udelay(150);
3625
Akshay Joshi0206e352011-08-16 15:34:10 -04003626 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003627 reg = FDI_TX_CTL(pipe);
3628 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003629 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3630 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003631 I915_WRITE(reg, temp);
3632
3633 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003634 udelay(500);
3635
Sean Paulfa37d392012-03-02 12:53:39 -05003636 for (retry = 0; retry < 5; retry++) {
3637 reg = FDI_RX_IIR(pipe);
3638 temp = I915_READ(reg);
3639 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3640 if (temp & FDI_RX_SYMBOL_LOCK) {
3641 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3642 DRM_DEBUG_KMS("FDI train 2 done.\n");
3643 break;
3644 }
3645 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003646 }
Sean Paulfa37d392012-03-02 12:53:39 -05003647 if (retry < 5)
3648 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003649 }
3650 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003651 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003652
3653 DRM_DEBUG_KMS("FDI train done.\n");
3654}
3655
Jesse Barnes357555c2011-04-28 15:09:55 -07003656/* Manual link training for Ivy Bridge A0 parts */
3657static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3658{
3659 struct drm_device *dev = crtc->dev;
3660 struct drm_i915_private *dev_priv = dev->dev_private;
3661 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3662 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003663 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003664
3665 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3666 for train result */
3667 reg = FDI_RX_IMR(pipe);
3668 temp = I915_READ(reg);
3669 temp &= ~FDI_RX_SYMBOL_LOCK;
3670 temp &= ~FDI_RX_BIT_LOCK;
3671 I915_WRITE(reg, temp);
3672
3673 POSTING_READ(reg);
3674 udelay(150);
3675
Daniel Vetter01a415f2012-10-27 15:58:40 +02003676 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3677 I915_READ(FDI_RX_IIR(pipe)));
3678
Jesse Barnes139ccd32013-08-19 11:04:55 -07003679 /* Try each vswing and preemphasis setting twice before moving on */
3680 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3681 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003682 reg = FDI_TX_CTL(pipe);
3683 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003684 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3685 temp &= ~FDI_TX_ENABLE;
3686 I915_WRITE(reg, temp);
3687
3688 reg = FDI_RX_CTL(pipe);
3689 temp = I915_READ(reg);
3690 temp &= ~FDI_LINK_TRAIN_AUTO;
3691 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3692 temp &= ~FDI_RX_ENABLE;
3693 I915_WRITE(reg, temp);
3694
3695 /* enable CPU FDI TX and PCH FDI RX */
3696 reg = FDI_TX_CTL(pipe);
3697 temp = I915_READ(reg);
3698 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003699 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003700 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003701 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003702 temp |= snb_b_fdi_train_param[j/2];
3703 temp |= FDI_COMPOSITE_SYNC;
3704 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3705
3706 I915_WRITE(FDI_RX_MISC(pipe),
3707 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3708
3709 reg = FDI_RX_CTL(pipe);
3710 temp = I915_READ(reg);
3711 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3712 temp |= FDI_COMPOSITE_SYNC;
3713 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3714
3715 POSTING_READ(reg);
3716 udelay(1); /* should be 0.5us */
3717
3718 for (i = 0; i < 4; i++) {
3719 reg = FDI_RX_IIR(pipe);
3720 temp = I915_READ(reg);
3721 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3722
3723 if (temp & FDI_RX_BIT_LOCK ||
3724 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3725 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3726 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3727 i);
3728 break;
3729 }
3730 udelay(1); /* should be 0.5us */
3731 }
3732 if (i == 4) {
3733 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3734 continue;
3735 }
3736
3737 /* Train 2 */
3738 reg = FDI_TX_CTL(pipe);
3739 temp = I915_READ(reg);
3740 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3741 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3742 I915_WRITE(reg, temp);
3743
3744 reg = FDI_RX_CTL(pipe);
3745 temp = I915_READ(reg);
3746 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3747 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003748 I915_WRITE(reg, temp);
3749
3750 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003751 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003752
Jesse Barnes139ccd32013-08-19 11:04:55 -07003753 for (i = 0; i < 4; i++) {
3754 reg = FDI_RX_IIR(pipe);
3755 temp = I915_READ(reg);
3756 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003757
Jesse Barnes139ccd32013-08-19 11:04:55 -07003758 if (temp & FDI_RX_SYMBOL_LOCK ||
3759 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3760 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3761 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3762 i);
3763 goto train_done;
3764 }
3765 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003766 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003767 if (i == 4)
3768 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003769 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003770
Jesse Barnes139ccd32013-08-19 11:04:55 -07003771train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003772 DRM_DEBUG_KMS("FDI train done.\n");
3773}
3774
Daniel Vetter88cefb62012-08-12 19:27:14 +02003775static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003776{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003777 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003778 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003779 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003780 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003781
Jesse Barnesc64e3112010-09-10 11:27:03 -07003782
Jesse Barnes0e23b992010-09-10 11:10:00 -07003783 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003784 reg = FDI_RX_CTL(pipe);
3785 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003786 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003787 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003788 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003789 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3790
3791 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003792 udelay(200);
3793
3794 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003795 temp = I915_READ(reg);
3796 I915_WRITE(reg, temp | FDI_PCDCLK);
3797
3798 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003799 udelay(200);
3800
Paulo Zanoni20749732012-11-23 15:30:38 -02003801 /* Enable CPU FDI TX PLL, always on for Ironlake */
3802 reg = FDI_TX_CTL(pipe);
3803 temp = I915_READ(reg);
3804 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3805 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003806
Paulo Zanoni20749732012-11-23 15:30:38 -02003807 POSTING_READ(reg);
3808 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003809 }
3810}
3811
Daniel Vetter88cefb62012-08-12 19:27:14 +02003812static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3813{
3814 struct drm_device *dev = intel_crtc->base.dev;
3815 struct drm_i915_private *dev_priv = dev->dev_private;
3816 int pipe = intel_crtc->pipe;
3817 u32 reg, temp;
3818
3819 /* Switch from PCDclk to Rawclk */
3820 reg = FDI_RX_CTL(pipe);
3821 temp = I915_READ(reg);
3822 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3823
3824 /* Disable CPU FDI TX PLL */
3825 reg = FDI_TX_CTL(pipe);
3826 temp = I915_READ(reg);
3827 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3828
3829 POSTING_READ(reg);
3830 udelay(100);
3831
3832 reg = FDI_RX_CTL(pipe);
3833 temp = I915_READ(reg);
3834 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3835
3836 /* Wait for the clocks to turn off. */
3837 POSTING_READ(reg);
3838 udelay(100);
3839}
3840
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003841static void ironlake_fdi_disable(struct drm_crtc *crtc)
3842{
3843 struct drm_device *dev = crtc->dev;
3844 struct drm_i915_private *dev_priv = dev->dev_private;
3845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3846 int pipe = intel_crtc->pipe;
3847 u32 reg, temp;
3848
3849 /* disable CPU FDI tx and PCH FDI rx */
3850 reg = FDI_TX_CTL(pipe);
3851 temp = I915_READ(reg);
3852 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3853 POSTING_READ(reg);
3854
3855 reg = FDI_RX_CTL(pipe);
3856 temp = I915_READ(reg);
3857 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003858 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003859 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3860
3861 POSTING_READ(reg);
3862 udelay(100);
3863
3864 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003865 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003866 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003867
3868 /* still set train pattern 1 */
3869 reg = FDI_TX_CTL(pipe);
3870 temp = I915_READ(reg);
3871 temp &= ~FDI_LINK_TRAIN_NONE;
3872 temp |= FDI_LINK_TRAIN_PATTERN_1;
3873 I915_WRITE(reg, temp);
3874
3875 reg = FDI_RX_CTL(pipe);
3876 temp = I915_READ(reg);
3877 if (HAS_PCH_CPT(dev)) {
3878 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3879 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3880 } else {
3881 temp &= ~FDI_LINK_TRAIN_NONE;
3882 temp |= FDI_LINK_TRAIN_PATTERN_1;
3883 }
3884 /* BPC in FDI rx is consistent with that in PIPECONF */
3885 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003886 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003887 I915_WRITE(reg, temp);
3888
3889 POSTING_READ(reg);
3890 udelay(100);
3891}
3892
Chris Wilson5dce5b932014-01-20 10:17:36 +00003893bool intel_has_pending_fb_unpin(struct drm_device *dev)
3894{
3895 struct intel_crtc *crtc;
3896
3897 /* Note that we don't need to be called with mode_config.lock here
3898 * as our list of CRTC objects is static for the lifetime of the
3899 * device and so cannot disappear as we iterate. Similarly, we can
3900 * happily treat the predicates as racy, atomic checks as userspace
3901 * cannot claim and pin a new fb without at least acquring the
3902 * struct_mutex and so serialising with us.
3903 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003904 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003905 if (atomic_read(&crtc->unpin_work_count) == 0)
3906 continue;
3907
3908 if (crtc->unpin_work)
3909 intel_wait_for_vblank(dev, crtc->pipe);
3910
3911 return true;
3912 }
3913
3914 return false;
3915}
3916
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003917static void page_flip_completed(struct intel_crtc *intel_crtc)
3918{
3919 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3920 struct intel_unpin_work *work = intel_crtc->unpin_work;
3921
3922 /* ensure that the unpin work is consistent wrt ->pending. */
3923 smp_rmb();
3924 intel_crtc->unpin_work = NULL;
3925
3926 if (work->event)
3927 drm_send_vblank_event(intel_crtc->base.dev,
3928 intel_crtc->pipe,
3929 work->event);
3930
3931 drm_crtc_vblank_put(&intel_crtc->base);
3932
3933 wake_up_all(&dev_priv->pending_flip_queue);
3934 queue_work(dev_priv->wq, &work->work);
3935
3936 trace_i915_flip_complete(intel_crtc->plane,
3937 work->pending_flip_obj);
3938}
3939
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003940void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003941{
Chris Wilson0f911282012-04-17 10:05:38 +01003942 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003943 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003944
Daniel Vetter2c10d572012-12-20 21:24:07 +01003945 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003946 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3947 !intel_crtc_has_pending_flip(crtc),
3948 60*HZ) == 0)) {
3949 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003950
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003951 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003952 if (intel_crtc->unpin_work) {
3953 WARN_ONCE(1, "Removing stuck page flip\n");
3954 page_flip_completed(intel_crtc);
3955 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003956 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003957 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003958
Chris Wilson975d5682014-08-20 13:13:34 +01003959 if (crtc->primary->fb) {
3960 mutex_lock(&dev->struct_mutex);
3961 intel_finish_fb(crtc->primary->fb);
3962 mutex_unlock(&dev->struct_mutex);
3963 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003964}
3965
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003966/* Program iCLKIP clock to the desired frequency */
3967static void lpt_program_iclkip(struct drm_crtc *crtc)
3968{
3969 struct drm_device *dev = crtc->dev;
3970 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003971 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003972 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3973 u32 temp;
3974
Ville Syrjäläa5805162015-05-26 20:42:30 +03003975 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003976
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003977 /* It is necessary to ungate the pixclk gate prior to programming
3978 * the divisors, and gate it back when it is done.
3979 */
3980 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3981
3982 /* Disable SSCCTL */
3983 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003984 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3985 SBI_SSCCTL_DISABLE,
3986 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003987
3988 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003989 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003990 auxdiv = 1;
3991 divsel = 0x41;
3992 phaseinc = 0x20;
3993 } else {
3994 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003995 * but the adjusted_mode->crtc_clock in in KHz. To get the
3996 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003997 * convert the virtual clock precision to KHz here for higher
3998 * precision.
3999 */
4000 u32 iclk_virtual_root_freq = 172800 * 1000;
4001 u32 iclk_pi_range = 64;
4002 u32 desired_divisor, msb_divisor_value, pi_value;
4003
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03004004 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004005 msb_divisor_value = desired_divisor / iclk_pi_range;
4006 pi_value = desired_divisor % iclk_pi_range;
4007
4008 auxdiv = 0;
4009 divsel = msb_divisor_value - 2;
4010 phaseinc = pi_value;
4011 }
4012
4013 /* This should not happen with any sane values */
4014 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4015 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4016 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4017 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4018
4019 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 +03004020 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004021 auxdiv,
4022 divsel,
4023 phasedir,
4024 phaseinc);
4025
4026 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004027 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004028 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4029 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4030 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4031 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4032 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4033 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004034 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004035
4036 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004037 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004038 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4039 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004040 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004041
4042 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004043 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004044 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004045 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004046
4047 /* Wait for initialization time */
4048 udelay(24);
4049
4050 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004051
Ville Syrjäläa5805162015-05-26 20:42:30 +03004052 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004053}
4054
Daniel Vetter275f01b22013-05-03 11:49:47 +02004055static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4056 enum pipe pch_transcoder)
4057{
4058 struct drm_device *dev = crtc->base.dev;
4059 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004060 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004061
4062 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4063 I915_READ(HTOTAL(cpu_transcoder)));
4064 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4065 I915_READ(HBLANK(cpu_transcoder)));
4066 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4067 I915_READ(HSYNC(cpu_transcoder)));
4068
4069 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4070 I915_READ(VTOTAL(cpu_transcoder)));
4071 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4072 I915_READ(VBLANK(cpu_transcoder)));
4073 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4074 I915_READ(VSYNC(cpu_transcoder)));
4075 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4076 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4077}
4078
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004079static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004080{
4081 struct drm_i915_private *dev_priv = dev->dev_private;
4082 uint32_t temp;
4083
4084 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004085 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004086 return;
4087
4088 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4089 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4090
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004091 temp &= ~FDI_BC_BIFURCATION_SELECT;
4092 if (enable)
4093 temp |= FDI_BC_BIFURCATION_SELECT;
4094
4095 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004096 I915_WRITE(SOUTH_CHICKEN1, temp);
4097 POSTING_READ(SOUTH_CHICKEN1);
4098}
4099
4100static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4101{
4102 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004103
4104 switch (intel_crtc->pipe) {
4105 case PIPE_A:
4106 break;
4107 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004108 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004109 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004110 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004111 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004112
4113 break;
4114 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004115 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004116
4117 break;
4118 default:
4119 BUG();
4120 }
4121}
4122
Jesse Barnesf67a5592011-01-05 10:31:48 -08004123/*
4124 * Enable PCH resources required for PCH ports:
4125 * - PCH PLLs
4126 * - FDI training & RX/TX
4127 * - update transcoder timings
4128 * - DP transcoding bits
4129 * - transcoder
4130 */
4131static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004132{
4133 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004134 struct drm_i915_private *dev_priv = dev->dev_private;
4135 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4136 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004137 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004138
Daniel Vetterab9412b2013-05-03 11:49:46 +02004139 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004140
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004141 if (IS_IVYBRIDGE(dev))
4142 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4143
Daniel Vettercd986ab2012-10-26 10:58:12 +02004144 /* Write the TU size bits before fdi link training, so that error
4145 * detection works. */
4146 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4147 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4148
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004149 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004150 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004151
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004152 /* We need to program the right clock selection before writing the pixel
4153 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004154 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004155 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004156
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004157 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004158 temp |= TRANS_DPLL_ENABLE(pipe);
4159 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004160 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004161 temp |= sel;
4162 else
4163 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004164 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004165 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004166
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004167 /* XXX: pch pll's can be enabled any time before we enable the PCH
4168 * transcoder, and we actually should do this to not upset any PCH
4169 * transcoder that already use the clock when we share it.
4170 *
4171 * Note that enable_shared_dpll tries to do the right thing, but
4172 * get_shared_dpll unconditionally resets the pll - we need that to have
4173 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004174 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004175
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004176 /* set transcoder timing, panel must allow it */
4177 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004178 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004179
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004180 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004181
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004182 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004183 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004184 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004185 reg = TRANS_DP_CTL(pipe);
4186 temp = I915_READ(reg);
4187 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004188 TRANS_DP_SYNC_MASK |
4189 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004190 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004191 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004192
4193 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004194 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004195 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004196 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004197
4198 switch (intel_trans_dp_port_sel(crtc)) {
4199 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004200 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004201 break;
4202 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004203 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004204 break;
4205 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004206 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004207 break;
4208 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004209 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004210 }
4211
Chris Wilson5eddb702010-09-11 13:48:45 +01004212 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004213 }
4214
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004215 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004216}
4217
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004218static void lpt_pch_enable(struct drm_crtc *crtc)
4219{
4220 struct drm_device *dev = crtc->dev;
4221 struct drm_i915_private *dev_priv = dev->dev_private;
4222 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004223 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004224
Daniel Vetterab9412b2013-05-03 11:49:46 +02004225 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004226
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004227 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004228
Paulo Zanoni0540e482012-10-31 18:12:40 -02004229 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004230 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004231
Paulo Zanoni937bb612012-10-31 18:12:47 -02004232 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004233}
4234
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004235struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4236 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004237{
Daniel Vettere2b78262013-06-07 23:10:03 +02004238 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004239 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004240 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004241 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004242
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004243 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4244
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004245 if (HAS_PCH_IBX(dev_priv->dev)) {
4246 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004247 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004248 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004249
Daniel Vetter46edb022013-06-05 13:34:12 +02004250 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4251 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004252
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004253 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004254
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004255 goto found;
4256 }
4257
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304258 if (IS_BROXTON(dev_priv->dev)) {
4259 /* PLL is attached to port in bxt */
4260 struct intel_encoder *encoder;
4261 struct intel_digital_port *intel_dig_port;
4262
4263 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4264 if (WARN_ON(!encoder))
4265 return NULL;
4266
4267 intel_dig_port = enc_to_dig_port(&encoder->base);
4268 /* 1:1 mapping between ports and PLLs */
4269 i = (enum intel_dpll_id)intel_dig_port->port;
4270 pll = &dev_priv->shared_dplls[i];
4271 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4272 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004273 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304274
4275 goto found;
4276 }
4277
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004278 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4279 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004280
4281 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004282 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004283 continue;
4284
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004285 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004286 &shared_dpll[i].hw_state,
4287 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004288 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004289 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004290 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004291 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004292 goto found;
4293 }
4294 }
4295
4296 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004297 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4298 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004299 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004300 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4301 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004302 goto found;
4303 }
4304 }
4305
4306 return NULL;
4307
4308found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004309 if (shared_dpll[i].crtc_mask == 0)
4310 shared_dpll[i].hw_state =
4311 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004312
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004313 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004314 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4315 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004316
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004317 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004318
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004319 return pll;
4320}
4321
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004322static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004323{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004324 struct drm_i915_private *dev_priv = to_i915(state->dev);
4325 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004326 struct intel_shared_dpll *pll;
4327 enum intel_dpll_id i;
4328
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004329 if (!to_intel_atomic_state(state)->dpll_set)
4330 return;
4331
4332 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004333 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4334 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004335 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004336 }
4337}
4338
Daniel Vettera1520312013-05-03 11:49:50 +02004339static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004340{
4341 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004342 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004343 u32 temp;
4344
4345 temp = I915_READ(dslreg);
4346 udelay(500);
4347 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004348 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004349 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004350 }
4351}
4352
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004353static int
4354skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4355 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4356 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004357{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004358 struct intel_crtc_scaler_state *scaler_state =
4359 &crtc_state->scaler_state;
4360 struct intel_crtc *intel_crtc =
4361 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004362 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004363
4364 need_scaling = intel_rotation_90_or_270(rotation) ?
4365 (src_h != dst_w || src_w != dst_h):
4366 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004367
4368 /*
4369 * if plane is being disabled or scaler is no more required or force detach
4370 * - free scaler binded to this plane/crtc
4371 * - in order to do this, update crtc->scaler_usage
4372 *
4373 * Here scaler state in crtc_state is set free so that
4374 * scaler can be assigned to other user. Actual register
4375 * update to free the scaler is done in plane/panel-fit programming.
4376 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4377 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004378 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004379 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004380 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004381 scaler_state->scalers[*scaler_id].in_use = 0;
4382
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004383 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4384 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4385 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004386 scaler_state->scaler_users);
4387 *scaler_id = -1;
4388 }
4389 return 0;
4390 }
4391
4392 /* range checks */
4393 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4394 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4395
4396 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4397 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004398 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004399 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004400 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004401 return -EINVAL;
4402 }
4403
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004404 /* mark this plane as a scaler user in crtc_state */
4405 scaler_state->scaler_users |= (1 << scaler_user);
4406 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4407 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4408 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4409 scaler_state->scaler_users);
4410
4411 return 0;
4412}
4413
4414/**
4415 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4416 *
4417 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004418 *
4419 * Return
4420 * 0 - scaler_usage updated successfully
4421 * error - requested scaling cannot be supported or other error condition
4422 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004423int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004424{
4425 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004426 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004427
4428 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4429 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4430
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004431 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004432 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4433 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004434 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004435}
4436
4437/**
4438 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4439 *
4440 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004441 * @plane_state: atomic plane state to update
4442 *
4443 * Return
4444 * 0 - scaler_usage updated successfully
4445 * error - requested scaling cannot be supported or other error condition
4446 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004447static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4448 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004449{
4450
4451 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004452 struct intel_plane *intel_plane =
4453 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004454 struct drm_framebuffer *fb = plane_state->base.fb;
4455 int ret;
4456
4457 bool force_detach = !fb || !plane_state->visible;
4458
4459 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4460 intel_plane->base.base.id, intel_crtc->pipe,
4461 drm_plane_index(&intel_plane->base));
4462
4463 ret = skl_update_scaler(crtc_state, force_detach,
4464 drm_plane_index(&intel_plane->base),
4465 &plane_state->scaler_id,
4466 plane_state->base.rotation,
4467 drm_rect_width(&plane_state->src) >> 16,
4468 drm_rect_height(&plane_state->src) >> 16,
4469 drm_rect_width(&plane_state->dst),
4470 drm_rect_height(&plane_state->dst));
4471
4472 if (ret || plane_state->scaler_id < 0)
4473 return ret;
4474
Chandra Kondurua1b22782015-04-07 15:28:45 -07004475 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004476 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004477 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004478 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004479 return -EINVAL;
4480 }
4481
4482 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004483 switch (fb->pixel_format) {
4484 case DRM_FORMAT_RGB565:
4485 case DRM_FORMAT_XBGR8888:
4486 case DRM_FORMAT_XRGB8888:
4487 case DRM_FORMAT_ABGR8888:
4488 case DRM_FORMAT_ARGB8888:
4489 case DRM_FORMAT_XRGB2101010:
4490 case DRM_FORMAT_XBGR2101010:
4491 case DRM_FORMAT_YUYV:
4492 case DRM_FORMAT_YVYU:
4493 case DRM_FORMAT_UYVY:
4494 case DRM_FORMAT_VYUY:
4495 break;
4496 default:
4497 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4498 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4499 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004500 }
4501
Chandra Kondurua1b22782015-04-07 15:28:45 -07004502 return 0;
4503}
4504
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004505static void skylake_scaler_disable(struct intel_crtc *crtc)
4506{
4507 int i;
4508
4509 for (i = 0; i < crtc->num_scalers; i++)
4510 skl_detach_scaler(crtc, i);
4511}
4512
4513static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004514{
4515 struct drm_device *dev = crtc->base.dev;
4516 struct drm_i915_private *dev_priv = dev->dev_private;
4517 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004518 struct intel_crtc_scaler_state *scaler_state =
4519 &crtc->config->scaler_state;
4520
4521 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4522
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004523 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004524 int id;
4525
4526 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4527 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4528 return;
4529 }
4530
4531 id = scaler_state->scaler_id;
4532 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4533 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4534 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4535 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4536
4537 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004538 }
4539}
4540
Jesse Barnesb074cec2013-04-25 12:55:02 -07004541static void ironlake_pfit_enable(struct intel_crtc *crtc)
4542{
4543 struct drm_device *dev = crtc->base.dev;
4544 struct drm_i915_private *dev_priv = dev->dev_private;
4545 int pipe = crtc->pipe;
4546
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004547 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004548 /* Force use of hard-coded filter coefficients
4549 * as some pre-programmed values are broken,
4550 * e.g. x201.
4551 */
4552 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4553 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4554 PF_PIPE_SEL_IVB(pipe));
4555 else
4556 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004557 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4558 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004559 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004560}
4561
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004562void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004563{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004564 struct drm_device *dev = crtc->base.dev;
4565 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004566
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004567 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004568 return;
4569
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004570 /* We can only enable IPS after we enable a plane and wait for a vblank */
4571 intel_wait_for_vblank(dev, crtc->pipe);
4572
Paulo Zanonid77e4532013-09-24 13:52:55 -03004573 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004574 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004575 mutex_lock(&dev_priv->rps.hw_lock);
4576 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4577 mutex_unlock(&dev_priv->rps.hw_lock);
4578 /* Quoting Art Runyan: "its not safe to expect any particular
4579 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004580 * mailbox." Moreover, the mailbox may return a bogus state,
4581 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004582 */
4583 } else {
4584 I915_WRITE(IPS_CTL, IPS_ENABLE);
4585 /* The bit only becomes 1 in the next vblank, so this wait here
4586 * is essentially intel_wait_for_vblank. If we don't have this
4587 * and don't wait for vblanks until the end of crtc_enable, then
4588 * the HW state readout code will complain that the expected
4589 * IPS_CTL value is not the one we read. */
4590 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4591 DRM_ERROR("Timed out waiting for IPS enable\n");
4592 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004593}
4594
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004595void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004596{
4597 struct drm_device *dev = crtc->base.dev;
4598 struct drm_i915_private *dev_priv = dev->dev_private;
4599
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004600 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004601 return;
4602
4603 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004604 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004605 mutex_lock(&dev_priv->rps.hw_lock);
4606 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4607 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004608 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4609 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4610 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004611 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004612 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004613 POSTING_READ(IPS_CTL);
4614 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004615
4616 /* We need to wait for a vblank before we can disable the plane. */
4617 intel_wait_for_vblank(dev, crtc->pipe);
4618}
4619
4620/** Loads the palette/gamma unit for the CRTC with the prepared values */
4621static void intel_crtc_load_lut(struct drm_crtc *crtc)
4622{
4623 struct drm_device *dev = crtc->dev;
4624 struct drm_i915_private *dev_priv = dev->dev_private;
4625 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4626 enum pipe pipe = intel_crtc->pipe;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004627 int i;
4628 bool reenable_ips = false;
4629
4630 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004631 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004632 return;
4633
Imre Deak50360402015-01-16 00:55:16 -08004634 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004635 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004636 assert_dsi_pll_enabled(dev_priv);
4637 else
4638 assert_pll_enabled(dev_priv, pipe);
4639 }
4640
Paulo Zanonid77e4532013-09-24 13:52:55 -03004641 /* Workaround : Do not read or write the pipe palette/gamma data while
4642 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4643 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004644 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004645 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4646 GAMMA_MODE_MODE_SPLIT)) {
4647 hsw_disable_ips(intel_crtc);
4648 reenable_ips = true;
4649 }
4650
4651 for (i = 0; i < 256; i++) {
Ville Syrjäläf65a9c52015-09-18 20:03:28 +03004652 u32 palreg;
4653
4654 if (HAS_GMCH_DISPLAY(dev))
4655 palreg = PALETTE(pipe, i);
4656 else
4657 palreg = LGC_PALETTE(pipe, i);
4658
4659 I915_WRITE(palreg,
Paulo Zanonid77e4532013-09-24 13:52:55 -03004660 (intel_crtc->lut_r[i] << 16) |
4661 (intel_crtc->lut_g[i] << 8) |
4662 intel_crtc->lut_b[i]);
4663 }
4664
4665 if (reenable_ips)
4666 hsw_enable_ips(intel_crtc);
4667}
4668
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004669static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004670{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004671 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004672 struct drm_device *dev = intel_crtc->base.dev;
4673 struct drm_i915_private *dev_priv = dev->dev_private;
4674
4675 mutex_lock(&dev->struct_mutex);
4676 dev_priv->mm.interruptible = false;
4677 (void) intel_overlay_switch_off(intel_crtc->overlay);
4678 dev_priv->mm.interruptible = true;
4679 mutex_unlock(&dev->struct_mutex);
4680 }
4681
4682 /* Let userspace switch the overlay on again. In most cases userspace
4683 * has to recompute where to put it anyway.
4684 */
4685}
4686
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004687/**
4688 * intel_post_enable_primary - Perform operations after enabling primary plane
4689 * @crtc: the CRTC whose primary plane was just enabled
4690 *
4691 * Performs potentially sleeping operations that must be done after the primary
4692 * plane is enabled, such as updating FBC and IPS. Note that this may be
4693 * called due to an explicit primary plane update, or due to an implicit
4694 * re-enable that is caused when a sprite plane is updated to no longer
4695 * completely hide the primary plane.
4696 */
4697static void
4698intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004699{
4700 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004701 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4703 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004704
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004705 /*
4706 * BDW signals flip done immediately if the plane
4707 * is disabled, even if the plane enable is already
4708 * armed to occur at the next vblank :(
4709 */
4710 if (IS_BROADWELL(dev))
4711 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004712
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004713 /*
4714 * FIXME IPS should be fine as long as one plane is
4715 * enabled, but in practice it seems to have problems
4716 * when going from primary only to sprite only and vice
4717 * versa.
4718 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004719 hsw_enable_ips(intel_crtc);
4720
Daniel Vetterf99d7062014-06-19 16:01:59 +02004721 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004722 * Gen2 reports pipe underruns whenever all planes are disabled.
4723 * So don't enable underrun reporting before at least some planes
4724 * are enabled.
4725 * FIXME: Need to fix the logic to work when we turn off all planes
4726 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004727 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004728 if (IS_GEN2(dev))
4729 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4730
4731 /* Underruns don't raise interrupts, so check manually. */
4732 if (HAS_GMCH_DISPLAY(dev))
4733 i9xx_check_fifo_underruns(dev_priv);
4734}
4735
4736/**
4737 * intel_pre_disable_primary - Perform operations before disabling primary plane
4738 * @crtc: the CRTC whose primary plane is to be disabled
4739 *
4740 * Performs potentially sleeping operations that must be done before the
4741 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4742 * be called due to an explicit primary plane update, or due to an implicit
4743 * disable that is caused when a sprite plane completely hides the primary
4744 * plane.
4745 */
4746static void
4747intel_pre_disable_primary(struct drm_crtc *crtc)
4748{
4749 struct drm_device *dev = crtc->dev;
4750 struct drm_i915_private *dev_priv = dev->dev_private;
4751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4752 int pipe = intel_crtc->pipe;
4753
4754 /*
4755 * Gen2 reports pipe underruns whenever all planes are disabled.
4756 * So diasble underrun reporting before all the planes get disabled.
4757 * FIXME: Need to fix the logic to work when we turn off all planes
4758 * but leave the pipe running.
4759 */
4760 if (IS_GEN2(dev))
4761 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4762
4763 /*
4764 * Vblank time updates from the shadow to live plane control register
4765 * are blocked if the memory self-refresh mode is active at that
4766 * moment. So to make sure the plane gets truly disabled, disable
4767 * first the self-refresh mode. The self-refresh enable bit in turn
4768 * will be checked/applied by the HW only at the next frame start
4769 * event which is after the vblank start event, so we need to have a
4770 * wait-for-vblank between disabling the plane and the pipe.
4771 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004772 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004773 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004774 dev_priv->wm.vlv.cxsr = false;
4775 intel_wait_for_vblank(dev, pipe);
4776 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004777
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004778 /*
4779 * FIXME IPS should be fine as long as one plane is
4780 * enabled, but in practice it seems to have problems
4781 * when going from primary only to sprite only and vice
4782 * versa.
4783 */
4784 hsw_disable_ips(intel_crtc);
4785}
4786
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004787static void intel_post_plane_update(struct intel_crtc *crtc)
4788{
4789 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4790 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni7733b492015-07-07 15:26:04 -03004791 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004792 struct drm_plane *plane;
4793
4794 if (atomic->wait_vblank)
4795 intel_wait_for_vblank(dev, crtc->pipe);
4796
4797 intel_frontbuffer_flip(dev, atomic->fb_bits);
4798
Ville Syrjälä852eb002015-06-24 22:00:07 +03004799 if (atomic->disable_cxsr)
4800 crtc->wm.cxsr_allowed = true;
4801
Ville Syrjäläf015c552015-06-24 22:00:02 +03004802 if (crtc->atomic.update_wm_post)
4803 intel_update_watermarks(&crtc->base);
4804
Paulo Zanonic80ac852015-07-02 19:25:13 -03004805 if (atomic->update_fbc)
Paulo Zanoni7733b492015-07-07 15:26:04 -03004806 intel_fbc_update(dev_priv);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004807
4808 if (atomic->post_enable_primary)
4809 intel_post_enable_primary(&crtc->base);
4810
4811 drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4812 intel_update_sprite_watermarks(plane, &crtc->base,
4813 0, 0, 0, false, false);
4814
4815 memset(atomic, 0, sizeof(*atomic));
4816}
4817
4818static void intel_pre_plane_update(struct intel_crtc *crtc)
4819{
4820 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004821 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004822 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4823 struct drm_plane *p;
4824
4825 /* Track fb's for any planes being disabled */
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004826 drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4827 struct intel_plane *plane = to_intel_plane(p);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004828
4829 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03004830 i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
4831 plane->frontbuffer_bit);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004832 mutex_unlock(&dev->struct_mutex);
4833 }
4834
4835 if (atomic->wait_for_flips)
4836 intel_crtc_wait_for_pending_flips(&crtc->base);
4837
Paulo Zanonic80ac852015-07-02 19:25:13 -03004838 if (atomic->disable_fbc)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03004839 intel_fbc_disable_crtc(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004840
Rodrigo Vivi066cf552015-06-26 13:55:54 -07004841 if (crtc->atomic.disable_ips)
4842 hsw_disable_ips(crtc);
4843
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004844 if (atomic->pre_disable_primary)
4845 intel_pre_disable_primary(&crtc->base);
Ville Syrjälä852eb002015-06-24 22:00:07 +03004846
4847 if (atomic->disable_cxsr) {
4848 crtc->wm.cxsr_allowed = false;
4849 intel_set_memory_cxsr(dev_priv, false);
4850 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004851}
4852
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004853static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004854{
4855 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004856 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004857 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004858 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004859
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004860 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004861
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004862 drm_for_each_plane_mask(p, dev, plane_mask)
4863 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004864
Daniel Vetterf99d7062014-06-19 16:01:59 +02004865 /*
4866 * FIXME: Once we grow proper nuclear flip support out of this we need
4867 * to compute the mask of flip planes precisely. For the time being
4868 * consider this a flip to a NULL plane.
4869 */
4870 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004871}
4872
Jesse Barnesf67a5592011-01-05 10:31:48 -08004873static void ironlake_crtc_enable(struct drm_crtc *crtc)
4874{
4875 struct drm_device *dev = crtc->dev;
4876 struct drm_i915_private *dev_priv = dev->dev_private;
4877 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004878 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004879 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004880
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004881 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004882 return;
4883
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004884 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004885 intel_prepare_shared_dpll(intel_crtc);
4886
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004887 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304888 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004889
4890 intel_set_pipe_timings(intel_crtc);
4891
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004892 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004893 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004894 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004895 }
4896
4897 ironlake_set_pipeconf(crtc);
4898
Jesse Barnesf67a5592011-01-05 10:31:48 -08004899 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004900
Daniel Vettera72e4c92014-09-30 10:56:47 +02004901 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4902 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004903
Daniel Vetterf6736a12013-06-05 13:34:30 +02004904 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004905 if (encoder->pre_enable)
4906 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004907
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004908 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004909 /* Note: FDI PLL enabling _must_ be done before we enable the
4910 * cpu pipes, hence this is separate from all the other fdi/pch
4911 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004912 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004913 } else {
4914 assert_fdi_tx_disabled(dev_priv, pipe);
4915 assert_fdi_rx_disabled(dev_priv, pipe);
4916 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004917
Jesse Barnesb074cec2013-04-25 12:55:02 -07004918 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004919
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004920 /*
4921 * On ILK+ LUT must be loaded before the pipe is running but with
4922 * clocks enabled
4923 */
4924 intel_crtc_load_lut(crtc);
4925
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004926 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004927 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004928
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004929 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004930 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004931
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004932 assert_vblank_disabled(crtc);
4933 drm_crtc_vblank_on(crtc);
4934
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004935 for_each_encoder_on_crtc(dev, crtc, encoder)
4936 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004937
4938 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004939 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004940}
4941
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004942/* IPS only exists on ULT machines and is tied to pipe A. */
4943static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4944{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004945 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004946}
4947
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004948static void haswell_crtc_enable(struct drm_crtc *crtc)
4949{
4950 struct drm_device *dev = crtc->dev;
4951 struct drm_i915_private *dev_priv = dev->dev_private;
4952 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4953 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004954 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4955 struct intel_crtc_state *pipe_config =
4956 to_intel_crtc_state(crtc->state);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304957 bool is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004958
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004959 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004960 return;
4961
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004962 if (intel_crtc_to_shared_dpll(intel_crtc))
4963 intel_enable_shared_dpll(intel_crtc);
4964
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004965 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304966 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004967
4968 intel_set_pipe_timings(intel_crtc);
4969
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004970 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4971 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4972 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004973 }
4974
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004975 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004976 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004977 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004978 }
4979
4980 haswell_set_pipeconf(crtc);
4981
4982 intel_set_pipe_csc(crtc);
4983
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004984 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004985
Daniel Vettera72e4c92014-09-30 10:56:47 +02004986 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304987 for_each_encoder_on_crtc(dev, crtc, encoder) {
4988 if (encoder->pre_pll_enable)
4989 encoder->pre_pll_enable(encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004990 if (encoder->pre_enable)
4991 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304992 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004993
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004994 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004995 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4996 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004997 dev_priv->display.fdi_link_train(crtc);
4998 }
4999
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305000 if (!is_dsi)
5001 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005002
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005003 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005004 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005005 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005006 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005007
5008 /*
5009 * On ILK+ LUT must be loaded before the pipe is running but with
5010 * clocks enabled
5011 */
5012 intel_crtc_load_lut(crtc);
5013
Paulo Zanoni1f544382012-10-24 11:32:00 -02005014 intel_ddi_set_pipe_settings(crtc);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305015 if (!is_dsi)
5016 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005017
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005018 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005019 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005020
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005021 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005022 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005023
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305024 if (intel_crtc->config->dp_encoder_is_mst && !is_dsi)
Dave Airlie0e32b392014-05-02 14:02:48 +10005025 intel_ddi_set_vc_payload_alloc(crtc, true);
5026
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005027 assert_vblank_disabled(crtc);
5028 drm_crtc_vblank_on(crtc);
5029
Jani Nikula8807e552013-08-30 19:40:32 +03005030 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005031 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005032 intel_opregion_notify_encoder(encoder, true);
5033 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005034
Paulo Zanonie4916942013-09-20 16:21:19 -03005035 /* If we change the relative order between pipe/planes enabling, we need
5036 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005037 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5038 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5039 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5040 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5041 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005042}
5043
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005044static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005045{
5046 struct drm_device *dev = crtc->base.dev;
5047 struct drm_i915_private *dev_priv = dev->dev_private;
5048 int pipe = crtc->pipe;
5049
5050 /* To avoid upsetting the power well on haswell only disable the pfit if
5051 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005052 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005053 I915_WRITE(PF_CTL(pipe), 0);
5054 I915_WRITE(PF_WIN_POS(pipe), 0);
5055 I915_WRITE(PF_WIN_SZ(pipe), 0);
5056 }
5057}
5058
Jesse Barnes6be4a602010-09-10 10:26:01 -07005059static void ironlake_crtc_disable(struct drm_crtc *crtc)
5060{
5061 struct drm_device *dev = crtc->dev;
5062 struct drm_i915_private *dev_priv = dev->dev_private;
5063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005064 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005065 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005066 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005067
Daniel Vetterea9d7582012-07-10 10:42:52 +02005068 for_each_encoder_on_crtc(dev, crtc, encoder)
5069 encoder->disable(encoder);
5070
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005071 drm_crtc_vblank_off(crtc);
5072 assert_vblank_disabled(crtc);
5073
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005074 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005075 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005076
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005077 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005078
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005079 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005080
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005081 if (intel_crtc->config->has_pch_encoder)
5082 ironlake_fdi_disable(crtc);
5083
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005084 for_each_encoder_on_crtc(dev, crtc, encoder)
5085 if (encoder->post_disable)
5086 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005087
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005088 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005089 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005090
Daniel Vetterd925c592013-06-05 13:34:04 +02005091 if (HAS_PCH_CPT(dev)) {
5092 /* disable TRANS_DP_CTL */
5093 reg = TRANS_DP_CTL(pipe);
5094 temp = I915_READ(reg);
5095 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5096 TRANS_DP_PORT_SEL_MASK);
5097 temp |= TRANS_DP_PORT_SEL_NONE;
5098 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005099
Daniel Vetterd925c592013-06-05 13:34:04 +02005100 /* disable DPLL_SEL */
5101 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005102 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005103 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005104 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005105
Daniel Vetterd925c592013-06-05 13:34:04 +02005106 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005107 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07005108}
5109
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005110static void haswell_crtc_disable(struct drm_crtc *crtc)
5111{
5112 struct drm_device *dev = crtc->dev;
5113 struct drm_i915_private *dev_priv = dev->dev_private;
5114 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5115 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005116 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305117 bool is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005118
Jani Nikula8807e552013-08-30 19:40:32 +03005119 for_each_encoder_on_crtc(dev, crtc, encoder) {
5120 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005121 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005122 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005123
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005124 drm_crtc_vblank_off(crtc);
5125 assert_vblank_disabled(crtc);
5126
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005127 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005128 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5129 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005130 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005131
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005132 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005133 intel_ddi_set_vc_payload_alloc(crtc, false);
5134
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305135 if (!is_dsi)
5136 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005137
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005138 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005139 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005140 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005141 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005142
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305143 if (!is_dsi)
5144 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005145
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005146 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005147 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005148 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005149 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005150
Imre Deak97b040a2014-06-25 22:01:50 +03005151 for_each_encoder_on_crtc(dev, crtc, encoder)
5152 if (encoder->post_disable)
5153 encoder->post_disable(encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005154}
5155
Jesse Barnes2dd24552013-04-25 12:55:01 -07005156static void i9xx_pfit_enable(struct intel_crtc *crtc)
5157{
5158 struct drm_device *dev = crtc->base.dev;
5159 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005160 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005161
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005162 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005163 return;
5164
Daniel Vetterc0b03412013-05-28 12:05:54 +02005165 /*
5166 * The panel fitter should only be adjusted whilst the pipe is disabled,
5167 * according to register description and PRM.
5168 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005169 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5170 assert_pipe_disabled(dev_priv, crtc->pipe);
5171
Jesse Barnesb074cec2013-04-25 12:55:02 -07005172 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5173 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005174
5175 /* Border color in case we don't scale up to the full screen. Black by
5176 * default, change to something else for debugging. */
5177 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005178}
5179
Dave Airlied05410f2014-06-05 13:22:59 +10005180static enum intel_display_power_domain port_to_power_domain(enum port port)
5181{
5182 switch (port) {
5183 case PORT_A:
5184 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5185 case PORT_B:
5186 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5187 case PORT_C:
5188 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5189 case PORT_D:
5190 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005191 case PORT_E:
5192 return POWER_DOMAIN_PORT_DDI_E_2_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005193 default:
5194 WARN_ON_ONCE(1);
5195 return POWER_DOMAIN_PORT_OTHER;
5196 }
5197}
5198
Imre Deak77d22dc2014-03-05 16:20:52 +02005199#define for_each_power_domain(domain, mask) \
5200 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5201 if ((1 << (domain)) & (mask))
5202
Imre Deak319be8a2014-03-04 19:22:57 +02005203enum intel_display_power_domain
5204intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005205{
Imre Deak319be8a2014-03-04 19:22:57 +02005206 struct drm_device *dev = intel_encoder->base.dev;
5207 struct intel_digital_port *intel_dig_port;
5208
5209 switch (intel_encoder->type) {
5210 case INTEL_OUTPUT_UNKNOWN:
5211 /* Only DDI platforms should ever use this output type */
5212 WARN_ON_ONCE(!HAS_DDI(dev));
5213 case INTEL_OUTPUT_DISPLAYPORT:
5214 case INTEL_OUTPUT_HDMI:
5215 case INTEL_OUTPUT_EDP:
5216 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005217 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005218 case INTEL_OUTPUT_DP_MST:
5219 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5220 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005221 case INTEL_OUTPUT_ANALOG:
5222 return POWER_DOMAIN_PORT_CRT;
5223 case INTEL_OUTPUT_DSI:
5224 return POWER_DOMAIN_PORT_DSI;
5225 default:
5226 return POWER_DOMAIN_PORT_OTHER;
5227 }
5228}
5229
5230static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5231{
5232 struct drm_device *dev = crtc->dev;
5233 struct intel_encoder *intel_encoder;
5234 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5235 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005236 unsigned long mask;
5237 enum transcoder transcoder;
5238
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005239 if (!crtc->state->active)
5240 return 0;
5241
Imre Deak77d22dc2014-03-05 16:20:52 +02005242 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5243
5244 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5245 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005246 if (intel_crtc->config->pch_pfit.enabled ||
5247 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005248 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5249
Imre Deak319be8a2014-03-04 19:22:57 +02005250 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5251 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5252
Imre Deak77d22dc2014-03-05 16:20:52 +02005253 return mask;
5254}
5255
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005256static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
5257{
5258 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5259 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5260 enum intel_display_power_domain domain;
5261 unsigned long domains, new_domains, old_domains;
5262
5263 old_domains = intel_crtc->enabled_power_domains;
5264 intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
5265
5266 domains = new_domains & ~old_domains;
5267
5268 for_each_power_domain(domain, domains)
5269 intel_display_power_get(dev_priv, domain);
5270
5271 return old_domains & ~new_domains;
5272}
5273
5274static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5275 unsigned long domains)
5276{
5277 enum intel_display_power_domain domain;
5278
5279 for_each_power_domain(domain, domains)
5280 intel_display_power_put(dev_priv, domain);
5281}
5282
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005283static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005284{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005285 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005286 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005287 unsigned long put_domains[I915_MAX_PIPES] = {};
5288 struct drm_crtc_state *crtc_state;
5289 struct drm_crtc *crtc;
5290 int i;
Imre Deak77d22dc2014-03-05 16:20:52 +02005291
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005292 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5293 if (needs_modeset(crtc->state))
5294 put_domains[to_intel_crtc(crtc)->pipe] =
5295 modeset_get_crtc_power_domains(crtc);
Imre Deak77d22dc2014-03-05 16:20:52 +02005296 }
5297
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005298 if (dev_priv->display.modeset_commit_cdclk) {
5299 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5300
5301 if (cdclk != dev_priv->cdclk_freq &&
5302 !WARN_ON(!state->allow_modeset))
5303 dev_priv->display.modeset_commit_cdclk(state);
5304 }
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005305
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005306 for (i = 0; i < I915_MAX_PIPES; i++)
5307 if (put_domains[i])
5308 modeset_put_power_domains(dev_priv, put_domains[i]);
Imre Deak77d22dc2014-03-05 16:20:52 +02005309}
5310
Mika Kaholaadafdc62015-08-18 14:36:59 +03005311static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5312{
5313 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5314
5315 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5316 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5317 return max_cdclk_freq;
5318 else if (IS_CHERRYVIEW(dev_priv))
5319 return max_cdclk_freq*95/100;
5320 else if (INTEL_INFO(dev_priv)->gen < 4)
5321 return 2*max_cdclk_freq*90/100;
5322 else
5323 return max_cdclk_freq*90/100;
5324}
5325
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005326static void intel_update_max_cdclk(struct drm_device *dev)
5327{
5328 struct drm_i915_private *dev_priv = dev->dev_private;
5329
5330 if (IS_SKYLAKE(dev)) {
5331 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5332
5333 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5334 dev_priv->max_cdclk_freq = 675000;
5335 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5336 dev_priv->max_cdclk_freq = 540000;
5337 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5338 dev_priv->max_cdclk_freq = 450000;
5339 else
5340 dev_priv->max_cdclk_freq = 337500;
5341 } else if (IS_BROADWELL(dev)) {
5342 /*
5343 * FIXME with extra cooling we can allow
5344 * 540 MHz for ULX and 675 Mhz for ULT.
5345 * How can we know if extra cooling is
5346 * available? PCI ID, VTB, something else?
5347 */
5348 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5349 dev_priv->max_cdclk_freq = 450000;
5350 else if (IS_BDW_ULX(dev))
5351 dev_priv->max_cdclk_freq = 450000;
5352 else if (IS_BDW_ULT(dev))
5353 dev_priv->max_cdclk_freq = 540000;
5354 else
5355 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005356 } else if (IS_CHERRYVIEW(dev)) {
5357 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005358 } else if (IS_VALLEYVIEW(dev)) {
5359 dev_priv->max_cdclk_freq = 400000;
5360 } else {
5361 /* otherwise assume cdclk is fixed */
5362 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5363 }
5364
Mika Kaholaadafdc62015-08-18 14:36:59 +03005365 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5366
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005367 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5368 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005369
5370 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5371 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005372}
5373
5374static void intel_update_cdclk(struct drm_device *dev)
5375{
5376 struct drm_i915_private *dev_priv = dev->dev_private;
5377
5378 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5379 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5380 dev_priv->cdclk_freq);
5381
5382 /*
5383 * Program the gmbus_freq based on the cdclk frequency.
5384 * BSpec erroneously claims we should aim for 4MHz, but
5385 * in fact 1MHz is the correct frequency.
5386 */
5387 if (IS_VALLEYVIEW(dev)) {
5388 /*
5389 * Program the gmbus_freq based on the cdclk frequency.
5390 * BSpec erroneously claims we should aim for 4MHz, but
5391 * in fact 1MHz is the correct frequency.
5392 */
5393 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5394 }
5395
5396 if (dev_priv->max_cdclk_freq == 0)
5397 intel_update_max_cdclk(dev);
5398}
5399
Damien Lespiau70d0c572015-06-04 18:21:29 +01005400static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305401{
5402 struct drm_i915_private *dev_priv = dev->dev_private;
5403 uint32_t divider;
5404 uint32_t ratio;
5405 uint32_t current_freq;
5406 int ret;
5407
5408 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5409 switch (frequency) {
5410 case 144000:
5411 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5412 ratio = BXT_DE_PLL_RATIO(60);
5413 break;
5414 case 288000:
5415 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5416 ratio = BXT_DE_PLL_RATIO(60);
5417 break;
5418 case 384000:
5419 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5420 ratio = BXT_DE_PLL_RATIO(60);
5421 break;
5422 case 576000:
5423 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5424 ratio = BXT_DE_PLL_RATIO(60);
5425 break;
5426 case 624000:
5427 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5428 ratio = BXT_DE_PLL_RATIO(65);
5429 break;
5430 case 19200:
5431 /*
5432 * Bypass frequency with DE PLL disabled. Init ratio, divider
5433 * to suppress GCC warning.
5434 */
5435 ratio = 0;
5436 divider = 0;
5437 break;
5438 default:
5439 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5440
5441 return;
5442 }
5443
5444 mutex_lock(&dev_priv->rps.hw_lock);
5445 /* Inform power controller of upcoming frequency change */
5446 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5447 0x80000000);
5448 mutex_unlock(&dev_priv->rps.hw_lock);
5449
5450 if (ret) {
5451 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5452 ret, frequency);
5453 return;
5454 }
5455
5456 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5457 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5458 current_freq = current_freq * 500 + 1000;
5459
5460 /*
5461 * DE PLL has to be disabled when
5462 * - setting to 19.2MHz (bypass, PLL isn't used)
5463 * - before setting to 624MHz (PLL needs toggling)
5464 * - before setting to any frequency from 624MHz (PLL needs toggling)
5465 */
5466 if (frequency == 19200 || frequency == 624000 ||
5467 current_freq == 624000) {
5468 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5469 /* Timeout 200us */
5470 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5471 1))
5472 DRM_ERROR("timout waiting for DE PLL unlock\n");
5473 }
5474
5475 if (frequency != 19200) {
5476 uint32_t val;
5477
5478 val = I915_READ(BXT_DE_PLL_CTL);
5479 val &= ~BXT_DE_PLL_RATIO_MASK;
5480 val |= ratio;
5481 I915_WRITE(BXT_DE_PLL_CTL, val);
5482
5483 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5484 /* Timeout 200us */
5485 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5486 DRM_ERROR("timeout waiting for DE PLL lock\n");
5487
5488 val = I915_READ(CDCLK_CTL);
5489 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5490 val |= divider;
5491 /*
5492 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5493 * enable otherwise.
5494 */
5495 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5496 if (frequency >= 500000)
5497 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5498
5499 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5500 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5501 val |= (frequency - 1000) / 500;
5502 I915_WRITE(CDCLK_CTL, val);
5503 }
5504
5505 mutex_lock(&dev_priv->rps.hw_lock);
5506 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5507 DIV_ROUND_UP(frequency, 25000));
5508 mutex_unlock(&dev_priv->rps.hw_lock);
5509
5510 if (ret) {
5511 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5512 ret, frequency);
5513 return;
5514 }
5515
Damien Lespiaua47871b2015-06-04 18:21:34 +01005516 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305517}
5518
5519void broxton_init_cdclk(struct drm_device *dev)
5520{
5521 struct drm_i915_private *dev_priv = dev->dev_private;
5522 uint32_t val;
5523
5524 /*
5525 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5526 * or else the reset will hang because there is no PCH to respond.
5527 * Move the handshake programming to initialization sequence.
5528 * Previously was left up to BIOS.
5529 */
5530 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5531 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5532 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5533
5534 /* Enable PG1 for cdclk */
5535 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5536
5537 /* check if cd clock is enabled */
5538 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5539 DRM_DEBUG_KMS("Display already initialized\n");
5540 return;
5541 }
5542
5543 /*
5544 * FIXME:
5545 * - The initial CDCLK needs to be read from VBT.
5546 * Need to make this change after VBT has changes for BXT.
5547 * - check if setting the max (or any) cdclk freq is really necessary
5548 * here, it belongs to modeset time
5549 */
5550 broxton_set_cdclk(dev, 624000);
5551
5552 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005553 POSTING_READ(DBUF_CTL);
5554
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305555 udelay(10);
5556
5557 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5558 DRM_ERROR("DBuf power enable timeout!\n");
5559}
5560
5561void broxton_uninit_cdclk(struct drm_device *dev)
5562{
5563 struct drm_i915_private *dev_priv = dev->dev_private;
5564
5565 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005566 POSTING_READ(DBUF_CTL);
5567
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305568 udelay(10);
5569
5570 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5571 DRM_ERROR("DBuf power disable timeout!\n");
5572
5573 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5574 broxton_set_cdclk(dev, 19200);
5575
5576 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5577}
5578
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005579static const struct skl_cdclk_entry {
5580 unsigned int freq;
5581 unsigned int vco;
5582} skl_cdclk_frequencies[] = {
5583 { .freq = 308570, .vco = 8640 },
5584 { .freq = 337500, .vco = 8100 },
5585 { .freq = 432000, .vco = 8640 },
5586 { .freq = 450000, .vco = 8100 },
5587 { .freq = 540000, .vco = 8100 },
5588 { .freq = 617140, .vco = 8640 },
5589 { .freq = 675000, .vco = 8100 },
5590};
5591
5592static unsigned int skl_cdclk_decimal(unsigned int freq)
5593{
5594 return (freq - 1000) / 500;
5595}
5596
5597static unsigned int skl_cdclk_get_vco(unsigned int freq)
5598{
5599 unsigned int i;
5600
5601 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5602 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5603
5604 if (e->freq == freq)
5605 return e->vco;
5606 }
5607
5608 return 8100;
5609}
5610
5611static void
5612skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5613{
5614 unsigned int min_freq;
5615 u32 val;
5616
5617 /* select the minimum CDCLK before enabling DPLL 0 */
5618 val = I915_READ(CDCLK_CTL);
5619 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5620 val |= CDCLK_FREQ_337_308;
5621
5622 if (required_vco == 8640)
5623 min_freq = 308570;
5624 else
5625 min_freq = 337500;
5626
5627 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5628
5629 I915_WRITE(CDCLK_CTL, val);
5630 POSTING_READ(CDCLK_CTL);
5631
5632 /*
5633 * We always enable DPLL0 with the lowest link rate possible, but still
5634 * taking into account the VCO required to operate the eDP panel at the
5635 * desired frequency. The usual DP link rates operate with a VCO of
5636 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5637 * The modeset code is responsible for the selection of the exact link
5638 * rate later on, with the constraint of choosing a frequency that
5639 * works with required_vco.
5640 */
5641 val = I915_READ(DPLL_CTRL1);
5642
5643 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5644 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5645 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5646 if (required_vco == 8640)
5647 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5648 SKL_DPLL0);
5649 else
5650 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5651 SKL_DPLL0);
5652
5653 I915_WRITE(DPLL_CTRL1, val);
5654 POSTING_READ(DPLL_CTRL1);
5655
5656 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5657
5658 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5659 DRM_ERROR("DPLL0 not locked\n");
5660}
5661
5662static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5663{
5664 int ret;
5665 u32 val;
5666
5667 /* inform PCU we want to change CDCLK */
5668 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5669 mutex_lock(&dev_priv->rps.hw_lock);
5670 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5671 mutex_unlock(&dev_priv->rps.hw_lock);
5672
5673 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5674}
5675
5676static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5677{
5678 unsigned int i;
5679
5680 for (i = 0; i < 15; i++) {
5681 if (skl_cdclk_pcu_ready(dev_priv))
5682 return true;
5683 udelay(10);
5684 }
5685
5686 return false;
5687}
5688
5689static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5690{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005691 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005692 u32 freq_select, pcu_ack;
5693
5694 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5695
5696 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5697 DRM_ERROR("failed to inform PCU about cdclk change\n");
5698 return;
5699 }
5700
5701 /* set CDCLK_CTL */
5702 switch(freq) {
5703 case 450000:
5704 case 432000:
5705 freq_select = CDCLK_FREQ_450_432;
5706 pcu_ack = 1;
5707 break;
5708 case 540000:
5709 freq_select = CDCLK_FREQ_540;
5710 pcu_ack = 2;
5711 break;
5712 case 308570:
5713 case 337500:
5714 default:
5715 freq_select = CDCLK_FREQ_337_308;
5716 pcu_ack = 0;
5717 break;
5718 case 617140:
5719 case 675000:
5720 freq_select = CDCLK_FREQ_675_617;
5721 pcu_ack = 3;
5722 break;
5723 }
5724
5725 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5726 POSTING_READ(CDCLK_CTL);
5727
5728 /* inform PCU of the change */
5729 mutex_lock(&dev_priv->rps.hw_lock);
5730 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5731 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005732
5733 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005734}
5735
5736void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5737{
5738 /* disable DBUF power */
5739 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5740 POSTING_READ(DBUF_CTL);
5741
5742 udelay(10);
5743
5744 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5745 DRM_ERROR("DBuf power disable timeout\n");
5746
Animesh Manna4e961e42015-08-26 01:36:08 +05305747 /*
5748 * DMC assumes ownership of LCPLL and will get confused if we touch it.
5749 */
5750 if (dev_priv->csr.dmc_payload) {
5751 /* disable DPLL0 */
5752 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) &
5753 ~LCPLL_PLL_ENABLE);
5754 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5755 DRM_ERROR("Couldn't disable DPLL0\n");
5756 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005757
5758 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5759}
5760
5761void skl_init_cdclk(struct drm_i915_private *dev_priv)
5762{
5763 u32 val;
5764 unsigned int required_vco;
5765
5766 /* enable PCH reset handshake */
5767 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5768 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5769
5770 /* enable PG1 and Misc I/O */
5771 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5772
Gary Wang39d9b852015-08-28 16:40:34 +08005773 /* DPLL0 not enabled (happens on early BIOS versions) */
5774 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5775 /* enable DPLL0 */
5776 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5777 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005778 }
5779
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005780 /* set CDCLK to the frequency the BIOS chose */
5781 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5782
5783 /* enable DBUF power */
5784 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5785 POSTING_READ(DBUF_CTL);
5786
5787 udelay(10);
5788
5789 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5790 DRM_ERROR("DBuf power enable timeout\n");
5791}
5792
Jesse Barnes30a970c2013-11-04 13:48:12 -08005793/* Adjust CDclk dividers to allow high res or save power if possible */
5794static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5795{
5796 struct drm_i915_private *dev_priv = dev->dev_private;
5797 u32 val, cmd;
5798
Vandana Kannan164dfd22014-11-24 13:37:41 +05305799 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5800 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005801
Ville Syrjälädfcab172014-06-13 13:37:47 +03005802 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005803 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005804 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005805 cmd = 1;
5806 else
5807 cmd = 0;
5808
5809 mutex_lock(&dev_priv->rps.hw_lock);
5810 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5811 val &= ~DSPFREQGUAR_MASK;
5812 val |= (cmd << DSPFREQGUAR_SHIFT);
5813 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5814 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5815 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5816 50)) {
5817 DRM_ERROR("timed out waiting for CDclk change\n");
5818 }
5819 mutex_unlock(&dev_priv->rps.hw_lock);
5820
Ville Syrjälä54433e92015-05-26 20:42:31 +03005821 mutex_lock(&dev_priv->sb_lock);
5822
Ville Syrjälädfcab172014-06-13 13:37:47 +03005823 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005824 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005825
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005826 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005827
Jesse Barnes30a970c2013-11-04 13:48:12 -08005828 /* adjust cdclk divider */
5829 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005830 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005831 val |= divider;
5832 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005833
5834 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005835 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005836 50))
5837 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005838 }
5839
Jesse Barnes30a970c2013-11-04 13:48:12 -08005840 /* adjust self-refresh exit latency value */
5841 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5842 val &= ~0x7f;
5843
5844 /*
5845 * For high bandwidth configs, we set a higher latency in the bunit
5846 * so that the core display fetch happens in time to avoid underruns.
5847 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005848 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005849 val |= 4500 / 250; /* 4.5 usec */
5850 else
5851 val |= 3000 / 250; /* 3.0 usec */
5852 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005853
Ville Syrjäläa5805162015-05-26 20:42:30 +03005854 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005855
Ville Syrjäläb6283052015-06-03 15:45:07 +03005856 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005857}
5858
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005859static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5860{
5861 struct drm_i915_private *dev_priv = dev->dev_private;
5862 u32 val, cmd;
5863
Vandana Kannan164dfd22014-11-24 13:37:41 +05305864 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5865 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005866
5867 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005868 case 333333:
5869 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005870 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005871 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005872 break;
5873 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005874 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005875 return;
5876 }
5877
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005878 /*
5879 * Specs are full of misinformation, but testing on actual
5880 * hardware has shown that we just need to write the desired
5881 * CCK divider into the Punit register.
5882 */
5883 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5884
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005885 mutex_lock(&dev_priv->rps.hw_lock);
5886 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5887 val &= ~DSPFREQGUAR_MASK_CHV;
5888 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5889 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5890 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5891 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5892 50)) {
5893 DRM_ERROR("timed out waiting for CDclk change\n");
5894 }
5895 mutex_unlock(&dev_priv->rps.hw_lock);
5896
Ville Syrjäläb6283052015-06-03 15:45:07 +03005897 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005898}
5899
Jesse Barnes30a970c2013-11-04 13:48:12 -08005900static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5901 int max_pixclk)
5902{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005903 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005904 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005905
Jesse Barnes30a970c2013-11-04 13:48:12 -08005906 /*
5907 * Really only a few cases to deal with, as only 4 CDclks are supported:
5908 * 200MHz
5909 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005910 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005911 * 400MHz (VLV only)
5912 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5913 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005914 *
5915 * We seem to get an unstable or solid color picture at 200MHz.
5916 * Not sure what's wrong. For now use 200MHz only when all pipes
5917 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005918 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005919 if (!IS_CHERRYVIEW(dev_priv) &&
5920 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005921 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005922 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005923 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005924 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005925 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005926 else
5927 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005928}
5929
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305930static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5931 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005932{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305933 /*
5934 * FIXME:
5935 * - remove the guardband, it's not needed on BXT
5936 * - set 19.2MHz bypass frequency if there are no active pipes
5937 */
5938 if (max_pixclk > 576000*9/10)
5939 return 624000;
5940 else if (max_pixclk > 384000*9/10)
5941 return 576000;
5942 else if (max_pixclk > 288000*9/10)
5943 return 384000;
5944 else if (max_pixclk > 144000*9/10)
5945 return 288000;
5946 else
5947 return 144000;
5948}
5949
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005950/* Compute the max pixel clock for new configuration. Uses atomic state if
5951 * that's non-NULL, look at current state otherwise. */
5952static int intel_mode_max_pixclk(struct drm_device *dev,
5953 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005954{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005955 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005956 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005957 int max_pixclk = 0;
5958
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005959 for_each_intel_crtc(dev, intel_crtc) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005960 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005961 if (IS_ERR(crtc_state))
5962 return PTR_ERR(crtc_state);
5963
5964 if (!crtc_state->base.enable)
5965 continue;
5966
5967 max_pixclk = max(max_pixclk,
5968 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005969 }
5970
5971 return max_pixclk;
5972}
5973
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005974static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005975{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005976 struct drm_device *dev = state->dev;
5977 struct drm_i915_private *dev_priv = dev->dev_private;
5978 int max_pixclk = intel_mode_max_pixclk(dev, state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005979
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005980 if (max_pixclk < 0)
5981 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005982
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005983 to_intel_atomic_state(state)->cdclk =
5984 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305985
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005986 return 0;
5987}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005988
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005989static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5990{
5991 struct drm_device *dev = state->dev;
5992 struct drm_i915_private *dev_priv = dev->dev_private;
5993 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005994
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005995 if (max_pixclk < 0)
5996 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005997
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005998 to_intel_atomic_state(state)->cdclk =
5999 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006000
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006001 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006002}
6003
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006004static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6005{
6006 unsigned int credits, default_credits;
6007
6008 if (IS_CHERRYVIEW(dev_priv))
6009 default_credits = PFI_CREDIT(12);
6010 else
6011 default_credits = PFI_CREDIT(8);
6012
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006013 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006014 /* CHV suggested value is 31 or 63 */
6015 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006016 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006017 else
6018 credits = PFI_CREDIT(15);
6019 } else {
6020 credits = default_credits;
6021 }
6022
6023 /*
6024 * WA - write default credits before re-programming
6025 * FIXME: should we also set the resend bit here?
6026 */
6027 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6028 default_credits);
6029
6030 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6031 credits | PFI_CREDIT_RESEND);
6032
6033 /*
6034 * FIXME is this guaranteed to clear
6035 * immediately or should we poll for it?
6036 */
6037 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6038}
6039
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006040static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006041{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006042 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006043 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006044 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006045
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006046 /*
6047 * FIXME: We can end up here with all power domains off, yet
6048 * with a CDCLK frequency other than the minimum. To account
6049 * for this take the PIPE-A power domain, which covers the HW
6050 * blocks needed for the following programming. This can be
6051 * removed once it's guaranteed that we get here either with
6052 * the minimum CDCLK set, or the required power domains
6053 * enabled.
6054 */
6055 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006056
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006057 if (IS_CHERRYVIEW(dev))
6058 cherryview_set_cdclk(dev, req_cdclk);
6059 else
6060 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006061
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006062 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006063
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006064 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006065}
6066
Jesse Barnes89b667f2013-04-18 14:51:36 -07006067static void valleyview_crtc_enable(struct drm_crtc *crtc)
6068{
6069 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006070 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006071 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6072 struct intel_encoder *encoder;
6073 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006074 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006075
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006076 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006077 return;
6078
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006079 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306080
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006081 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306082 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006083
6084 intel_set_pipe_timings(intel_crtc);
6085
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006086 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6087 struct drm_i915_private *dev_priv = dev->dev_private;
6088
6089 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6090 I915_WRITE(CHV_CANVAS(pipe), 0);
6091 }
6092
Daniel Vetter5b18e572014-04-24 23:55:06 +02006093 i9xx_set_pipeconf(intel_crtc);
6094
Jesse Barnes89b667f2013-04-18 14:51:36 -07006095 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006096
Daniel Vettera72e4c92014-09-30 10:56:47 +02006097 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006098
Jesse Barnes89b667f2013-04-18 14:51:36 -07006099 for_each_encoder_on_crtc(dev, crtc, encoder)
6100 if (encoder->pre_pll_enable)
6101 encoder->pre_pll_enable(encoder);
6102
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006103 if (!is_dsi) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006104 if (IS_CHERRYVIEW(dev)) {
6105 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006106 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006107 } else {
6108 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006109 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006110 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006111 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006112
6113 for_each_encoder_on_crtc(dev, crtc, encoder)
6114 if (encoder->pre_enable)
6115 encoder->pre_enable(encoder);
6116
Jesse Barnes2dd24552013-04-25 12:55:01 -07006117 i9xx_pfit_enable(intel_crtc);
6118
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006119 intel_crtc_load_lut(crtc);
6120
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006121 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006122
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006123 assert_vblank_disabled(crtc);
6124 drm_crtc_vblank_on(crtc);
6125
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006126 for_each_encoder_on_crtc(dev, crtc, encoder)
6127 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006128}
6129
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006130static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6131{
6132 struct drm_device *dev = crtc->base.dev;
6133 struct drm_i915_private *dev_priv = dev->dev_private;
6134
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006135 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6136 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006137}
6138
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006139static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006140{
6141 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006142 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006143 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006144 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006145 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006146
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006147 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006148 return;
6149
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006150 i9xx_set_pll_dividers(intel_crtc);
6151
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006152 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306153 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006154
6155 intel_set_pipe_timings(intel_crtc);
6156
Daniel Vetter5b18e572014-04-24 23:55:06 +02006157 i9xx_set_pipeconf(intel_crtc);
6158
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006159 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006160
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006161 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006162 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006163
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006164 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006165 if (encoder->pre_enable)
6166 encoder->pre_enable(encoder);
6167
Daniel Vetterf6736a12013-06-05 13:34:30 +02006168 i9xx_enable_pll(intel_crtc);
6169
Jesse Barnes2dd24552013-04-25 12:55:01 -07006170 i9xx_pfit_enable(intel_crtc);
6171
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006172 intel_crtc_load_lut(crtc);
6173
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006174 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006175 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006176
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006177 assert_vblank_disabled(crtc);
6178 drm_crtc_vblank_on(crtc);
6179
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006180 for_each_encoder_on_crtc(dev, crtc, encoder)
6181 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006182}
6183
Daniel Vetter87476d62013-04-11 16:29:06 +02006184static void i9xx_pfit_disable(struct intel_crtc *crtc)
6185{
6186 struct drm_device *dev = crtc->base.dev;
6187 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006188
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006189 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006190 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006191
6192 assert_pipe_disabled(dev_priv, crtc->pipe);
6193
Daniel Vetter328d8e82013-05-08 10:36:31 +02006194 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6195 I915_READ(PFIT_CONTROL));
6196 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006197}
6198
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006199static void i9xx_crtc_disable(struct drm_crtc *crtc)
6200{
6201 struct drm_device *dev = crtc->dev;
6202 struct drm_i915_private *dev_priv = dev->dev_private;
6203 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006204 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006205 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006206
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006207 /*
6208 * On gen2 planes are double buffered but the pipe isn't, so we must
6209 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006210 * We also need to wait on all gmch platforms because of the
6211 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006212 */
Imre Deak564ed192014-06-13 14:54:21 +03006213 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006214
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006215 for_each_encoder_on_crtc(dev, crtc, encoder)
6216 encoder->disable(encoder);
6217
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006218 drm_crtc_vblank_off(crtc);
6219 assert_vblank_disabled(crtc);
6220
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006221 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006222
Daniel Vetter87476d62013-04-11 16:29:06 +02006223 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006224
Jesse Barnes89b667f2013-04-18 14:51:36 -07006225 for_each_encoder_on_crtc(dev, crtc, encoder)
6226 if (encoder->post_disable)
6227 encoder->post_disable(encoder);
6228
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006229 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006230 if (IS_CHERRYVIEW(dev))
6231 chv_disable_pll(dev_priv, pipe);
6232 else if (IS_VALLEYVIEW(dev))
6233 vlv_disable_pll(dev_priv, pipe);
6234 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006235 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006236 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006237
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006238 for_each_encoder_on_crtc(dev, crtc, encoder)
6239 if (encoder->post_pll_disable)
6240 encoder->post_pll_disable(encoder);
6241
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006242 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006243 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006244}
6245
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006246static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006247{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006248 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006249 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006250 enum intel_display_power_domain domain;
6251 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006252
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006253 if (!intel_crtc->active)
6254 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006255
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006256 if (to_intel_plane_state(crtc->primary->state)->visible) {
6257 intel_crtc_wait_for_pending_flips(crtc);
6258 intel_pre_disable_primary(crtc);
6259 }
6260
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02006261 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006262 dev_priv->display.crtc_disable(crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006263 intel_crtc->active = false;
6264 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006265 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006266
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006267 domains = intel_crtc->enabled_power_domains;
6268 for_each_power_domain(domain, domains)
6269 intel_display_power_put(dev_priv, domain);
6270 intel_crtc->enabled_power_domains = 0;
6271}
6272
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006273/*
6274 * turn all crtc's off, but do not adjust state
6275 * This has to be paired with a call to intel_modeset_setup_hw_state.
6276 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006277int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006278{
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006279 struct drm_mode_config *config = &dev->mode_config;
6280 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
6281 struct drm_atomic_state *state;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006282 struct drm_crtc *crtc;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006283 unsigned crtc_mask = 0;
6284 int ret = 0;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006285
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006286 if (WARN_ON(!ctx))
6287 return 0;
6288
6289 lockdep_assert_held(&ctx->ww_ctx);
6290 state = drm_atomic_state_alloc(dev);
6291 if (WARN_ON(!state))
6292 return -ENOMEM;
6293
6294 state->acquire_ctx = ctx;
6295 state->allow_modeset = true;
6296
6297 for_each_crtc(dev, crtc) {
6298 struct drm_crtc_state *crtc_state =
6299 drm_atomic_get_crtc_state(state, crtc);
6300
6301 ret = PTR_ERR_OR_ZERO(crtc_state);
6302 if (ret)
6303 goto free;
6304
6305 if (!crtc_state->active)
6306 continue;
6307
6308 crtc_state->active = false;
6309 crtc_mask |= 1 << drm_crtc_index(crtc);
6310 }
6311
6312 if (crtc_mask) {
Maarten Lankhorst74c090b2015-07-13 16:30:30 +02006313 ret = drm_atomic_commit(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006314
6315 if (!ret) {
6316 for_each_crtc(dev, crtc)
6317 if (crtc_mask & (1 << drm_crtc_index(crtc)))
6318 crtc->state->active = true;
6319
6320 return ret;
6321 }
6322 }
6323
6324free:
6325 if (ret)
6326 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
6327 drm_atomic_state_free(state);
6328 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006329}
6330
Chris Wilsonea5b2132010-08-04 13:50:23 +01006331void intel_encoder_destroy(struct drm_encoder *encoder)
6332{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006333 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006334
Chris Wilsonea5b2132010-08-04 13:50:23 +01006335 drm_encoder_cleanup(encoder);
6336 kfree(intel_encoder);
6337}
6338
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006339/* Cross check the actual hw state with our own modeset state tracking (and it's
6340 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006341static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006342{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006343 struct drm_crtc *crtc = connector->base.state->crtc;
6344
6345 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6346 connector->base.base.id,
6347 connector->base.name);
6348
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006349 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006350 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006351 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006352
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006353 I915_STATE_WARN(!crtc,
6354 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006355
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006356 if (!crtc)
Dave Airlie0e32b392014-05-02 14:02:48 +10006357 return;
6358
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006359 I915_STATE_WARN(!crtc->state->active,
6360 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006361
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006362 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006363 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006364
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006365 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006366 "atomic encoder doesn't match attached encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006367
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006368 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006369 "attached encoder crtc differs from connector crtc\n");
6370 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006371 I915_STATE_WARN(crtc && crtc->state->active,
6372 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006373 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6374 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006375 }
6376}
6377
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006378int intel_connector_init(struct intel_connector *connector)
6379{
6380 struct drm_connector_state *connector_state;
6381
6382 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6383 if (!connector_state)
6384 return -ENOMEM;
6385
6386 connector->base.state = connector_state;
6387 return 0;
6388}
6389
6390struct intel_connector *intel_connector_alloc(void)
6391{
6392 struct intel_connector *connector;
6393
6394 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6395 if (!connector)
6396 return NULL;
6397
6398 if (intel_connector_init(connector) < 0) {
6399 kfree(connector);
6400 return NULL;
6401 }
6402
6403 return connector;
6404}
6405
Daniel Vetterf0947c32012-07-02 13:10:34 +02006406/* Simple connector->get_hw_state implementation for encoders that support only
6407 * one connector and no cloning and hence the encoder state determines the state
6408 * of the connector. */
6409bool intel_connector_get_hw_state(struct intel_connector *connector)
6410{
Daniel Vetter24929352012-07-02 20:28:59 +02006411 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006412 struct intel_encoder *encoder = connector->encoder;
6413
6414 return encoder->get_hw_state(encoder, &pipe);
6415}
6416
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006417static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006418{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006419 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6420 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006421
6422 return 0;
6423}
6424
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006425static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006426 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006427{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006428 struct drm_atomic_state *state = pipe_config->base.state;
6429 struct intel_crtc *other_crtc;
6430 struct intel_crtc_state *other_crtc_state;
6431
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006432 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6433 pipe_name(pipe), pipe_config->fdi_lanes);
6434 if (pipe_config->fdi_lanes > 4) {
6435 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6436 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006437 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006438 }
6439
Paulo Zanonibafb6552013-11-02 21:07:44 -07006440 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006441 if (pipe_config->fdi_lanes > 2) {
6442 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6443 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006444 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006445 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006446 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006447 }
6448 }
6449
6450 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006451 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006452
6453 /* Ivybridge 3 pipe is really complicated */
6454 switch (pipe) {
6455 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006456 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006457 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006458 if (pipe_config->fdi_lanes <= 2)
6459 return 0;
6460
6461 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6462 other_crtc_state =
6463 intel_atomic_get_crtc_state(state, other_crtc);
6464 if (IS_ERR(other_crtc_state))
6465 return PTR_ERR(other_crtc_state);
6466
6467 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006468 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6469 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006470 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006471 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006472 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006473 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006474 if (pipe_config->fdi_lanes > 2) {
6475 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6476 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006477 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006478 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006479
6480 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6481 other_crtc_state =
6482 intel_atomic_get_crtc_state(state, other_crtc);
6483 if (IS_ERR(other_crtc_state))
6484 return PTR_ERR(other_crtc_state);
6485
6486 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006487 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006488 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006489 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006490 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006491 default:
6492 BUG();
6493 }
6494}
6495
Daniel Vettere29c22c2013-02-21 00:00:16 +01006496#define RETRY 1
6497static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006498 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006499{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006500 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006501 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006502 int lane, link_bw, fdi_dotclock, ret;
6503 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006504
Daniel Vettere29c22c2013-02-21 00:00:16 +01006505retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006506 /* FDI is a binary signal running at ~2.7GHz, encoding
6507 * each output octet as 10 bits. The actual frequency
6508 * is stored as a divider into a 100MHz clock, and the
6509 * mode pixel clock is stored in units of 1KHz.
6510 * Hence the bw of each lane in terms of the mode signal
6511 * is:
6512 */
6513 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6514
Damien Lespiau241bfc32013-09-25 16:45:37 +01006515 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006516
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006517 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006518 pipe_config->pipe_bpp);
6519
6520 pipe_config->fdi_lanes = lane;
6521
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006522 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006523 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006524
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006525 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6526 intel_crtc->pipe, pipe_config);
6527 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006528 pipe_config->pipe_bpp -= 2*3;
6529 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6530 pipe_config->pipe_bpp);
6531 needs_recompute = true;
6532 pipe_config->bw_constrained = true;
6533
6534 goto retry;
6535 }
6536
6537 if (needs_recompute)
6538 return RETRY;
6539
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006540 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006541}
6542
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006543static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6544 struct intel_crtc_state *pipe_config)
6545{
6546 if (pipe_config->pipe_bpp > 24)
6547 return false;
6548
6549 /* HSW can handle pixel rate up to cdclk? */
6550 if (IS_HASWELL(dev_priv->dev))
6551 return true;
6552
6553 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006554 * We compare against max which means we must take
6555 * the increased cdclk requirement into account when
6556 * calculating the new cdclk.
6557 *
6558 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006559 */
6560 return ilk_pipe_pixel_rate(pipe_config) <=
6561 dev_priv->max_cdclk_freq * 95 / 100;
6562}
6563
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006564static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006565 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006566{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006567 struct drm_device *dev = crtc->base.dev;
6568 struct drm_i915_private *dev_priv = dev->dev_private;
6569
Jani Nikulad330a952014-01-21 11:24:25 +02006570 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006571 hsw_crtc_supports_ips(crtc) &&
6572 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006573}
6574
Daniel Vettera43f6e02013-06-07 23:10:32 +02006575static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006576 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006577{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006578 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006579 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006580 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006581
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006582 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006583 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006584 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006585
6586 /*
6587 * Enable pixel doubling when the dot clock
6588 * is > 90% of the (display) core speed.
6589 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006590 * GDG double wide on either pipe,
6591 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006592 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006593 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006594 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006595 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006596 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006597 }
6598
Damien Lespiau241bfc32013-09-25 16:45:37 +01006599 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006600 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006601 }
Chris Wilson89749352010-09-12 18:25:19 +01006602
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006603 /*
6604 * Pipe horizontal size must be even in:
6605 * - DVO ganged mode
6606 * - LVDS dual channel mode
6607 * - Double wide pipe
6608 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006609 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006610 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6611 pipe_config->pipe_src_w &= ~1;
6612
Damien Lespiau8693a822013-05-03 18:48:11 +01006613 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6614 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006615 */
6616 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006617 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006618 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006619
Damien Lespiauf5adf942013-06-24 18:29:34 +01006620 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006621 hsw_compute_ips_config(crtc, pipe_config);
6622
Daniel Vetter877d48d2013-04-19 11:24:43 +02006623 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006624 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006625
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006626 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006627}
6628
Ville Syrjälä1652d192015-03-31 14:12:01 +03006629static int skylake_get_display_clock_speed(struct drm_device *dev)
6630{
6631 struct drm_i915_private *dev_priv = to_i915(dev);
6632 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6633 uint32_t cdctl = I915_READ(CDCLK_CTL);
6634 uint32_t linkrate;
6635
Damien Lespiau414355a2015-06-04 18:21:31 +01006636 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006637 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006638
6639 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6640 return 540000;
6641
6642 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006643 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006644
Damien Lespiau71cd8422015-04-30 16:39:17 +01006645 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6646 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006647 /* vco 8640 */
6648 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6649 case CDCLK_FREQ_450_432:
6650 return 432000;
6651 case CDCLK_FREQ_337_308:
6652 return 308570;
6653 case CDCLK_FREQ_675_617:
6654 return 617140;
6655 default:
6656 WARN(1, "Unknown cd freq selection\n");
6657 }
6658 } else {
6659 /* vco 8100 */
6660 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6661 case CDCLK_FREQ_450_432:
6662 return 450000;
6663 case CDCLK_FREQ_337_308:
6664 return 337500;
6665 case CDCLK_FREQ_675_617:
6666 return 675000;
6667 default:
6668 WARN(1, "Unknown cd freq selection\n");
6669 }
6670 }
6671
6672 /* error case, do as if DPLL0 isn't enabled */
6673 return 24000;
6674}
6675
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006676static int broxton_get_display_clock_speed(struct drm_device *dev)
6677{
6678 struct drm_i915_private *dev_priv = to_i915(dev);
6679 uint32_t cdctl = I915_READ(CDCLK_CTL);
6680 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6681 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6682 int cdclk;
6683
6684 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6685 return 19200;
6686
6687 cdclk = 19200 * pll_ratio / 2;
6688
6689 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6690 case BXT_CDCLK_CD2X_DIV_SEL_1:
6691 return cdclk; /* 576MHz or 624MHz */
6692 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6693 return cdclk * 2 / 3; /* 384MHz */
6694 case BXT_CDCLK_CD2X_DIV_SEL_2:
6695 return cdclk / 2; /* 288MHz */
6696 case BXT_CDCLK_CD2X_DIV_SEL_4:
6697 return cdclk / 4; /* 144MHz */
6698 }
6699
6700 /* error case, do as if DE PLL isn't enabled */
6701 return 19200;
6702}
6703
Ville Syrjälä1652d192015-03-31 14:12:01 +03006704static int broadwell_get_display_clock_speed(struct drm_device *dev)
6705{
6706 struct drm_i915_private *dev_priv = dev->dev_private;
6707 uint32_t lcpll = I915_READ(LCPLL_CTL);
6708 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6709
6710 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6711 return 800000;
6712 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6713 return 450000;
6714 else if (freq == LCPLL_CLK_FREQ_450)
6715 return 450000;
6716 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6717 return 540000;
6718 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6719 return 337500;
6720 else
6721 return 675000;
6722}
6723
6724static int haswell_get_display_clock_speed(struct drm_device *dev)
6725{
6726 struct drm_i915_private *dev_priv = dev->dev_private;
6727 uint32_t lcpll = I915_READ(LCPLL_CTL);
6728 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6729
6730 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6731 return 800000;
6732 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6733 return 450000;
6734 else if (freq == LCPLL_CLK_FREQ_450)
6735 return 450000;
6736 else if (IS_HSW_ULT(dev))
6737 return 337500;
6738 else
6739 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006740}
6741
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006742static int valleyview_get_display_clock_speed(struct drm_device *dev)
6743{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006744 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6745 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006746}
6747
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006748static int ilk_get_display_clock_speed(struct drm_device *dev)
6749{
6750 return 450000;
6751}
6752
Jesse Barnese70236a2009-09-21 10:42:27 -07006753static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006754{
Jesse Barnese70236a2009-09-21 10:42:27 -07006755 return 400000;
6756}
Jesse Barnes79e53942008-11-07 14:24:08 -08006757
Jesse Barnese70236a2009-09-21 10:42:27 -07006758static int i915_get_display_clock_speed(struct drm_device *dev)
6759{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006760 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006761}
Jesse Barnes79e53942008-11-07 14:24:08 -08006762
Jesse Barnese70236a2009-09-21 10:42:27 -07006763static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6764{
6765 return 200000;
6766}
Jesse Barnes79e53942008-11-07 14:24:08 -08006767
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006768static int pnv_get_display_clock_speed(struct drm_device *dev)
6769{
6770 u16 gcfgc = 0;
6771
6772 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6773
6774 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6775 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006776 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006777 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006778 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006779 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006780 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006781 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6782 return 200000;
6783 default:
6784 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6785 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006786 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006787 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006788 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006789 }
6790}
6791
Jesse Barnese70236a2009-09-21 10:42:27 -07006792static int i915gm_get_display_clock_speed(struct drm_device *dev)
6793{
6794 u16 gcfgc = 0;
6795
6796 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6797
6798 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006799 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006800 else {
6801 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6802 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006803 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006804 default:
6805 case GC_DISPLAY_CLOCK_190_200_MHZ:
6806 return 190000;
6807 }
6808 }
6809}
Jesse Barnes79e53942008-11-07 14:24:08 -08006810
Jesse Barnese70236a2009-09-21 10:42:27 -07006811static int i865_get_display_clock_speed(struct drm_device *dev)
6812{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006813 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006814}
6815
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006816static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006817{
6818 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006819
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006820 /*
6821 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6822 * encoding is different :(
6823 * FIXME is this the right way to detect 852GM/852GMV?
6824 */
6825 if (dev->pdev->revision == 0x1)
6826 return 133333;
6827
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006828 pci_bus_read_config_word(dev->pdev->bus,
6829 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6830
Jesse Barnese70236a2009-09-21 10:42:27 -07006831 /* Assume that the hardware is in the high speed state. This
6832 * should be the default.
6833 */
6834 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6835 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006836 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006837 case GC_CLOCK_100_200:
6838 return 200000;
6839 case GC_CLOCK_166_250:
6840 return 250000;
6841 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006842 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006843 case GC_CLOCK_133_266:
6844 case GC_CLOCK_133_266_2:
6845 case GC_CLOCK_166_266:
6846 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006847 }
6848
6849 /* Shouldn't happen */
6850 return 0;
6851}
6852
6853static int i830_get_display_clock_speed(struct drm_device *dev)
6854{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006855 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006856}
6857
Ville Syrjälä34edce22015-05-22 11:22:33 +03006858static unsigned int intel_hpll_vco(struct drm_device *dev)
6859{
6860 struct drm_i915_private *dev_priv = dev->dev_private;
6861 static const unsigned int blb_vco[8] = {
6862 [0] = 3200000,
6863 [1] = 4000000,
6864 [2] = 5333333,
6865 [3] = 4800000,
6866 [4] = 6400000,
6867 };
6868 static const unsigned int pnv_vco[8] = {
6869 [0] = 3200000,
6870 [1] = 4000000,
6871 [2] = 5333333,
6872 [3] = 4800000,
6873 [4] = 2666667,
6874 };
6875 static const unsigned int cl_vco[8] = {
6876 [0] = 3200000,
6877 [1] = 4000000,
6878 [2] = 5333333,
6879 [3] = 6400000,
6880 [4] = 3333333,
6881 [5] = 3566667,
6882 [6] = 4266667,
6883 };
6884 static const unsigned int elk_vco[8] = {
6885 [0] = 3200000,
6886 [1] = 4000000,
6887 [2] = 5333333,
6888 [3] = 4800000,
6889 };
6890 static const unsigned int ctg_vco[8] = {
6891 [0] = 3200000,
6892 [1] = 4000000,
6893 [2] = 5333333,
6894 [3] = 6400000,
6895 [4] = 2666667,
6896 [5] = 4266667,
6897 };
6898 const unsigned int *vco_table;
6899 unsigned int vco;
6900 uint8_t tmp = 0;
6901
6902 /* FIXME other chipsets? */
6903 if (IS_GM45(dev))
6904 vco_table = ctg_vco;
6905 else if (IS_G4X(dev))
6906 vco_table = elk_vco;
6907 else if (IS_CRESTLINE(dev))
6908 vco_table = cl_vco;
6909 else if (IS_PINEVIEW(dev))
6910 vco_table = pnv_vco;
6911 else if (IS_G33(dev))
6912 vco_table = blb_vco;
6913 else
6914 return 0;
6915
6916 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6917
6918 vco = vco_table[tmp & 0x7];
6919 if (vco == 0)
6920 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6921 else
6922 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6923
6924 return vco;
6925}
6926
6927static int gm45_get_display_clock_speed(struct drm_device *dev)
6928{
6929 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6930 uint16_t tmp = 0;
6931
6932 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6933
6934 cdclk_sel = (tmp >> 12) & 0x1;
6935
6936 switch (vco) {
6937 case 2666667:
6938 case 4000000:
6939 case 5333333:
6940 return cdclk_sel ? 333333 : 222222;
6941 case 3200000:
6942 return cdclk_sel ? 320000 : 228571;
6943 default:
6944 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6945 return 222222;
6946 }
6947}
6948
6949static int i965gm_get_display_clock_speed(struct drm_device *dev)
6950{
6951 static const uint8_t div_3200[] = { 16, 10, 8 };
6952 static const uint8_t div_4000[] = { 20, 12, 10 };
6953 static const uint8_t div_5333[] = { 24, 16, 14 };
6954 const uint8_t *div_table;
6955 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6956 uint16_t tmp = 0;
6957
6958 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6959
6960 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6961
6962 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6963 goto fail;
6964
6965 switch (vco) {
6966 case 3200000:
6967 div_table = div_3200;
6968 break;
6969 case 4000000:
6970 div_table = div_4000;
6971 break;
6972 case 5333333:
6973 div_table = div_5333;
6974 break;
6975 default:
6976 goto fail;
6977 }
6978
6979 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6980
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006981fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006982 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6983 return 200000;
6984}
6985
6986static int g33_get_display_clock_speed(struct drm_device *dev)
6987{
6988 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6989 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6990 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6991 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6992 const uint8_t *div_table;
6993 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6994 uint16_t tmp = 0;
6995
6996 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6997
6998 cdclk_sel = (tmp >> 4) & 0x7;
6999
7000 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7001 goto fail;
7002
7003 switch (vco) {
7004 case 3200000:
7005 div_table = div_3200;
7006 break;
7007 case 4000000:
7008 div_table = div_4000;
7009 break;
7010 case 4800000:
7011 div_table = div_4800;
7012 break;
7013 case 5333333:
7014 div_table = div_5333;
7015 break;
7016 default:
7017 goto fail;
7018 }
7019
7020 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7021
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007022fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007023 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7024 return 190476;
7025}
7026
Zhenyu Wang2c072452009-06-05 15:38:42 +08007027static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007028intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007029{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007030 while (*num > DATA_LINK_M_N_MASK ||
7031 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007032 *num >>= 1;
7033 *den >>= 1;
7034 }
7035}
7036
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007037static void compute_m_n(unsigned int m, unsigned int n,
7038 uint32_t *ret_m, uint32_t *ret_n)
7039{
7040 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7041 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7042 intel_reduce_m_n_ratio(ret_m, ret_n);
7043}
7044
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007045void
7046intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7047 int pixel_clock, int link_clock,
7048 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007049{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007050 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007051
7052 compute_m_n(bits_per_pixel * pixel_clock,
7053 link_clock * nlanes * 8,
7054 &m_n->gmch_m, &m_n->gmch_n);
7055
7056 compute_m_n(pixel_clock, link_clock,
7057 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007058}
7059
Chris Wilsona7615032011-01-12 17:04:08 +00007060static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7061{
Jani Nikulad330a952014-01-21 11:24:25 +02007062 if (i915.panel_use_ssc >= 0)
7063 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007064 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007065 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007066}
7067
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007068static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7069 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007070{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007071 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007072 struct drm_i915_private *dev_priv = dev->dev_private;
7073 int refclk;
7074
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007075 WARN_ON(!crtc_state->base.state);
7076
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007077 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007078 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007079 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007080 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007081 refclk = dev_priv->vbt.lvds_ssc_freq;
7082 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007083 } else if (!IS_GEN2(dev)) {
7084 refclk = 96000;
7085 } else {
7086 refclk = 48000;
7087 }
7088
7089 return refclk;
7090}
7091
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007092static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007093{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007094 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007095}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007096
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007097static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7098{
7099 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007100}
7101
Daniel Vetterf47709a2013-03-28 10:42:02 +01007102static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007103 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007104 intel_clock_t *reduced_clock)
7105{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007106 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007107 u32 fp, fp2 = 0;
7108
7109 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007110 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007111 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007112 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007113 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007114 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007115 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007116 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007117 }
7118
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007119 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007120
Daniel Vetterf47709a2013-03-28 10:42:02 +01007121 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007122 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007123 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007124 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007125 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007126 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007127 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007128 }
7129}
7130
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007131static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7132 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007133{
7134 u32 reg_val;
7135
7136 /*
7137 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7138 * and set it to a reasonable value instead.
7139 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007140 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007141 reg_val &= 0xffffff00;
7142 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007143 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007144
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007145 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007146 reg_val &= 0x8cffffff;
7147 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007148 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007149
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007150 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007151 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007152 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007153
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007154 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007155 reg_val &= 0x00ffffff;
7156 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007157 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007158}
7159
Daniel Vetterb5518422013-05-03 11:49:48 +02007160static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7161 struct intel_link_m_n *m_n)
7162{
7163 struct drm_device *dev = crtc->base.dev;
7164 struct drm_i915_private *dev_priv = dev->dev_private;
7165 int pipe = crtc->pipe;
7166
Daniel Vettere3b95f12013-05-03 11:49:49 +02007167 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7168 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7169 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7170 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007171}
7172
7173static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007174 struct intel_link_m_n *m_n,
7175 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007176{
7177 struct drm_device *dev = crtc->base.dev;
7178 struct drm_i915_private *dev_priv = dev->dev_private;
7179 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007180 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007181
7182 if (INTEL_INFO(dev)->gen >= 5) {
7183 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7184 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7185 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7186 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007187 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7188 * for gen < 8) and if DRRS is supported (to make sure the
7189 * registers are not unnecessarily accessed).
7190 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307191 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007192 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007193 I915_WRITE(PIPE_DATA_M2(transcoder),
7194 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7195 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7196 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7197 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7198 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007199 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007200 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7201 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7202 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7203 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007204 }
7205}
7206
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307207void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007208{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307209 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7210
7211 if (m_n == M1_N1) {
7212 dp_m_n = &crtc->config->dp_m_n;
7213 dp_m2_n2 = &crtc->config->dp_m2_n2;
7214 } else if (m_n == M2_N2) {
7215
7216 /*
7217 * M2_N2 registers are not supported. Hence m2_n2 divider value
7218 * needs to be programmed into M1_N1.
7219 */
7220 dp_m_n = &crtc->config->dp_m2_n2;
7221 } else {
7222 DRM_ERROR("Unsupported divider value\n");
7223 return;
7224 }
7225
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007226 if (crtc->config->has_pch_encoder)
7227 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007228 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307229 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007230}
7231
Daniel Vetter251ac862015-06-18 10:30:24 +02007232static void vlv_compute_dpll(struct intel_crtc *crtc,
7233 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007234{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007235 u32 dpll, dpll_md;
7236
7237 /*
7238 * Enable DPIO clock input. We should never disable the reference
7239 * clock for pipe B, since VGA hotplug / manual detection depends
7240 * on it.
7241 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007242 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7243 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007244 /* We should never disable this, set it here for state tracking */
7245 if (crtc->pipe == PIPE_B)
7246 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7247 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007248 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007249
Ville Syrjäläd288f652014-10-28 13:20:22 +02007250 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007251 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007252 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007253}
7254
Ville Syrjäläd288f652014-10-28 13:20:22 +02007255static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007256 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007257{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007258 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007259 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007260 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007261 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007262 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007263 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007264
Ville Syrjäläa5805162015-05-26 20:42:30 +03007265 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007266
Ville Syrjäläd288f652014-10-28 13:20:22 +02007267 bestn = pipe_config->dpll.n;
7268 bestm1 = pipe_config->dpll.m1;
7269 bestm2 = pipe_config->dpll.m2;
7270 bestp1 = pipe_config->dpll.p1;
7271 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007272
Jesse Barnes89b667f2013-04-18 14:51:36 -07007273 /* See eDP HDMI DPIO driver vbios notes doc */
7274
7275 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007276 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007277 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007278
7279 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007280 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007281
7282 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007283 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007284 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007285 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007286
7287 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007288 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007289
7290 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007291 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7292 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7293 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007294 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007295
7296 /*
7297 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7298 * but we don't support that).
7299 * Note: don't use the DAC post divider as it seems unstable.
7300 */
7301 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007302 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007303
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007304 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007305 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007306
Jesse Barnes89b667f2013-04-18 14:51:36 -07007307 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007308 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007309 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7310 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007311 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007312 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007313 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007314 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007315 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007316
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007317 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007318 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007319 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007320 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007321 0x0df40000);
7322 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007323 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007324 0x0df70000);
7325 } else { /* HDMI or VGA */
7326 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007327 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007328 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007329 0x0df70000);
7330 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007331 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007332 0x0df40000);
7333 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007334
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007335 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007336 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007337 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7338 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007339 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007340 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007341
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007342 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007343 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007344}
7345
Daniel Vetter251ac862015-06-18 10:30:24 +02007346static void chv_compute_dpll(struct intel_crtc *crtc,
7347 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007348{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007349 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7350 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007351 DPLL_VCO_ENABLE;
7352 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007353 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007354
Ville Syrjäläd288f652014-10-28 13:20:22 +02007355 pipe_config->dpll_hw_state.dpll_md =
7356 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007357}
7358
Ville Syrjäläd288f652014-10-28 13:20:22 +02007359static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007360 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007361{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007362 struct drm_device *dev = crtc->base.dev;
7363 struct drm_i915_private *dev_priv = dev->dev_private;
7364 int pipe = crtc->pipe;
7365 int dpll_reg = DPLL(crtc->pipe);
7366 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307367 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007368 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307369 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307370 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007371
Ville Syrjäläd288f652014-10-28 13:20:22 +02007372 bestn = pipe_config->dpll.n;
7373 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7374 bestm1 = pipe_config->dpll.m1;
7375 bestm2 = pipe_config->dpll.m2 >> 22;
7376 bestp1 = pipe_config->dpll.p1;
7377 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307378 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307379 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307380 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007381
7382 /*
7383 * Enable Refclk and SSC
7384 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007385 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007386 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007387
Ville Syrjäläa5805162015-05-26 20:42:30 +03007388 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007389
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007390 /* p1 and p2 divider */
7391 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7392 5 << DPIO_CHV_S1_DIV_SHIFT |
7393 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7394 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7395 1 << DPIO_CHV_K_DIV_SHIFT);
7396
7397 /* Feedback post-divider - m2 */
7398 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7399
7400 /* Feedback refclk divider - n and m1 */
7401 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7402 DPIO_CHV_M1_DIV_BY_2 |
7403 1 << DPIO_CHV_N_DIV_SHIFT);
7404
7405 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007406 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007407
7408 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307409 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7410 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7411 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7412 if (bestm2_frac)
7413 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7414 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007415
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307416 /* Program digital lock detect threshold */
7417 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7418 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7419 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7420 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7421 if (!bestm2_frac)
7422 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7423 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7424
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007425 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307426 if (vco == 5400000) {
7427 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7428 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7429 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7430 tribuf_calcntr = 0x9;
7431 } else if (vco <= 6200000) {
7432 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7433 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7434 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7435 tribuf_calcntr = 0x9;
7436 } else if (vco <= 6480000) {
7437 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7438 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7439 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7440 tribuf_calcntr = 0x8;
7441 } else {
7442 /* Not supported. Apply the same limits as in the max case */
7443 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7444 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7445 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7446 tribuf_calcntr = 0;
7447 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007448 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7449
Ville Syrjälä968040b2015-03-11 22:52:08 +02007450 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307451 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7452 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7453 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7454
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007455 /* AFC Recal */
7456 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7457 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7458 DPIO_AFC_RECAL);
7459
Ville Syrjäläa5805162015-05-26 20:42:30 +03007460 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007461}
7462
Ville Syrjäläd288f652014-10-28 13:20:22 +02007463/**
7464 * vlv_force_pll_on - forcibly enable just the PLL
7465 * @dev_priv: i915 private structure
7466 * @pipe: pipe PLL to enable
7467 * @dpll: PLL configuration
7468 *
7469 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7470 * in cases where we need the PLL enabled even when @pipe is not going to
7471 * be enabled.
7472 */
7473void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7474 const struct dpll *dpll)
7475{
7476 struct intel_crtc *crtc =
7477 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007478 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007479 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007480 .pixel_multiplier = 1,
7481 .dpll = *dpll,
7482 };
7483
7484 if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007485 chv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007486 chv_prepare_pll(crtc, &pipe_config);
7487 chv_enable_pll(crtc, &pipe_config);
7488 } else {
Daniel Vetter251ac862015-06-18 10:30:24 +02007489 vlv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007490 vlv_prepare_pll(crtc, &pipe_config);
7491 vlv_enable_pll(crtc, &pipe_config);
7492 }
7493}
7494
7495/**
7496 * vlv_force_pll_off - forcibly disable just the PLL
7497 * @dev_priv: i915 private structure
7498 * @pipe: pipe PLL to disable
7499 *
7500 * Disable the PLL for @pipe. To be used in cases where we need
7501 * the PLL enabled even when @pipe is not going to be enabled.
7502 */
7503void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7504{
7505 if (IS_CHERRYVIEW(dev))
7506 chv_disable_pll(to_i915(dev), pipe);
7507 else
7508 vlv_disable_pll(to_i915(dev), pipe);
7509}
7510
Daniel Vetter251ac862015-06-18 10:30:24 +02007511static void i9xx_compute_dpll(struct intel_crtc *crtc,
7512 struct intel_crtc_state *crtc_state,
7513 intel_clock_t *reduced_clock,
7514 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007515{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007516 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007517 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007518 u32 dpll;
7519 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007520 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007521
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007522 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307523
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007524 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7525 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007526
7527 dpll = DPLL_VGA_MODE_DIS;
7528
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007529 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007530 dpll |= DPLLB_MODE_LVDS;
7531 else
7532 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007533
Daniel Vetteref1b4602013-06-01 17:17:04 +02007534 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007535 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007536 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007537 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007538
7539 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007540 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007541
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007542 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007543 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007544
7545 /* compute bitmask from p1 value */
7546 if (IS_PINEVIEW(dev))
7547 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7548 else {
7549 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7550 if (IS_G4X(dev) && reduced_clock)
7551 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7552 }
7553 switch (clock->p2) {
7554 case 5:
7555 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7556 break;
7557 case 7:
7558 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7559 break;
7560 case 10:
7561 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7562 break;
7563 case 14:
7564 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7565 break;
7566 }
7567 if (INTEL_INFO(dev)->gen >= 4)
7568 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7569
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007570 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007571 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007572 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007573 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7574 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7575 else
7576 dpll |= PLL_REF_INPUT_DREFCLK;
7577
7578 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007579 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007580
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007581 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007582 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007583 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007584 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007585 }
7586}
7587
Daniel Vetter251ac862015-06-18 10:30:24 +02007588static void i8xx_compute_dpll(struct intel_crtc *crtc,
7589 struct intel_crtc_state *crtc_state,
7590 intel_clock_t *reduced_clock,
7591 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007592{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007593 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007594 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007595 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007596 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007597
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007598 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307599
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007600 dpll = DPLL_VGA_MODE_DIS;
7601
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007602 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007603 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7604 } else {
7605 if (clock->p1 == 2)
7606 dpll |= PLL_P1_DIVIDE_BY_TWO;
7607 else
7608 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7609 if (clock->p2 == 4)
7610 dpll |= PLL_P2_DIVIDE_BY_4;
7611 }
7612
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007613 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007614 dpll |= DPLL_DVO_2X_MODE;
7615
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007616 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007617 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7618 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7619 else
7620 dpll |= PLL_REF_INPUT_DREFCLK;
7621
7622 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007623 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007624}
7625
Daniel Vetter8a654f32013-06-01 17:16:22 +02007626static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007627{
7628 struct drm_device *dev = intel_crtc->base.dev;
7629 struct drm_i915_private *dev_priv = dev->dev_private;
7630 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007631 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007632 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007633 uint32_t crtc_vtotal, crtc_vblank_end;
7634 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007635
7636 /* We need to be careful not to changed the adjusted mode, for otherwise
7637 * the hw state checker will get angry at the mismatch. */
7638 crtc_vtotal = adjusted_mode->crtc_vtotal;
7639 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007640
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007641 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007642 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007643 crtc_vtotal -= 1;
7644 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007645
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007646 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007647 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7648 else
7649 vsyncshift = adjusted_mode->crtc_hsync_start -
7650 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007651 if (vsyncshift < 0)
7652 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007653 }
7654
7655 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007656 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007657
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007658 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007659 (adjusted_mode->crtc_hdisplay - 1) |
7660 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007661 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007662 (adjusted_mode->crtc_hblank_start - 1) |
7663 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007664 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007665 (adjusted_mode->crtc_hsync_start - 1) |
7666 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7667
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007668 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007669 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007670 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007671 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007672 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007673 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007674 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007675 (adjusted_mode->crtc_vsync_start - 1) |
7676 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7677
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007678 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7679 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7680 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7681 * bits. */
7682 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7683 (pipe == PIPE_B || pipe == PIPE_C))
7684 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7685
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007686 /* pipesrc controls the size that is scaled from, which should
7687 * always be the user's requested size.
7688 */
7689 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007690 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7691 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007692}
7693
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007694static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007695 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007696{
7697 struct drm_device *dev = crtc->base.dev;
7698 struct drm_i915_private *dev_priv = dev->dev_private;
7699 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7700 uint32_t tmp;
7701
7702 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007703 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7704 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007705 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007706 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7707 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007708 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007709 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7710 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007711
7712 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007713 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7714 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007715 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007716 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7717 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007718 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007719 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7720 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007721
7722 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007723 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7724 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7725 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007726 }
7727
7728 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007729 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7730 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7731
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007732 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7733 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007734}
7735
Daniel Vetterf6a83282014-02-11 15:28:57 -08007736void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007737 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007738{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007739 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7740 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7741 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7742 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007743
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007744 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7745 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7746 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7747 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007748
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007749 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007750 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007751
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007752 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7753 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007754
7755 mode->hsync = drm_mode_hsync(mode);
7756 mode->vrefresh = drm_mode_vrefresh(mode);
7757 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007758}
7759
Daniel Vetter84b046f2013-02-19 18:48:54 +01007760static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7761{
7762 struct drm_device *dev = intel_crtc->base.dev;
7763 struct drm_i915_private *dev_priv = dev->dev_private;
7764 uint32_t pipeconf;
7765
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007766 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007767
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007768 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7769 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7770 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007771
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007772 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007773 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007774
Daniel Vetterff9ce462013-04-24 14:57:17 +02007775 /* only g4x and later have fancy bpc/dither controls */
7776 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007777 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007778 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007779 pipeconf |= PIPECONF_DITHER_EN |
7780 PIPECONF_DITHER_TYPE_SP;
7781
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007782 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007783 case 18:
7784 pipeconf |= PIPECONF_6BPC;
7785 break;
7786 case 24:
7787 pipeconf |= PIPECONF_8BPC;
7788 break;
7789 case 30:
7790 pipeconf |= PIPECONF_10BPC;
7791 break;
7792 default:
7793 /* Case prevented by intel_choose_pipe_bpp_dither. */
7794 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007795 }
7796 }
7797
7798 if (HAS_PIPE_CXSR(dev)) {
7799 if (intel_crtc->lowfreq_avail) {
7800 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7801 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7802 } else {
7803 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007804 }
7805 }
7806
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007807 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007808 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007809 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007810 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7811 else
7812 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7813 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007814 pipeconf |= PIPECONF_PROGRESSIVE;
7815
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007816 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007817 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007818
Daniel Vetter84b046f2013-02-19 18:48:54 +01007819 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7820 POSTING_READ(PIPECONF(intel_crtc->pipe));
7821}
7822
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007823static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7824 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007825{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007826 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007827 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007828 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007829 intel_clock_t clock;
7830 bool ok;
7831 bool is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007832 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007833 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007834 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007835 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007836 struct drm_connector_state *connector_state;
7837 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007838
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007839 memset(&crtc_state->dpll_hw_state, 0,
7840 sizeof(crtc_state->dpll_hw_state));
7841
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007842 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007843 if (connector_state->crtc != &crtc->base)
7844 continue;
7845
7846 encoder = to_intel_encoder(connector_state->best_encoder);
7847
Chris Wilson5eddb702010-09-11 13:48:45 +01007848 switch (encoder->type) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007849 case INTEL_OUTPUT_DSI:
7850 is_dsi = true;
7851 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007852 default:
7853 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007854 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007855
Eric Anholtc751ce42010-03-25 11:48:48 -07007856 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007857 }
7858
Jani Nikulaf2335332013-09-13 11:03:09 +03007859 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007860 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007861
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007862 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007863 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007864
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007865 /*
7866 * Returns a set of divisors for the desired target clock with
7867 * the given refclk, or FALSE. The returned values represent
7868 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7869 * 2) / p1 / p2.
7870 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007871 limit = intel_limit(crtc_state, refclk);
7872 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007873 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007874 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007875 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007876 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7877 return -EINVAL;
7878 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007879
Jani Nikulaf2335332013-09-13 11:03:09 +03007880 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007881 crtc_state->dpll.n = clock.n;
7882 crtc_state->dpll.m1 = clock.m1;
7883 crtc_state->dpll.m2 = clock.m2;
7884 crtc_state->dpll.p1 = clock.p1;
7885 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007886 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007887
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007888 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007889 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007890 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007891 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007892 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007893 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007894 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007895 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007896 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007897 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007898 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007899
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007900 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007901}
7902
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007903static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007904 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007905{
7906 struct drm_device *dev = crtc->base.dev;
7907 struct drm_i915_private *dev_priv = dev->dev_private;
7908 uint32_t tmp;
7909
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007910 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7911 return;
7912
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007913 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007914 if (!(tmp & PFIT_ENABLE))
7915 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007916
Daniel Vetter06922822013-07-11 13:35:40 +02007917 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007918 if (INTEL_INFO(dev)->gen < 4) {
7919 if (crtc->pipe != PIPE_B)
7920 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007921 } else {
7922 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7923 return;
7924 }
7925
Daniel Vetter06922822013-07-11 13:35:40 +02007926 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007927 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7928 if (INTEL_INFO(dev)->gen < 5)
7929 pipe_config->gmch_pfit.lvds_border_bits =
7930 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7931}
7932
Jesse Barnesacbec812013-09-20 11:29:32 -07007933static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007934 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007935{
7936 struct drm_device *dev = crtc->base.dev;
7937 struct drm_i915_private *dev_priv = dev->dev_private;
7938 int pipe = pipe_config->cpu_transcoder;
7939 intel_clock_t clock;
7940 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007941 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007942
Shobhit Kumarf573de52014-07-30 20:32:37 +05307943 /* In case of MIPI DPLL will not even be used */
7944 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7945 return;
7946
Ville Syrjäläa5805162015-05-26 20:42:30 +03007947 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007948 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007949 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007950
7951 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7952 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7953 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7954 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7955 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7956
Imre Deakdccbea32015-06-22 23:35:51 +03007957 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007958}
7959
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007960static void
7961i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7962 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007963{
7964 struct drm_device *dev = crtc->base.dev;
7965 struct drm_i915_private *dev_priv = dev->dev_private;
7966 u32 val, base, offset;
7967 int pipe = crtc->pipe, plane = crtc->plane;
7968 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007969 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007970 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007971 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007972
Damien Lespiau42a7b082015-02-05 19:35:13 +00007973 val = I915_READ(DSPCNTR(plane));
7974 if (!(val & DISPLAY_PLANE_ENABLE))
7975 return;
7976
Damien Lespiaud9806c92015-01-21 14:07:19 +00007977 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007978 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007979 DRM_DEBUG_KMS("failed to alloc fb\n");
7980 return;
7981 }
7982
Damien Lespiau1b842c82015-01-21 13:50:54 +00007983 fb = &intel_fb->base;
7984
Daniel Vetter18c52472015-02-10 17:16:09 +00007985 if (INTEL_INFO(dev)->gen >= 4) {
7986 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007987 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007988 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7989 }
7990 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007991
7992 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007993 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007994 fb->pixel_format = fourcc;
7995 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007996
7997 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007998 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007999 offset = I915_READ(DSPTILEOFF(plane));
8000 else
8001 offset = I915_READ(DSPLINOFF(plane));
8002 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8003 } else {
8004 base = I915_READ(DSPADDR(plane));
8005 }
8006 plane_config->base = base;
8007
8008 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008009 fb->width = ((val >> 16) & 0xfff) + 1;
8010 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008011
8012 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008013 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008014
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008015 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008016 fb->pixel_format,
8017 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008018
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008019 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008020
Damien Lespiau2844a922015-01-20 12:51:48 +00008021 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8022 pipe_name(pipe), plane, fb->width, fb->height,
8023 fb->bits_per_pixel, base, fb->pitches[0],
8024 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008025
Damien Lespiau2d140302015-02-05 17:22:18 +00008026 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008027}
8028
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008029static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008030 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008031{
8032 struct drm_device *dev = crtc->base.dev;
8033 struct drm_i915_private *dev_priv = dev->dev_private;
8034 int pipe = pipe_config->cpu_transcoder;
8035 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8036 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008037 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008038 int refclk = 100000;
8039
Ville Syrjäläa5805162015-05-26 20:42:30 +03008040 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008041 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8042 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8043 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8044 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008045 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008046 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008047
8048 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008049 clock.m2 = (pll_dw0 & 0xff) << 22;
8050 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8051 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008052 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8053 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8054 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8055
Imre Deakdccbea32015-06-22 23:35:51 +03008056 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008057}
8058
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008059static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008060 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008061{
8062 struct drm_device *dev = crtc->base.dev;
8063 struct drm_i915_private *dev_priv = dev->dev_private;
8064 uint32_t tmp;
8065
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008066 if (!intel_display_power_is_enabled(dev_priv,
8067 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008068 return false;
8069
Daniel Vettere143a212013-07-04 12:01:15 +02008070 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008071 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008072
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008073 tmp = I915_READ(PIPECONF(crtc->pipe));
8074 if (!(tmp & PIPECONF_ENABLE))
8075 return false;
8076
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008077 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8078 switch (tmp & PIPECONF_BPC_MASK) {
8079 case PIPECONF_6BPC:
8080 pipe_config->pipe_bpp = 18;
8081 break;
8082 case PIPECONF_8BPC:
8083 pipe_config->pipe_bpp = 24;
8084 break;
8085 case PIPECONF_10BPC:
8086 pipe_config->pipe_bpp = 30;
8087 break;
8088 default:
8089 break;
8090 }
8091 }
8092
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008093 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8094 pipe_config->limited_color_range = true;
8095
Ville Syrjälä282740f2013-09-04 18:30:03 +03008096 if (INTEL_INFO(dev)->gen < 4)
8097 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8098
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008099 intel_get_pipe_timings(crtc, pipe_config);
8100
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008101 i9xx_get_pfit_config(crtc, pipe_config);
8102
Daniel Vetter6c49f242013-06-06 12:45:25 +02008103 if (INTEL_INFO(dev)->gen >= 4) {
8104 tmp = I915_READ(DPLL_MD(crtc->pipe));
8105 pipe_config->pixel_multiplier =
8106 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8107 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008108 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008109 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8110 tmp = I915_READ(DPLL(crtc->pipe));
8111 pipe_config->pixel_multiplier =
8112 ((tmp & SDVO_MULTIPLIER_MASK)
8113 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8114 } else {
8115 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8116 * port and will be fixed up in the encoder->get_config
8117 * function. */
8118 pipe_config->pixel_multiplier = 1;
8119 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008120 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8121 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008122 /*
8123 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8124 * on 830. Filter it out here so that we don't
8125 * report errors due to that.
8126 */
8127 if (IS_I830(dev))
8128 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8129
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008130 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8131 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008132 } else {
8133 /* Mask out read-only status bits. */
8134 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8135 DPLL_PORTC_READY_MASK |
8136 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008137 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008138
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008139 if (IS_CHERRYVIEW(dev))
8140 chv_crtc_clock_get(crtc, pipe_config);
8141 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008142 vlv_crtc_clock_get(crtc, pipe_config);
8143 else
8144 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008145
Ville Syrjälä0f646142015-08-26 19:39:18 +03008146 /*
8147 * Normally the dotclock is filled in by the encoder .get_config()
8148 * but in case the pipe is enabled w/o any ports we need a sane
8149 * default.
8150 */
8151 pipe_config->base.adjusted_mode.crtc_clock =
8152 pipe_config->port_clock / pipe_config->pixel_multiplier;
8153
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008154 return true;
8155}
8156
Paulo Zanonidde86e22012-12-01 12:04:25 -02008157static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008158{
8159 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008160 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008161 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008162 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008163 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008164 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008165 bool has_ck505 = false;
8166 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008167
8168 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008169 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008170 switch (encoder->type) {
8171 case INTEL_OUTPUT_LVDS:
8172 has_panel = true;
8173 has_lvds = true;
8174 break;
8175 case INTEL_OUTPUT_EDP:
8176 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008177 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008178 has_cpu_edp = true;
8179 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008180 default:
8181 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008182 }
8183 }
8184
Keith Packard99eb6a02011-09-26 14:29:12 -07008185 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008186 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008187 can_ssc = has_ck505;
8188 } else {
8189 has_ck505 = false;
8190 can_ssc = true;
8191 }
8192
Imre Deak2de69052013-05-08 13:14:04 +03008193 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8194 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008195
8196 /* Ironlake: try to setup display ref clock before DPLL
8197 * enabling. This is only under driver's control after
8198 * PCH B stepping, previous chipset stepping should be
8199 * ignoring this setting.
8200 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008201 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008202
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008203 /* As we must carefully and slowly disable/enable each source in turn,
8204 * compute the final state we want first and check if we need to
8205 * make any changes at all.
8206 */
8207 final = val;
8208 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008209 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008210 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008211 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008212 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8213
8214 final &= ~DREF_SSC_SOURCE_MASK;
8215 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8216 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008217
Keith Packard199e5d72011-09-22 12:01:57 -07008218 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008219 final |= DREF_SSC_SOURCE_ENABLE;
8220
8221 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8222 final |= DREF_SSC1_ENABLE;
8223
8224 if (has_cpu_edp) {
8225 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8226 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8227 else
8228 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8229 } else
8230 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8231 } else {
8232 final |= DREF_SSC_SOURCE_DISABLE;
8233 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8234 }
8235
8236 if (final == val)
8237 return;
8238
8239 /* Always enable nonspread source */
8240 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8241
8242 if (has_ck505)
8243 val |= DREF_NONSPREAD_CK505_ENABLE;
8244 else
8245 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8246
8247 if (has_panel) {
8248 val &= ~DREF_SSC_SOURCE_MASK;
8249 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008250
Keith Packard199e5d72011-09-22 12:01:57 -07008251 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008252 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008253 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008254 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008255 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008256 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008257
8258 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008259 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008260 POSTING_READ(PCH_DREF_CONTROL);
8261 udelay(200);
8262
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008263 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008264
8265 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008266 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008267 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008268 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008269 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008270 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008271 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008272 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008273 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008274
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008275 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008276 POSTING_READ(PCH_DREF_CONTROL);
8277 udelay(200);
8278 } else {
8279 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8280
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008281 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008282
8283 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008284 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008285
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008286 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008287 POSTING_READ(PCH_DREF_CONTROL);
8288 udelay(200);
8289
8290 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008291 val &= ~DREF_SSC_SOURCE_MASK;
8292 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008293
8294 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008295 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008296
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008297 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008298 POSTING_READ(PCH_DREF_CONTROL);
8299 udelay(200);
8300 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008301
8302 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008303}
8304
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008305static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008306{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008307 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008308
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008309 tmp = I915_READ(SOUTH_CHICKEN2);
8310 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8311 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008312
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008313 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8314 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8315 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008316
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008317 tmp = I915_READ(SOUTH_CHICKEN2);
8318 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8319 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008320
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008321 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8322 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8323 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008324}
8325
8326/* WaMPhyProgramming:hsw */
8327static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8328{
8329 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008330
8331 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8332 tmp &= ~(0xFF << 24);
8333 tmp |= (0x12 << 24);
8334 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8335
Paulo Zanonidde86e22012-12-01 12:04:25 -02008336 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8337 tmp |= (1 << 11);
8338 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8339
8340 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8341 tmp |= (1 << 11);
8342 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8343
Paulo Zanonidde86e22012-12-01 12:04:25 -02008344 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8345 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8346 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8347
8348 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8349 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8350 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8351
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008352 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8353 tmp &= ~(7 << 13);
8354 tmp |= (5 << 13);
8355 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008356
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008357 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8358 tmp &= ~(7 << 13);
8359 tmp |= (5 << 13);
8360 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008361
8362 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8363 tmp &= ~0xFF;
8364 tmp |= 0x1C;
8365 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8366
8367 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8368 tmp &= ~0xFF;
8369 tmp |= 0x1C;
8370 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8371
8372 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8373 tmp &= ~(0xFF << 16);
8374 tmp |= (0x1C << 16);
8375 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8376
8377 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8378 tmp &= ~(0xFF << 16);
8379 tmp |= (0x1C << 16);
8380 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8381
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008382 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8383 tmp |= (1 << 27);
8384 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008385
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008386 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8387 tmp |= (1 << 27);
8388 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008389
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008390 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8391 tmp &= ~(0xF << 28);
8392 tmp |= (4 << 28);
8393 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008394
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008395 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8396 tmp &= ~(0xF << 28);
8397 tmp |= (4 << 28);
8398 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008399}
8400
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008401/* Implements 3 different sequences from BSpec chapter "Display iCLK
8402 * Programming" based on the parameters passed:
8403 * - Sequence to enable CLKOUT_DP
8404 * - Sequence to enable CLKOUT_DP without spread
8405 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8406 */
8407static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8408 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008409{
8410 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008411 uint32_t reg, tmp;
8412
8413 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8414 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008415 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008416 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008417
Ville Syrjäläa5805162015-05-26 20:42:30 +03008418 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008419
8420 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8421 tmp &= ~SBI_SSCCTL_DISABLE;
8422 tmp |= SBI_SSCCTL_PATHALT;
8423 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8424
8425 udelay(24);
8426
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008427 if (with_spread) {
8428 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8429 tmp &= ~SBI_SSCCTL_PATHALT;
8430 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008431
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008432 if (with_fdi) {
8433 lpt_reset_fdi_mphy(dev_priv);
8434 lpt_program_fdi_mphy(dev_priv);
8435 }
8436 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008437
Ville Syrjäläc2699522015-08-27 23:55:59 +03008438 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008439 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8440 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8441 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008442
Ville Syrjäläa5805162015-05-26 20:42:30 +03008443 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008444}
8445
Paulo Zanoni47701c32013-07-23 11:19:25 -03008446/* Sequence to disable CLKOUT_DP */
8447static void lpt_disable_clkout_dp(struct drm_device *dev)
8448{
8449 struct drm_i915_private *dev_priv = dev->dev_private;
8450 uint32_t reg, tmp;
8451
Ville Syrjäläa5805162015-05-26 20:42:30 +03008452 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008453
Ville Syrjäläc2699522015-08-27 23:55:59 +03008454 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008455 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8456 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8457 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8458
8459 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8460 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8461 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8462 tmp |= SBI_SSCCTL_PATHALT;
8463 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8464 udelay(32);
8465 }
8466 tmp |= SBI_SSCCTL_DISABLE;
8467 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8468 }
8469
Ville Syrjäläa5805162015-05-26 20:42:30 +03008470 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008471}
8472
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008473static void lpt_init_pch_refclk(struct drm_device *dev)
8474{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008475 struct intel_encoder *encoder;
8476 bool has_vga = false;
8477
Damien Lespiaub2784e12014-08-05 11:29:37 +01008478 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008479 switch (encoder->type) {
8480 case INTEL_OUTPUT_ANALOG:
8481 has_vga = true;
8482 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008483 default:
8484 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008485 }
8486 }
8487
Paulo Zanoni47701c32013-07-23 11:19:25 -03008488 if (has_vga)
8489 lpt_enable_clkout_dp(dev, true, true);
8490 else
8491 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008492}
8493
Paulo Zanonidde86e22012-12-01 12:04:25 -02008494/*
8495 * Initialize reference clocks when the driver loads
8496 */
8497void intel_init_pch_refclk(struct drm_device *dev)
8498{
8499 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8500 ironlake_init_pch_refclk(dev);
8501 else if (HAS_PCH_LPT(dev))
8502 lpt_init_pch_refclk(dev);
8503}
8504
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008505static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008506{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008507 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008508 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008509 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008510 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008511 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008512 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008513 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008514 bool is_lvds = false;
8515
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008516 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008517 if (connector_state->crtc != crtc_state->base.crtc)
8518 continue;
8519
8520 encoder = to_intel_encoder(connector_state->best_encoder);
8521
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008522 switch (encoder->type) {
8523 case INTEL_OUTPUT_LVDS:
8524 is_lvds = true;
8525 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008526 default:
8527 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008528 }
8529 num_connectors++;
8530 }
8531
8532 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008533 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008534 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008535 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008536 }
8537
8538 return 120000;
8539}
8540
Daniel Vetter6ff93602013-04-19 11:24:36 +02008541static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008542{
8543 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8544 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8545 int pipe = intel_crtc->pipe;
8546 uint32_t val;
8547
Daniel Vetter78114072013-06-13 00:54:57 +02008548 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008549
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008550 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008551 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008552 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008553 break;
8554 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008555 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008556 break;
8557 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008558 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008559 break;
8560 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008561 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008562 break;
8563 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008564 /* Case prevented by intel_choose_pipe_bpp_dither. */
8565 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008566 }
8567
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008568 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008569 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8570
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008571 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008572 val |= PIPECONF_INTERLACED_ILK;
8573 else
8574 val |= PIPECONF_PROGRESSIVE;
8575
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008576 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008577 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008578
Paulo Zanonic8203562012-09-12 10:06:29 -03008579 I915_WRITE(PIPECONF(pipe), val);
8580 POSTING_READ(PIPECONF(pipe));
8581}
8582
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008583/*
8584 * Set up the pipe CSC unit.
8585 *
8586 * Currently only full range RGB to limited range RGB conversion
8587 * is supported, but eventually this should handle various
8588 * RGB<->YCbCr scenarios as well.
8589 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008590static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008591{
8592 struct drm_device *dev = crtc->dev;
8593 struct drm_i915_private *dev_priv = dev->dev_private;
8594 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8595 int pipe = intel_crtc->pipe;
8596 uint16_t coeff = 0x7800; /* 1.0 */
8597
8598 /*
8599 * TODO: Check what kind of values actually come out of the pipe
8600 * with these coeff/postoff values and adjust to get the best
8601 * accuracy. Perhaps we even need to take the bpc value into
8602 * consideration.
8603 */
8604
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008605 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008606 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8607
8608 /*
8609 * GY/GU and RY/RU should be the other way around according
8610 * to BSpec, but reality doesn't agree. Just set them up in
8611 * a way that results in the correct picture.
8612 */
8613 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8614 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8615
8616 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8617 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8618
8619 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8620 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8621
8622 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8623 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8624 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8625
8626 if (INTEL_INFO(dev)->gen > 6) {
8627 uint16_t postoff = 0;
8628
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008629 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008630 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008631
8632 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8633 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8634 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8635
8636 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8637 } else {
8638 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8639
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008640 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008641 mode |= CSC_BLACK_SCREEN_OFFSET;
8642
8643 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8644 }
8645}
8646
Daniel Vetter6ff93602013-04-19 11:24:36 +02008647static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008648{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008649 struct drm_device *dev = crtc->dev;
8650 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008651 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008652 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008653 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008654 uint32_t val;
8655
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008656 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008657
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008658 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008659 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8660
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008661 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008662 val |= PIPECONF_INTERLACED_ILK;
8663 else
8664 val |= PIPECONF_PROGRESSIVE;
8665
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008666 I915_WRITE(PIPECONF(cpu_transcoder), val);
8667 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008668
8669 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8670 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008671
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308672 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008673 val = 0;
8674
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008675 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008676 case 18:
8677 val |= PIPEMISC_DITHER_6_BPC;
8678 break;
8679 case 24:
8680 val |= PIPEMISC_DITHER_8_BPC;
8681 break;
8682 case 30:
8683 val |= PIPEMISC_DITHER_10_BPC;
8684 break;
8685 case 36:
8686 val |= PIPEMISC_DITHER_12_BPC;
8687 break;
8688 default:
8689 /* Case prevented by pipe_config_set_bpp. */
8690 BUG();
8691 }
8692
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008693 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008694 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8695
8696 I915_WRITE(PIPEMISC(pipe), val);
8697 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008698}
8699
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008700static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008701 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008702 intel_clock_t *clock,
8703 bool *has_reduced_clock,
8704 intel_clock_t *reduced_clock)
8705{
8706 struct drm_device *dev = crtc->dev;
8707 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008708 int refclk;
8709 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008710 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008711
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008712 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008713
8714 /*
8715 * Returns a set of divisors for the desired target clock with the given
8716 * refclk, or FALSE. The returned values represent the clock equation:
8717 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8718 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008719 limit = intel_limit(crtc_state, refclk);
8720 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008721 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008722 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008723 if (!ret)
8724 return false;
8725
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008726 return true;
8727}
8728
Paulo Zanonid4b19312012-11-29 11:29:32 -02008729int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8730{
8731 /*
8732 * Account for spread spectrum to avoid
8733 * oversubscribing the link. Max center spread
8734 * is 2.5%; use 5% for safety's sake.
8735 */
8736 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008737 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008738}
8739
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008740static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008741{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008742 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008743}
8744
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008745static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008746 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008747 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008748 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008749{
8750 struct drm_crtc *crtc = &intel_crtc->base;
8751 struct drm_device *dev = crtc->dev;
8752 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008753 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008754 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008755 struct drm_connector_state *connector_state;
8756 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008757 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008758 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008759 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008760
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008761 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008762 if (connector_state->crtc != crtc_state->base.crtc)
8763 continue;
8764
8765 encoder = to_intel_encoder(connector_state->best_encoder);
8766
8767 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008768 case INTEL_OUTPUT_LVDS:
8769 is_lvds = true;
8770 break;
8771 case INTEL_OUTPUT_SDVO:
8772 case INTEL_OUTPUT_HDMI:
8773 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008774 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008775 default:
8776 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008777 }
8778
8779 num_connectors++;
8780 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008781
Chris Wilsonc1858122010-12-03 21:35:48 +00008782 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008783 factor = 21;
8784 if (is_lvds) {
8785 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008786 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008787 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008788 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008789 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008790 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008791
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008792 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008793 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008794
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008795 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8796 *fp2 |= FP_CB_TUNE;
8797
Chris Wilson5eddb702010-09-11 13:48:45 +01008798 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008799
Eric Anholta07d6782011-03-30 13:01:08 -07008800 if (is_lvds)
8801 dpll |= DPLLB_MODE_LVDS;
8802 else
8803 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008804
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008805 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008806 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008807
8808 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008809 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008810 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008811 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008812
Eric Anholta07d6782011-03-30 13:01:08 -07008813 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008814 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008815 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008816 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008817
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008818 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008819 case 5:
8820 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8821 break;
8822 case 7:
8823 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8824 break;
8825 case 10:
8826 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8827 break;
8828 case 14:
8829 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8830 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008831 }
8832
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008833 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008834 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008835 else
8836 dpll |= PLL_REF_INPUT_DREFCLK;
8837
Daniel Vetter959e16d2013-06-05 13:34:21 +02008838 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008839}
8840
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008841static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8842 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008843{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008844 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008845 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008846 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008847 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008848 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008849 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008850
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008851 memset(&crtc_state->dpll_hw_state, 0,
8852 sizeof(crtc_state->dpll_hw_state));
8853
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008854 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008855
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008856 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8857 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8858
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008859 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008860 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008861 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008862 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8863 return -EINVAL;
8864 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008865 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008866 if (!crtc_state->clock_set) {
8867 crtc_state->dpll.n = clock.n;
8868 crtc_state->dpll.m1 = clock.m1;
8869 crtc_state->dpll.m2 = clock.m2;
8870 crtc_state->dpll.p1 = clock.p1;
8871 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008872 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008873
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008874 /* 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 +02008875 if (crtc_state->has_pch_encoder) {
8876 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008877 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008878 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008879
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008880 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008881 &fp, &reduced_clock,
8882 has_reduced_clock ? &fp2 : NULL);
8883
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008884 crtc_state->dpll_hw_state.dpll = dpll;
8885 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008886 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008887 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008888 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008889 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008890
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008891 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008892 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008893 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008894 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008895 return -EINVAL;
8896 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008897 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008898
Rodrigo Viviab585de2015-03-24 12:40:09 -07008899 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008900 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008901 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008902 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008903
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008904 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008905}
8906
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008907static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8908 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008909{
8910 struct drm_device *dev = crtc->base.dev;
8911 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008912 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008913
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008914 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8915 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8916 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8917 & ~TU_SIZE_MASK;
8918 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8919 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8920 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8921}
8922
8923static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8924 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008925 struct intel_link_m_n *m_n,
8926 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008927{
8928 struct drm_device *dev = crtc->base.dev;
8929 struct drm_i915_private *dev_priv = dev->dev_private;
8930 enum pipe pipe = crtc->pipe;
8931
8932 if (INTEL_INFO(dev)->gen >= 5) {
8933 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8934 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8935 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8936 & ~TU_SIZE_MASK;
8937 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8938 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8939 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008940 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8941 * gen < 8) and if DRRS is supported (to make sure the
8942 * registers are not unnecessarily read).
8943 */
8944 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008945 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008946 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8947 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8948 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8949 & ~TU_SIZE_MASK;
8950 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8951 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8952 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8953 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008954 } else {
8955 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8956 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8957 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8958 & ~TU_SIZE_MASK;
8959 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8960 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8961 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8962 }
8963}
8964
8965void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008966 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008967{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008968 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008969 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8970 else
8971 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008972 &pipe_config->dp_m_n,
8973 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008974}
8975
Daniel Vetter72419202013-04-04 13:28:53 +02008976static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008977 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008978{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008979 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008980 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008981}
8982
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008983static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008984 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008985{
8986 struct drm_device *dev = crtc->base.dev;
8987 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008988 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8989 uint32_t ps_ctrl = 0;
8990 int id = -1;
8991 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008992
Chandra Kondurua1b22782015-04-07 15:28:45 -07008993 /* find scaler attached to this pipe */
8994 for (i = 0; i < crtc->num_scalers; i++) {
8995 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8996 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8997 id = i;
8998 pipe_config->pch_pfit.enabled = true;
8999 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9000 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9001 break;
9002 }
9003 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009004
Chandra Kondurua1b22782015-04-07 15:28:45 -07009005 scaler_state->scaler_id = id;
9006 if (id >= 0) {
9007 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9008 } else {
9009 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009010 }
9011}
9012
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009013static void
9014skylake_get_initial_plane_config(struct intel_crtc *crtc,
9015 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009016{
9017 struct drm_device *dev = crtc->base.dev;
9018 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009019 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009020 int pipe = crtc->pipe;
9021 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009022 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009023 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009024 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009025
Damien Lespiaud9806c92015-01-21 14:07:19 +00009026 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009027 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009028 DRM_DEBUG_KMS("failed to alloc fb\n");
9029 return;
9030 }
9031
Damien Lespiau1b842c82015-01-21 13:50:54 +00009032 fb = &intel_fb->base;
9033
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009034 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009035 if (!(val & PLANE_CTL_ENABLE))
9036 goto error;
9037
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009038 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9039 fourcc = skl_format_to_fourcc(pixel_format,
9040 val & PLANE_CTL_ORDER_RGBX,
9041 val & PLANE_CTL_ALPHA_MASK);
9042 fb->pixel_format = fourcc;
9043 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9044
Damien Lespiau40f46282015-02-27 11:15:21 +00009045 tiling = val & PLANE_CTL_TILED_MASK;
9046 switch (tiling) {
9047 case PLANE_CTL_TILED_LINEAR:
9048 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9049 break;
9050 case PLANE_CTL_TILED_X:
9051 plane_config->tiling = I915_TILING_X;
9052 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9053 break;
9054 case PLANE_CTL_TILED_Y:
9055 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9056 break;
9057 case PLANE_CTL_TILED_YF:
9058 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9059 break;
9060 default:
9061 MISSING_CASE(tiling);
9062 goto error;
9063 }
9064
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009065 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9066 plane_config->base = base;
9067
9068 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9069
9070 val = I915_READ(PLANE_SIZE(pipe, 0));
9071 fb->height = ((val >> 16) & 0xfff) + 1;
9072 fb->width = ((val >> 0) & 0x1fff) + 1;
9073
9074 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009075 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9076 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009077 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9078
9079 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009080 fb->pixel_format,
9081 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009082
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009083 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009084
9085 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9086 pipe_name(pipe), fb->width, fb->height,
9087 fb->bits_per_pixel, base, fb->pitches[0],
9088 plane_config->size);
9089
Damien Lespiau2d140302015-02-05 17:22:18 +00009090 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009091 return;
9092
9093error:
9094 kfree(fb);
9095}
9096
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009097static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009098 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009099{
9100 struct drm_device *dev = crtc->base.dev;
9101 struct drm_i915_private *dev_priv = dev->dev_private;
9102 uint32_t tmp;
9103
9104 tmp = I915_READ(PF_CTL(crtc->pipe));
9105
9106 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009107 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009108 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9109 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009110
9111 /* We currently do not free assignements of panel fitters on
9112 * ivb/hsw (since we don't use the higher upscaling modes which
9113 * differentiates them) so just WARN about this case for now. */
9114 if (IS_GEN7(dev)) {
9115 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9116 PF_PIPE_SEL_IVB(crtc->pipe));
9117 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009118 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009119}
9120
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009121static void
9122ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9123 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009124{
9125 struct drm_device *dev = crtc->base.dev;
9126 struct drm_i915_private *dev_priv = dev->dev_private;
9127 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009128 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009129 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009130 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009131 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009132 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009133
Damien Lespiau42a7b082015-02-05 19:35:13 +00009134 val = I915_READ(DSPCNTR(pipe));
9135 if (!(val & DISPLAY_PLANE_ENABLE))
9136 return;
9137
Damien Lespiaud9806c92015-01-21 14:07:19 +00009138 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009139 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009140 DRM_DEBUG_KMS("failed to alloc fb\n");
9141 return;
9142 }
9143
Damien Lespiau1b842c82015-01-21 13:50:54 +00009144 fb = &intel_fb->base;
9145
Daniel Vetter18c52472015-02-10 17:16:09 +00009146 if (INTEL_INFO(dev)->gen >= 4) {
9147 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009148 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009149 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9150 }
9151 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009152
9153 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009154 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009155 fb->pixel_format = fourcc;
9156 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009157
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009158 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009159 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009160 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009161 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009162 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009163 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009164 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009165 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009166 }
9167 plane_config->base = base;
9168
9169 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009170 fb->width = ((val >> 16) & 0xfff) + 1;
9171 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009172
9173 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009174 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009175
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009176 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009177 fb->pixel_format,
9178 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009179
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009180 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009181
Damien Lespiau2844a922015-01-20 12:51:48 +00009182 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9183 pipe_name(pipe), fb->width, fb->height,
9184 fb->bits_per_pixel, base, fb->pitches[0],
9185 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009186
Damien Lespiau2d140302015-02-05 17:22:18 +00009187 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009188}
9189
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009190static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009191 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009192{
9193 struct drm_device *dev = crtc->base.dev;
9194 struct drm_i915_private *dev_priv = dev->dev_private;
9195 uint32_t tmp;
9196
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009197 if (!intel_display_power_is_enabled(dev_priv,
9198 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009199 return false;
9200
Daniel Vettere143a212013-07-04 12:01:15 +02009201 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009202 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009203
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009204 tmp = I915_READ(PIPECONF(crtc->pipe));
9205 if (!(tmp & PIPECONF_ENABLE))
9206 return false;
9207
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009208 switch (tmp & PIPECONF_BPC_MASK) {
9209 case PIPECONF_6BPC:
9210 pipe_config->pipe_bpp = 18;
9211 break;
9212 case PIPECONF_8BPC:
9213 pipe_config->pipe_bpp = 24;
9214 break;
9215 case PIPECONF_10BPC:
9216 pipe_config->pipe_bpp = 30;
9217 break;
9218 case PIPECONF_12BPC:
9219 pipe_config->pipe_bpp = 36;
9220 break;
9221 default:
9222 break;
9223 }
9224
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009225 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9226 pipe_config->limited_color_range = true;
9227
Daniel Vetterab9412b2013-05-03 11:49:46 +02009228 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009229 struct intel_shared_dpll *pll;
9230
Daniel Vetter88adfff2013-03-28 10:42:01 +01009231 pipe_config->has_pch_encoder = true;
9232
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009233 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9234 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9235 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009236
9237 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009238
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009239 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009240 pipe_config->shared_dpll =
9241 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009242 } else {
9243 tmp = I915_READ(PCH_DPLL_SEL);
9244 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9245 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9246 else
9247 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9248 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009249
9250 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9251
9252 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9253 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009254
9255 tmp = pipe_config->dpll_hw_state.dpll;
9256 pipe_config->pixel_multiplier =
9257 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9258 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009259
9260 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009261 } else {
9262 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009263 }
9264
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009265 intel_get_pipe_timings(crtc, pipe_config);
9266
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009267 ironlake_get_pfit_config(crtc, pipe_config);
9268
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009269 return true;
9270}
9271
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009272static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9273{
9274 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009275 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009276
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009277 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009278 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009279 pipe_name(crtc->pipe));
9280
Rob Clarke2c719b2014-12-15 13:56:32 -05009281 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9282 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9283 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9284 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9285 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9286 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009287 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009288 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009289 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009290 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009291 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009292 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009293 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009294 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009295 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009296
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009297 /*
9298 * In theory we can still leave IRQs enabled, as long as only the HPD
9299 * interrupts remain enabled. We used to check for that, but since it's
9300 * gen-specific and since we only disable LCPLL after we fully disable
9301 * the interrupts, the check below should be enough.
9302 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009303 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009304}
9305
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009306static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9307{
9308 struct drm_device *dev = dev_priv->dev;
9309
9310 if (IS_HASWELL(dev))
9311 return I915_READ(D_COMP_HSW);
9312 else
9313 return I915_READ(D_COMP_BDW);
9314}
9315
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009316static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9317{
9318 struct drm_device *dev = dev_priv->dev;
9319
9320 if (IS_HASWELL(dev)) {
9321 mutex_lock(&dev_priv->rps.hw_lock);
9322 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9323 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009324 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009325 mutex_unlock(&dev_priv->rps.hw_lock);
9326 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009327 I915_WRITE(D_COMP_BDW, val);
9328 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009329 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009330}
9331
9332/*
9333 * This function implements pieces of two sequences from BSpec:
9334 * - Sequence for display software to disable LCPLL
9335 * - Sequence for display software to allow package C8+
9336 * The steps implemented here are just the steps that actually touch the LCPLL
9337 * register. Callers should take care of disabling all the display engine
9338 * functions, doing the mode unset, fixing interrupts, etc.
9339 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009340static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9341 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009342{
9343 uint32_t val;
9344
9345 assert_can_disable_lcpll(dev_priv);
9346
9347 val = I915_READ(LCPLL_CTL);
9348
9349 if (switch_to_fclk) {
9350 val |= LCPLL_CD_SOURCE_FCLK;
9351 I915_WRITE(LCPLL_CTL, val);
9352
9353 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9354 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9355 DRM_ERROR("Switching to FCLK failed\n");
9356
9357 val = I915_READ(LCPLL_CTL);
9358 }
9359
9360 val |= LCPLL_PLL_DISABLE;
9361 I915_WRITE(LCPLL_CTL, val);
9362 POSTING_READ(LCPLL_CTL);
9363
9364 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9365 DRM_ERROR("LCPLL still locked\n");
9366
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009367 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009368 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009369 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009370 ndelay(100);
9371
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009372 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9373 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009374 DRM_ERROR("D_COMP RCOMP still in progress\n");
9375
9376 if (allow_power_down) {
9377 val = I915_READ(LCPLL_CTL);
9378 val |= LCPLL_POWER_DOWN_ALLOW;
9379 I915_WRITE(LCPLL_CTL, val);
9380 POSTING_READ(LCPLL_CTL);
9381 }
9382}
9383
9384/*
9385 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9386 * source.
9387 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009388static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009389{
9390 uint32_t val;
9391
9392 val = I915_READ(LCPLL_CTL);
9393
9394 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9395 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9396 return;
9397
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009398 /*
9399 * Make sure we're not on PC8 state before disabling PC8, otherwise
9400 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009401 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009402 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009403
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009404 if (val & LCPLL_POWER_DOWN_ALLOW) {
9405 val &= ~LCPLL_POWER_DOWN_ALLOW;
9406 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009407 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009408 }
9409
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009410 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009411 val |= D_COMP_COMP_FORCE;
9412 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009413 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009414
9415 val = I915_READ(LCPLL_CTL);
9416 val &= ~LCPLL_PLL_DISABLE;
9417 I915_WRITE(LCPLL_CTL, val);
9418
9419 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9420 DRM_ERROR("LCPLL not locked yet\n");
9421
9422 if (val & LCPLL_CD_SOURCE_FCLK) {
9423 val = I915_READ(LCPLL_CTL);
9424 val &= ~LCPLL_CD_SOURCE_FCLK;
9425 I915_WRITE(LCPLL_CTL, val);
9426
9427 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9428 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9429 DRM_ERROR("Switching back to LCPLL failed\n");
9430 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009431
Mika Kuoppala59bad942015-01-16 11:34:40 +02009432 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009433 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009434}
9435
Paulo Zanoni765dab672014-03-07 20:08:18 -03009436/*
9437 * Package states C8 and deeper are really deep PC states that can only be
9438 * reached when all the devices on the system allow it, so even if the graphics
9439 * device allows PC8+, it doesn't mean the system will actually get to these
9440 * states. Our driver only allows PC8+ when going into runtime PM.
9441 *
9442 * The requirements for PC8+ are that all the outputs are disabled, the power
9443 * well is disabled and most interrupts are disabled, and these are also
9444 * requirements for runtime PM. When these conditions are met, we manually do
9445 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9446 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9447 * hang the machine.
9448 *
9449 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9450 * the state of some registers, so when we come back from PC8+ we need to
9451 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9452 * need to take care of the registers kept by RC6. Notice that this happens even
9453 * if we don't put the device in PCI D3 state (which is what currently happens
9454 * because of the runtime PM support).
9455 *
9456 * For more, read "Display Sequences for Package C8" on the hardware
9457 * documentation.
9458 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009459void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009460{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009461 struct drm_device *dev = dev_priv->dev;
9462 uint32_t val;
9463
Paulo Zanonic67a4702013-08-19 13:18:09 -03009464 DRM_DEBUG_KMS("Enabling package C8+\n");
9465
Ville Syrjäläc2699522015-08-27 23:55:59 +03009466 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009467 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9468 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9469 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9470 }
9471
9472 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009473 hsw_disable_lcpll(dev_priv, true, true);
9474}
9475
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009476void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009477{
9478 struct drm_device *dev = dev_priv->dev;
9479 uint32_t val;
9480
Paulo Zanonic67a4702013-08-19 13:18:09 -03009481 DRM_DEBUG_KMS("Disabling package C8+\n");
9482
9483 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009484 lpt_init_pch_refclk(dev);
9485
Ville Syrjäläc2699522015-08-27 23:55:59 +03009486 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009487 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9488 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9489 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9490 }
9491
9492 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009493}
9494
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009495static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309496{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009497 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009498 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309499
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009500 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309501}
9502
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009503/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009504static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009505{
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009506 struct intel_crtc *intel_crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009507 struct intel_crtc_state *crtc_state;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009508 int max_pixel_rate = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009509
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009510 for_each_intel_crtc(state->dev, intel_crtc) {
9511 int pixel_rate;
9512
9513 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9514 if (IS_ERR(crtc_state))
9515 return PTR_ERR(crtc_state);
9516
9517 if (!crtc_state->base.enable)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009518 continue;
9519
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009520 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009521
9522 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009523 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009524 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9525
9526 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9527 }
9528
9529 return max_pixel_rate;
9530}
9531
9532static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9533{
9534 struct drm_i915_private *dev_priv = dev->dev_private;
9535 uint32_t val, data;
9536 int ret;
9537
9538 if (WARN((I915_READ(LCPLL_CTL) &
9539 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9540 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9541 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9542 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9543 "trying to change cdclk frequency with cdclk not enabled\n"))
9544 return;
9545
9546 mutex_lock(&dev_priv->rps.hw_lock);
9547 ret = sandybridge_pcode_write(dev_priv,
9548 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9549 mutex_unlock(&dev_priv->rps.hw_lock);
9550 if (ret) {
9551 DRM_ERROR("failed to inform pcode about cdclk change\n");
9552 return;
9553 }
9554
9555 val = I915_READ(LCPLL_CTL);
9556 val |= LCPLL_CD_SOURCE_FCLK;
9557 I915_WRITE(LCPLL_CTL, val);
9558
9559 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9560 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9561 DRM_ERROR("Switching to FCLK failed\n");
9562
9563 val = I915_READ(LCPLL_CTL);
9564 val &= ~LCPLL_CLK_FREQ_MASK;
9565
9566 switch (cdclk) {
9567 case 450000:
9568 val |= LCPLL_CLK_FREQ_450;
9569 data = 0;
9570 break;
9571 case 540000:
9572 val |= LCPLL_CLK_FREQ_54O_BDW;
9573 data = 1;
9574 break;
9575 case 337500:
9576 val |= LCPLL_CLK_FREQ_337_5_BDW;
9577 data = 2;
9578 break;
9579 case 675000:
9580 val |= LCPLL_CLK_FREQ_675_BDW;
9581 data = 3;
9582 break;
9583 default:
9584 WARN(1, "invalid cdclk frequency\n");
9585 return;
9586 }
9587
9588 I915_WRITE(LCPLL_CTL, val);
9589
9590 val = I915_READ(LCPLL_CTL);
9591 val &= ~LCPLL_CD_SOURCE_FCLK;
9592 I915_WRITE(LCPLL_CTL, val);
9593
9594 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9595 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9596 DRM_ERROR("Switching back to LCPLL failed\n");
9597
9598 mutex_lock(&dev_priv->rps.hw_lock);
9599 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9600 mutex_unlock(&dev_priv->rps.hw_lock);
9601
9602 intel_update_cdclk(dev);
9603
9604 WARN(cdclk != dev_priv->cdclk_freq,
9605 "cdclk requested %d kHz but got %d kHz\n",
9606 cdclk, dev_priv->cdclk_freq);
9607}
9608
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009609static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009610{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009611 struct drm_i915_private *dev_priv = to_i915(state->dev);
9612 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009613 int cdclk;
9614
9615 /*
9616 * FIXME should also account for plane ratio
9617 * once 64bpp pixel formats are supported.
9618 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009619 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009620 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009621 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009622 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009623 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009624 cdclk = 450000;
9625 else
9626 cdclk = 337500;
9627
9628 /*
9629 * FIXME move the cdclk caclulation to
9630 * compute_config() so we can fail gracegully.
9631 */
9632 if (cdclk > dev_priv->max_cdclk_freq) {
9633 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9634 cdclk, dev_priv->max_cdclk_freq);
9635 cdclk = dev_priv->max_cdclk_freq;
9636 }
9637
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009638 to_intel_atomic_state(state)->cdclk = cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009639
9640 return 0;
9641}
9642
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009643static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009644{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009645 struct drm_device *dev = old_state->dev;
9646 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009647
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009648 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009649}
9650
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009651static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9652 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009653{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009654 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009655 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009656
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009657 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009658
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009659 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009660}
9661
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309662static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9663 enum port port,
9664 struct intel_crtc_state *pipe_config)
9665{
9666 switch (port) {
9667 case PORT_A:
9668 pipe_config->ddi_pll_sel = SKL_DPLL0;
9669 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9670 break;
9671 case PORT_B:
9672 pipe_config->ddi_pll_sel = SKL_DPLL1;
9673 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9674 break;
9675 case PORT_C:
9676 pipe_config->ddi_pll_sel = SKL_DPLL2;
9677 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9678 break;
9679 default:
9680 DRM_ERROR("Incorrect port type\n");
9681 }
9682}
9683
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009684static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9685 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009686 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009687{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009688 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009689
9690 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9691 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9692
9693 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009694 case SKL_DPLL0:
9695 /*
9696 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9697 * of the shared DPLL framework and thus needs to be read out
9698 * separately
9699 */
9700 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9701 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9702 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009703 case SKL_DPLL1:
9704 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9705 break;
9706 case SKL_DPLL2:
9707 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9708 break;
9709 case SKL_DPLL3:
9710 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9711 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009712 }
9713}
9714
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009715static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9716 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009717 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009718{
9719 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9720
9721 switch (pipe_config->ddi_pll_sel) {
9722 case PORT_CLK_SEL_WRPLL1:
9723 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9724 break;
9725 case PORT_CLK_SEL_WRPLL2:
9726 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9727 break;
9728 }
9729}
9730
Daniel Vetter26804af2014-06-25 22:01:55 +03009731static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009732 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009733{
9734 struct drm_device *dev = crtc->base.dev;
9735 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009736 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009737 enum port port;
9738 uint32_t tmp;
9739
9740 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9741
9742 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9743
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009744 if (IS_SKYLAKE(dev))
9745 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309746 else if (IS_BROXTON(dev))
9747 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009748 else
9749 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009750
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009751 if (pipe_config->shared_dpll >= 0) {
9752 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9753
9754 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9755 &pipe_config->dpll_hw_state));
9756 }
9757
Daniel Vetter26804af2014-06-25 22:01:55 +03009758 /*
9759 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9760 * DDI E. So just check whether this pipe is wired to DDI E and whether
9761 * the PCH transcoder is on.
9762 */
Damien Lespiauca370452013-12-03 13:56:24 +00009763 if (INTEL_INFO(dev)->gen < 9 &&
9764 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009765 pipe_config->has_pch_encoder = true;
9766
9767 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9768 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9769 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9770
9771 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9772 }
9773}
9774
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009775static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009776 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009777{
9778 struct drm_device *dev = crtc->base.dev;
9779 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009780 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009781 uint32_t tmp;
9782
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009783 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009784 POWER_DOMAIN_PIPE(crtc->pipe)))
9785 return false;
9786
Daniel Vettere143a212013-07-04 12:01:15 +02009787 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009788 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9789
Daniel Vettereccb1402013-05-22 00:50:22 +02009790 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9791 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9792 enum pipe trans_edp_pipe;
9793 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9794 default:
9795 WARN(1, "unknown pipe linked to edp transcoder\n");
9796 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9797 case TRANS_DDI_EDP_INPUT_A_ON:
9798 trans_edp_pipe = PIPE_A;
9799 break;
9800 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9801 trans_edp_pipe = PIPE_B;
9802 break;
9803 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9804 trans_edp_pipe = PIPE_C;
9805 break;
9806 }
9807
9808 if (trans_edp_pipe == crtc->pipe)
9809 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9810 }
9811
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009812 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009813 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009814 return false;
9815
Daniel Vettereccb1402013-05-22 00:50:22 +02009816 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009817 if (!(tmp & PIPECONF_ENABLE))
9818 return false;
9819
Daniel Vetter26804af2014-06-25 22:01:55 +03009820 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009821
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009822 intel_get_pipe_timings(crtc, pipe_config);
9823
Chandra Kondurua1b22782015-04-07 15:28:45 -07009824 if (INTEL_INFO(dev)->gen >= 9) {
9825 skl_init_scalers(dev, crtc, pipe_config);
9826 }
9827
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009828 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009829
9830 if (INTEL_INFO(dev)->gen >= 9) {
9831 pipe_config->scaler_state.scaler_id = -1;
9832 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9833 }
9834
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009835 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009836 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009837 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009838 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009839 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009840 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009841
Jesse Barnese59150d2014-01-07 13:30:45 -08009842 if (IS_HASWELL(dev))
9843 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9844 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009845
Clint Taylorebb69c92014-09-30 10:30:22 -07009846 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9847 pipe_config->pixel_multiplier =
9848 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9849 } else {
9850 pipe_config->pixel_multiplier = 1;
9851 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009852
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009853 return true;
9854}
9855
Chris Wilson560b85b2010-08-07 11:01:38 +01009856static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9857{
9858 struct drm_device *dev = crtc->dev;
9859 struct drm_i915_private *dev_priv = dev->dev_private;
9860 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009861 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009862
Ville Syrjälädc41c152014-08-13 11:57:05 +03009863 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009864 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9865 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009866 unsigned int stride = roundup_pow_of_two(width) * 4;
9867
9868 switch (stride) {
9869 default:
9870 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9871 width, stride);
9872 stride = 256;
9873 /* fallthrough */
9874 case 256:
9875 case 512:
9876 case 1024:
9877 case 2048:
9878 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009879 }
9880
Ville Syrjälädc41c152014-08-13 11:57:05 +03009881 cntl |= CURSOR_ENABLE |
9882 CURSOR_GAMMA_ENABLE |
9883 CURSOR_FORMAT_ARGB |
9884 CURSOR_STRIDE(stride);
9885
9886 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009887 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009888
Ville Syrjälädc41c152014-08-13 11:57:05 +03009889 if (intel_crtc->cursor_cntl != 0 &&
9890 (intel_crtc->cursor_base != base ||
9891 intel_crtc->cursor_size != size ||
9892 intel_crtc->cursor_cntl != cntl)) {
9893 /* On these chipsets we can only modify the base/size/stride
9894 * whilst the cursor is disabled.
9895 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009896 I915_WRITE(CURCNTR(PIPE_A), 0);
9897 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +03009898 intel_crtc->cursor_cntl = 0;
9899 }
9900
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009901 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009902 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009903 intel_crtc->cursor_base = base;
9904 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009905
9906 if (intel_crtc->cursor_size != size) {
9907 I915_WRITE(CURSIZE, size);
9908 intel_crtc->cursor_size = size;
9909 }
9910
Chris Wilson4b0e3332014-05-30 16:35:26 +03009911 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009912 I915_WRITE(CURCNTR(PIPE_A), cntl);
9913 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +03009914 intel_crtc->cursor_cntl = cntl;
9915 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009916}
9917
9918static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9919{
9920 struct drm_device *dev = crtc->dev;
9921 struct drm_i915_private *dev_priv = dev->dev_private;
9922 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9923 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009924 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009925
Chris Wilson4b0e3332014-05-30 16:35:26 +03009926 cntl = 0;
9927 if (base) {
9928 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009929 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309930 case 64:
9931 cntl |= CURSOR_MODE_64_ARGB_AX;
9932 break;
9933 case 128:
9934 cntl |= CURSOR_MODE_128_ARGB_AX;
9935 break;
9936 case 256:
9937 cntl |= CURSOR_MODE_256_ARGB_AX;
9938 break;
9939 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009940 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309941 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009942 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009943 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009944
Bob Paauwefc6f93b2015-08-31 14:03:30 -07009945 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009946 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009947 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009948
Matt Roper8e7d6882015-01-21 16:35:41 -08009949 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009950 cntl |= CURSOR_ROTATE_180;
9951
Chris Wilson4b0e3332014-05-30 16:35:26 +03009952 if (intel_crtc->cursor_cntl != cntl) {
9953 I915_WRITE(CURCNTR(pipe), cntl);
9954 POSTING_READ(CURCNTR(pipe));
9955 intel_crtc->cursor_cntl = cntl;
9956 }
9957
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009958 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009959 I915_WRITE(CURBASE(pipe), base);
9960 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009961
9962 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009963}
9964
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009965/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009966static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9967 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009968{
9969 struct drm_device *dev = crtc->dev;
9970 struct drm_i915_private *dev_priv = dev->dev_private;
9971 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9972 int pipe = intel_crtc->pipe;
Maarten Lankhorst9b4101b2015-09-10 16:07:59 +02009973 struct drm_plane_state *cursor_state = crtc->cursor->state;
9974 int x = cursor_state->crtc_x;
9975 int y = cursor_state->crtc_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009976 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009977
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009978 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009979 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009980
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009981 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009982 base = 0;
9983
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009984 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009985 base = 0;
9986
9987 if (x < 0) {
Maarten Lankhorst9b4101b2015-09-10 16:07:59 +02009988 if (x + cursor_state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009989 base = 0;
9990
9991 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9992 x = -x;
9993 }
9994 pos |= x << CURSOR_X_SHIFT;
9995
9996 if (y < 0) {
Maarten Lankhorst9b4101b2015-09-10 16:07:59 +02009997 if (y + cursor_state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009998 base = 0;
9999
10000 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10001 y = -y;
10002 }
10003 pos |= y << CURSOR_Y_SHIFT;
10004
Chris Wilson4b0e3332014-05-30 16:35:26 +030010005 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010006 return;
10007
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010008 I915_WRITE(CURPOS(pipe), pos);
10009
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010010 /* ILK+ do this automagically */
10011 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -080010012 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Maarten Lankhorst9b4101b2015-09-10 16:07:59 +020010013 base += (cursor_state->crtc_h *
10014 cursor_state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010015 }
10016
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010017 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010018 i845_update_cursor(crtc, base);
10019 else
10020 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010021}
10022
Ville Syrjälädc41c152014-08-13 11:57:05 +030010023static bool cursor_size_ok(struct drm_device *dev,
10024 uint32_t width, uint32_t height)
10025{
10026 if (width == 0 || height == 0)
10027 return false;
10028
10029 /*
10030 * 845g/865g are special in that they are only limited by
10031 * the width of their cursors, the height is arbitrary up to
10032 * the precision of the register. Everything else requires
10033 * square cursors, limited to a few power-of-two sizes.
10034 */
10035 if (IS_845G(dev) || IS_I865G(dev)) {
10036 if ((width & 63) != 0)
10037 return false;
10038
10039 if (width > (IS_845G(dev) ? 64 : 512))
10040 return false;
10041
10042 if (height > 1023)
10043 return false;
10044 } else {
10045 switch (width | height) {
10046 case 256:
10047 case 128:
10048 if (IS_GEN2(dev))
10049 return false;
10050 case 64:
10051 break;
10052 default:
10053 return false;
10054 }
10055 }
10056
10057 return true;
10058}
10059
Jesse Barnes79e53942008-11-07 14:24:08 -080010060static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010061 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010062{
James Simmons72034252010-08-03 01:33:19 +010010063 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010064 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010065
James Simmons72034252010-08-03 01:33:19 +010010066 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010067 intel_crtc->lut_r[i] = red[i] >> 8;
10068 intel_crtc->lut_g[i] = green[i] >> 8;
10069 intel_crtc->lut_b[i] = blue[i] >> 8;
10070 }
10071
10072 intel_crtc_load_lut(crtc);
10073}
10074
Jesse Barnes79e53942008-11-07 14:24:08 -080010075/* VESA 640x480x72Hz mode to set on the pipe */
10076static struct drm_display_mode load_detect_mode = {
10077 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10078 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10079};
10080
Daniel Vettera8bb6812014-02-10 18:00:39 +010010081struct drm_framebuffer *
10082__intel_framebuffer_create(struct drm_device *dev,
10083 struct drm_mode_fb_cmd2 *mode_cmd,
10084 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010085{
10086 struct intel_framebuffer *intel_fb;
10087 int ret;
10088
10089 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10090 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010091 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010092 return ERR_PTR(-ENOMEM);
10093 }
10094
10095 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010096 if (ret)
10097 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010098
10099 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010100err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010101 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010102 kfree(intel_fb);
10103
10104 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010105}
10106
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010107static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010108intel_framebuffer_create(struct drm_device *dev,
10109 struct drm_mode_fb_cmd2 *mode_cmd,
10110 struct drm_i915_gem_object *obj)
10111{
10112 struct drm_framebuffer *fb;
10113 int ret;
10114
10115 ret = i915_mutex_lock_interruptible(dev);
10116 if (ret)
10117 return ERR_PTR(ret);
10118 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10119 mutex_unlock(&dev->struct_mutex);
10120
10121 return fb;
10122}
10123
Chris Wilsond2dff872011-04-19 08:36:26 +010010124static u32
10125intel_framebuffer_pitch_for_width(int width, int bpp)
10126{
10127 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10128 return ALIGN(pitch, 64);
10129}
10130
10131static u32
10132intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10133{
10134 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010135 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010136}
10137
10138static struct drm_framebuffer *
10139intel_framebuffer_create_for_mode(struct drm_device *dev,
10140 struct drm_display_mode *mode,
10141 int depth, int bpp)
10142{
10143 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010144 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010145
10146 obj = i915_gem_alloc_object(dev,
10147 intel_framebuffer_size_for_mode(mode, bpp));
10148 if (obj == NULL)
10149 return ERR_PTR(-ENOMEM);
10150
10151 mode_cmd.width = mode->hdisplay;
10152 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010153 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10154 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010155 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010156
10157 return intel_framebuffer_create(dev, &mode_cmd, obj);
10158}
10159
10160static struct drm_framebuffer *
10161mode_fits_in_fbdev(struct drm_device *dev,
10162 struct drm_display_mode *mode)
10163{
Daniel Vetter06957262015-08-10 13:34:08 +020010164#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010165 struct drm_i915_private *dev_priv = dev->dev_private;
10166 struct drm_i915_gem_object *obj;
10167 struct drm_framebuffer *fb;
10168
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010169 if (!dev_priv->fbdev)
10170 return NULL;
10171
10172 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010173 return NULL;
10174
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010175 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010176 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010177
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010178 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010179 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10180 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010181 return NULL;
10182
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010183 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010184 return NULL;
10185
10186 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010187#else
10188 return NULL;
10189#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010190}
10191
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010192static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10193 struct drm_crtc *crtc,
10194 struct drm_display_mode *mode,
10195 struct drm_framebuffer *fb,
10196 int x, int y)
10197{
10198 struct drm_plane_state *plane_state;
10199 int hdisplay, vdisplay;
10200 int ret;
10201
10202 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10203 if (IS_ERR(plane_state))
10204 return PTR_ERR(plane_state);
10205
10206 if (mode)
10207 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10208 else
10209 hdisplay = vdisplay = 0;
10210
10211 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10212 if (ret)
10213 return ret;
10214 drm_atomic_set_fb_for_plane(plane_state, fb);
10215 plane_state->crtc_x = 0;
10216 plane_state->crtc_y = 0;
10217 plane_state->crtc_w = hdisplay;
10218 plane_state->crtc_h = vdisplay;
10219 plane_state->src_x = x << 16;
10220 plane_state->src_y = y << 16;
10221 plane_state->src_w = hdisplay << 16;
10222 plane_state->src_h = vdisplay << 16;
10223
10224 return 0;
10225}
10226
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010227bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010228 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010229 struct intel_load_detect_pipe *old,
10230 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010231{
10232 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010233 struct intel_encoder *intel_encoder =
10234 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010235 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010236 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010237 struct drm_crtc *crtc = NULL;
10238 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010239 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010240 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010241 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010242 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010243 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010244 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010245
Chris Wilsond2dff872011-04-19 08:36:26 +010010246 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010247 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010248 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010249
Rob Clark51fd3712013-11-19 12:10:12 -050010250retry:
10251 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10252 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010253 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010254
Jesse Barnes79e53942008-11-07 14:24:08 -080010255 /*
10256 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010257 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010258 * - if the connector already has an assigned crtc, use it (but make
10259 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010260 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010261 * - try to find the first unused crtc that can drive this connector,
10262 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010263 */
10264
10265 /* See if we already have a CRTC for this connector */
10266 if (encoder->crtc) {
10267 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010268
Rob Clark51fd3712013-11-19 12:10:12 -050010269 ret = drm_modeset_lock(&crtc->mutex, ctx);
10270 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010271 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010272 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10273 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010274 goto fail;
Daniel Vetter7b240562012-12-12 00:35:33 +010010275
Daniel Vetter24218aa2012-08-12 19:27:11 +020010276 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010277 old->load_detect_temp = false;
10278
10279 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010280 if (connector->dpms != DRM_MODE_DPMS_ON)
10281 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010282
Chris Wilson71731882011-04-19 23:10:58 +010010283 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010284 }
10285
10286 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010287 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010288 i++;
10289 if (!(encoder->possible_crtcs & (1 << i)))
10290 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010291 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010292 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +030010293
10294 crtc = possible_crtc;
10295 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010296 }
10297
10298 /*
10299 * If we didn't find an unused CRTC, don't use any.
10300 */
10301 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010302 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010303 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010304 }
10305
Rob Clark51fd3712013-11-19 12:10:12 -050010306 ret = drm_modeset_lock(&crtc->mutex, ctx);
10307 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010308 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010309 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10310 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010311 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010312
10313 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +020010314 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010315 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010316 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010317
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010318 state = drm_atomic_state_alloc(dev);
10319 if (!state)
10320 return false;
10321
10322 state->acquire_ctx = ctx;
10323
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010324 connector_state = drm_atomic_get_connector_state(state, connector);
10325 if (IS_ERR(connector_state)) {
10326 ret = PTR_ERR(connector_state);
10327 goto fail;
10328 }
10329
10330 connector_state->crtc = crtc;
10331 connector_state->best_encoder = &intel_encoder->base;
10332
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010333 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10334 if (IS_ERR(crtc_state)) {
10335 ret = PTR_ERR(crtc_state);
10336 goto fail;
10337 }
10338
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010339 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010340
Chris Wilson64927112011-04-20 07:25:26 +010010341 if (!mode)
10342 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010343
Chris Wilsond2dff872011-04-19 08:36:26 +010010344 /* We need a framebuffer large enough to accommodate all accesses
10345 * that the plane may generate whilst we perform load detection.
10346 * We can not rely on the fbcon either being present (we get called
10347 * during its initialisation to detect all boot displays, or it may
10348 * not even exist) or that it is large enough to satisfy the
10349 * requested mode.
10350 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010351 fb = mode_fits_in_fbdev(dev, mode);
10352 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010353 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010354 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10355 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010356 } else
10357 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010358 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010359 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010360 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010361 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010362
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010363 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10364 if (ret)
10365 goto fail;
10366
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010367 drm_mode_copy(&crtc_state->base.mode, mode);
10368
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020010369 if (drm_atomic_commit(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010370 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010371 if (old->release_fb)
10372 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010373 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010374 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010375 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010376
Jesse Barnes79e53942008-11-07 14:24:08 -080010377 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010378 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010379 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010380
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010381fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010382 drm_atomic_state_free(state);
10383 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010384
Rob Clark51fd3712013-11-19 12:10:12 -050010385 if (ret == -EDEADLK) {
10386 drm_modeset_backoff(ctx);
10387 goto retry;
10388 }
10389
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010390 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010391}
10392
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010393void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010394 struct intel_load_detect_pipe *old,
10395 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010396{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010397 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010398 struct intel_encoder *intel_encoder =
10399 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010400 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010401 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010402 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010403 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010404 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010405 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010406 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010407
Chris Wilsond2dff872011-04-19 08:36:26 +010010408 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010409 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010410 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010411
Chris Wilson8261b192011-04-19 23:18:09 +010010412 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010413 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010414 if (!state)
10415 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010416
10417 state->acquire_ctx = ctx;
10418
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010419 connector_state = drm_atomic_get_connector_state(state, connector);
10420 if (IS_ERR(connector_state))
10421 goto fail;
10422
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010423 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10424 if (IS_ERR(crtc_state))
10425 goto fail;
10426
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010427 connector_state->best_encoder = NULL;
10428 connector_state->crtc = NULL;
10429
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010430 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010431
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010432 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10433 0, 0);
10434 if (ret)
10435 goto fail;
10436
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020010437 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010438 if (ret)
10439 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010440
Daniel Vetter36206362012-12-10 20:42:17 +010010441 if (old->release_fb) {
10442 drm_framebuffer_unregister_private(old->release_fb);
10443 drm_framebuffer_unreference(old->release_fb);
10444 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010445
Chris Wilson0622a532011-04-21 09:32:11 +010010446 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010447 }
10448
Eric Anholtc751ce42010-03-25 11:48:48 -070010449 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010450 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10451 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010452
10453 return;
10454fail:
10455 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10456 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010457}
10458
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010459static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010460 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010461{
10462 struct drm_i915_private *dev_priv = dev->dev_private;
10463 u32 dpll = pipe_config->dpll_hw_state.dpll;
10464
10465 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010466 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010467 else if (HAS_PCH_SPLIT(dev))
10468 return 120000;
10469 else if (!IS_GEN2(dev))
10470 return 96000;
10471 else
10472 return 48000;
10473}
10474
Jesse Barnes79e53942008-11-07 14:24:08 -080010475/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010476static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010477 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010478{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010479 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010480 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010481 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010482 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010483 u32 fp;
10484 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010485 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010486 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010487
10488 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010489 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010490 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010491 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010492
10493 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010494 if (IS_PINEVIEW(dev)) {
10495 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10496 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010497 } else {
10498 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10499 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10500 }
10501
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010502 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010503 if (IS_PINEVIEW(dev))
10504 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10505 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010506 else
10507 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010508 DPLL_FPA01_P1_POST_DIV_SHIFT);
10509
10510 switch (dpll & DPLL_MODE_MASK) {
10511 case DPLLB_MODE_DAC_SERIAL:
10512 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10513 5 : 10;
10514 break;
10515 case DPLLB_MODE_LVDS:
10516 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10517 7 : 14;
10518 break;
10519 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010520 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010521 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010522 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010523 }
10524
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010525 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010526 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010527 else
Imre Deakdccbea32015-06-22 23:35:51 +030010528 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010529 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010530 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010531 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010532
10533 if (is_lvds) {
10534 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10535 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010536
10537 if (lvds & LVDS_CLKB_POWER_UP)
10538 clock.p2 = 7;
10539 else
10540 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010541 } else {
10542 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10543 clock.p1 = 2;
10544 else {
10545 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10546 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10547 }
10548 if (dpll & PLL_P2_DIVIDE_BY_4)
10549 clock.p2 = 4;
10550 else
10551 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010552 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010553
Imre Deakdccbea32015-06-22 23:35:51 +030010554 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010555 }
10556
Ville Syrjälä18442d02013-09-13 16:00:08 +030010557 /*
10558 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010559 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010560 * encoder's get_config() function.
10561 */
Imre Deakdccbea32015-06-22 23:35:51 +030010562 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010563}
10564
Ville Syrjälä6878da02013-09-13 15:59:11 +030010565int intel_dotclock_calculate(int link_freq,
10566 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010567{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010568 /*
10569 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010570 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010571 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010572 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010573 *
10574 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010575 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010576 */
10577
Ville Syrjälä6878da02013-09-13 15:59:11 +030010578 if (!m_n->link_n)
10579 return 0;
10580
10581 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10582}
10583
Ville Syrjälä18442d02013-09-13 16:00:08 +030010584static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010585 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010586{
10587 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010588
10589 /* read out port_clock from the DPLL */
10590 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010591
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010592 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010593 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010594 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010595 * agree once we know their relationship in the encoder's
10596 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010597 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010598 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010599 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10600 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010601}
10602
10603/** Returns the currently programmed mode of the given pipe. */
10604struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10605 struct drm_crtc *crtc)
10606{
Jesse Barnes548f2452011-02-17 10:40:53 -080010607 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010608 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010609 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010610 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010611 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010612 int htot = I915_READ(HTOTAL(cpu_transcoder));
10613 int hsync = I915_READ(HSYNC(cpu_transcoder));
10614 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10615 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010616 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010617
10618 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10619 if (!mode)
10620 return NULL;
10621
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010622 /*
10623 * Construct a pipe_config sufficient for getting the clock info
10624 * back out of crtc_clock_get.
10625 *
10626 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10627 * to use a real value here instead.
10628 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010629 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010630 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010631 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10632 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10633 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010634 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10635
Ville Syrjälä773ae032013-09-23 17:48:20 +030010636 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010637 mode->hdisplay = (htot & 0xffff) + 1;
10638 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10639 mode->hsync_start = (hsync & 0xffff) + 1;
10640 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10641 mode->vdisplay = (vtot & 0xffff) + 1;
10642 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10643 mode->vsync_start = (vsync & 0xffff) + 1;
10644 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10645
10646 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010647
10648 return mode;
10649}
10650
Chris Wilsonf047e392012-07-21 12:31:41 +010010651void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010652{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010653 struct drm_i915_private *dev_priv = dev->dev_private;
10654
Chris Wilsonf62a0072014-02-21 17:55:39 +000010655 if (dev_priv->mm.busy)
10656 return;
10657
Paulo Zanoni43694d62014-03-07 20:08:08 -030010658 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010659 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010660 if (INTEL_INFO(dev)->gen >= 6)
10661 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010662 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010663}
10664
10665void intel_mark_idle(struct drm_device *dev)
10666{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010667 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010668
Chris Wilsonf62a0072014-02-21 17:55:39 +000010669 if (!dev_priv->mm.busy)
10670 return;
10671
10672 dev_priv->mm.busy = false;
10673
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010674 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010675 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010676
Paulo Zanoni43694d62014-03-07 20:08:08 -030010677 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010678}
10679
Jesse Barnes79e53942008-11-07 14:24:08 -080010680static void intel_crtc_destroy(struct drm_crtc *crtc)
10681{
10682 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010683 struct drm_device *dev = crtc->dev;
10684 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010685
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010686 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010687 work = intel_crtc->unpin_work;
10688 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010689 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010690
10691 if (work) {
10692 cancel_work_sync(&work->work);
10693 kfree(work);
10694 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010695
10696 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010697
Jesse Barnes79e53942008-11-07 14:24:08 -080010698 kfree(intel_crtc);
10699}
10700
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010701static void intel_unpin_work_fn(struct work_struct *__work)
10702{
10703 struct intel_unpin_work *work =
10704 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010705 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10706 struct drm_device *dev = crtc->base.dev;
10707 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010708
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010709 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010710 intel_unpin_fb_obj(work->old_fb, primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010711 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010712
John Harrisonf06cc1b2014-11-24 18:49:37 +000010713 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010714 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010715 mutex_unlock(&dev->struct_mutex);
10716
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010717 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010718 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010719
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010720 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10721 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010722
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010723 kfree(work);
10724}
10725
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010726static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010727 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010728{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10730 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010731 unsigned long flags;
10732
10733 /* Ignore early vblank irqs */
10734 if (intel_crtc == NULL)
10735 return;
10736
Daniel Vetterf3260382014-09-15 14:55:23 +020010737 /*
10738 * This is called both by irq handlers and the reset code (to complete
10739 * lost pageflips) so needs the full irqsave spinlocks.
10740 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010741 spin_lock_irqsave(&dev->event_lock, flags);
10742 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010743
10744 /* Ensure we don't miss a work->pending update ... */
10745 smp_rmb();
10746
10747 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010748 spin_unlock_irqrestore(&dev->event_lock, flags);
10749 return;
10750 }
10751
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010752 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010753
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010754 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010755}
10756
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010757void intel_finish_page_flip(struct drm_device *dev, int pipe)
10758{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010759 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010760 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10761
Mario Kleiner49b14a52010-12-09 07:00:07 +010010762 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010763}
10764
10765void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10766{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010767 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010768 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10769
Mario Kleiner49b14a52010-12-09 07:00:07 +010010770 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010771}
10772
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010773/* Is 'a' after or equal to 'b'? */
10774static bool g4x_flip_count_after_eq(u32 a, u32 b)
10775{
10776 return !((a - b) & 0x80000000);
10777}
10778
10779static bool page_flip_finished(struct intel_crtc *crtc)
10780{
10781 struct drm_device *dev = crtc->base.dev;
10782 struct drm_i915_private *dev_priv = dev->dev_private;
10783
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010784 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10785 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10786 return true;
10787
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010788 /*
10789 * The relevant registers doen't exist on pre-ctg.
10790 * As the flip done interrupt doesn't trigger for mmio
10791 * flips on gmch platforms, a flip count check isn't
10792 * really needed there. But since ctg has the registers,
10793 * include it in the check anyway.
10794 */
10795 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10796 return true;
10797
10798 /*
10799 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10800 * used the same base address. In that case the mmio flip might
10801 * have completed, but the CS hasn't even executed the flip yet.
10802 *
10803 * A flip count check isn't enough as the CS might have updated
10804 * the base address just after start of vblank, but before we
10805 * managed to process the interrupt. This means we'd complete the
10806 * CS flip too soon.
10807 *
10808 * Combining both checks should get us a good enough result. It may
10809 * still happen that the CS flip has been executed, but has not
10810 * yet actually completed. But in case the base address is the same
10811 * anyway, we don't really care.
10812 */
10813 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10814 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030010815 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010816 crtc->unpin_work->flip_count);
10817}
10818
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010819void intel_prepare_page_flip(struct drm_device *dev, int plane)
10820{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010821 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010822 struct intel_crtc *intel_crtc =
10823 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10824 unsigned long flags;
10825
Daniel Vetterf3260382014-09-15 14:55:23 +020010826
10827 /*
10828 * This is called both by irq handlers and the reset code (to complete
10829 * lost pageflips) so needs the full irqsave spinlocks.
10830 *
10831 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010832 * generate a page-flip completion irq, i.e. every modeset
10833 * is also accompanied by a spurious intel_prepare_page_flip().
10834 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010835 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010836 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010837 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010838 spin_unlock_irqrestore(&dev->event_lock, flags);
10839}
10840
Chris Wilson60426392015-10-10 10:44:32 +010010841static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010842{
10843 /* Ensure that the work item is consistent when activating it ... */
10844 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010010845 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010846 /* and that it is marked active as soon as the irq could fire. */
10847 smp_wmb();
10848}
10849
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010850static int intel_gen2_queue_flip(struct drm_device *dev,
10851 struct drm_crtc *crtc,
10852 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010853 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010854 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010855 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010856{
John Harrison6258fbe2015-05-29 17:43:48 +010010857 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010859 u32 flip_mask;
10860 int ret;
10861
John Harrison5fb9de12015-05-29 17:44:07 +010010862 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010863 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010864 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010865
10866 /* Can't queue multiple flips, so wait for the previous
10867 * one to finish before executing the next.
10868 */
10869 if (intel_crtc->plane)
10870 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10871 else
10872 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010873 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10874 intel_ring_emit(ring, MI_NOOP);
10875 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10876 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10877 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010878 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010879 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010880
Chris Wilson60426392015-10-10 10:44:32 +010010881 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010882 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010883}
10884
10885static int intel_gen3_queue_flip(struct drm_device *dev,
10886 struct drm_crtc *crtc,
10887 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010888 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010889 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010890 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010891{
John Harrison6258fbe2015-05-29 17:43:48 +010010892 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010893 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010894 u32 flip_mask;
10895 int ret;
10896
John Harrison5fb9de12015-05-29 17:44:07 +010010897 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010898 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010899 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010900
10901 if (intel_crtc->plane)
10902 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10903 else
10904 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010905 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10906 intel_ring_emit(ring, MI_NOOP);
10907 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10908 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10909 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010910 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010911 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010912
Chris Wilson60426392015-10-10 10:44:32 +010010913 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010914 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010915}
10916
10917static int intel_gen4_queue_flip(struct drm_device *dev,
10918 struct drm_crtc *crtc,
10919 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010920 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010921 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010922 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010923{
John Harrison6258fbe2015-05-29 17:43:48 +010010924 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010925 struct drm_i915_private *dev_priv = dev->dev_private;
10926 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10927 uint32_t pf, pipesrc;
10928 int ret;
10929
John Harrison5fb9de12015-05-29 17:44:07 +010010930 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010931 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010932 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010933
10934 /* i965+ uses the linear or tiled offsets from the
10935 * Display Registers (which do not change across a page-flip)
10936 * so we need only reprogram the base address.
10937 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010938 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10939 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10940 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010941 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010942 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010943
10944 /* XXX Enabling the panel-fitter across page-flip is so far
10945 * untested on non-native modes, so ignore it for now.
10946 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10947 */
10948 pf = 0;
10949 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010950 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010951
Chris Wilson60426392015-10-10 10:44:32 +010010952 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010953 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010954}
10955
10956static int intel_gen6_queue_flip(struct drm_device *dev,
10957 struct drm_crtc *crtc,
10958 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010959 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010960 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010961 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010962{
John Harrison6258fbe2015-05-29 17:43:48 +010010963 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010964 struct drm_i915_private *dev_priv = dev->dev_private;
10965 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10966 uint32_t pf, pipesrc;
10967 int ret;
10968
John Harrison5fb9de12015-05-29 17:44:07 +010010969 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010970 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010971 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010972
Daniel Vetter6d90c952012-04-26 23:28:05 +020010973 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10974 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10975 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010976 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010977
Chris Wilson99d9acd2012-04-17 20:37:00 +010010978 /* Contrary to the suggestions in the documentation,
10979 * "Enable Panel Fitter" does not seem to be required when page
10980 * flipping with a non-native mode, and worse causes a normal
10981 * modeset to fail.
10982 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10983 */
10984 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010985 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010986 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010987
Chris Wilson60426392015-10-10 10:44:32 +010010988 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010010989 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010990}
10991
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010992static int intel_gen7_queue_flip(struct drm_device *dev,
10993 struct drm_crtc *crtc,
10994 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010995 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010996 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010997 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010998{
John Harrison6258fbe2015-05-29 17:43:48 +010010999 struct intel_engine_cs *ring = req->ring;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011000 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011001 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011002 int len, ret;
11003
Robin Schroereba905b2014-05-18 02:24:50 +020011004 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011005 case PLANE_A:
11006 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11007 break;
11008 case PLANE_B:
11009 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11010 break;
11011 case PLANE_C:
11012 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11013 break;
11014 default:
11015 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011016 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011017 }
11018
Chris Wilsonffe74d72013-08-26 20:58:12 +010011019 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011020 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011021 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011022 /*
11023 * On Gen 8, SRM is now taking an extra dword to accommodate
11024 * 48bits addresses, and we need a NOOP for the batch size to
11025 * stay even.
11026 */
11027 if (IS_GEN8(dev))
11028 len += 2;
11029 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011030
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011031 /*
11032 * BSpec MI_DISPLAY_FLIP for IVB:
11033 * "The full packet must be contained within the same cache line."
11034 *
11035 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11036 * cacheline, if we ever start emitting more commands before
11037 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11038 * then do the cacheline alignment, and finally emit the
11039 * MI_DISPLAY_FLIP.
11040 */
John Harrisonbba09b12015-05-29 17:44:06 +010011041 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011042 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011043 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011044
John Harrison5fb9de12015-05-29 17:44:07 +010011045 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011046 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011047 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011048
Chris Wilsonffe74d72013-08-26 20:58:12 +010011049 /* Unmask the flip-done completion message. Note that the bspec says that
11050 * we should do this for both the BCS and RCS, and that we must not unmask
11051 * more than one flip event at any time (or ensure that one flip message
11052 * can be sent by waiting for flip-done prior to queueing new flips).
11053 * Experimentation says that BCS works despite DERRMR masking all
11054 * flip-done completion events and that unmasking all planes at once
11055 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11056 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11057 */
11058 if (ring->id == RCS) {
11059 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11060 intel_ring_emit(ring, DERRMR);
11061 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11062 DERRMR_PIPEB_PRI_FLIP_DONE |
11063 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011064 if (IS_GEN8(dev))
Arun Siluveryf1afe242015-08-04 16:22:20 +010011065 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011066 MI_SRM_LRM_GLOBAL_GTT);
11067 else
Arun Siluveryf1afe242015-08-04 16:22:20 +010011068 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011069 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011070 intel_ring_emit(ring, DERRMR);
11071 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011072 if (IS_GEN8(dev)) {
11073 intel_ring_emit(ring, 0);
11074 intel_ring_emit(ring, MI_NOOP);
11075 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011076 }
11077
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011078 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011079 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011080 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011081 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011082
Chris Wilson60426392015-10-10 10:44:32 +010011083 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011084 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011085}
11086
Sourab Gupta84c33a62014-06-02 16:47:17 +053011087static bool use_mmio_flip(struct intel_engine_cs *ring,
11088 struct drm_i915_gem_object *obj)
11089{
11090 /*
11091 * This is not being used for older platforms, because
11092 * non-availability of flip done interrupt forces us to use
11093 * CS flips. Older platforms derive flip done using some clever
11094 * tricks involving the flip_pending status bits and vblank irqs.
11095 * So using MMIO flips there would disrupt this mechanism.
11096 */
11097
Chris Wilson8e09bf82014-07-08 10:40:30 +010011098 if (ring == NULL)
11099 return true;
11100
Sourab Gupta84c33a62014-06-02 16:47:17 +053011101 if (INTEL_INFO(ring->dev)->gen < 5)
11102 return false;
11103
11104 if (i915.use_mmio_flip < 0)
11105 return false;
11106 else if (i915.use_mmio_flip > 0)
11107 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011108 else if (i915.enable_execlists)
11109 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011110 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011111 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011112}
11113
Chris Wilson60426392015-10-10 10:44:32 +010011114static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11115 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011116{
11117 struct drm_device *dev = intel_crtc->base.dev;
11118 struct drm_i915_private *dev_priv = dev->dev_private;
11119 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011120 const enum pipe pipe = intel_crtc->pipe;
11121 u32 ctl, stride;
11122
11123 ctl = I915_READ(PLANE_CTL(pipe, 0));
11124 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011125 switch (fb->modifier[0]) {
11126 case DRM_FORMAT_MOD_NONE:
11127 break;
11128 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011129 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011130 break;
11131 case I915_FORMAT_MOD_Y_TILED:
11132 ctl |= PLANE_CTL_TILED_Y;
11133 break;
11134 case I915_FORMAT_MOD_Yf_TILED:
11135 ctl |= PLANE_CTL_TILED_YF;
11136 break;
11137 default:
11138 MISSING_CASE(fb->modifier[0]);
11139 }
Damien Lespiauff944562014-11-20 14:58:16 +000011140
11141 /*
11142 * The stride is either expressed as a multiple of 64 bytes chunks for
11143 * linear buffers or in number of tiles for tiled buffers.
11144 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011145 stride = fb->pitches[0] /
11146 intel_fb_stride_alignment(dev, fb->modifier[0],
11147 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011148
11149 /*
11150 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11151 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11152 */
11153 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11154 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11155
Chris Wilson60426392015-10-10 10:44:32 +010011156 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011157 POSTING_READ(PLANE_SURF(pipe, 0));
11158}
11159
Chris Wilson60426392015-10-10 10:44:32 +010011160static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11161 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011162{
11163 struct drm_device *dev = intel_crtc->base.dev;
11164 struct drm_i915_private *dev_priv = dev->dev_private;
11165 struct intel_framebuffer *intel_fb =
11166 to_intel_framebuffer(intel_crtc->base.primary->fb);
11167 struct drm_i915_gem_object *obj = intel_fb->obj;
11168 u32 dspcntr;
11169 u32 reg;
11170
Sourab Gupta84c33a62014-06-02 16:47:17 +053011171 reg = DSPCNTR(intel_crtc->plane);
11172 dspcntr = I915_READ(reg);
11173
Damien Lespiauc5d97472014-10-25 00:11:11 +010011174 if (obj->tiling_mode != I915_TILING_NONE)
11175 dspcntr |= DISPPLANE_TILED;
11176 else
11177 dspcntr &= ~DISPPLANE_TILED;
11178
Sourab Gupta84c33a62014-06-02 16:47:17 +053011179 I915_WRITE(reg, dspcntr);
11180
Chris Wilson60426392015-10-10 10:44:32 +010011181 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011182 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011183}
11184
11185/*
11186 * XXX: This is the temporary way to update the plane registers until we get
11187 * around to using the usual plane update functions for MMIO flips
11188 */
Chris Wilson60426392015-10-10 10:44:32 +010011189static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011190{
Chris Wilson60426392015-10-10 10:44:32 +010011191 struct intel_crtc *crtc = mmio_flip->crtc;
11192 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011193
Chris Wilson60426392015-10-10 10:44:32 +010011194 spin_lock_irq(&crtc->base.dev->event_lock);
11195 work = crtc->unpin_work;
11196 spin_unlock_irq(&crtc->base.dev->event_lock);
11197 if (work == NULL)
11198 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011199
Chris Wilson60426392015-10-10 10:44:32 +010011200 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011201
Chris Wilson60426392015-10-10 10:44:32 +010011202 intel_pipe_update_start(crtc);
11203
11204 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
11205 skl_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011206 else
11207 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011208 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011209
Chris Wilson60426392015-10-10 10:44:32 +010011210 intel_pipe_update_end(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
Chris Wilson60426392015-10-10 10:44:32 +010011218 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011219 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));
Chris Wilson60426392015-10-10 10:44:32 +010011223 i915_gem_request_unreference__unlocked(mmio_flip->req);
11224 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011225
Chris Wilson60426392015-10-10 10:44:32 +010011226 intel_do_mmio_flip(mmio_flip);
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))
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030011427 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(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 Vivi066cf552015-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 Vivi066cf552015-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 Lankhorst8a75d152015-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 Lankhorst8a75d152015-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 Vetter6897b4b2014-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);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012517 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012518
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);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012522
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 Vetter8af6cf82012-07-10 09:50:11 +020012527 PIPE_CONF_CHECK_I(shared_dpll);
12528 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
12529 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
12530 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 Vettere2e1ed42012-07-08 21:14:38 +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 Vettere2e1ed42012-07-08 21:14:38 +020012543#undef PIPE_CONF_CHECK_X
12544#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012545#undef PIPE_CONF_CHECK_I_ALT
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012546#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012547#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012548#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012549#undef INTEL_ERR_OR_DBG_KMS
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012550
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012551 return ret;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012552}
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 Vetter8af6cf82012-07-10 09:50:11 +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);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012653
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012654 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012655 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012656
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012657 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 Konduruaf99ced2015-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 Konduruaf99ced2015-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 Wilsoncc917ab2015-10-13 14:22:26 +010014158 if (obj->userptr.mm) {
14159 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14160 return -EINVAL;
14161 }
14162
Chris Wilson05394f32010-11-08 19:18:58 +000014163 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014164}
14165
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014166static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14167 struct drm_file *file,
14168 unsigned flags, unsigned color,
14169 struct drm_clip_rect *clips,
14170 unsigned num_clips)
14171{
14172 struct drm_device *dev = fb->dev;
14173 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14174 struct drm_i915_gem_object *obj = intel_fb->obj;
14175
14176 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014177 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014178 mutex_unlock(&dev->struct_mutex);
14179
14180 return 0;
14181}
14182
Jesse Barnes79e53942008-11-07 14:24:08 -080014183static const struct drm_framebuffer_funcs intel_fb_funcs = {
14184 .destroy = intel_user_framebuffer_destroy,
14185 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014186 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014187};
14188
Damien Lespiaub3218032015-02-27 11:15:18 +000014189static
14190u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14191 uint32_t pixel_format)
14192{
14193 u32 gen = INTEL_INFO(dev)->gen;
14194
14195 if (gen >= 9) {
14196 /* "The stride in bytes must not exceed the of the size of 8K
14197 * pixels and 32K bytes."
14198 */
14199 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14200 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14201 return 32*1024;
14202 } else if (gen >= 4) {
14203 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14204 return 16*1024;
14205 else
14206 return 32*1024;
14207 } else if (gen >= 3) {
14208 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14209 return 8*1024;
14210 else
14211 return 16*1024;
14212 } else {
14213 /* XXX DSPC is limited to 4k tiled */
14214 return 8*1024;
14215 }
14216}
14217
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014218static int intel_framebuffer_init(struct drm_device *dev,
14219 struct intel_framebuffer *intel_fb,
14220 struct drm_mode_fb_cmd2 *mode_cmd,
14221 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014222{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014223 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014224 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014225 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014226
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014227 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14228
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014229 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14230 /* Enforce that fb modifier and tiling mode match, but only for
14231 * X-tiled. This is needed for FBC. */
14232 if (!!(obj->tiling_mode == I915_TILING_X) !=
14233 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14234 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14235 return -EINVAL;
14236 }
14237 } else {
14238 if (obj->tiling_mode == I915_TILING_X)
14239 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14240 else if (obj->tiling_mode == I915_TILING_Y) {
14241 DRM_DEBUG("No Y tiling for legacy addfb\n");
14242 return -EINVAL;
14243 }
14244 }
14245
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014246 /* Passed in modifier sanity checking. */
14247 switch (mode_cmd->modifier[0]) {
14248 case I915_FORMAT_MOD_Y_TILED:
14249 case I915_FORMAT_MOD_Yf_TILED:
14250 if (INTEL_INFO(dev)->gen < 9) {
14251 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14252 mode_cmd->modifier[0]);
14253 return -EINVAL;
14254 }
14255 case DRM_FORMAT_MOD_NONE:
14256 case I915_FORMAT_MOD_X_TILED:
14257 break;
14258 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014259 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14260 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014261 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014262 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014263
Damien Lespiaub3218032015-02-27 11:15:18 +000014264 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14265 mode_cmd->pixel_format);
14266 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14267 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14268 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014269 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014270 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014271
Damien Lespiaub3218032015-02-27 11:15:18 +000014272 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14273 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014274 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014275 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14276 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014277 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014278 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014279 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014280 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014281
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014282 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014283 mode_cmd->pitches[0] != obj->stride) {
14284 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14285 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014286 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014287 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014288
Ville Syrjälä57779d02012-10-31 17:50:14 +020014289 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014290 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014291 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014292 case DRM_FORMAT_RGB565:
14293 case DRM_FORMAT_XRGB8888:
14294 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014295 break;
14296 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014297 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014298 DRM_DEBUG("unsupported pixel format: %s\n",
14299 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014300 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014301 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014302 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014303 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014304 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14305 DRM_DEBUG("unsupported pixel format: %s\n",
14306 drm_get_format_name(mode_cmd->pixel_format));
14307 return -EINVAL;
14308 }
14309 break;
14310 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014311 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014312 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014313 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014314 DRM_DEBUG("unsupported pixel format: %s\n",
14315 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014316 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014317 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014318 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014319 case DRM_FORMAT_ABGR2101010:
14320 if (!IS_VALLEYVIEW(dev)) {
14321 DRM_DEBUG("unsupported pixel format: %s\n",
14322 drm_get_format_name(mode_cmd->pixel_format));
14323 return -EINVAL;
14324 }
14325 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014326 case DRM_FORMAT_YUYV:
14327 case DRM_FORMAT_UYVY:
14328 case DRM_FORMAT_YVYU:
14329 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014330 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014331 DRM_DEBUG("unsupported pixel format: %s\n",
14332 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014333 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014334 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014335 break;
14336 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014337 DRM_DEBUG("unsupported pixel format: %s\n",
14338 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014339 return -EINVAL;
14340 }
14341
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014342 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14343 if (mode_cmd->offsets[0] != 0)
14344 return -EINVAL;
14345
Damien Lespiauec2c9812015-01-20 12:51:45 +000014346 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014347 mode_cmd->pixel_format,
14348 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014349 /* FIXME drm helper for size checks (especially planar formats)? */
14350 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14351 return -EINVAL;
14352
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014353 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14354 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014355 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014356
Jesse Barnes79e53942008-11-07 14:24:08 -080014357 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14358 if (ret) {
14359 DRM_ERROR("framebuffer init failed %d\n", ret);
14360 return ret;
14361 }
14362
Jesse Barnes79e53942008-11-07 14:24:08 -080014363 return 0;
14364}
14365
Jesse Barnes79e53942008-11-07 14:24:08 -080014366static struct drm_framebuffer *
14367intel_user_framebuffer_create(struct drm_device *dev,
14368 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014369 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014370{
Chris Wilson05394f32010-11-08 19:18:58 +000014371 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014372
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014373 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14374 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014375 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014376 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014377
Chris Wilsond2dff872011-04-19 08:36:26 +010014378 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014379}
14380
Daniel Vetter06957262015-08-10 13:34:08 +020014381#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014382static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014383{
14384}
14385#endif
14386
Jesse Barnes79e53942008-11-07 14:24:08 -080014387static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014388 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014389 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014390 .atomic_check = intel_atomic_check,
14391 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014392 .atomic_state_alloc = intel_atomic_state_alloc,
14393 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014394};
14395
Jesse Barnese70236a2009-09-21 10:42:27 -070014396/* Set up chip specific display functions */
14397static void intel_init_display(struct drm_device *dev)
14398{
14399 struct drm_i915_private *dev_priv = dev->dev_private;
14400
Daniel Vetteree9300b2013-06-03 22:40:22 +020014401 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14402 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014403 else if (IS_CHERRYVIEW(dev))
14404 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014405 else if (IS_VALLEYVIEW(dev))
14406 dev_priv->display.find_dpll = vlv_find_best_dpll;
14407 else if (IS_PINEVIEW(dev))
14408 dev_priv->display.find_dpll = pnv_find_best_dpll;
14409 else
14410 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14411
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014412 if (INTEL_INFO(dev)->gen >= 9) {
14413 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014414 dev_priv->display.get_initial_plane_config =
14415 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014416 dev_priv->display.crtc_compute_clock =
14417 haswell_crtc_compute_clock;
14418 dev_priv->display.crtc_enable = haswell_crtc_enable;
14419 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014420 dev_priv->display.update_primary_plane =
14421 skylake_update_primary_plane;
14422 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014423 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014424 dev_priv->display.get_initial_plane_config =
14425 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014426 dev_priv->display.crtc_compute_clock =
14427 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014428 dev_priv->display.crtc_enable = haswell_crtc_enable;
14429 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014430 dev_priv->display.update_primary_plane =
14431 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014432 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014433 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014434 dev_priv->display.get_initial_plane_config =
14435 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014436 dev_priv->display.crtc_compute_clock =
14437 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014438 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14439 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014440 dev_priv->display.update_primary_plane =
14441 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014442 } else if (IS_VALLEYVIEW(dev)) {
14443 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014444 dev_priv->display.get_initial_plane_config =
14445 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014446 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014447 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14448 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014449 dev_priv->display.update_primary_plane =
14450 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014451 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014452 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014453 dev_priv->display.get_initial_plane_config =
14454 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014455 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014456 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14457 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014458 dev_priv->display.update_primary_plane =
14459 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014460 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014461
Jesse Barnese70236a2009-09-21 10:42:27 -070014462 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014463 if (IS_SKYLAKE(dev))
14464 dev_priv->display.get_display_clock_speed =
14465 skylake_get_display_clock_speed;
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014466 else if (IS_BROXTON(dev))
14467 dev_priv->display.get_display_clock_speed =
14468 broxton_get_display_clock_speed;
Ville Syrjälä1652d192015-03-31 14:12:01 +030014469 else if (IS_BROADWELL(dev))
14470 dev_priv->display.get_display_clock_speed =
14471 broadwell_get_display_clock_speed;
14472 else if (IS_HASWELL(dev))
14473 dev_priv->display.get_display_clock_speed =
14474 haswell_get_display_clock_speed;
14475 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014476 dev_priv->display.get_display_clock_speed =
14477 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014478 else if (IS_GEN5(dev))
14479 dev_priv->display.get_display_clock_speed =
14480 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014481 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014482 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014483 dev_priv->display.get_display_clock_speed =
14484 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014485 else if (IS_GM45(dev))
14486 dev_priv->display.get_display_clock_speed =
14487 gm45_get_display_clock_speed;
14488 else if (IS_CRESTLINE(dev))
14489 dev_priv->display.get_display_clock_speed =
14490 i965gm_get_display_clock_speed;
14491 else if (IS_PINEVIEW(dev))
14492 dev_priv->display.get_display_clock_speed =
14493 pnv_get_display_clock_speed;
14494 else if (IS_G33(dev) || IS_G4X(dev))
14495 dev_priv->display.get_display_clock_speed =
14496 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014497 else if (IS_I915G(dev))
14498 dev_priv->display.get_display_clock_speed =
14499 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014500 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014501 dev_priv->display.get_display_clock_speed =
14502 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014503 else if (IS_PINEVIEW(dev))
14504 dev_priv->display.get_display_clock_speed =
14505 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014506 else if (IS_I915GM(dev))
14507 dev_priv->display.get_display_clock_speed =
14508 i915gm_get_display_clock_speed;
14509 else if (IS_I865G(dev))
14510 dev_priv->display.get_display_clock_speed =
14511 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014512 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014513 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014514 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014515 else { /* 830 */
14516 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014517 dev_priv->display.get_display_clock_speed =
14518 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014519 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014520
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014521 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014522 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014523 } else if (IS_GEN6(dev)) {
14524 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014525 } else if (IS_IVYBRIDGE(dev)) {
14526 /* FIXME: detect B0+ stepping and use auto training */
14527 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014528 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014529 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014530 if (IS_BROADWELL(dev)) {
14531 dev_priv->display.modeset_commit_cdclk =
14532 broadwell_modeset_commit_cdclk;
14533 dev_priv->display.modeset_calc_cdclk =
14534 broadwell_modeset_calc_cdclk;
14535 }
Jesse Barnes30a970c2013-11-04 13:48:12 -080014536 } else if (IS_VALLEYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014537 dev_priv->display.modeset_commit_cdclk =
14538 valleyview_modeset_commit_cdclk;
14539 dev_priv->display.modeset_calc_cdclk =
14540 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014541 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014542 dev_priv->display.modeset_commit_cdclk =
14543 broxton_modeset_commit_cdclk;
14544 dev_priv->display.modeset_calc_cdclk =
14545 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014546 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014547
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014548 switch (INTEL_INFO(dev)->gen) {
14549 case 2:
14550 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14551 break;
14552
14553 case 3:
14554 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14555 break;
14556
14557 case 4:
14558 case 5:
14559 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14560 break;
14561
14562 case 6:
14563 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14564 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014565 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014566 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014567 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14568 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014569 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014570 /* Drop through - unsupported since execlist only. */
14571 default:
14572 /* Default just returns -ENODEV to indicate unsupported */
14573 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014574 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014575
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014576 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014577}
14578
Jesse Barnesb690e962010-07-19 13:53:12 -070014579/*
14580 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14581 * resume, or other times. This quirk makes sure that's the case for
14582 * affected systems.
14583 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014584static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014585{
14586 struct drm_i915_private *dev_priv = dev->dev_private;
14587
14588 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014589 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014590}
14591
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014592static void quirk_pipeb_force(struct drm_device *dev)
14593{
14594 struct drm_i915_private *dev_priv = dev->dev_private;
14595
14596 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14597 DRM_INFO("applying pipe b force quirk\n");
14598}
14599
Keith Packard435793d2011-07-12 14:56:22 -070014600/*
14601 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14602 */
14603static void quirk_ssc_force_disable(struct drm_device *dev)
14604{
14605 struct drm_i915_private *dev_priv = dev->dev_private;
14606 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014607 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014608}
14609
Carsten Emde4dca20e2012-03-15 15:56:26 +010014610/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014611 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14612 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014613 */
14614static void quirk_invert_brightness(struct drm_device *dev)
14615{
14616 struct drm_i915_private *dev_priv = dev->dev_private;
14617 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014618 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014619}
14620
Scot Doyle9c72cc62014-07-03 23:27:50 +000014621/* Some VBT's incorrectly indicate no backlight is present */
14622static void quirk_backlight_present(struct drm_device *dev)
14623{
14624 struct drm_i915_private *dev_priv = dev->dev_private;
14625 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14626 DRM_INFO("applying backlight present quirk\n");
14627}
14628
Jesse Barnesb690e962010-07-19 13:53:12 -070014629struct intel_quirk {
14630 int device;
14631 int subsystem_vendor;
14632 int subsystem_device;
14633 void (*hook)(struct drm_device *dev);
14634};
14635
Egbert Eich5f85f172012-10-14 15:46:38 +020014636/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14637struct intel_dmi_quirk {
14638 void (*hook)(struct drm_device *dev);
14639 const struct dmi_system_id (*dmi_id_list)[];
14640};
14641
14642static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14643{
14644 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14645 return 1;
14646}
14647
14648static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14649 {
14650 .dmi_id_list = &(const struct dmi_system_id[]) {
14651 {
14652 .callback = intel_dmi_reverse_brightness,
14653 .ident = "NCR Corporation",
14654 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14655 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14656 },
14657 },
14658 { } /* terminating entry */
14659 },
14660 .hook = quirk_invert_brightness,
14661 },
14662};
14663
Ben Widawskyc43b5632012-04-16 14:07:40 -070014664static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014665 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14666 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14667
Jesse Barnesb690e962010-07-19 13:53:12 -070014668 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14669 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14670
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014671 /* 830 needs to leave pipe A & dpll A up */
14672 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14673
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014674 /* 830 needs to leave pipe B & dpll B up */
14675 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14676
Keith Packard435793d2011-07-12 14:56:22 -070014677 /* Lenovo U160 cannot use SSC on LVDS */
14678 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014679
14680 /* Sony Vaio Y cannot use SSC on LVDS */
14681 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014682
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014683 /* Acer Aspire 5734Z must invert backlight brightness */
14684 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14685
14686 /* Acer/eMachines G725 */
14687 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14688
14689 /* Acer/eMachines e725 */
14690 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14691
14692 /* Acer/Packard Bell NCL20 */
14693 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14694
14695 /* Acer Aspire 4736Z */
14696 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014697
14698 /* Acer Aspire 5336 */
14699 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014700
14701 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14702 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014703
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014704 /* Acer C720 Chromebook (Core i3 4005U) */
14705 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14706
jens steinb2a96012014-10-28 20:25:53 +010014707 /* Apple Macbook 2,1 (Core 2 T7400) */
14708 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14709
Jani Nikula1b9448b2015-11-05 11:49:59 +020014710 /* Apple Macbook 4,1 */
14711 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14712
Scot Doyled4967d82014-07-03 23:27:52 +000014713 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14714 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014715
14716 /* HP Chromebook 14 (Celeron 2955U) */
14717 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014718
14719 /* Dell Chromebook 11 */
14720 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020014721
14722 /* Dell Chromebook 11 (2015 version) */
14723 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014724};
14725
14726static void intel_init_quirks(struct drm_device *dev)
14727{
14728 struct pci_dev *d = dev->pdev;
14729 int i;
14730
14731 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14732 struct intel_quirk *q = &intel_quirks[i];
14733
14734 if (d->device == q->device &&
14735 (d->subsystem_vendor == q->subsystem_vendor ||
14736 q->subsystem_vendor == PCI_ANY_ID) &&
14737 (d->subsystem_device == q->subsystem_device ||
14738 q->subsystem_device == PCI_ANY_ID))
14739 q->hook(dev);
14740 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014741 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14742 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14743 intel_dmi_quirks[i].hook(dev);
14744 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014745}
14746
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014747/* Disable the VGA plane that we never use */
14748static void i915_disable_vga(struct drm_device *dev)
14749{
14750 struct drm_i915_private *dev_priv = dev->dev_private;
14751 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014752 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014753
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014754 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014755 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014756 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014757 sr1 = inb(VGA_SR_DATA);
14758 outb(sr1 | 1<<5, VGA_SR_DATA);
14759 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14760 udelay(300);
14761
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014762 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014763 POSTING_READ(vga_reg);
14764}
14765
Daniel Vetterf8175862012-04-10 15:50:11 +020014766void intel_modeset_init_hw(struct drm_device *dev)
14767{
Ville Syrjäläb6283052015-06-03 15:45:07 +030014768 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014769 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014770 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014771 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014772}
14773
Jesse Barnes79e53942008-11-07 14:24:08 -080014774void intel_modeset_init(struct drm_device *dev)
14775{
Jesse Barnes652c3932009-08-17 13:31:43 -070014776 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014777 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014778 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014779 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014780
14781 drm_mode_config_init(dev);
14782
14783 dev->mode_config.min_width = 0;
14784 dev->mode_config.min_height = 0;
14785
Dave Airlie019d96c2011-09-29 16:20:42 +010014786 dev->mode_config.preferred_depth = 24;
14787 dev->mode_config.prefer_shadow = 1;
14788
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014789 dev->mode_config.allow_fb_modifiers = true;
14790
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014791 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014792
Jesse Barnesb690e962010-07-19 13:53:12 -070014793 intel_init_quirks(dev);
14794
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014795 intel_init_pm(dev);
14796
Ben Widawskye3c74752013-04-05 13:12:39 -070014797 if (INTEL_INFO(dev)->num_pipes == 0)
14798 return;
14799
Lukas Wunner69f92f62015-07-15 13:57:35 +020014800 /*
14801 * There may be no VBT; and if the BIOS enabled SSC we can
14802 * just keep using it to avoid unnecessary flicker. Whereas if the
14803 * BIOS isn't using it, don't assume it will work even if the VBT
14804 * indicates as much.
14805 */
14806 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
14807 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14808 DREF_SSC1_ENABLE);
14809
14810 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
14811 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
14812 bios_lvds_use_ssc ? "en" : "dis",
14813 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
14814 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
14815 }
14816 }
14817
Jesse Barnese70236a2009-09-21 10:42:27 -070014818 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014819 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014820
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014821 if (IS_GEN2(dev)) {
14822 dev->mode_config.max_width = 2048;
14823 dev->mode_config.max_height = 2048;
14824 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014825 dev->mode_config.max_width = 4096;
14826 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014827 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014828 dev->mode_config.max_width = 8192;
14829 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014830 }
Damien Lespiau068be562014-03-28 14:17:49 +000014831
Ville Syrjälädc41c152014-08-13 11:57:05 +030014832 if (IS_845G(dev) || IS_I865G(dev)) {
14833 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14834 dev->mode_config.cursor_height = 1023;
14835 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014836 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14837 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14838 } else {
14839 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14840 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14841 }
14842
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014843 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014844
Zhao Yakui28c97732009-10-09 11:39:41 +080014845 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014846 INTEL_INFO(dev)->num_pipes,
14847 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014848
Damien Lespiau055e3932014-08-18 13:49:10 +010014849 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014850 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014851 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014852 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014853 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014854 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014855 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014856 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014857 }
14858
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030014859 intel_update_czclk(dev_priv);
14860 intel_update_cdclk(dev);
Jesse Barnesf42bb702013-12-16 16:34:23 -080014861
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014862 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014863
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014864 /* Just disable it once at startup */
14865 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014866 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014867
14868 /* Just in case the BIOS is doing something questionable. */
Paulo Zanoni7733b492015-07-07 15:26:04 -030014869 intel_fbc_disable(dev_priv);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014870
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014871 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020014872 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014873 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014874
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014875 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014876 struct intel_initial_plane_config plane_config = {};
14877
Jesse Barnes46f297f2014-03-07 08:57:48 -080014878 if (!crtc->active)
14879 continue;
14880
Jesse Barnes46f297f2014-03-07 08:57:48 -080014881 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014882 * Note that reserving the BIOS fb up front prevents us
14883 * from stuffing other stolen allocations like the ring
14884 * on top. This prevents some ugliness at boot time, and
14885 * can even allow for smooth boot transitions if the BIOS
14886 * fb is large enough for the active pipe configuration.
14887 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014888 dev_priv->display.get_initial_plane_config(crtc,
14889 &plane_config);
14890
14891 /*
14892 * If the fb is shared between multiple heads, we'll
14893 * just get the first one.
14894 */
14895 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014896 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014897}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014898
Daniel Vetter7fad7982012-07-04 17:51:47 +020014899static void intel_enable_pipe_a(struct drm_device *dev)
14900{
14901 struct intel_connector *connector;
14902 struct drm_connector *crt = NULL;
14903 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014904 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014905
14906 /* We can't just switch on the pipe A, we need to set things up with a
14907 * proper mode and output configuration. As a gross hack, enable pipe A
14908 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014909 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014910 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14911 crt = &connector->base;
14912 break;
14913 }
14914 }
14915
14916 if (!crt)
14917 return;
14918
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014919 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014920 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014921}
14922
Daniel Vetterfa555832012-10-10 23:14:00 +020014923static bool
14924intel_check_plane_mapping(struct intel_crtc *crtc)
14925{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014926 struct drm_device *dev = crtc->base.dev;
14927 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030014928 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020014929
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014930 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014931 return true;
14932
Ville Syrjälä649636e2015-09-22 19:50:01 +030014933 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020014934
14935 if ((val & DISPLAY_PLANE_ENABLE) &&
14936 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14937 return false;
14938
14939 return true;
14940}
14941
Ville Syrjälä02e93c32015-08-26 19:39:19 +030014942static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
14943{
14944 struct drm_device *dev = crtc->base.dev;
14945 struct intel_encoder *encoder;
14946
14947 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
14948 return true;
14949
14950 return false;
14951}
14952
Daniel Vetter24929352012-07-02 20:28:59 +020014953static void intel_sanitize_crtc(struct intel_crtc *crtc)
14954{
14955 struct drm_device *dev = crtc->base.dev;
14956 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014957 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014958
Daniel Vetter24929352012-07-02 20:28:59 +020014959 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014960 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014961 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14962
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014963 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014964 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014965 if (crtc->active) {
Ville Syrjälä0836e6d2015-09-10 18:59:08 +030014966 struct intel_plane *plane;
14967
Daniel Vetter96256042015-02-13 21:03:42 +010014968 drm_crtc_vblank_on(&crtc->base);
Ville Syrjälä0836e6d2015-09-10 18:59:08 +030014969
14970 /* Disable everything but the primary plane */
14971 for_each_intel_plane_on_crtc(dev, crtc, plane) {
14972 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
14973 continue;
14974
14975 plane->disable_plane(&plane->base, &crtc->base);
14976 }
Daniel Vetter96256042015-02-13 21:03:42 +010014977 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014978
Daniel Vetter24929352012-07-02 20:28:59 +020014979 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014980 * disable the crtc (and hence change the state) if it is wrong. Note
14981 * that gen4+ has a fixed plane -> pipe mapping. */
14982 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014983 bool plane;
14984
Daniel Vetter24929352012-07-02 20:28:59 +020014985 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14986 crtc->base.base.id);
14987
14988 /* Pipe has the wrong plane attached and the plane is active.
14989 * Temporarily change the plane mapping and disable everything
14990 * ... */
14991 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014992 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014993 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020014994 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014995 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020014996 }
Daniel Vetter24929352012-07-02 20:28:59 +020014997
Daniel Vetter7fad7982012-07-04 17:51:47 +020014998 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14999 crtc->pipe == PIPE_A && !crtc->active) {
15000 /* BIOS forgot to enable pipe A, this mostly happens after
15001 * resume. Force-enable the pipe to fix this, the update_dpms
15002 * call below we restore the pipe to the right state, but leave
15003 * the required bits on. */
15004 intel_enable_pipe_a(dev);
15005 }
15006
Daniel Vetter24929352012-07-02 20:28:59 +020015007 /* Adjust the state of the output pipe according to whether we
15008 * have active connectors/encoders. */
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015009 if (!intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015010 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015011
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015012 if (crtc->active != crtc->base.state->active) {
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015013 struct intel_encoder *encoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015014
15015 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015016 * functions or because of calls to intel_crtc_disable_noatomic,
15017 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015018 * pipe A quirk. */
15019 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15020 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015021 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015022 crtc->active ? "enabled" : "disabled");
15023
Maarten Lankhorst4be40c92015-07-14 13:45:32 +020015024 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015025 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015026 crtc->base.enabled = crtc->active;
15027
15028 /* Because we only establish the connector -> encoder ->
15029 * crtc links if something is active, this means the
15030 * crtc is now deactivated. Break the links. connector
15031 * -> encoder links are only establish when things are
15032 * actually up, hence no need to break them. */
15033 WARN_ON(crtc->active);
15034
Maarten Lankhorst2d406bb2015-08-05 12:37:09 +020015035 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetter24929352012-07-02 20:28:59 +020015036 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015037 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015038
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015039 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015040 /*
15041 * We start out with underrun reporting disabled to avoid races.
15042 * For correct bookkeeping mark this on active crtcs.
15043 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015044 * Also on gmch platforms we dont have any hardware bits to
15045 * disable the underrun reporting. Which means we need to start
15046 * out with underrun reporting disabled also on inactive pipes,
15047 * since otherwise we'll complain about the garbage we read when
15048 * e.g. coming up after runtime pm.
15049 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015050 * No protection against concurrent access is required - at
15051 * worst a fifo underrun happens which also sets this to false.
15052 */
15053 crtc->cpu_fifo_underrun_disabled = true;
15054 crtc->pch_fifo_underrun_disabled = true;
15055 }
Daniel Vetter24929352012-07-02 20:28:59 +020015056}
15057
15058static void intel_sanitize_encoder(struct intel_encoder *encoder)
15059{
15060 struct intel_connector *connector;
15061 struct drm_device *dev = encoder->base.dev;
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020015062 bool active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015063
15064 /* We need to check both for a crtc link (meaning that the
15065 * encoder is active and trying to read from a pipe) and the
15066 * pipe itself being active. */
15067 bool has_active_crtc = encoder->base.crtc &&
15068 to_intel_crtc(encoder->base.crtc)->active;
15069
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020015070 for_each_intel_connector(dev, connector) {
15071 if (connector->base.encoder != &encoder->base)
15072 continue;
15073
15074 active = true;
15075 break;
15076 }
15077
15078 if (active && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015079 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15080 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015081 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015082
15083 /* Connector is active, but has no active pipe. This is
15084 * fallout from our resume register restoring. Disable
15085 * the encoder manually again. */
15086 if (encoder->base.crtc) {
15087 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15088 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015089 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015090 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015091 if (encoder->post_disable)
15092 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015093 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015094 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015095
15096 /* Inconsistent output/port/pipe state happens presumably due to
15097 * a bug in one of the get_hw_state functions. Or someplace else
15098 * in our code, like the register restore mess on resume. Clamp
15099 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015100 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015101 if (connector->encoder != encoder)
15102 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015103 connector->base.dpms = DRM_MODE_DPMS_OFF;
15104 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015105 }
15106 }
15107 /* Enabled encoders without active connectors will be fixed in
15108 * the crtc fixup. */
15109}
15110
Imre Deak04098752014-02-18 00:02:16 +020015111void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015112{
15113 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015114 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015115
Imre Deak04098752014-02-18 00:02:16 +020015116 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15117 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15118 i915_disable_vga(dev);
15119 }
15120}
15121
15122void i915_redisable_vga(struct drm_device *dev)
15123{
15124 struct drm_i915_private *dev_priv = dev->dev_private;
15125
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015126 /* This function can be called both from intel_modeset_setup_hw_state or
15127 * at a very early point in our resume sequence, where the power well
15128 * structures are not yet restored. Since this function is at a very
15129 * paranoid "someone might have enabled VGA while we were not looking"
15130 * level, just check if the power well is enabled instead of trying to
15131 * follow the "don't touch the power well if we don't need it" policy
15132 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015133 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015134 return;
15135
Imre Deak04098752014-02-18 00:02:16 +020015136 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015137}
15138
Ville Syrjälä0836e6d2015-09-10 18:59:08 +030015139static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015140{
Ville Syrjälä0836e6d2015-09-10 18:59:08 +030015141 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015142
Ville Syrjälä0836e6d2015-09-10 18:59:08 +030015143 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015144}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015145
Ville Syrjälä0836e6d2015-09-10 18:59:08 +030015146/* FIXME read out full plane state for all planes */
15147static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015148{
Maarten Lankhorst18e93452015-09-23 16:11:41 +020015149 struct drm_plane *primary = crtc->base.primary;
Ville Syrjälä0836e6d2015-09-10 18:59:08 +030015150 struct intel_plane_state *plane_state =
Maarten Lankhorst18e93452015-09-23 16:11:41 +020015151 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015152
Ville Syrjälä0836e6d2015-09-10 18:59:08 +030015153 plane_state->visible =
Maarten Lankhorst18e93452015-09-23 16:11:41 +020015154 primary_get_hw_state(to_intel_plane(primary));
15155
15156 if (plane_state->visible)
15157 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015158}
15159
Daniel Vetter30e984d2013-06-05 13:34:17 +020015160static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015161{
15162 struct drm_i915_private *dev_priv = dev->dev_private;
15163 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015164 struct intel_crtc *crtc;
15165 struct intel_encoder *encoder;
15166 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015167 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015168
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015169 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb06f8b02015-07-14 13:42:49 +020015170 __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015171 memset(crtc->config, 0, sizeof(*crtc->config));
Maarten Lankhorstf7217902015-06-10 10:24:20 +020015172 crtc->config->base.crtc = &crtc->base;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015173
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015174 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015175 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015176
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015177 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015178 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015179
Ville Syrjälä0836e6d2015-09-10 18:59:08 +030015180 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015181
15182 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15183 crtc->base.base.id,
15184 crtc->active ? "enabled" : "disabled");
15185 }
15186
Daniel Vetter53589012013-06-05 13:34:16 +020015187 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15188 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15189
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015190 pll->on = pll->get_hw_state(dev_priv, pll,
15191 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015192 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015193 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015194 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015195 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015196 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015197 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015198 }
Daniel Vetter53589012013-06-05 13:34:16 +020015199 }
Daniel Vetter53589012013-06-05 13:34:16 +020015200
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015201 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015202 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015203
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015204 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015205 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015206 }
15207
Damien Lespiaub2784e12014-08-05 11:29:37 +010015208 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015209 pipe = 0;
15210
15211 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015212 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15213 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015214 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015215 } else {
15216 encoder->base.crtc = NULL;
15217 }
15218
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015219 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015220 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015221 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015222 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015223 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015224 }
15225
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015226 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015227 if (connector->get_hw_state(connector)) {
15228 connector->base.dpms = DRM_MODE_DPMS_ON;
Daniel Vetter24929352012-07-02 20:28:59 +020015229 connector->base.encoder = &connector->encoder->base;
15230 } else {
15231 connector->base.dpms = DRM_MODE_DPMS_OFF;
15232 connector->base.encoder = NULL;
15233 }
15234 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15235 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015236 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015237 connector->base.encoder ? "enabled" : "disabled");
15238 }
Ville Syrjäläc4816c72015-09-10 18:59:07 +030015239
15240 for_each_intel_crtc(dev, crtc) {
15241 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15242
15243 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15244 if (crtc->base.state->active) {
15245 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15246 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15247 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15248
15249 /*
15250 * The initial mode needs to be set in order to keep
15251 * the atomic core happy. It wants a valid mode if the
15252 * crtc's enabled, so we do the above call.
15253 *
15254 * At this point some state updated by the connectors
15255 * in their ->detect() callback has not run yet, so
15256 * no recalculation can be done yet.
15257 *
15258 * Even if we could do a recalculation and modeset
15259 * right now it would cause a double modeset if
15260 * fbdev or userspace chooses a different initial mode.
15261 *
15262 * If that happens, someone indicated they wanted a
15263 * mode change, which means it's safe to do a full
15264 * recalculation.
15265 */
15266 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015267
15268 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15269 update_scanline_offset(crtc);
Ville Syrjäläc4816c72015-09-10 18:59:07 +030015270 }
15271 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015272}
15273
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015274/* Scan out the current hw modeset state,
15275 * and sanitizes it to the current state
15276 */
15277static void
15278intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015279{
15280 struct drm_i915_private *dev_priv = dev->dev_private;
15281 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015282 struct intel_crtc *crtc;
15283 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015284 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015285
15286 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015287
15288 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015289 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015290 intel_sanitize_encoder(encoder);
15291 }
15292
Damien Lespiau055e3932014-08-18 13:49:10 +010015293 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015294 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15295 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015296 intel_dump_pipe_config(crtc, crtc->config,
15297 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015298 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015299
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015300 intel_modeset_update_connector_atomic_state(dev);
15301
Daniel Vetter35c95372013-07-17 06:55:04 +020015302 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15303 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15304
15305 if (!pll->on || pll->active)
15306 continue;
15307
15308 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15309
15310 pll->disable(dev_priv, pll);
15311 pll->on = false;
15312 }
15313
Ville Syrjälä26e1fe42015-06-24 22:00:06 +030015314 if (IS_VALLEYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015315 vlv_wm_get_hw_state(dev);
15316 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015317 skl_wm_get_hw_state(dev);
15318 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015319 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015320
15321 for_each_intel_crtc(dev, crtc) {
15322 unsigned long put_domains;
15323
15324 put_domains = modeset_get_crtc_power_domains(&crtc->base);
15325 if (WARN_ON(put_domains))
15326 modeset_put_power_domains(dev_priv, put_domains);
15327 }
15328 intel_display_set_init_power(dev_priv, false);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015329}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015330
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015331void intel_display_resume(struct drm_device *dev)
15332{
15333 struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
15334 struct intel_connector *conn;
15335 struct intel_plane *plane;
15336 struct drm_crtc *crtc;
15337 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015338
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015339 if (!state)
15340 return;
15341
15342 state->acquire_ctx = dev->mode_config.acquire_ctx;
15343
15344 /* preserve complete old state, including dpll */
15345 intel_atomic_get_shared_dpll_state(state);
15346
15347 for_each_crtc(dev, crtc) {
15348 struct drm_crtc_state *crtc_state =
15349 drm_atomic_get_crtc_state(state, crtc);
15350
15351 ret = PTR_ERR_OR_ZERO(crtc_state);
15352 if (ret)
15353 goto err;
15354
15355 /* force a restore */
15356 crtc_state->mode_changed = true;
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015357 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015358
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015359 for_each_intel_plane(dev, plane) {
15360 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
15361 if (ret)
15362 goto err;
15363 }
15364
15365 for_each_intel_connector(dev, conn) {
15366 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
15367 if (ret)
15368 goto err;
15369 }
15370
15371 intel_modeset_setup_hw_state(dev);
15372
15373 i915_redisable_vga(dev);
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020015374 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015375 if (!ret)
15376 return;
15377
15378err:
15379 DRM_ERROR("Restoring old state failed with %i\n", ret);
15380 drm_atomic_state_free(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015381}
15382
15383void intel_modeset_gem_init(struct drm_device *dev)
15384{
Jesse Barnes484b41d2014-03-07 08:57:55 -080015385 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015386 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015387 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015388
Imre Deakae484342014-03-31 15:10:44 +030015389 mutex_lock(&dev->struct_mutex);
15390 intel_init_gt_powersave(dev);
15391 mutex_unlock(&dev->struct_mutex);
15392
Chris Wilson1833b132012-05-09 11:56:28 +010015393 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015394
15395 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015396
15397 /*
15398 * Make sure any fbs we allocated at startup are properly
15399 * pinned & fenced. When we do the allocation it's too early
15400 * for this.
15401 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015402 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015403 obj = intel_fb_obj(c->primary->fb);
15404 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015405 continue;
15406
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015407 mutex_lock(&dev->struct_mutex);
15408 ret = intel_pin_and_fence_fb_obj(c->primary,
15409 c->primary->fb,
15410 c->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010015411 NULL, NULL);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015412 mutex_unlock(&dev->struct_mutex);
15413 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015414 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15415 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015416 drm_framebuffer_unreference(c->primary->fb);
15417 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015418 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015419 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015420 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015421 }
15422 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015423
15424 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015425}
15426
Imre Deak4932e2c2014-02-11 17:12:48 +020015427void intel_connector_unregister(struct intel_connector *intel_connector)
15428{
15429 struct drm_connector *connector = &intel_connector->base;
15430
15431 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015432 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015433}
15434
Jesse Barnes79e53942008-11-07 14:24:08 -080015435void intel_modeset_cleanup(struct drm_device *dev)
15436{
Jesse Barnes652c3932009-08-17 13:31:43 -070015437 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015438 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015439
Imre Deak2eb52522014-11-19 15:30:05 +020015440 intel_disable_gt_powersave(dev);
15441
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015442 intel_backlight_unregister(dev);
15443
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015444 /*
15445 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015446 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015447 * experience fancy races otherwise.
15448 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015449 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015450
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015451 /*
15452 * Due to the hpd irq storm handling the hotplug work can re-arm the
15453 * poll handlers. Hence disable polling after hpd handling is shut down.
15454 */
Keith Packardf87ea762010-10-03 19:36:26 -070015455 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015456
Jesse Barnes723bfd72010-10-07 16:01:13 -070015457 intel_unregister_dsm_handler();
15458
Paulo Zanoni7733b492015-07-07 15:26:04 -030015459 intel_fbc_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015460
Chris Wilson1630fe72011-07-08 12:22:42 +010015461 /* flush any delayed tasks or pending work */
15462 flush_scheduled_work();
15463
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015464 /* destroy the backlight and sysfs files before encoders/connectors */
15465 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015466 struct intel_connector *intel_connector;
15467
15468 intel_connector = to_intel_connector(connector);
15469 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015470 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015471
Jesse Barnes79e53942008-11-07 14:24:08 -080015472 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015473
15474 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015475
15476 mutex_lock(&dev->struct_mutex);
15477 intel_cleanup_gt_powersave(dev);
15478 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015479}
15480
Dave Airlie28d52042009-09-21 14:33:58 +100015481/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015482 * Return which encoder is currently attached for connector.
15483 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015484struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015485{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015486 return &intel_attached_encoder(connector)->base;
15487}
Jesse Barnes79e53942008-11-07 14:24:08 -080015488
Chris Wilsondf0e9242010-09-09 16:20:55 +010015489void intel_connector_attach_encoder(struct intel_connector *connector,
15490 struct intel_encoder *encoder)
15491{
15492 connector->encoder = encoder;
15493 drm_mode_connector_attach_encoder(&connector->base,
15494 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015495}
Dave Airlie28d52042009-09-21 14:33:58 +100015496
15497/*
15498 * set vga decode state - true == enable VGA decode
15499 */
15500int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15501{
15502 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015503 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015504 u16 gmch_ctrl;
15505
Chris Wilson75fa0412014-02-07 18:37:02 -020015506 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15507 DRM_ERROR("failed to read control word\n");
15508 return -EIO;
15509 }
15510
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015511 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15512 return 0;
15513
Dave Airlie28d52042009-09-21 14:33:58 +100015514 if (state)
15515 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15516 else
15517 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015518
15519 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15520 DRM_ERROR("failed to write control word\n");
15521 return -EIO;
15522 }
15523
Dave Airlie28d52042009-09-21 14:33:58 +100015524 return 0;
15525}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015526
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015527struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015528
15529 u32 power_well_driver;
15530
Chris Wilson63b66e52013-08-08 15:12:06 +020015531 int num_transcoders;
15532
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015533 struct intel_cursor_error_state {
15534 u32 control;
15535 u32 position;
15536 u32 base;
15537 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015538 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015539
15540 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015541 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015542 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015543 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015544 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015545
15546 struct intel_plane_error_state {
15547 u32 control;
15548 u32 stride;
15549 u32 size;
15550 u32 pos;
15551 u32 addr;
15552 u32 surface;
15553 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015554 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015555
15556 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015557 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015558 enum transcoder cpu_transcoder;
15559
15560 u32 conf;
15561
15562 u32 htotal;
15563 u32 hblank;
15564 u32 hsync;
15565 u32 vtotal;
15566 u32 vblank;
15567 u32 vsync;
15568 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015569};
15570
15571struct intel_display_error_state *
15572intel_display_capture_error_state(struct drm_device *dev)
15573{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015574 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015575 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015576 int transcoders[] = {
15577 TRANSCODER_A,
15578 TRANSCODER_B,
15579 TRANSCODER_C,
15580 TRANSCODER_EDP,
15581 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015582 int i;
15583
Chris Wilson63b66e52013-08-08 15:12:06 +020015584 if (INTEL_INFO(dev)->num_pipes == 0)
15585 return NULL;
15586
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015587 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015588 if (error == NULL)
15589 return NULL;
15590
Imre Deak190be112013-11-25 17:15:31 +020015591 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015592 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15593
Damien Lespiau055e3932014-08-18 13:49:10 +010015594 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015595 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015596 __intel_display_power_is_enabled(dev_priv,
15597 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015598 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015599 continue;
15600
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015601 error->cursor[i].control = I915_READ(CURCNTR(i));
15602 error->cursor[i].position = I915_READ(CURPOS(i));
15603 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015604
15605 error->plane[i].control = I915_READ(DSPCNTR(i));
15606 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015607 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015608 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015609 error->plane[i].pos = I915_READ(DSPPOS(i));
15610 }
Paulo Zanonica291362013-03-06 20:03:14 -030015611 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15612 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015613 if (INTEL_INFO(dev)->gen >= 4) {
15614 error->plane[i].surface = I915_READ(DSPSURF(i));
15615 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15616 }
15617
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015618 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015619
Sonika Jindal3abfce72014-07-21 15:23:43 +053015620 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015621 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015622 }
15623
15624 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15625 if (HAS_DDI(dev_priv->dev))
15626 error->num_transcoders++; /* Account for eDP. */
15627
15628 for (i = 0; i < error->num_transcoders; i++) {
15629 enum transcoder cpu_transcoder = transcoders[i];
15630
Imre Deakddf9c532013-11-27 22:02:02 +020015631 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015632 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015633 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015634 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015635 continue;
15636
Chris Wilson63b66e52013-08-08 15:12:06 +020015637 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15638
15639 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15640 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15641 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15642 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15643 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15644 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15645 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015646 }
15647
15648 return error;
15649}
15650
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015651#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15652
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015653void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015654intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015655 struct drm_device *dev,
15656 struct intel_display_error_state *error)
15657{
Damien Lespiau055e3932014-08-18 13:49:10 +010015658 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015659 int i;
15660
Chris Wilson63b66e52013-08-08 15:12:06 +020015661 if (!error)
15662 return;
15663
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015664 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015665 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015666 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015667 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015668 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015669 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015670 err_printf(m, " Power: %s\n",
15671 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015672 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015673 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015674
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015675 err_printf(m, "Plane [%d]:\n", i);
15676 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15677 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015678 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015679 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15680 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015681 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015682 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015683 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015684 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015685 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15686 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015687 }
15688
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015689 err_printf(m, "Cursor [%d]:\n", i);
15690 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15691 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15692 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015693 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015694
15695 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015696 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015697 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015698 err_printf(m, " Power: %s\n",
15699 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015700 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15701 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15702 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15703 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15704 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15705 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15706 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15707 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015708}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015709
15710void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15711{
15712 struct intel_crtc *crtc;
15713
15714 for_each_intel_crtc(dev, crtc) {
15715 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015716
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015717 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015718
15719 work = crtc->unpin_work;
15720
15721 if (work && work->event &&
15722 work->event->base.file_priv == file) {
15723 kfree(work->event);
15724 work->event = NULL;
15725 }
15726
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015727 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015728 }
15729}