blob: ba0c85e69f6f04ba389a5a4d0722aab30614c0c2 [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,
Matt Roper465c1202014-05-29 08:06:54 -070075};
76
Matt Roper3d7d6512014-06-10 08:28:13 -070077/* Cursor formats */
78static const uint32_t intel_cursor_formats[] = {
79 DRM_FORMAT_ARGB8888,
80};
81
Chris Wilson6b383a72010-09-13 13:54:26 +010082static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080083
Jesse Barnesf1f644d2013-06-27 00:39:25 +030084static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020085 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030086static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020087 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030088
Jesse Barneseb1bfe82014-02-12 12:26:25 -080089static int intel_framebuffer_init(struct drm_device *dev,
90 struct intel_framebuffer *ifb,
91 struct drm_mode_fb_cmd2 *mode_cmd,
92 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020093static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
94static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020095static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070096 struct intel_link_m_n *m_n,
97 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020098static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +020099static void haswell_set_pipeconf(struct drm_crtc *crtc);
100static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200101static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200102 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200103static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200104 const struct intel_crtc_state *pipe_config);
Maarten Lankhorst613d2b22015-07-21 13:28:58 +0200105static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
106static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700107static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
108 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200109static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
110 int num_connectors);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200111static void intel_modeset_setup_hw_state(struct drm_device *dev);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100112
Jesse Barnes79e53942008-11-07 14:24:08 -0800113typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400114 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800115} intel_range_t;
116
117typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400118 int dot_limit;
119 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800120} intel_p2_t;
121
Ma Lingd4906092009-03-18 20:13:27 +0800122typedef struct intel_limit intel_limit_t;
123struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400124 intel_range_t dot, vco, n, m, m1, m2, p, p1;
125 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800126};
Jesse Barnes79e53942008-11-07 14:24:08 -0800127
Daniel Vetterd2acd212012-10-20 20:57:43 +0200128int
129intel_pch_rawclk(struct drm_device *dev)
130{
131 struct drm_i915_private *dev_priv = dev->dev_private;
132
133 WARN_ON(!HAS_PCH_SPLIT(dev));
134
135 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
136}
137
Chris Wilson021357a2010-09-07 20:54:59 +0100138static inline u32 /* units of 100MHz */
139intel_fdi_link_freq(struct drm_device *dev)
140{
Chris Wilson8b99e682010-10-13 09:59:17 +0100141 if (IS_GEN5(dev)) {
142 struct drm_i915_private *dev_priv = dev->dev_private;
143 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
144 } else
145 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100146}
147
Daniel Vetter5d536e22013-07-06 12:52:06 +0200148static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400149 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200150 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200151 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400152 .m = { .min = 96, .max = 140 },
153 .m1 = { .min = 18, .max = 26 },
154 .m2 = { .min = 6, .max = 16 },
155 .p = { .min = 4, .max = 128 },
156 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700157 .p2 = { .dot_limit = 165000,
158 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700159};
160
Daniel Vetter5d536e22013-07-06 12:52:06 +0200161static const intel_limit_t intel_limits_i8xx_dvo = {
162 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200163 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200164 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200165 .m = { .min = 96, .max = 140 },
166 .m1 = { .min = 18, .max = 26 },
167 .m2 = { .min = 6, .max = 16 },
168 .p = { .min = 4, .max = 128 },
169 .p1 = { .min = 2, .max = 33 },
170 .p2 = { .dot_limit = 165000,
171 .p2_slow = 4, .p2_fast = 4 },
172};
173
Keith Packarde4b36692009-06-05 19:22:17 -0700174static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400175 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200176 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200177 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400178 .m = { .min = 96, .max = 140 },
179 .m1 = { .min = 18, .max = 26 },
180 .m2 = { .min = 6, .max = 16 },
181 .p = { .min = 4, .max = 128 },
182 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700183 .p2 = { .dot_limit = 165000,
184 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700185};
Eric Anholt273e27c2011-03-30 13:01:10 -0700186
Keith Packarde4b36692009-06-05 19:22:17 -0700187static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400188 .dot = { .min = 20000, .max = 400000 },
189 .vco = { .min = 1400000, .max = 2800000 },
190 .n = { .min = 1, .max = 6 },
191 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100192 .m1 = { .min = 8, .max = 18 },
193 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400194 .p = { .min = 5, .max = 80 },
195 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700196 .p2 = { .dot_limit = 200000,
197 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700198};
199
200static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400201 .dot = { .min = 20000, .max = 400000 },
202 .vco = { .min = 1400000, .max = 2800000 },
203 .n = { .min = 1, .max = 6 },
204 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100205 .m1 = { .min = 8, .max = 18 },
206 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400207 .p = { .min = 7, .max = 98 },
208 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700209 .p2 = { .dot_limit = 112000,
210 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700211};
212
Eric Anholt273e27c2011-03-30 13:01:10 -0700213
Keith Packarde4b36692009-06-05 19:22:17 -0700214static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700215 .dot = { .min = 25000, .max = 270000 },
216 .vco = { .min = 1750000, .max = 3500000},
217 .n = { .min = 1, .max = 4 },
218 .m = { .min = 104, .max = 138 },
219 .m1 = { .min = 17, .max = 23 },
220 .m2 = { .min = 5, .max = 11 },
221 .p = { .min = 10, .max = 30 },
222 .p1 = { .min = 1, .max = 3},
223 .p2 = { .dot_limit = 270000,
224 .p2_slow = 10,
225 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800226 },
Keith Packarde4b36692009-06-05 19:22:17 -0700227};
228
229static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700230 .dot = { .min = 22000, .max = 400000 },
231 .vco = { .min = 1750000, .max = 3500000},
232 .n = { .min = 1, .max = 4 },
233 .m = { .min = 104, .max = 138 },
234 .m1 = { .min = 16, .max = 23 },
235 .m2 = { .min = 5, .max = 11 },
236 .p = { .min = 5, .max = 80 },
237 .p1 = { .min = 1, .max = 8},
238 .p2 = { .dot_limit = 165000,
239 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700240};
241
242static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700243 .dot = { .min = 20000, .max = 115000 },
244 .vco = { .min = 1750000, .max = 3500000 },
245 .n = { .min = 1, .max = 3 },
246 .m = { .min = 104, .max = 138 },
247 .m1 = { .min = 17, .max = 23 },
248 .m2 = { .min = 5, .max = 11 },
249 .p = { .min = 28, .max = 112 },
250 .p1 = { .min = 2, .max = 8 },
251 .p2 = { .dot_limit = 0,
252 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800253 },
Keith Packarde4b36692009-06-05 19:22:17 -0700254};
255
256static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700257 .dot = { .min = 80000, .max = 224000 },
258 .vco = { .min = 1750000, .max = 3500000 },
259 .n = { .min = 1, .max = 3 },
260 .m = { .min = 104, .max = 138 },
261 .m1 = { .min = 17, .max = 23 },
262 .m2 = { .min = 5, .max = 11 },
263 .p = { .min = 14, .max = 42 },
264 .p1 = { .min = 2, .max = 6 },
265 .p2 = { .dot_limit = 0,
266 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800267 },
Keith Packarde4b36692009-06-05 19:22:17 -0700268};
269
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500270static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400271 .dot = { .min = 20000, .max = 400000},
272 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700273 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400274 .n = { .min = 3, .max = 6 },
275 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700276 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400277 .m1 = { .min = 0, .max = 0 },
278 .m2 = { .min = 0, .max = 254 },
279 .p = { .min = 5, .max = 80 },
280 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700281 .p2 = { .dot_limit = 200000,
282 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700283};
284
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500285static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400286 .dot = { .min = 20000, .max = 400000 },
287 .vco = { .min = 1700000, .max = 3500000 },
288 .n = { .min = 3, .max = 6 },
289 .m = { .min = 2, .max = 256 },
290 .m1 = { .min = 0, .max = 0 },
291 .m2 = { .min = 0, .max = 254 },
292 .p = { .min = 7, .max = 112 },
293 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700294 .p2 = { .dot_limit = 112000,
295 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700296};
297
Eric Anholt273e27c2011-03-30 13:01:10 -0700298/* Ironlake / Sandybridge
299 *
300 * We calculate clock using (register_value + 2) for N/M1/M2, so here
301 * the range value for them is (actual_value - 2).
302 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800303static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700304 .dot = { .min = 25000, .max = 350000 },
305 .vco = { .min = 1760000, .max = 3510000 },
306 .n = { .min = 1, .max = 5 },
307 .m = { .min = 79, .max = 127 },
308 .m1 = { .min = 12, .max = 22 },
309 .m2 = { .min = 5, .max = 9 },
310 .p = { .min = 5, .max = 80 },
311 .p1 = { .min = 1, .max = 8 },
312 .p2 = { .dot_limit = 225000,
313 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700314};
315
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800316static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700317 .dot = { .min = 25000, .max = 350000 },
318 .vco = { .min = 1760000, .max = 3510000 },
319 .n = { .min = 1, .max = 3 },
320 .m = { .min = 79, .max = 118 },
321 .m1 = { .min = 12, .max = 22 },
322 .m2 = { .min = 5, .max = 9 },
323 .p = { .min = 28, .max = 112 },
324 .p1 = { .min = 2, .max = 8 },
325 .p2 = { .dot_limit = 225000,
326 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800327};
328
329static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700330 .dot = { .min = 25000, .max = 350000 },
331 .vco = { .min = 1760000, .max = 3510000 },
332 .n = { .min = 1, .max = 3 },
333 .m = { .min = 79, .max = 127 },
334 .m1 = { .min = 12, .max = 22 },
335 .m2 = { .min = 5, .max = 9 },
336 .p = { .min = 14, .max = 56 },
337 .p1 = { .min = 2, .max = 8 },
338 .p2 = { .dot_limit = 225000,
339 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800340};
341
Eric Anholt273e27c2011-03-30 13:01:10 -0700342/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800343static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700344 .dot = { .min = 25000, .max = 350000 },
345 .vco = { .min = 1760000, .max = 3510000 },
346 .n = { .min = 1, .max = 2 },
347 .m = { .min = 79, .max = 126 },
348 .m1 = { .min = 12, .max = 22 },
349 .m2 = { .min = 5, .max = 9 },
350 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400351 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700352 .p2 = { .dot_limit = 225000,
353 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800354};
355
356static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700357 .dot = { .min = 25000, .max = 350000 },
358 .vco = { .min = 1760000, .max = 3510000 },
359 .n = { .min = 1, .max = 3 },
360 .m = { .min = 79, .max = 126 },
361 .m1 = { .min = 12, .max = 22 },
362 .m2 = { .min = 5, .max = 9 },
363 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400364 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700365 .p2 = { .dot_limit = 225000,
366 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800367};
368
Ville Syrjälädc730512013-09-24 21:26:30 +0300369static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300370 /*
371 * These are the data rate limits (measured in fast clocks)
372 * since those are the strictest limits we have. The fast
373 * clock and actual rate limits are more relaxed, so checking
374 * them would make no difference.
375 */
376 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200377 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700378 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700379 .m1 = { .min = 2, .max = 3 },
380 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300381 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300382 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700383};
384
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300385static const intel_limit_t intel_limits_chv = {
386 /*
387 * These are the data rate limits (measured in fast clocks)
388 * since those are the strictest limits we have. The fast
389 * clock and actual rate limits are more relaxed, so checking
390 * them would make no difference.
391 */
392 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200393 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300394 .n = { .min = 1, .max = 1 },
395 .m1 = { .min = 2, .max = 2 },
396 .m2 = { .min = 24 << 22, .max = 175 << 22 },
397 .p1 = { .min = 2, .max = 4 },
398 .p2 = { .p2_slow = 1, .p2_fast = 14 },
399};
400
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200401static const intel_limit_t intel_limits_bxt = {
402 /* FIXME: find real dot limits */
403 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530404 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200405 .n = { .min = 1, .max = 1 },
406 .m1 = { .min = 2, .max = 2 },
407 /* FIXME: find real m2 limits */
408 .m2 = { .min = 2 << 22, .max = 255 << 22 },
409 .p1 = { .min = 2, .max = 4 },
410 .p2 = { .p2_slow = 1, .p2_fast = 20 },
411};
412
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200413static bool
414needs_modeset(struct drm_crtc_state *state)
415{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200416 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200417}
418
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300419/**
420 * Returns whether any output on the specified pipe is of the specified type
421 */
Damien Lespiau40935612014-10-29 11:16:59 +0000422bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300423{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300424 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300425 struct intel_encoder *encoder;
426
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300427 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300428 if (encoder->type == type)
429 return true;
430
431 return false;
432}
433
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200434/**
435 * Returns whether any output on the specified pipe will have the specified
436 * type after a staged modeset is complete, i.e., the same as
437 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
438 * encoder->crtc.
439 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200440static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
441 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200442{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200443 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300444 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200445 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200446 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200447 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200448
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300449 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200450 if (connector_state->crtc != crtc_state->base.crtc)
451 continue;
452
453 num_connectors++;
454
455 encoder = to_intel_encoder(connector_state->best_encoder);
456 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200457 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200458 }
459
460 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200461
462 return false;
463}
464
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200465static const intel_limit_t *
466intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800467{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200468 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800469 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800470
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200471 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100472 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000473 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800474 limit = &intel_limits_ironlake_dual_lvds_100m;
475 else
476 limit = &intel_limits_ironlake_dual_lvds;
477 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000478 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800479 limit = &intel_limits_ironlake_single_lvds_100m;
480 else
481 limit = &intel_limits_ironlake_single_lvds;
482 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200483 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800484 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800485
486 return limit;
487}
488
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200489static const intel_limit_t *
490intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800491{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200492 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800493 const intel_limit_t *limit;
494
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200495 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100496 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700497 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800498 else
Keith Packarde4b36692009-06-05 19:22:17 -0700499 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200500 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
501 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700502 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200503 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700504 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800505 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700506 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800507
508 return limit;
509}
510
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200511static const intel_limit_t *
512intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800513{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200514 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800515 const intel_limit_t *limit;
516
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200517 if (IS_BROXTON(dev))
518 limit = &intel_limits_bxt;
519 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200520 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800521 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200522 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500523 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200524 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500525 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800526 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500527 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300528 } else if (IS_CHERRYVIEW(dev)) {
529 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700530 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300531 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100532 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200533 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100534 limit = &intel_limits_i9xx_lvds;
535 else
536 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800537 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200538 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700539 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200540 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700541 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200542 else
543 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800544 }
545 return limit;
546}
547
Imre Deakdccbea32015-06-22 23:35:51 +0300548/*
549 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
550 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
551 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
552 * The helpers' return value is the rate of the clock that is fed to the
553 * display engine's pipe which can be the above fast dot clock rate or a
554 * divided-down version of it.
555 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500556/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300557static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800558{
Shaohua Li21778322009-02-23 15:19:16 +0800559 clock->m = clock->m2 + 2;
560 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200561 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300562 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300563 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
564 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300565
566 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800567}
568
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200569static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
570{
571 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
572}
573
Imre Deakdccbea32015-06-22 23:35:51 +0300574static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800575{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200576 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800577 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200578 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300579 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300580 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
581 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300582
583 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800584}
585
Imre Deakdccbea32015-06-22 23:35:51 +0300586static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300587{
588 clock->m = clock->m1 * clock->m2;
589 clock->p = clock->p1 * clock->p2;
590 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300591 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300592 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
593 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300594
595 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300596}
597
Imre Deakdccbea32015-06-22 23:35:51 +0300598int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300599{
600 clock->m = clock->m1 * clock->m2;
601 clock->p = clock->p1 * clock->p2;
602 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300603 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300604 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
605 clock->n << 22);
606 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300607
608 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300609}
610
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800611#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800612/**
613 * Returns whether the given set of divisors are valid for a given refclk with
614 * the given connectors.
615 */
616
Chris Wilson1b894b52010-12-14 20:04:54 +0000617static bool intel_PLL_is_valid(struct drm_device *dev,
618 const intel_limit_t *limit,
619 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800620{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300621 if (clock->n < limit->n.min || limit->n.max < clock->n)
622 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800623 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400624 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800625 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400626 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800627 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400628 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300629
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200630 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300631 if (clock->m1 <= clock->m2)
632 INTELPllInvalid("m1 <= m2\n");
633
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200634 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300635 if (clock->p < limit->p.min || limit->p.max < clock->p)
636 INTELPllInvalid("p out of range\n");
637 if (clock->m < limit->m.min || limit->m.max < clock->m)
638 INTELPllInvalid("m out of range\n");
639 }
640
Jesse Barnes79e53942008-11-07 14:24:08 -0800641 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400642 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800643 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
644 * connector, etc., rather than just a single range.
645 */
646 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400647 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800648
649 return true;
650}
651
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300652static int
653i9xx_select_p2_div(const intel_limit_t *limit,
654 const struct intel_crtc_state *crtc_state,
655 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800656{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300657 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800658
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200659 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800660 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100661 * For LVDS just rely on its current settings for dual-channel.
662 * We haven't figured out how to reliably set up different
663 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800664 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100665 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300666 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800667 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300668 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800669 } else {
670 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300671 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800672 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300673 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800674 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300675}
676
677static bool
678i9xx_find_best_dpll(const intel_limit_t *limit,
679 struct intel_crtc_state *crtc_state,
680 int target, int refclk, intel_clock_t *match_clock,
681 intel_clock_t *best_clock)
682{
683 struct drm_device *dev = crtc_state->base.crtc->dev;
684 intel_clock_t clock;
685 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800686
Akshay Joshi0206e352011-08-16 15:34:10 -0400687 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800688
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300689 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
690
Zhao Yakui42158662009-11-20 11:24:18 +0800691 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
692 clock.m1++) {
693 for (clock.m2 = limit->m2.min;
694 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200695 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800696 break;
697 for (clock.n = limit->n.min;
698 clock.n <= limit->n.max; clock.n++) {
699 for (clock.p1 = limit->p1.min;
700 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800701 int this_err;
702
Imre Deakdccbea32015-06-22 23:35:51 +0300703 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000704 if (!intel_PLL_is_valid(dev, limit,
705 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800706 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800707 if (match_clock &&
708 clock.p != match_clock->p)
709 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800710
711 this_err = abs(clock.dot - target);
712 if (this_err < err) {
713 *best_clock = clock;
714 err = this_err;
715 }
716 }
717 }
718 }
719 }
720
721 return (err != target);
722}
723
Ma Lingd4906092009-03-18 20:13:27 +0800724static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200725pnv_find_best_dpll(const intel_limit_t *limit,
726 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200727 int target, int refclk, intel_clock_t *match_clock,
728 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200729{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300730 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200731 intel_clock_t clock;
732 int err = target;
733
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200734 memset(best_clock, 0, sizeof(*best_clock));
735
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300736 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
737
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200738 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
739 clock.m1++) {
740 for (clock.m2 = limit->m2.min;
741 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200742 for (clock.n = limit->n.min;
743 clock.n <= limit->n.max; clock.n++) {
744 for (clock.p1 = limit->p1.min;
745 clock.p1 <= limit->p1.max; clock.p1++) {
746 int this_err;
747
Imre Deakdccbea32015-06-22 23:35:51 +0300748 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800749 if (!intel_PLL_is_valid(dev, limit,
750 &clock))
751 continue;
752 if (match_clock &&
753 clock.p != match_clock->p)
754 continue;
755
756 this_err = abs(clock.dot - target);
757 if (this_err < err) {
758 *best_clock = clock;
759 err = this_err;
760 }
761 }
762 }
763 }
764 }
765
766 return (err != target);
767}
768
Ma Lingd4906092009-03-18 20:13:27 +0800769static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200770g4x_find_best_dpll(const intel_limit_t *limit,
771 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200772 int target, int refclk, intel_clock_t *match_clock,
773 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800774{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300775 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800776 intel_clock_t clock;
777 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300778 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400779 /* approximately equals target * 0.00585 */
780 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800781
782 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300783
784 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
785
Ma Lingd4906092009-03-18 20:13:27 +0800786 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200787 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800788 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200789 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800790 for (clock.m1 = limit->m1.max;
791 clock.m1 >= limit->m1.min; clock.m1--) {
792 for (clock.m2 = limit->m2.max;
793 clock.m2 >= limit->m2.min; clock.m2--) {
794 for (clock.p1 = limit->p1.max;
795 clock.p1 >= limit->p1.min; clock.p1--) {
796 int this_err;
797
Imre Deakdccbea32015-06-22 23:35:51 +0300798 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000799 if (!intel_PLL_is_valid(dev, limit,
800 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800801 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000802
803 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800804 if (this_err < err_most) {
805 *best_clock = clock;
806 err_most = this_err;
807 max_n = clock.n;
808 found = true;
809 }
810 }
811 }
812 }
813 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800814 return found;
815}
Ma Lingd4906092009-03-18 20:13:27 +0800816
Imre Deakd5dd62b2015-03-17 11:40:03 +0200817/*
818 * Check if the calculated PLL configuration is more optimal compared to the
819 * best configuration and error found so far. Return the calculated error.
820 */
821static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
822 const intel_clock_t *calculated_clock,
823 const intel_clock_t *best_clock,
824 unsigned int best_error_ppm,
825 unsigned int *error_ppm)
826{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200827 /*
828 * For CHV ignore the error and consider only the P value.
829 * Prefer a bigger P value based on HW requirements.
830 */
831 if (IS_CHERRYVIEW(dev)) {
832 *error_ppm = 0;
833
834 return calculated_clock->p > best_clock->p;
835 }
836
Imre Deak24be4e42015-03-17 11:40:04 +0200837 if (WARN_ON_ONCE(!target_freq))
838 return false;
839
Imre Deakd5dd62b2015-03-17 11:40:03 +0200840 *error_ppm = div_u64(1000000ULL *
841 abs(target_freq - calculated_clock->dot),
842 target_freq);
843 /*
844 * Prefer a better P value over a better (smaller) error if the error
845 * is small. Ensure this preference for future configurations too by
846 * setting the error to 0.
847 */
848 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
849 *error_ppm = 0;
850
851 return true;
852 }
853
854 return *error_ppm + 10 < best_error_ppm;
855}
856
Zhenyu Wang2c072452009-06-05 15:38:42 +0800857static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200858vlv_find_best_dpll(const intel_limit_t *limit,
859 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200860 int target, int refclk, intel_clock_t *match_clock,
861 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700862{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200863 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300864 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300865 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300866 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300867 /* min update 19.2 MHz */
868 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300869 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700870
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300871 target *= 5; /* fast clock */
872
873 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700874
875 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300876 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300877 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300878 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300879 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300880 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700881 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300882 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200883 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300884
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300885 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
886 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300887
Imre Deakdccbea32015-06-22 23:35:51 +0300888 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300889
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300890 if (!intel_PLL_is_valid(dev, limit,
891 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300892 continue;
893
Imre Deakd5dd62b2015-03-17 11:40:03 +0200894 if (!vlv_PLL_is_optimal(dev, target,
895 &clock,
896 best_clock,
897 bestppm, &ppm))
898 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300899
Imre Deakd5dd62b2015-03-17 11:40:03 +0200900 *best_clock = clock;
901 bestppm = ppm;
902 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700903 }
904 }
905 }
906 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700907
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300908 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700909}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700910
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300911static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200912chv_find_best_dpll(const intel_limit_t *limit,
913 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300914 int target, int refclk, intel_clock_t *match_clock,
915 intel_clock_t *best_clock)
916{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200917 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300918 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200919 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300920 intel_clock_t clock;
921 uint64_t m2;
922 int found = false;
923
924 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200925 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300926
927 /*
928 * Based on hardware doc, the n always set to 1, and m1 always
929 * set to 2. If requires to support 200Mhz refclk, we need to
930 * revisit this because n may not 1 anymore.
931 */
932 clock.n = 1, clock.m1 = 2;
933 target *= 5; /* fast clock */
934
935 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
936 for (clock.p2 = limit->p2.p2_fast;
937 clock.p2 >= limit->p2.p2_slow;
938 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200939 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300940
941 clock.p = clock.p1 * clock.p2;
942
943 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
944 clock.n) << 22, refclk * clock.m1);
945
946 if (m2 > INT_MAX/clock.m1)
947 continue;
948
949 clock.m2 = m2;
950
Imre Deakdccbea32015-06-22 23:35:51 +0300951 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300952
953 if (!intel_PLL_is_valid(dev, limit, &clock))
954 continue;
955
Imre Deak9ca3ba02015-03-17 11:40:05 +0200956 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
957 best_error_ppm, &error_ppm))
958 continue;
959
960 *best_clock = clock;
961 best_error_ppm = error_ppm;
962 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300963 }
964 }
965
966 return found;
967}
968
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200969bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
970 intel_clock_t *best_clock)
971{
972 int refclk = i9xx_get_refclk(crtc_state, 0);
973
974 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
975 target_clock, refclk, NULL, best_clock);
976}
977
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300978bool intel_crtc_active(struct drm_crtc *crtc)
979{
980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
981
982 /* Be paranoid as we can arrive here with only partial
983 * state retrieved from the hardware during setup.
984 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100985 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300986 * as Haswell has gained clock readout/fastboot support.
987 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000988 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300989 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700990 *
991 * FIXME: The intel_crtc->active here should be switched to
992 * crtc->state->active once we have proper CRTC states wired up
993 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300994 */
Matt Roperc3d1f432015-03-09 10:19:23 -0700995 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200996 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300997}
998
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200999enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1000 enum pipe pipe)
1001{
1002 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1004
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001005 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001006}
1007
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001008static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1009{
1010 struct drm_i915_private *dev_priv = dev->dev_private;
1011 u32 reg = PIPEDSL(pipe);
1012 u32 line1, line2;
1013 u32 line_mask;
1014
1015 if (IS_GEN2(dev))
1016 line_mask = DSL_LINEMASK_GEN2;
1017 else
1018 line_mask = DSL_LINEMASK_GEN3;
1019
1020 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001021 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001022 line2 = I915_READ(reg) & line_mask;
1023
1024 return line1 == line2;
1025}
1026
Keith Packardab7ad7f2010-10-03 00:33:06 -07001027/*
1028 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001029 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001030 *
1031 * After disabling a pipe, we can't wait for vblank in the usual way,
1032 * spinning on the vblank interrupt status bit, since we won't actually
1033 * see an interrupt when the pipe is disabled.
1034 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001035 * On Gen4 and above:
1036 * wait for the pipe register state bit to turn off
1037 *
1038 * Otherwise:
1039 * wait for the display line value to settle (it usually
1040 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001041 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001042 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001043static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001044{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001045 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001046 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001047 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001048 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001049
Keith Packardab7ad7f2010-10-03 00:33:06 -07001050 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001051 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001052
Keith Packardab7ad7f2010-10-03 00:33:06 -07001053 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001054 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1055 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001056 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001057 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001058 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001059 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001060 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001061 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001062}
1063
Jesse Barnesb24e7172011-01-04 15:09:30 -08001064static const char *state_string(bool enabled)
1065{
1066 return enabled ? "on" : "off";
1067}
1068
1069/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001070void assert_pll(struct drm_i915_private *dev_priv,
1071 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001072{
1073 int reg;
1074 u32 val;
1075 bool cur_state;
1076
1077 reg = DPLL(pipe);
1078 val = I915_READ(reg);
1079 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001080 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001081 "PLL state assertion failure (expected %s, current %s)\n",
1082 state_string(state), state_string(cur_state));
1083}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001084
Jani Nikula23538ef2013-08-27 15:12:22 +03001085/* XXX: the dsi pll is shared between MIPI DSI ports */
1086static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1087{
1088 u32 val;
1089 bool cur_state;
1090
Ville Syrjäläa5805162015-05-26 20:42:30 +03001091 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001092 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001093 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001094
1095 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001096 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001097 "DSI PLL state assertion failure (expected %s, current %s)\n",
1098 state_string(state), state_string(cur_state));
1099}
1100#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1101#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1102
Daniel Vetter55607e82013-06-16 21:42:39 +02001103struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001104intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001105{
Daniel Vettere2b78262013-06-07 23:10:03 +02001106 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1107
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001108 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001109 return NULL;
1110
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001111 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001112}
1113
Jesse Barnesb24e7172011-01-04 15:09:30 -08001114/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001115void assert_shared_dpll(struct drm_i915_private *dev_priv,
1116 struct intel_shared_dpll *pll,
1117 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001118{
Jesse Barnes040484a2011-01-03 12:14:26 -08001119 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001120 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001121
Chris Wilson92b27b02012-05-20 18:10:50 +01001122 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001123 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001124 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001125
Daniel Vetter53589012013-06-05 13:34:16 +02001126 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001127 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001128 "%s assertion failure (expected %s, current %s)\n",
1129 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001130}
Jesse Barnes040484a2011-01-03 12:14:26 -08001131
1132static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1133 enum pipe pipe, bool state)
1134{
1135 int reg;
1136 u32 val;
1137 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001138 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1139 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001140
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001141 if (HAS_DDI(dev_priv->dev)) {
1142 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001143 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001144 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001145 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001146 } else {
1147 reg = FDI_TX_CTL(pipe);
1148 val = I915_READ(reg);
1149 cur_state = !!(val & FDI_TX_ENABLE);
1150 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001151 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001152 "FDI TX state assertion failure (expected %s, current %s)\n",
1153 state_string(state), state_string(cur_state));
1154}
1155#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1156#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1157
1158static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1159 enum pipe pipe, bool state)
1160{
1161 int reg;
1162 u32 val;
1163 bool cur_state;
1164
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001165 reg = FDI_RX_CTL(pipe);
1166 val = I915_READ(reg);
1167 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001168 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001169 "FDI RX state assertion failure (expected %s, current %s)\n",
1170 state_string(state), state_string(cur_state));
1171}
1172#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1173#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1174
1175static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1176 enum pipe pipe)
1177{
1178 int reg;
1179 u32 val;
1180
1181 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001182 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001183 return;
1184
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001185 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001186 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001187 return;
1188
Jesse Barnes040484a2011-01-03 12:14:26 -08001189 reg = FDI_TX_CTL(pipe);
1190 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001191 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 -08001192}
1193
Daniel Vetter55607e82013-06-16 21:42:39 +02001194void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1195 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001196{
1197 int reg;
1198 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001199 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001200
1201 reg = FDI_RX_CTL(pipe);
1202 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001203 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001204 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001205 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1206 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001207}
1208
Daniel Vetterb680c372014-09-19 18:27:27 +02001209void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1210 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001211{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001212 struct drm_device *dev = dev_priv->dev;
1213 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001214 u32 val;
1215 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001216 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001217
Jani Nikulabedd4db2014-08-22 15:04:13 +03001218 if (WARN_ON(HAS_DDI(dev)))
1219 return;
1220
1221 if (HAS_PCH_SPLIT(dev)) {
1222 u32 port_sel;
1223
Jesse Barnesea0760c2011-01-04 15:09:32 -08001224 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001225 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1226
1227 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1228 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1229 panel_pipe = PIPE_B;
1230 /* XXX: else fix for eDP */
1231 } else if (IS_VALLEYVIEW(dev)) {
1232 /* presumably write lock depends on pipe, not port select */
1233 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1234 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001235 } else {
1236 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001237 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1238 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001239 }
1240
1241 val = I915_READ(pp_reg);
1242 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001243 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001244 locked = false;
1245
Rob Clarke2c719b2014-12-15 13:56:32 -05001246 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001247 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001248 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001249}
1250
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001251static void assert_cursor(struct drm_i915_private *dev_priv,
1252 enum pipe pipe, bool state)
1253{
1254 struct drm_device *dev = dev_priv->dev;
1255 bool cur_state;
1256
Paulo Zanonid9d82082014-02-27 16:30:56 -03001257 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001258 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001259 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001260 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001261
Rob Clarke2c719b2014-12-15 13:56:32 -05001262 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001263 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1264 pipe_name(pipe), state_string(state), state_string(cur_state));
1265}
1266#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1267#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1268
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001269void assert_pipe(struct drm_i915_private *dev_priv,
1270 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001271{
1272 int reg;
1273 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001274 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001275 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1276 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001277
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001278 /* if we need the pipe quirk it must be always on */
1279 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1280 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001281 state = true;
1282
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001283 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001284 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001285 cur_state = false;
1286 } else {
1287 reg = PIPECONF(cpu_transcoder);
1288 val = I915_READ(reg);
1289 cur_state = !!(val & PIPECONF_ENABLE);
1290 }
1291
Rob Clarke2c719b2014-12-15 13:56:32 -05001292 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001293 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001294 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001295}
1296
Chris Wilson931872f2012-01-16 23:01:13 +00001297static void assert_plane(struct drm_i915_private *dev_priv,
1298 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001299{
1300 int reg;
1301 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001302 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001303
1304 reg = DSPCNTR(plane);
1305 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001306 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001307 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001308 "plane %c assertion failure (expected %s, current %s)\n",
1309 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001310}
1311
Chris Wilson931872f2012-01-16 23:01:13 +00001312#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1313#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1314
Jesse Barnesb24e7172011-01-04 15:09:30 -08001315static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1316 enum pipe pipe)
1317{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001318 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001319 int reg, i;
1320 u32 val;
1321 int cur_pipe;
1322
Ville Syrjälä653e1022013-06-04 13:49:05 +03001323 /* Primary planes are fixed to pipes on gen4+ */
1324 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001325 reg = DSPCNTR(pipe);
1326 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001327 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001328 "plane %c assertion failure, should be disabled but not\n",
1329 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001330 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001331 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001332
Jesse Barnesb24e7172011-01-04 15:09:30 -08001333 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001334 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001335 reg = DSPCNTR(i);
1336 val = I915_READ(reg);
1337 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1338 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001339 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001340 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1341 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001342 }
1343}
1344
Jesse Barnes19332d72013-03-28 09:55:38 -07001345static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1346 enum pipe pipe)
1347{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001348 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001349 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001350 u32 val;
1351
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001352 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001353 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001354 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001355 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001356 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1357 sprite, pipe_name(pipe));
1358 }
1359 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001360 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001361 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001362 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001363 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001364 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001365 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001366 }
1367 } else if (INTEL_INFO(dev)->gen >= 7) {
1368 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001369 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001370 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001371 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001372 plane_name(pipe), pipe_name(pipe));
1373 } else if (INTEL_INFO(dev)->gen >= 5) {
1374 reg = DVSCNTR(pipe);
1375 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001376 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001377 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1378 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001379 }
1380}
1381
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001382static void assert_vblank_disabled(struct drm_crtc *crtc)
1383{
Rob Clarke2c719b2014-12-15 13:56:32 -05001384 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001385 drm_crtc_vblank_put(crtc);
1386}
1387
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001388static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001389{
1390 u32 val;
1391 bool enabled;
1392
Rob Clarke2c719b2014-12-15 13:56:32 -05001393 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001394
Jesse Barnes92f25842011-01-04 15:09:34 -08001395 val = I915_READ(PCH_DREF_CONTROL);
1396 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1397 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001398 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001399}
1400
Daniel Vetterab9412b2013-05-03 11:49:46 +02001401static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1402 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001403{
1404 int reg;
1405 u32 val;
1406 bool enabled;
1407
Daniel Vetterab9412b2013-05-03 11:49:46 +02001408 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001409 val = I915_READ(reg);
1410 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001411 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001412 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1413 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001414}
1415
Keith Packard4e634382011-08-06 10:39:45 -07001416static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1417 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001418{
1419 if ((val & DP_PORT_EN) == 0)
1420 return false;
1421
1422 if (HAS_PCH_CPT(dev_priv->dev)) {
1423 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1424 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1425 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1426 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001427 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1428 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1429 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001430 } else {
1431 if ((val & DP_PIPE_MASK) != (pipe << 30))
1432 return false;
1433 }
1434 return true;
1435}
1436
Keith Packard1519b992011-08-06 10:35:34 -07001437static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1438 enum pipe pipe, u32 val)
1439{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001440 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001441 return false;
1442
1443 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001444 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001445 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001446 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1447 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1448 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001449 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001450 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001451 return false;
1452 }
1453 return true;
1454}
1455
1456static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1457 enum pipe pipe, u32 val)
1458{
1459 if ((val & LVDS_PORT_EN) == 0)
1460 return false;
1461
1462 if (HAS_PCH_CPT(dev_priv->dev)) {
1463 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1464 return false;
1465 } else {
1466 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1467 return false;
1468 }
1469 return true;
1470}
1471
1472static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1473 enum pipe pipe, u32 val)
1474{
1475 if ((val & ADPA_DAC_ENABLE) == 0)
1476 return false;
1477 if (HAS_PCH_CPT(dev_priv->dev)) {
1478 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1479 return false;
1480 } else {
1481 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1482 return false;
1483 }
1484 return true;
1485}
1486
Jesse Barnes291906f2011-02-02 12:28:03 -08001487static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001488 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001489{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001490 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001491 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001492 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001493 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001494
Rob Clarke2c719b2014-12-15 13:56:32 -05001495 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001496 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001497 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001498}
1499
1500static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1501 enum pipe pipe, int reg)
1502{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001503 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001504 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001505 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001506 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001507
Rob Clarke2c719b2014-12-15 13:56:32 -05001508 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001509 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001510 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001511}
1512
1513static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1514 enum pipe pipe)
1515{
1516 int reg;
1517 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001518
Keith Packardf0575e92011-07-25 22:12:43 -07001519 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1520 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1521 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001522
1523 reg = PCH_ADPA;
1524 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001525 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001526 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001527 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001528
1529 reg = PCH_LVDS;
1530 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001531 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001532 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001533 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001534
Paulo Zanonie2debe92013-02-18 19:00:27 -03001535 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1536 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1537 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001538}
1539
Ville Syrjäläd288f652014-10-28 13:20:22 +02001540static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001541 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001542{
Daniel Vetter426115c2013-07-11 22:13:42 +02001543 struct drm_device *dev = crtc->base.dev;
1544 struct drm_i915_private *dev_priv = dev->dev_private;
1545 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001546 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001547
Daniel Vetter426115c2013-07-11 22:13:42 +02001548 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001549
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001550 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001551 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1552
1553 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001554 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001555 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001556
Daniel Vetter426115c2013-07-11 22:13:42 +02001557 I915_WRITE(reg, dpll);
1558 POSTING_READ(reg);
1559 udelay(150);
1560
1561 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1562 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1563
Ville Syrjäläd288f652014-10-28 13:20:22 +02001564 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001565 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001566
1567 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001568 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001569 POSTING_READ(reg);
1570 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001571 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001572 POSTING_READ(reg);
1573 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001574 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001575 POSTING_READ(reg);
1576 udelay(150); /* wait for warmup */
1577}
1578
Ville Syrjäläd288f652014-10-28 13:20:22 +02001579static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001580 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001581{
1582 struct drm_device *dev = crtc->base.dev;
1583 struct drm_i915_private *dev_priv = dev->dev_private;
1584 int pipe = crtc->pipe;
1585 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001586 u32 tmp;
1587
1588 assert_pipe_disabled(dev_priv, crtc->pipe);
1589
1590 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1591
Ville Syrjäläa5805162015-05-26 20:42:30 +03001592 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001593
1594 /* Enable back the 10bit clock to display controller */
1595 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1596 tmp |= DPIO_DCLKP_EN;
1597 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1598
Ville Syrjälä54433e92015-05-26 20:42:31 +03001599 mutex_unlock(&dev_priv->sb_lock);
1600
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001601 /*
1602 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1603 */
1604 udelay(1);
1605
1606 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001607 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001608
1609 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001610 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001611 DRM_ERROR("PLL %d failed to lock\n", pipe);
1612
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001613 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001614 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001615 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001616}
1617
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001618static int intel_num_dvo_pipes(struct drm_device *dev)
1619{
1620 struct intel_crtc *crtc;
1621 int count = 0;
1622
1623 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001624 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001625 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001626
1627 return count;
1628}
1629
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001630static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001631{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001632 struct drm_device *dev = crtc->base.dev;
1633 struct drm_i915_private *dev_priv = dev->dev_private;
1634 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001635 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001636
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001637 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001638
1639 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001640 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001641
1642 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001643 if (IS_MOBILE(dev) && !IS_I830(dev))
1644 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001645
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001646 /* Enable DVO 2x clock on both PLLs if necessary */
1647 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1648 /*
1649 * It appears to be important that we don't enable this
1650 * for the current pipe before otherwise configuring the
1651 * PLL. No idea how this should be handled if multiple
1652 * DVO outputs are enabled simultaneosly.
1653 */
1654 dpll |= DPLL_DVO_2X_MODE;
1655 I915_WRITE(DPLL(!crtc->pipe),
1656 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1657 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001658
1659 /* Wait for the clocks to stabilize. */
1660 POSTING_READ(reg);
1661 udelay(150);
1662
1663 if (INTEL_INFO(dev)->gen >= 4) {
1664 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001665 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001666 } else {
1667 /* The pixel multiplier can only be updated once the
1668 * DPLL is enabled and the clocks are stable.
1669 *
1670 * So write it again.
1671 */
1672 I915_WRITE(reg, dpll);
1673 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001674
1675 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001676 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001677 POSTING_READ(reg);
1678 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001679 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001680 POSTING_READ(reg);
1681 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001682 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001683 POSTING_READ(reg);
1684 udelay(150); /* wait for warmup */
1685}
1686
1687/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001688 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001689 * @dev_priv: i915 private structure
1690 * @pipe: pipe PLL to disable
1691 *
1692 * Disable the PLL for @pipe, making sure the pipe is off first.
1693 *
1694 * Note! This is for pre-ILK only.
1695 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001696static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001697{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001698 struct drm_device *dev = crtc->base.dev;
1699 struct drm_i915_private *dev_priv = dev->dev_private;
1700 enum pipe pipe = crtc->pipe;
1701
1702 /* Disable DVO 2x clock on both PLLs if necessary */
1703 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001704 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001705 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001706 I915_WRITE(DPLL(PIPE_B),
1707 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1708 I915_WRITE(DPLL(PIPE_A),
1709 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1710 }
1711
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001712 /* Don't disable pipe or pipe PLLs if needed */
1713 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1714 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001715 return;
1716
1717 /* Make sure the pipe isn't still relying on us */
1718 assert_pipe_disabled(dev_priv, pipe);
1719
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001720 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001721 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001722}
1723
Jesse Barnesf6071162013-10-01 10:41:38 -07001724static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1725{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001726 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001727
1728 /* Make sure the pipe isn't still relying on us */
1729 assert_pipe_disabled(dev_priv, pipe);
1730
Imre Deake5cbfbf2014-01-09 17:08:16 +02001731 /*
1732 * Leave integrated clock source and reference clock enabled for pipe B.
1733 * The latter is needed for VGA hotplug / manual detection.
1734 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001735 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001736 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001737 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001738 I915_WRITE(DPLL(pipe), val);
1739 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001740
1741}
1742
1743static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1744{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001745 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001746 u32 val;
1747
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001748 /* Make sure the pipe isn't still relying on us */
1749 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001750
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001751 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001752 val = DPLL_SSC_REF_CLK_CHV |
1753 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001754 if (pipe != PIPE_A)
1755 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1756 I915_WRITE(DPLL(pipe), val);
1757 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001758
Ville Syrjäläa5805162015-05-26 20:42:30 +03001759 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001760
1761 /* Disable 10bit clock to display controller */
1762 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1763 val &= ~DPIO_DCLKP_EN;
1764 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1765
Ville Syrjäläa5805162015-05-26 20:42:30 +03001766 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001767}
1768
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001769void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001770 struct intel_digital_port *dport,
1771 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001772{
1773 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001774 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001775
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001776 switch (dport->port) {
1777 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001778 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001779 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001780 break;
1781 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001782 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001783 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001784 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001785 break;
1786 case PORT_D:
1787 port_mask = DPLL_PORTD_READY_MASK;
1788 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001789 break;
1790 default:
1791 BUG();
1792 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001793
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001794 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1795 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1796 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001797}
1798
Daniel Vetterb14b1052014-04-24 23:55:13 +02001799static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1800{
1801 struct drm_device *dev = crtc->base.dev;
1802 struct drm_i915_private *dev_priv = dev->dev_private;
1803 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1804
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001805 if (WARN_ON(pll == NULL))
1806 return;
1807
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001808 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001809 if (pll->active == 0) {
1810 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1811 WARN_ON(pll->on);
1812 assert_shared_dpll_disabled(dev_priv, pll);
1813
1814 pll->mode_set(dev_priv, pll);
1815 }
1816}
1817
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001818/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001819 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001820 * @dev_priv: i915 private structure
1821 * @pipe: pipe PLL to enable
1822 *
1823 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1824 * drives the transcoder clock.
1825 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001826static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001827{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001828 struct drm_device *dev = crtc->base.dev;
1829 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001830 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001831
Daniel Vetter87a875b2013-06-05 13:34:19 +02001832 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001833 return;
1834
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001835 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001836 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001837
Damien Lespiau74dd6922014-07-29 18:06:17 +01001838 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001839 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001840 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001841
Daniel Vettercdbd2312013-06-05 13:34:03 +02001842 if (pll->active++) {
1843 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001844 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001845 return;
1846 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001847 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001848
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001849 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1850
Daniel Vetter46edb022013-06-05 13:34:12 +02001851 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001852 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001853 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001854}
1855
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001856static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001857{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001858 struct drm_device *dev = crtc->base.dev;
1859 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001860 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001861
Jesse Barnes92f25842011-01-04 15:09:34 -08001862 /* PCH only available on ILK+ */
Jesse Barnes80aa9312015-08-03 13:09:11 -07001863 if (INTEL_INFO(dev)->gen < 5)
1864 return;
1865
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001866 if (pll == NULL)
1867 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001868
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001869 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
Chris Wilson48da64a2012-05-13 20:16:12 +01001870 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001871
Daniel Vetter46edb022013-06-05 13:34:12 +02001872 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1873 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001874 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001875
Chris Wilson48da64a2012-05-13 20:16:12 +01001876 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001877 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001878 return;
1879 }
1880
Daniel Vettere9d69442013-06-05 13:34:15 +02001881 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001882 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001883 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001884 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001885
Daniel Vetter46edb022013-06-05 13:34:12 +02001886 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001887 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001888 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001889
1890 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001891}
1892
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001893static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1894 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001895{
Daniel Vetter23670b322012-11-01 09:15:30 +01001896 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001897 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001898 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001899 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001900
1901 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001902 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001903
1904 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001905 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001906 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001907
1908 /* FDI must be feeding us bits for PCH ports */
1909 assert_fdi_tx_enabled(dev_priv, pipe);
1910 assert_fdi_rx_enabled(dev_priv, pipe);
1911
Daniel Vetter23670b322012-11-01 09:15:30 +01001912 if (HAS_PCH_CPT(dev)) {
1913 /* Workaround: Set the timing override bit before enabling the
1914 * pch transcoder. */
1915 reg = TRANS_CHICKEN2(pipe);
1916 val = I915_READ(reg);
1917 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1918 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001919 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001920
Daniel Vetterab9412b2013-05-03 11:49:46 +02001921 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001922 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001923 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001924
1925 if (HAS_PCH_IBX(dev_priv->dev)) {
1926 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001927 * Make the BPC in transcoder be consistent with
1928 * that in pipeconf reg. For HDMI we must use 8bpc
1929 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001930 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001931 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001932 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1933 val |= PIPECONF_8BPC;
1934 else
1935 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001936 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001937
1938 val &= ~TRANS_INTERLACE_MASK;
1939 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001940 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001941 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001942 val |= TRANS_LEGACY_INTERLACED_ILK;
1943 else
1944 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001945 else
1946 val |= TRANS_PROGRESSIVE;
1947
Jesse Barnes040484a2011-01-03 12:14:26 -08001948 I915_WRITE(reg, val | TRANS_ENABLE);
1949 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001950 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001951}
1952
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001953static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001954 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001955{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001956 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001957
1958 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001959 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001960
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001961 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001962 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001963 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001964
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001965 /* Workaround: set timing override bit. */
1966 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001967 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001968 I915_WRITE(_TRANSA_CHICKEN2, val);
1969
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001970 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001971 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001972
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001973 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1974 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001975 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001976 else
1977 val |= TRANS_PROGRESSIVE;
1978
Daniel Vetterab9412b2013-05-03 11:49:46 +02001979 I915_WRITE(LPT_TRANSCONF, val);
1980 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001981 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001982}
1983
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001984static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1985 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001986{
Daniel Vetter23670b322012-11-01 09:15:30 +01001987 struct drm_device *dev = dev_priv->dev;
1988 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001989
1990 /* FDI relies on the transcoder */
1991 assert_fdi_tx_disabled(dev_priv, pipe);
1992 assert_fdi_rx_disabled(dev_priv, pipe);
1993
Jesse Barnes291906f2011-02-02 12:28:03 -08001994 /* Ports must be off as well */
1995 assert_pch_ports_disabled(dev_priv, pipe);
1996
Daniel Vetterab9412b2013-05-03 11:49:46 +02001997 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001998 val = I915_READ(reg);
1999 val &= ~TRANS_ENABLE;
2000 I915_WRITE(reg, val);
2001 /* wait for PCH transcoder off, transcoder state */
2002 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002003 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002004
2005 if (!HAS_PCH_IBX(dev)) {
2006 /* Workaround: Clear the timing override chicken bit again. */
2007 reg = TRANS_CHICKEN2(pipe);
2008 val = I915_READ(reg);
2009 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2010 I915_WRITE(reg, val);
2011 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002012}
2013
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002014static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002015{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002016 u32 val;
2017
Daniel Vetterab9412b2013-05-03 11:49:46 +02002018 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002019 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002020 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002021 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002022 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002023 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002024
2025 /* Workaround: clear timing override bit. */
2026 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002027 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002028 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002029}
2030
2031/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002032 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002033 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002034 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002035 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002036 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002037 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002038static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002039{
Paulo Zanoni03722642014-01-17 13:51:09 -02002040 struct drm_device *dev = crtc->base.dev;
2041 struct drm_i915_private *dev_priv = dev->dev_private;
2042 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002043 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2044 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002045 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002046 int reg;
2047 u32 val;
2048
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002049 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
2050
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002051 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002052 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002053 assert_sprites_disabled(dev_priv, pipe);
2054
Paulo Zanoni681e5812012-12-06 11:12:38 -02002055 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002056 pch_transcoder = TRANSCODER_A;
2057 else
2058 pch_transcoder = pipe;
2059
Jesse Barnesb24e7172011-01-04 15:09:30 -08002060 /*
2061 * A pipe without a PLL won't actually be able to drive bits from
2062 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2063 * need the check.
2064 */
Imre Deak50360402015-01-16 00:55:16 -08002065 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002066 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002067 assert_dsi_pll_enabled(dev_priv);
2068 else
2069 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002070 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002071 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002072 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002073 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002074 assert_fdi_tx_pll_enabled(dev_priv,
2075 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002076 }
2077 /* FIXME: assert CPU port conditions for SNB+ */
2078 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002079
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002080 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002081 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002082 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002083 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2084 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002085 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002086 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002087
2088 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002089 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002090}
2091
2092/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002093 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002094 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002095 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002096 * Disable the pipe of @crtc, making sure that various hardware
2097 * specific requirements are met, if applicable, e.g. plane
2098 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002099 *
2100 * Will wait until the pipe has shut down before returning.
2101 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002102static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002103{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002104 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002105 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002106 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002107 int reg;
2108 u32 val;
2109
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002110 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2111
Jesse Barnesb24e7172011-01-04 15:09:30 -08002112 /*
2113 * Make sure planes won't keep trying to pump pixels to us,
2114 * or we might hang the display.
2115 */
2116 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002117 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002118 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002119
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002120 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002121 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002122 if ((val & PIPECONF_ENABLE) == 0)
2123 return;
2124
Ville Syrjälä67adc642014-08-15 01:21:57 +03002125 /*
2126 * Double wide has implications for planes
2127 * so best keep it disabled when not needed.
2128 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002129 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002130 val &= ~PIPECONF_DOUBLE_WIDE;
2131
2132 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002133 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2134 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002135 val &= ~PIPECONF_ENABLE;
2136
2137 I915_WRITE(reg, val);
2138 if ((val & PIPECONF_ENABLE) == 0)
2139 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002140}
2141
Chris Wilson693db182013-03-05 14:52:39 +00002142static bool need_vtd_wa(struct drm_device *dev)
2143{
2144#ifdef CONFIG_INTEL_IOMMU
2145 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2146 return true;
2147#endif
2148 return false;
2149}
2150
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002151unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002152intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2153 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002154{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002155 unsigned int tile_height;
2156 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002157
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002158 switch (fb_format_modifier) {
2159 case DRM_FORMAT_MOD_NONE:
2160 tile_height = 1;
2161 break;
2162 case I915_FORMAT_MOD_X_TILED:
2163 tile_height = IS_GEN2(dev) ? 16 : 8;
2164 break;
2165 case I915_FORMAT_MOD_Y_TILED:
2166 tile_height = 32;
2167 break;
2168 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002169 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2170 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002171 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002172 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002173 tile_height = 64;
2174 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002175 case 2:
2176 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002177 tile_height = 32;
2178 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002179 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002180 tile_height = 16;
2181 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002182 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002183 WARN_ONCE(1,
2184 "128-bit pixels are not supported for display!");
2185 tile_height = 16;
2186 break;
2187 }
2188 break;
2189 default:
2190 MISSING_CASE(fb_format_modifier);
2191 tile_height = 1;
2192 break;
2193 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002194
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002195 return tile_height;
2196}
2197
2198unsigned int
2199intel_fb_align_height(struct drm_device *dev, unsigned int height,
2200 uint32_t pixel_format, uint64_t fb_format_modifier)
2201{
2202 return ALIGN(height, intel_tile_height(dev, pixel_format,
2203 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002204}
2205
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002206static int
2207intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2208 const struct drm_plane_state *plane_state)
2209{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002210 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002211 unsigned int tile_height, tile_pitch;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002212
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002213 *view = i915_ggtt_view_normal;
2214
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002215 if (!plane_state)
2216 return 0;
2217
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002218 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002219 return 0;
2220
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002221 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002222
2223 info->height = fb->height;
2224 info->pixel_format = fb->pixel_format;
2225 info->pitch = fb->pitches[0];
2226 info->fb_modifier = fb->modifier[0];
2227
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002228 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
2229 fb->modifier[0]);
2230 tile_pitch = PAGE_SIZE / tile_height;
2231 info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2232 info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
2233 info->size = info->width_pages * info->height_pages * PAGE_SIZE;
2234
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002235 return 0;
2236}
2237
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002238static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2239{
2240 if (INTEL_INFO(dev_priv)->gen >= 9)
2241 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002242 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2243 IS_VALLEYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002244 return 128 * 1024;
2245 else if (INTEL_INFO(dev_priv)->gen >= 4)
2246 return 4 * 1024;
2247 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002248 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002249}
2250
Chris Wilson127bd2a2010-07-23 23:32:05 +01002251int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002252intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2253 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002254 const struct drm_plane_state *plane_state,
John Harrison91af1272015-06-18 13:14:56 +01002255 struct intel_engine_cs *pipelined,
2256 struct drm_i915_gem_request **pipelined_request)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002257{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002258 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002259 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002260 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002261 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002262 u32 alignment;
2263 int ret;
2264
Matt Roperebcdd392014-07-09 16:22:11 -07002265 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2266
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002267 switch (fb->modifier[0]) {
2268 case DRM_FORMAT_MOD_NONE:
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002269 alignment = intel_linear_alignment(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002270 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002271 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002272 if (INTEL_INFO(dev)->gen >= 9)
2273 alignment = 256 * 1024;
2274 else {
2275 /* pin() will align the object as required by fence */
2276 alignment = 0;
2277 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002278 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002279 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002280 case I915_FORMAT_MOD_Yf_TILED:
2281 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2282 "Y tiling bo slipped through, driver bug!\n"))
2283 return -EINVAL;
2284 alignment = 1 * 1024 * 1024;
2285 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002286 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002287 MISSING_CASE(fb->modifier[0]);
2288 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002289 }
2290
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002291 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2292 if (ret)
2293 return ret;
2294
Chris Wilson693db182013-03-05 14:52:39 +00002295 /* Note that the w/a also requires 64 PTE of padding following the
2296 * bo. We currently fill all unused PTE with the shadow page and so
2297 * we should always have valid PTE following the scanout preventing
2298 * the VT-d warning.
2299 */
2300 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2301 alignment = 256 * 1024;
2302
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002303 /*
2304 * Global gtt pte registers are special registers which actually forward
2305 * writes to a chunk of system memory. Which means that there is no risk
2306 * that the register values disappear as soon as we call
2307 * intel_runtime_pm_put(), so it is correct to wrap only the
2308 * pin/unpin/fence and not more.
2309 */
2310 intel_runtime_pm_get(dev_priv);
2311
Chris Wilsonce453d82011-02-21 14:43:56 +00002312 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002313 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
John Harrison91af1272015-06-18 13:14:56 +01002314 pipelined_request, &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002315 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002316 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002317
2318 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2319 * fence, whereas 965+ only requires a fence if using
2320 * framebuffer compression. For simplicity, we always install
2321 * a fence as the cost is not that onerous.
2322 */
Chris Wilson06d98132012-04-17 15:31:24 +01002323 ret = i915_gem_object_get_fence(obj);
Maarten Lankhorst842315e2015-08-05 12:37:11 +02002324 if (ret == -EDEADLK) {
2325 /*
2326 * -EDEADLK means there are no free fences
2327 * no pending flips.
2328 *
2329 * This is propagated to atomic, but it uses
2330 * -EDEADLK to force a locking recovery, so
2331 * change the returned error to -EBUSY.
2332 */
2333 ret = -EBUSY;
2334 goto err_unpin;
2335 } else if (ret)
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002336 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002337
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002338 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002339
Chris Wilsonce453d82011-02-21 14:43:56 +00002340 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002341 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002342 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002343
2344err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002345 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002346err_interruptible:
2347 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002348 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002349 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002350}
2351
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002352static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2353 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002354{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002355 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002356 struct i915_ggtt_view view;
2357 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002358
Matt Roperebcdd392014-07-09 16:22:11 -07002359 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2360
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002361 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2362 WARN_ONCE(ret, "Couldn't get view from plane state!");
2363
Chris Wilson1690e1e2011-12-14 13:57:08 +01002364 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002365 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002366}
2367
Daniel Vetterc2c75132012-07-05 12:17:30 +02002368/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2369 * is assumed to be a power-of-two. */
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002370unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2371 int *x, int *y,
Chris Wilsonbc752862013-02-21 20:04:31 +00002372 unsigned int tiling_mode,
2373 unsigned int cpp,
2374 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002375{
Chris Wilsonbc752862013-02-21 20:04:31 +00002376 if (tiling_mode != I915_TILING_NONE) {
2377 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002378
Chris Wilsonbc752862013-02-21 20:04:31 +00002379 tile_rows = *y / 8;
2380 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002381
Chris Wilsonbc752862013-02-21 20:04:31 +00002382 tiles = *x / (512/cpp);
2383 *x %= 512/cpp;
2384
2385 return tile_rows * pitch * 8 + tiles * 4096;
2386 } else {
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002387 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
Chris Wilsonbc752862013-02-21 20:04:31 +00002388 unsigned int offset;
2389
2390 offset = *y * pitch + *x * cpp;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002391 *y = (offset & alignment) / pitch;
2392 *x = ((offset & alignment) - *y * pitch) / cpp;
2393 return offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002394 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002395}
2396
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002397static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002398{
2399 switch (format) {
2400 case DISPPLANE_8BPP:
2401 return DRM_FORMAT_C8;
2402 case DISPPLANE_BGRX555:
2403 return DRM_FORMAT_XRGB1555;
2404 case DISPPLANE_BGRX565:
2405 return DRM_FORMAT_RGB565;
2406 default:
2407 case DISPPLANE_BGRX888:
2408 return DRM_FORMAT_XRGB8888;
2409 case DISPPLANE_RGBX888:
2410 return DRM_FORMAT_XBGR8888;
2411 case DISPPLANE_BGRX101010:
2412 return DRM_FORMAT_XRGB2101010;
2413 case DISPPLANE_RGBX101010:
2414 return DRM_FORMAT_XBGR2101010;
2415 }
2416}
2417
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002418static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2419{
2420 switch (format) {
2421 case PLANE_CTL_FORMAT_RGB_565:
2422 return DRM_FORMAT_RGB565;
2423 default:
2424 case PLANE_CTL_FORMAT_XRGB_8888:
2425 if (rgb_order) {
2426 if (alpha)
2427 return DRM_FORMAT_ABGR8888;
2428 else
2429 return DRM_FORMAT_XBGR8888;
2430 } else {
2431 if (alpha)
2432 return DRM_FORMAT_ARGB8888;
2433 else
2434 return DRM_FORMAT_XRGB8888;
2435 }
2436 case PLANE_CTL_FORMAT_XRGB_2101010:
2437 if (rgb_order)
2438 return DRM_FORMAT_XBGR2101010;
2439 else
2440 return DRM_FORMAT_XRGB2101010;
2441 }
2442}
2443
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002444static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002445intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2446 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002447{
2448 struct drm_device *dev = crtc->base.dev;
2449 struct drm_i915_gem_object *obj = NULL;
2450 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002451 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002452 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2453 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2454 PAGE_SIZE);
2455
2456 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002457
Chris Wilsonff2652e2014-03-10 08:07:02 +00002458 if (plane_config->size == 0)
2459 return false;
2460
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002461 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2462 base_aligned,
2463 base_aligned,
2464 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002465 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002466 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002467
Damien Lespiau49af4492015-01-20 12:51:44 +00002468 obj->tiling_mode = plane_config->tiling;
2469 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002470 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002471
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002472 mode_cmd.pixel_format = fb->pixel_format;
2473 mode_cmd.width = fb->width;
2474 mode_cmd.height = fb->height;
2475 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002476 mode_cmd.modifier[0] = fb->modifier[0];
2477 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002478
2479 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002480 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002481 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002482 DRM_DEBUG_KMS("intel fb init failed\n");
2483 goto out_unref_obj;
2484 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002485 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002486
Daniel Vetterf6936e22015-03-26 12:17:05 +01002487 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002488 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002489
2490out_unref_obj:
2491 drm_gem_object_unreference(&obj->base);
2492 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002493 return false;
2494}
2495
Matt Roperafd65eb2015-02-03 13:10:04 -08002496/* Update plane->state->fb to match plane->fb after driver-internal updates */
2497static void
2498update_state_fb(struct drm_plane *plane)
2499{
2500 if (plane->fb == plane->state->fb)
2501 return;
2502
2503 if (plane->state->fb)
2504 drm_framebuffer_unreference(plane->state->fb);
2505 plane->state->fb = plane->fb;
2506 if (plane->state->fb)
2507 drm_framebuffer_reference(plane->state->fb);
2508}
2509
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002510static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002511intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2512 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002513{
2514 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002515 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002516 struct drm_crtc *c;
2517 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002518 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002519 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002520 struct drm_plane_state *plane_state = primary->state;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002521 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002522
Damien Lespiau2d140302015-02-05 17:22:18 +00002523 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002524 return;
2525
Daniel Vetterf6936e22015-03-26 12:17:05 +01002526 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002527 fb = &plane_config->fb->base;
2528 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002529 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002530
Damien Lespiau2d140302015-02-05 17:22:18 +00002531 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002532
2533 /*
2534 * Failed to alloc the obj, check to see if we should share
2535 * an fb with another CRTC instead
2536 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002537 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002538 i = to_intel_crtc(c);
2539
2540 if (c == &intel_crtc->base)
2541 continue;
2542
Matt Roper2ff8fde2014-07-08 07:50:07 -07002543 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002544 continue;
2545
Daniel Vetter88595ac2015-03-26 12:42:24 +01002546 fb = c->primary->fb;
2547 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002548 continue;
2549
Daniel Vetter88595ac2015-03-26 12:42:24 +01002550 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002551 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002552 drm_framebuffer_reference(fb);
2553 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002554 }
2555 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002556
2557 return;
2558
2559valid_fb:
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002560 plane_state->src_x = plane_state->src_y = 0;
2561 plane_state->src_w = fb->width << 16;
2562 plane_state->src_h = fb->height << 16;
2563
2564 plane_state->crtc_x = plane_state->src_y = 0;
2565 plane_state->crtc_w = fb->width;
2566 plane_state->crtc_h = fb->height;
2567
Daniel Vetter88595ac2015-03-26 12:42:24 +01002568 obj = intel_fb_obj(fb);
2569 if (obj->tiling_mode != I915_TILING_NONE)
2570 dev_priv->preserve_bios_swizzle = true;
2571
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002572 drm_framebuffer_reference(fb);
2573 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002574 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002575 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002576 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002577}
2578
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002579static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2580 struct drm_framebuffer *fb,
2581 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002582{
2583 struct drm_device *dev = crtc->dev;
2584 struct drm_i915_private *dev_priv = dev->dev_private;
2585 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002586 struct drm_plane *primary = crtc->primary;
2587 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002588 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002589 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002590 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002591 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002592 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302593 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002594
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002595 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002596 I915_WRITE(reg, 0);
2597 if (INTEL_INFO(dev)->gen >= 4)
2598 I915_WRITE(DSPSURF(plane), 0);
2599 else
2600 I915_WRITE(DSPADDR(plane), 0);
2601 POSTING_READ(reg);
2602 return;
2603 }
2604
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002605 obj = intel_fb_obj(fb);
2606 if (WARN_ON(obj == NULL))
2607 return;
2608
2609 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2610
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002611 dspcntr = DISPPLANE_GAMMA_ENABLE;
2612
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002613 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002614
2615 if (INTEL_INFO(dev)->gen < 4) {
2616 if (intel_crtc->pipe == PIPE_B)
2617 dspcntr |= DISPPLANE_SEL_PIPE_B;
2618
2619 /* pipesrc and dspsize control the size that is scaled from,
2620 * which should always be the user's requested size.
2621 */
2622 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002623 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2624 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002625 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002626 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2627 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002628 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2629 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002630 I915_WRITE(PRIMPOS(plane), 0);
2631 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002632 }
2633
Ville Syrjälä57779d02012-10-31 17:50:14 +02002634 switch (fb->pixel_format) {
2635 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002636 dspcntr |= DISPPLANE_8BPP;
2637 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002638 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002639 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002640 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002641 case DRM_FORMAT_RGB565:
2642 dspcntr |= DISPPLANE_BGRX565;
2643 break;
2644 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002645 dspcntr |= DISPPLANE_BGRX888;
2646 break;
2647 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002648 dspcntr |= DISPPLANE_RGBX888;
2649 break;
2650 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002651 dspcntr |= DISPPLANE_BGRX101010;
2652 break;
2653 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002654 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002655 break;
2656 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002657 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002658 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002659
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002660 if (INTEL_INFO(dev)->gen >= 4 &&
2661 obj->tiling_mode != I915_TILING_NONE)
2662 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002663
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002664 if (IS_G4X(dev))
2665 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2666
Ville Syrjäläb98971272014-08-27 16:51:22 +03002667 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002668
Daniel Vetterc2c75132012-07-05 12:17:30 +02002669 if (INTEL_INFO(dev)->gen >= 4) {
2670 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002671 intel_gen4_compute_page_offset(dev_priv,
2672 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002673 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002674 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002675 linear_offset -= intel_crtc->dspaddr_offset;
2676 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002677 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002678 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002679
Matt Roper8e7d6882015-01-21 16:35:41 -08002680 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302681 dspcntr |= DISPPLANE_ROTATE_180;
2682
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002683 x += (intel_crtc->config->pipe_src_w - 1);
2684 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302685
2686 /* Finding the last pixel of the last line of the display
2687 data and adding to linear_offset*/
2688 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002689 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2690 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302691 }
2692
2693 I915_WRITE(reg, dspcntr);
2694
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002695 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002696 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002697 I915_WRITE(DSPSURF(plane),
2698 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002699 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002700 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002701 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002702 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002703 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002704}
2705
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002706static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2707 struct drm_framebuffer *fb,
2708 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002709{
2710 struct drm_device *dev = crtc->dev;
2711 struct drm_i915_private *dev_priv = dev->dev_private;
2712 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002713 struct drm_plane *primary = crtc->primary;
2714 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002715 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002716 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002717 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002718 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002719 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302720 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002721
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002722 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002723 I915_WRITE(reg, 0);
2724 I915_WRITE(DSPSURF(plane), 0);
2725 POSTING_READ(reg);
2726 return;
2727 }
2728
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002729 obj = intel_fb_obj(fb);
2730 if (WARN_ON(obj == NULL))
2731 return;
2732
2733 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2734
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002735 dspcntr = DISPPLANE_GAMMA_ENABLE;
2736
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002737 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002738
2739 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2740 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2741
Ville Syrjälä57779d02012-10-31 17:50:14 +02002742 switch (fb->pixel_format) {
2743 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002744 dspcntr |= DISPPLANE_8BPP;
2745 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002746 case DRM_FORMAT_RGB565:
2747 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002748 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002749 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002750 dspcntr |= DISPPLANE_BGRX888;
2751 break;
2752 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002753 dspcntr |= DISPPLANE_RGBX888;
2754 break;
2755 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002756 dspcntr |= DISPPLANE_BGRX101010;
2757 break;
2758 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002759 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002760 break;
2761 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002762 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002763 }
2764
2765 if (obj->tiling_mode != I915_TILING_NONE)
2766 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002767
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002768 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002769 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002770
Ville Syrjäläb98971272014-08-27 16:51:22 +03002771 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002772 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002773 intel_gen4_compute_page_offset(dev_priv,
2774 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002775 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002776 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002777 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002778 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302779 dspcntr |= DISPPLANE_ROTATE_180;
2780
2781 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002782 x += (intel_crtc->config->pipe_src_w - 1);
2783 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302784
2785 /* Finding the last pixel of the last line of the display
2786 data and adding to linear_offset*/
2787 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002788 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2789 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302790 }
2791 }
2792
2793 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002794
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002795 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002796 I915_WRITE(DSPSURF(plane),
2797 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002798 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002799 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2800 } else {
2801 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2802 I915_WRITE(DSPLINOFF(plane), linear_offset);
2803 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002804 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002805}
2806
Damien Lespiaub3218032015-02-27 11:15:18 +00002807u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2808 uint32_t pixel_format)
2809{
2810 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2811
2812 /*
2813 * The stride is either expressed as a multiple of 64 bytes
2814 * chunks for linear buffers or in number of tiles for tiled
2815 * buffers.
2816 */
2817 switch (fb_modifier) {
2818 case DRM_FORMAT_MOD_NONE:
2819 return 64;
2820 case I915_FORMAT_MOD_X_TILED:
2821 if (INTEL_INFO(dev)->gen == 2)
2822 return 128;
2823 return 512;
2824 case I915_FORMAT_MOD_Y_TILED:
2825 /* No need to check for old gens and Y tiling since this is
2826 * about the display engine and those will be blocked before
2827 * we get here.
2828 */
2829 return 128;
2830 case I915_FORMAT_MOD_Yf_TILED:
2831 if (bits_per_pixel == 8)
2832 return 64;
2833 else
2834 return 128;
2835 default:
2836 MISSING_CASE(fb_modifier);
2837 return 64;
2838 }
2839}
2840
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002841unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2842 struct drm_i915_gem_object *obj)
2843{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002844 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002845
2846 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002847 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002848
2849 return i915_gem_obj_ggtt_offset_view(obj, view);
2850}
2851
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002852static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2853{
2854 struct drm_device *dev = intel_crtc->base.dev;
2855 struct drm_i915_private *dev_priv = dev->dev_private;
2856
2857 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2858 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2859 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002860}
2861
Chandra Kondurua1b22782015-04-07 15:28:45 -07002862/*
2863 * This function detaches (aka. unbinds) unused scalers in hardware
2864 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002865static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002866{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002867 struct intel_crtc_scaler_state *scaler_state;
2868 int i;
2869
Chandra Kondurua1b22782015-04-07 15:28:45 -07002870 scaler_state = &intel_crtc->config->scaler_state;
2871
2872 /* loop through and disable scalers that aren't in use */
2873 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002874 if (!scaler_state->scalers[i].in_use)
2875 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002876 }
2877}
2878
Chandra Konduru6156a452015-04-27 13:48:39 -07002879u32 skl_plane_ctl_format(uint32_t pixel_format)
2880{
Chandra Konduru6156a452015-04-27 13:48:39 -07002881 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002882 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002883 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002884 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002885 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002886 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002887 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002888 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002889 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002890 /*
2891 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2892 * to be already pre-multiplied. We need to add a knob (or a different
2893 * DRM_FORMAT) for user-space to configure that.
2894 */
2895 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002896 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002897 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002898 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002899 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002900 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002901 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002902 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002903 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002904 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002905 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002906 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002907 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002908 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002909 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002910 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002911 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002912 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002913 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002914 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002915 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002916
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002917 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002918}
2919
2920u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2921{
Chandra Konduru6156a452015-04-27 13:48:39 -07002922 switch (fb_modifier) {
2923 case DRM_FORMAT_MOD_NONE:
2924 break;
2925 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002926 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002927 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002928 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002930 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002931 default:
2932 MISSING_CASE(fb_modifier);
2933 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002934
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002935 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002936}
2937
2938u32 skl_plane_ctl_rotation(unsigned int rotation)
2939{
Chandra Konduru6156a452015-04-27 13:48:39 -07002940 switch (rotation) {
2941 case BIT(DRM_ROTATE_0):
2942 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302943 /*
2944 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2945 * while i915 HW rotation is clockwise, thats why this swapping.
2946 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002947 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302948 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002949 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002950 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002951 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302952 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002953 default:
2954 MISSING_CASE(rotation);
2955 }
2956
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958}
2959
Damien Lespiau70d21f02013-07-03 21:06:04 +01002960static void skylake_update_primary_plane(struct drm_crtc *crtc,
2961 struct drm_framebuffer *fb,
2962 int x, int y)
2963{
2964 struct drm_device *dev = crtc->dev;
2965 struct drm_i915_private *dev_priv = dev->dev_private;
2966 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002967 struct drm_plane *plane = crtc->primary;
2968 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002969 struct drm_i915_gem_object *obj;
2970 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302971 u32 plane_ctl, stride_div, stride;
2972 u32 tile_height, plane_offset, plane_size;
2973 unsigned int rotation;
2974 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002975 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07002976 struct intel_crtc_state *crtc_state = intel_crtc->config;
2977 struct intel_plane_state *plane_state;
2978 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
2979 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
2980 int scaler_id = -1;
2981
Chandra Konduru6156a452015-04-27 13:48:39 -07002982 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002983
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002984 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01002985 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2986 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2987 POSTING_READ(PLANE_CTL(pipe, 0));
2988 return;
2989 }
2990
2991 plane_ctl = PLANE_CTL_ENABLE |
2992 PLANE_CTL_PIPE_GAMMA_ENABLE |
2993 PLANE_CTL_PIPE_CSC_ENABLE;
2994
Chandra Konduru6156a452015-04-27 13:48:39 -07002995 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
2996 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002997 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302998
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302999 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003000 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003001
Damien Lespiaub3218032015-02-27 11:15:18 +00003002 obj = intel_fb_obj(fb);
3003 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3004 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303005 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3006
Chandra Konduru6156a452015-04-27 13:48:39 -07003007 /*
3008 * FIXME: intel_plane_state->src, dst aren't set when transitional
3009 * update_plane helpers are called from legacy paths.
3010 * Once full atomic crtc is available, below check can be avoided.
3011 */
3012 if (drm_rect_width(&plane_state->src)) {
3013 scaler_id = plane_state->scaler_id;
3014 src_x = plane_state->src.x1 >> 16;
3015 src_y = plane_state->src.y1 >> 16;
3016 src_w = drm_rect_width(&plane_state->src) >> 16;
3017 src_h = drm_rect_height(&plane_state->src) >> 16;
3018 dst_x = plane_state->dst.x1;
3019 dst_y = plane_state->dst.y1;
3020 dst_w = drm_rect_width(&plane_state->dst);
3021 dst_h = drm_rect_height(&plane_state->dst);
3022
3023 WARN_ON(x != src_x || y != src_y);
3024 } else {
3025 src_w = intel_crtc->config->pipe_src_w;
3026 src_h = intel_crtc->config->pipe_src_h;
3027 }
3028
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303029 if (intel_rotation_90_or_270(rotation)) {
3030 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003031 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303032 fb->modifier[0]);
3033 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003034 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303035 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003036 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303037 } else {
3038 stride = fb->pitches[0] / stride_div;
3039 x_offset = x;
3040 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003041 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303042 }
3043 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003044
Damien Lespiau70d21f02013-07-03 21:06:04 +01003045 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303046 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3047 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3048 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003049
3050 if (scaler_id >= 0) {
3051 uint32_t ps_ctrl = 0;
3052
3053 WARN_ON(!dst_w || !dst_h);
3054 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3055 crtc_state->scaler_state.scalers[scaler_id].mode;
3056 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3057 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3058 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3059 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3060 I915_WRITE(PLANE_POS(pipe, 0), 0);
3061 } else {
3062 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3063 }
3064
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003065 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003066
3067 POSTING_READ(PLANE_SURF(pipe, 0));
3068}
3069
Jesse Barnes17638cd2011-06-24 12:19:23 -07003070/* Assume fb object is pinned & idle & fenced and just update base pointers */
3071static int
3072intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3073 int x, int y, enum mode_set_atomic state)
3074{
3075 struct drm_device *dev = crtc->dev;
3076 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003077
Paulo Zanoniff2a3112015-07-07 15:26:03 -03003078 if (dev_priv->fbc.disable_fbc)
Paulo Zanoni7733b492015-07-07 15:26:04 -03003079 dev_priv->fbc.disable_fbc(dev_priv);
Jesse Barnes81255562010-08-02 12:07:50 -07003080
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003081 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3082
3083 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003084}
3085
Ville Syrjälä75147472014-11-24 18:28:11 +02003086static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003087{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003088 struct drm_crtc *crtc;
3089
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003090 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003091 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3092 enum plane plane = intel_crtc->plane;
3093
3094 intel_prepare_page_flip(dev, plane);
3095 intel_finish_page_flip_plane(dev, plane);
3096 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003097}
3098
3099static void intel_update_primary_planes(struct drm_device *dev)
3100{
3101 struct drm_i915_private *dev_priv = dev->dev_private;
3102 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003103
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003104 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003105 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3106
Rob Clark51fd3712013-11-19 12:10:12 -05003107 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003108 /*
3109 * FIXME: Once we have proper support for primary planes (and
3110 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003111 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003112 */
Matt Roperf4510a22014-04-01 15:22:40 -07003113 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003114 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003115 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003116 crtc->x,
3117 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003118 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003119 }
3120}
3121
Ville Syrjälä75147472014-11-24 18:28:11 +02003122void intel_prepare_reset(struct drm_device *dev)
3123{
3124 /* no reset support for gen2 */
3125 if (IS_GEN2(dev))
3126 return;
3127
3128 /* reset doesn't touch the display */
3129 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3130 return;
3131
3132 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003133 /*
3134 * Disabling the crtcs gracefully seems nicer. Also the
3135 * g33 docs say we should at least disable all the planes.
3136 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003137 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003138}
3139
3140void intel_finish_reset(struct drm_device *dev)
3141{
3142 struct drm_i915_private *dev_priv = to_i915(dev);
3143
3144 /*
3145 * Flips in the rings will be nuked by the reset,
3146 * so complete all pending flips so that user space
3147 * will get its events and not get stuck.
3148 */
3149 intel_complete_page_flips(dev);
3150
3151 /* no reset support for gen2 */
3152 if (IS_GEN2(dev))
3153 return;
3154
3155 /* reset doesn't touch the display */
3156 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3157 /*
3158 * Flips in the rings have been nuked by the reset,
3159 * so update the base address of all primary
3160 * planes to the the last fb to make sure we're
3161 * showing the correct fb after a reset.
3162 */
3163 intel_update_primary_planes(dev);
3164 return;
3165 }
3166
3167 /*
3168 * The display has been reset as well,
3169 * so need a full re-initialization.
3170 */
3171 intel_runtime_pm_disable_interrupts(dev_priv);
3172 intel_runtime_pm_enable_interrupts(dev_priv);
3173
3174 intel_modeset_init_hw(dev);
3175
3176 spin_lock_irq(&dev_priv->irq_lock);
3177 if (dev_priv->display.hpd_irq_setup)
3178 dev_priv->display.hpd_irq_setup(dev);
3179 spin_unlock_irq(&dev_priv->irq_lock);
3180
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003181 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003182
3183 intel_hpd_init(dev_priv);
3184
3185 drm_modeset_unlock_all(dev);
3186}
3187
Chris Wilson2e2f3512015-04-27 13:41:14 +01003188static void
Chris Wilson14667a42012-04-03 17:58:35 +01003189intel_finish_fb(struct drm_framebuffer *old_fb)
3190{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003191 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003192 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003193 bool was_interruptible = dev_priv->mm.interruptible;
3194 int ret;
3195
Chris Wilson14667a42012-04-03 17:58:35 +01003196 /* Big Hammer, we also need to ensure that any pending
3197 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3198 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003199 * framebuffer. Note that we rely on userspace rendering
3200 * into the buffer attached to the pipe they are waiting
3201 * on. If not, userspace generates a GPU hang with IPEHR
3202 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003203 *
3204 * This should only fail upon a hung GPU, in which case we
3205 * can safely continue.
3206 */
3207 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003208 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003209 dev_priv->mm.interruptible = was_interruptible;
3210
Chris Wilson2e2f3512015-04-27 13:41:14 +01003211 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003212}
3213
Chris Wilson7d5e3792014-03-04 13:15:08 +00003214static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3215{
3216 struct drm_device *dev = crtc->dev;
3217 struct drm_i915_private *dev_priv = dev->dev_private;
3218 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003219 bool pending;
3220
3221 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3222 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3223 return false;
3224
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003225 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003226 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003227 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003228
3229 return pending;
3230}
3231
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003232static void intel_update_pipe_size(struct intel_crtc *crtc)
3233{
3234 struct drm_device *dev = crtc->base.dev;
3235 struct drm_i915_private *dev_priv = dev->dev_private;
3236 const struct drm_display_mode *adjusted_mode;
3237
3238 if (!i915.fastboot)
3239 return;
3240
3241 /*
3242 * Update pipe size and adjust fitter if needed: the reason for this is
3243 * that in compute_mode_changes we check the native mode (not the pfit
3244 * mode) to see if we can flip rather than do a full mode set. In the
3245 * fastboot case, we'll flip, but if we don't update the pipesrc and
3246 * pfit state, we'll end up with a big fb scanned out into the wrong
3247 * sized surface.
3248 *
3249 * To fix this properly, we need to hoist the checks up into
3250 * compute_mode_changes (or above), check the actual pfit state and
3251 * whether the platform allows pfit disable with pipe active, and only
3252 * then update the pipesrc and pfit state, even on the flip path.
3253 */
3254
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003255 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003256
3257 I915_WRITE(PIPESRC(crtc->pipe),
3258 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3259 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003260 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003261 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3262 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003263 I915_WRITE(PF_CTL(crtc->pipe), 0);
3264 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3265 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3266 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003267 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3268 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003269}
3270
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003271static void intel_fdi_normal_train(struct drm_crtc *crtc)
3272{
3273 struct drm_device *dev = crtc->dev;
3274 struct drm_i915_private *dev_priv = dev->dev_private;
3275 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3276 int pipe = intel_crtc->pipe;
3277 u32 reg, temp;
3278
3279 /* enable normal train */
3280 reg = FDI_TX_CTL(pipe);
3281 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003282 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003283 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3284 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003285 } else {
3286 temp &= ~FDI_LINK_TRAIN_NONE;
3287 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003288 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003289 I915_WRITE(reg, temp);
3290
3291 reg = FDI_RX_CTL(pipe);
3292 temp = I915_READ(reg);
3293 if (HAS_PCH_CPT(dev)) {
3294 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3295 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3296 } else {
3297 temp &= ~FDI_LINK_TRAIN_NONE;
3298 temp |= FDI_LINK_TRAIN_NONE;
3299 }
3300 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3301
3302 /* wait one idle pattern time */
3303 POSTING_READ(reg);
3304 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003305
3306 /* IVB wants error correction enabled */
3307 if (IS_IVYBRIDGE(dev))
3308 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3309 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003310}
3311
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003312/* The FDI link training functions for ILK/Ibexpeak. */
3313static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3314{
3315 struct drm_device *dev = crtc->dev;
3316 struct drm_i915_private *dev_priv = dev->dev_private;
3317 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3318 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003319 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003320
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003321 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003322 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003323
Adam Jacksone1a44742010-06-25 15:32:14 -04003324 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3325 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003326 reg = FDI_RX_IMR(pipe);
3327 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003328 temp &= ~FDI_RX_SYMBOL_LOCK;
3329 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003330 I915_WRITE(reg, temp);
3331 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003332 udelay(150);
3333
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003334 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003335 reg = FDI_TX_CTL(pipe);
3336 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003337 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003338 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003339 temp &= ~FDI_LINK_TRAIN_NONE;
3340 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003341 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003342
Chris Wilson5eddb702010-09-11 13:48:45 +01003343 reg = FDI_RX_CTL(pipe);
3344 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003345 temp &= ~FDI_LINK_TRAIN_NONE;
3346 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003347 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3348
3349 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003350 udelay(150);
3351
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003352 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003353 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3354 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3355 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003356
Chris Wilson5eddb702010-09-11 13:48:45 +01003357 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003358 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003359 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003360 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3361
3362 if ((temp & FDI_RX_BIT_LOCK)) {
3363 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003364 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003365 break;
3366 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003367 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003368 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003369 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003370
3371 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003372 reg = FDI_TX_CTL(pipe);
3373 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003374 temp &= ~FDI_LINK_TRAIN_NONE;
3375 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003376 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003377
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 reg = FDI_RX_CTL(pipe);
3379 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003380 temp &= ~FDI_LINK_TRAIN_NONE;
3381 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003382 I915_WRITE(reg, temp);
3383
3384 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003385 udelay(150);
3386
Chris Wilson5eddb702010-09-11 13:48:45 +01003387 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003388 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003389 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003390 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3391
3392 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003393 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003394 DRM_DEBUG_KMS("FDI train 2 done.\n");
3395 break;
3396 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003397 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003398 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003399 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003400
3401 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003402
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003403}
3404
Akshay Joshi0206e352011-08-16 15:34:10 -04003405static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003406 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3407 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3408 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3409 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3410};
3411
3412/* The FDI link training functions for SNB/Cougarpoint. */
3413static void gen6_fdi_link_train(struct drm_crtc *crtc)
3414{
3415 struct drm_device *dev = crtc->dev;
3416 struct drm_i915_private *dev_priv = dev->dev_private;
3417 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3418 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003419 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003420
Adam Jacksone1a44742010-06-25 15:32:14 -04003421 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3422 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003423 reg = FDI_RX_IMR(pipe);
3424 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003425 temp &= ~FDI_RX_SYMBOL_LOCK;
3426 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003427 I915_WRITE(reg, temp);
3428
3429 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003430 udelay(150);
3431
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003432 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003433 reg = FDI_TX_CTL(pipe);
3434 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003435 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003436 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003437 temp &= ~FDI_LINK_TRAIN_NONE;
3438 temp |= FDI_LINK_TRAIN_PATTERN_1;
3439 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3440 /* SNB-B */
3441 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003442 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003443
Daniel Vetterd74cf322012-10-26 10:58:13 +02003444 I915_WRITE(FDI_RX_MISC(pipe),
3445 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3446
Chris Wilson5eddb702010-09-11 13:48:45 +01003447 reg = FDI_RX_CTL(pipe);
3448 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003449 if (HAS_PCH_CPT(dev)) {
3450 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3451 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3452 } else {
3453 temp &= ~FDI_LINK_TRAIN_NONE;
3454 temp |= FDI_LINK_TRAIN_PATTERN_1;
3455 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003456 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3457
3458 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003459 udelay(150);
3460
Akshay Joshi0206e352011-08-16 15:34:10 -04003461 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003462 reg = FDI_TX_CTL(pipe);
3463 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003464 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3465 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003466 I915_WRITE(reg, temp);
3467
3468 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003469 udelay(500);
3470
Sean Paulfa37d392012-03-02 12:53:39 -05003471 for (retry = 0; retry < 5; retry++) {
3472 reg = FDI_RX_IIR(pipe);
3473 temp = I915_READ(reg);
3474 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3475 if (temp & FDI_RX_BIT_LOCK) {
3476 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3477 DRM_DEBUG_KMS("FDI train 1 done.\n");
3478 break;
3479 }
3480 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003481 }
Sean Paulfa37d392012-03-02 12:53:39 -05003482 if (retry < 5)
3483 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003484 }
3485 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003486 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003487
3488 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003489 reg = FDI_TX_CTL(pipe);
3490 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003491 temp &= ~FDI_LINK_TRAIN_NONE;
3492 temp |= FDI_LINK_TRAIN_PATTERN_2;
3493 if (IS_GEN6(dev)) {
3494 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3495 /* SNB-B */
3496 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3497 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003498 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499
Chris Wilson5eddb702010-09-11 13:48:45 +01003500 reg = FDI_RX_CTL(pipe);
3501 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003502 if (HAS_PCH_CPT(dev)) {
3503 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3504 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3505 } else {
3506 temp &= ~FDI_LINK_TRAIN_NONE;
3507 temp |= FDI_LINK_TRAIN_PATTERN_2;
3508 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003509 I915_WRITE(reg, temp);
3510
3511 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003512 udelay(150);
3513
Akshay Joshi0206e352011-08-16 15:34:10 -04003514 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003515 reg = FDI_TX_CTL(pipe);
3516 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003517 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3518 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003519 I915_WRITE(reg, temp);
3520
3521 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003522 udelay(500);
3523
Sean Paulfa37d392012-03-02 12:53:39 -05003524 for (retry = 0; retry < 5; retry++) {
3525 reg = FDI_RX_IIR(pipe);
3526 temp = I915_READ(reg);
3527 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3528 if (temp & FDI_RX_SYMBOL_LOCK) {
3529 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3530 DRM_DEBUG_KMS("FDI train 2 done.\n");
3531 break;
3532 }
3533 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003534 }
Sean Paulfa37d392012-03-02 12:53:39 -05003535 if (retry < 5)
3536 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003537 }
3538 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003539 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003540
3541 DRM_DEBUG_KMS("FDI train done.\n");
3542}
3543
Jesse Barnes357555c2011-04-28 15:09:55 -07003544/* Manual link training for Ivy Bridge A0 parts */
3545static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3546{
3547 struct drm_device *dev = crtc->dev;
3548 struct drm_i915_private *dev_priv = dev->dev_private;
3549 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3550 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003551 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003552
3553 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3554 for train result */
3555 reg = FDI_RX_IMR(pipe);
3556 temp = I915_READ(reg);
3557 temp &= ~FDI_RX_SYMBOL_LOCK;
3558 temp &= ~FDI_RX_BIT_LOCK;
3559 I915_WRITE(reg, temp);
3560
3561 POSTING_READ(reg);
3562 udelay(150);
3563
Daniel Vetter01a415f2012-10-27 15:58:40 +02003564 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3565 I915_READ(FDI_RX_IIR(pipe)));
3566
Jesse Barnes139ccd32013-08-19 11:04:55 -07003567 /* Try each vswing and preemphasis setting twice before moving on */
3568 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3569 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003570 reg = FDI_TX_CTL(pipe);
3571 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003572 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3573 temp &= ~FDI_TX_ENABLE;
3574 I915_WRITE(reg, temp);
3575
3576 reg = FDI_RX_CTL(pipe);
3577 temp = I915_READ(reg);
3578 temp &= ~FDI_LINK_TRAIN_AUTO;
3579 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3580 temp &= ~FDI_RX_ENABLE;
3581 I915_WRITE(reg, temp);
3582
3583 /* enable CPU FDI TX and PCH FDI RX */
3584 reg = FDI_TX_CTL(pipe);
3585 temp = I915_READ(reg);
3586 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003587 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003588 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003589 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003590 temp |= snb_b_fdi_train_param[j/2];
3591 temp |= FDI_COMPOSITE_SYNC;
3592 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3593
3594 I915_WRITE(FDI_RX_MISC(pipe),
3595 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3596
3597 reg = FDI_RX_CTL(pipe);
3598 temp = I915_READ(reg);
3599 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3600 temp |= FDI_COMPOSITE_SYNC;
3601 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3602
3603 POSTING_READ(reg);
3604 udelay(1); /* should be 0.5us */
3605
3606 for (i = 0; i < 4; i++) {
3607 reg = FDI_RX_IIR(pipe);
3608 temp = I915_READ(reg);
3609 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3610
3611 if (temp & FDI_RX_BIT_LOCK ||
3612 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3613 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3614 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3615 i);
3616 break;
3617 }
3618 udelay(1); /* should be 0.5us */
3619 }
3620 if (i == 4) {
3621 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3622 continue;
3623 }
3624
3625 /* Train 2 */
3626 reg = FDI_TX_CTL(pipe);
3627 temp = I915_READ(reg);
3628 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3629 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3630 I915_WRITE(reg, temp);
3631
3632 reg = FDI_RX_CTL(pipe);
3633 temp = I915_READ(reg);
3634 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3635 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003636 I915_WRITE(reg, temp);
3637
3638 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003639 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003640
Jesse Barnes139ccd32013-08-19 11:04:55 -07003641 for (i = 0; i < 4; i++) {
3642 reg = FDI_RX_IIR(pipe);
3643 temp = I915_READ(reg);
3644 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003645
Jesse Barnes139ccd32013-08-19 11:04:55 -07003646 if (temp & FDI_RX_SYMBOL_LOCK ||
3647 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3648 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3649 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3650 i);
3651 goto train_done;
3652 }
3653 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003654 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003655 if (i == 4)
3656 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003657 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003658
Jesse Barnes139ccd32013-08-19 11:04:55 -07003659train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003660 DRM_DEBUG_KMS("FDI train done.\n");
3661}
3662
Daniel Vetter88cefb62012-08-12 19:27:14 +02003663static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003664{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003665 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003666 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003667 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003668 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003669
Jesse Barnesc64e3112010-09-10 11:27:03 -07003670
Jesse Barnes0e23b992010-09-10 11:10:00 -07003671 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003672 reg = FDI_RX_CTL(pipe);
3673 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003674 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003675 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003676 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003677 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3678
3679 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003680 udelay(200);
3681
3682 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003683 temp = I915_READ(reg);
3684 I915_WRITE(reg, temp | FDI_PCDCLK);
3685
3686 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003687 udelay(200);
3688
Paulo Zanoni20749732012-11-23 15:30:38 -02003689 /* Enable CPU FDI TX PLL, always on for Ironlake */
3690 reg = FDI_TX_CTL(pipe);
3691 temp = I915_READ(reg);
3692 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3693 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003694
Paulo Zanoni20749732012-11-23 15:30:38 -02003695 POSTING_READ(reg);
3696 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003697 }
3698}
3699
Daniel Vetter88cefb62012-08-12 19:27:14 +02003700static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3701{
3702 struct drm_device *dev = intel_crtc->base.dev;
3703 struct drm_i915_private *dev_priv = dev->dev_private;
3704 int pipe = intel_crtc->pipe;
3705 u32 reg, temp;
3706
3707 /* Switch from PCDclk to Rawclk */
3708 reg = FDI_RX_CTL(pipe);
3709 temp = I915_READ(reg);
3710 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3711
3712 /* Disable CPU FDI TX PLL */
3713 reg = FDI_TX_CTL(pipe);
3714 temp = I915_READ(reg);
3715 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3716
3717 POSTING_READ(reg);
3718 udelay(100);
3719
3720 reg = FDI_RX_CTL(pipe);
3721 temp = I915_READ(reg);
3722 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3723
3724 /* Wait for the clocks to turn off. */
3725 POSTING_READ(reg);
3726 udelay(100);
3727}
3728
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003729static void ironlake_fdi_disable(struct drm_crtc *crtc)
3730{
3731 struct drm_device *dev = crtc->dev;
3732 struct drm_i915_private *dev_priv = dev->dev_private;
3733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3734 int pipe = intel_crtc->pipe;
3735 u32 reg, temp;
3736
3737 /* disable CPU FDI tx and PCH FDI rx */
3738 reg = FDI_TX_CTL(pipe);
3739 temp = I915_READ(reg);
3740 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3741 POSTING_READ(reg);
3742
3743 reg = FDI_RX_CTL(pipe);
3744 temp = I915_READ(reg);
3745 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003746 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003747 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3748
3749 POSTING_READ(reg);
3750 udelay(100);
3751
3752 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003753 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003754 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003755
3756 /* still set train pattern 1 */
3757 reg = FDI_TX_CTL(pipe);
3758 temp = I915_READ(reg);
3759 temp &= ~FDI_LINK_TRAIN_NONE;
3760 temp |= FDI_LINK_TRAIN_PATTERN_1;
3761 I915_WRITE(reg, temp);
3762
3763 reg = FDI_RX_CTL(pipe);
3764 temp = I915_READ(reg);
3765 if (HAS_PCH_CPT(dev)) {
3766 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3767 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3768 } else {
3769 temp &= ~FDI_LINK_TRAIN_NONE;
3770 temp |= FDI_LINK_TRAIN_PATTERN_1;
3771 }
3772 /* BPC in FDI rx is consistent with that in PIPECONF */
3773 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003774 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003775 I915_WRITE(reg, temp);
3776
3777 POSTING_READ(reg);
3778 udelay(100);
3779}
3780
Chris Wilson5dce5b932014-01-20 10:17:36 +00003781bool intel_has_pending_fb_unpin(struct drm_device *dev)
3782{
3783 struct intel_crtc *crtc;
3784
3785 /* Note that we don't need to be called with mode_config.lock here
3786 * as our list of CRTC objects is static for the lifetime of the
3787 * device and so cannot disappear as we iterate. Similarly, we can
3788 * happily treat the predicates as racy, atomic checks as userspace
3789 * cannot claim and pin a new fb without at least acquring the
3790 * struct_mutex and so serialising with us.
3791 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003792 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003793 if (atomic_read(&crtc->unpin_work_count) == 0)
3794 continue;
3795
3796 if (crtc->unpin_work)
3797 intel_wait_for_vblank(dev, crtc->pipe);
3798
3799 return true;
3800 }
3801
3802 return false;
3803}
3804
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003805static void page_flip_completed(struct intel_crtc *intel_crtc)
3806{
3807 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3808 struct intel_unpin_work *work = intel_crtc->unpin_work;
3809
3810 /* ensure that the unpin work is consistent wrt ->pending. */
3811 smp_rmb();
3812 intel_crtc->unpin_work = NULL;
3813
3814 if (work->event)
3815 drm_send_vblank_event(intel_crtc->base.dev,
3816 intel_crtc->pipe,
3817 work->event);
3818
3819 drm_crtc_vblank_put(&intel_crtc->base);
3820
3821 wake_up_all(&dev_priv->pending_flip_queue);
3822 queue_work(dev_priv->wq, &work->work);
3823
3824 trace_i915_flip_complete(intel_crtc->plane,
3825 work->pending_flip_obj);
3826}
3827
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003828void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003829{
Chris Wilson0f911282012-04-17 10:05:38 +01003830 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003831 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003832
Daniel Vetter2c10d572012-12-20 21:24:07 +01003833 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003834 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3835 !intel_crtc_has_pending_flip(crtc),
3836 60*HZ) == 0)) {
3837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003838
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003839 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003840 if (intel_crtc->unpin_work) {
3841 WARN_ONCE(1, "Removing stuck page flip\n");
3842 page_flip_completed(intel_crtc);
3843 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003844 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003845 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003846
Chris Wilson975d5682014-08-20 13:13:34 +01003847 if (crtc->primary->fb) {
3848 mutex_lock(&dev->struct_mutex);
3849 intel_finish_fb(crtc->primary->fb);
3850 mutex_unlock(&dev->struct_mutex);
3851 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003852}
3853
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003854/* Program iCLKIP clock to the desired frequency */
3855static void lpt_program_iclkip(struct drm_crtc *crtc)
3856{
3857 struct drm_device *dev = crtc->dev;
3858 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003859 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003860 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3861 u32 temp;
3862
Ville Syrjäläa5805162015-05-26 20:42:30 +03003863 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003864
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003865 /* It is necessary to ungate the pixclk gate prior to programming
3866 * the divisors, and gate it back when it is done.
3867 */
3868 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3869
3870 /* Disable SSCCTL */
3871 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003872 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3873 SBI_SSCCTL_DISABLE,
3874 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003875
3876 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003877 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003878 auxdiv = 1;
3879 divsel = 0x41;
3880 phaseinc = 0x20;
3881 } else {
3882 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003883 * but the adjusted_mode->crtc_clock in in KHz. To get the
3884 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003885 * convert the virtual clock precision to KHz here for higher
3886 * precision.
3887 */
3888 u32 iclk_virtual_root_freq = 172800 * 1000;
3889 u32 iclk_pi_range = 64;
3890 u32 desired_divisor, msb_divisor_value, pi_value;
3891
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003892 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003893 msb_divisor_value = desired_divisor / iclk_pi_range;
3894 pi_value = desired_divisor % iclk_pi_range;
3895
3896 auxdiv = 0;
3897 divsel = msb_divisor_value - 2;
3898 phaseinc = pi_value;
3899 }
3900
3901 /* This should not happen with any sane values */
3902 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3903 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3904 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3905 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3906
3907 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 +03003908 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003909 auxdiv,
3910 divsel,
3911 phasedir,
3912 phaseinc);
3913
3914 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003915 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003916 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3917 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3918 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3919 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3920 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3921 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003922 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003923
3924 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003925 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003926 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3927 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003928 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003929
3930 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003931 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003932 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003933 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003934
3935 /* Wait for initialization time */
3936 udelay(24);
3937
3938 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003939
Ville Syrjäläa5805162015-05-26 20:42:30 +03003940 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003941}
3942
Daniel Vetter275f01b22013-05-03 11:49:47 +02003943static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3944 enum pipe pch_transcoder)
3945{
3946 struct drm_device *dev = crtc->base.dev;
3947 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003948 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003949
3950 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3951 I915_READ(HTOTAL(cpu_transcoder)));
3952 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3953 I915_READ(HBLANK(cpu_transcoder)));
3954 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3955 I915_READ(HSYNC(cpu_transcoder)));
3956
3957 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3958 I915_READ(VTOTAL(cpu_transcoder)));
3959 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3960 I915_READ(VBLANK(cpu_transcoder)));
3961 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3962 I915_READ(VSYNC(cpu_transcoder)));
3963 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3964 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3965}
3966
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003967static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003968{
3969 struct drm_i915_private *dev_priv = dev->dev_private;
3970 uint32_t temp;
3971
3972 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003973 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003974 return;
3975
3976 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3977 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3978
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003979 temp &= ~FDI_BC_BIFURCATION_SELECT;
3980 if (enable)
3981 temp |= FDI_BC_BIFURCATION_SELECT;
3982
3983 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003984 I915_WRITE(SOUTH_CHICKEN1, temp);
3985 POSTING_READ(SOUTH_CHICKEN1);
3986}
3987
3988static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3989{
3990 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003991
3992 switch (intel_crtc->pipe) {
3993 case PIPE_A:
3994 break;
3995 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003996 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003997 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003998 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003999 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004000
4001 break;
4002 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004003 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004004
4005 break;
4006 default:
4007 BUG();
4008 }
4009}
4010
Jesse Barnesf67a5592011-01-05 10:31:48 -08004011/*
4012 * Enable PCH resources required for PCH ports:
4013 * - PCH PLLs
4014 * - FDI training & RX/TX
4015 * - update transcoder timings
4016 * - DP transcoding bits
4017 * - transcoder
4018 */
4019static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004020{
4021 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004022 struct drm_i915_private *dev_priv = dev->dev_private;
4023 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4024 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004025 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004026
Daniel Vetterab9412b2013-05-03 11:49:46 +02004027 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004028
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004029 if (IS_IVYBRIDGE(dev))
4030 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4031
Daniel Vettercd986ab2012-10-26 10:58:12 +02004032 /* Write the TU size bits before fdi link training, so that error
4033 * detection works. */
4034 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4035 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4036
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004037 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004038 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004039
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004040 /* We need to program the right clock selection before writing the pixel
4041 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004042 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004043 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004044
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004045 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004046 temp |= TRANS_DPLL_ENABLE(pipe);
4047 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004048 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004049 temp |= sel;
4050 else
4051 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004052 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004053 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004054
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004055 /* XXX: pch pll's can be enabled any time before we enable the PCH
4056 * transcoder, and we actually should do this to not upset any PCH
4057 * transcoder that already use the clock when we share it.
4058 *
4059 * Note that enable_shared_dpll tries to do the right thing, but
4060 * get_shared_dpll unconditionally resets the pll - we need that to have
4061 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004062 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004063
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004064 /* set transcoder timing, panel must allow it */
4065 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004066 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004067
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004068 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004069
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004070 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004071 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004072 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004073 reg = TRANS_DP_CTL(pipe);
4074 temp = I915_READ(reg);
4075 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004076 TRANS_DP_SYNC_MASK |
4077 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004078 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004079 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004080
4081 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004082 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004083 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004084 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004085
4086 switch (intel_trans_dp_port_sel(crtc)) {
4087 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004088 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004089 break;
4090 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004091 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004092 break;
4093 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004094 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004095 break;
4096 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004097 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004098 }
4099
Chris Wilson5eddb702010-09-11 13:48:45 +01004100 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004101 }
4102
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004103 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004104}
4105
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004106static void lpt_pch_enable(struct drm_crtc *crtc)
4107{
4108 struct drm_device *dev = crtc->dev;
4109 struct drm_i915_private *dev_priv = dev->dev_private;
4110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004111 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004112
Daniel Vetterab9412b2013-05-03 11:49:46 +02004113 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004114
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004115 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004116
Paulo Zanoni0540e482012-10-31 18:12:40 -02004117 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004118 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004119
Paulo Zanoni937bb612012-10-31 18:12:47 -02004120 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004121}
4122
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004123struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4124 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004125{
Daniel Vettere2b78262013-06-07 23:10:03 +02004126 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004127 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004128 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004129 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004130
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004131 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4132
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004133 if (HAS_PCH_IBX(dev_priv->dev)) {
4134 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004135 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004136 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004137
Daniel Vetter46edb022013-06-05 13:34:12 +02004138 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4139 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004140
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004141 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004142
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004143 goto found;
4144 }
4145
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304146 if (IS_BROXTON(dev_priv->dev)) {
4147 /* PLL is attached to port in bxt */
4148 struct intel_encoder *encoder;
4149 struct intel_digital_port *intel_dig_port;
4150
4151 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4152 if (WARN_ON(!encoder))
4153 return NULL;
4154
4155 intel_dig_port = enc_to_dig_port(&encoder->base);
4156 /* 1:1 mapping between ports and PLLs */
4157 i = (enum intel_dpll_id)intel_dig_port->port;
4158 pll = &dev_priv->shared_dplls[i];
4159 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4160 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004161 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304162
4163 goto found;
4164 }
4165
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004166 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4167 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004168
4169 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004170 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004171 continue;
4172
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004173 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004174 &shared_dpll[i].hw_state,
4175 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004176 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004177 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004178 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004179 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004180 goto found;
4181 }
4182 }
4183
4184 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004185 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4186 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004187 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004188 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4189 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004190 goto found;
4191 }
4192 }
4193
4194 return NULL;
4195
4196found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004197 if (shared_dpll[i].crtc_mask == 0)
4198 shared_dpll[i].hw_state =
4199 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004200
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004201 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004202 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4203 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004204
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004205 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004206
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004207 return pll;
4208}
4209
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004210static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004211{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004212 struct drm_i915_private *dev_priv = to_i915(state->dev);
4213 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004214 struct intel_shared_dpll *pll;
4215 enum intel_dpll_id i;
4216
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004217 if (!to_intel_atomic_state(state)->dpll_set)
4218 return;
4219
4220 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004221 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4222 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004223 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004224 }
4225}
4226
Daniel Vettera1520312013-05-03 11:49:50 +02004227static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004228{
4229 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004230 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004231 u32 temp;
4232
4233 temp = I915_READ(dslreg);
4234 udelay(500);
4235 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004236 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004237 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004238 }
4239}
4240
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004241static int
4242skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4243 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4244 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004245{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004246 struct intel_crtc_scaler_state *scaler_state =
4247 &crtc_state->scaler_state;
4248 struct intel_crtc *intel_crtc =
4249 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004250 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004251
4252 need_scaling = intel_rotation_90_or_270(rotation) ?
4253 (src_h != dst_w || src_w != dst_h):
4254 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004255
4256 /*
4257 * if plane is being disabled or scaler is no more required or force detach
4258 * - free scaler binded to this plane/crtc
4259 * - in order to do this, update crtc->scaler_usage
4260 *
4261 * Here scaler state in crtc_state is set free so that
4262 * scaler can be assigned to other user. Actual register
4263 * update to free the scaler is done in plane/panel-fit programming.
4264 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4265 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004266 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004267 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004268 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004269 scaler_state->scalers[*scaler_id].in_use = 0;
4270
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004271 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4272 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4273 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004274 scaler_state->scaler_users);
4275 *scaler_id = -1;
4276 }
4277 return 0;
4278 }
4279
4280 /* range checks */
4281 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4282 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4283
4284 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4285 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004286 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004287 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004288 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004289 return -EINVAL;
4290 }
4291
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004292 /* mark this plane as a scaler user in crtc_state */
4293 scaler_state->scaler_users |= (1 << scaler_user);
4294 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4295 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4296 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4297 scaler_state->scaler_users);
4298
4299 return 0;
4300}
4301
4302/**
4303 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4304 *
4305 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004306 *
4307 * Return
4308 * 0 - scaler_usage updated successfully
4309 * error - requested scaling cannot be supported or other error condition
4310 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004311int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004312{
4313 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
4314 struct drm_display_mode *adjusted_mode =
4315 &state->base.adjusted_mode;
4316
4317 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4318 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4319
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004320 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004321 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4322 state->pipe_src_w, state->pipe_src_h,
Imre Deak8c6cda22015-06-23 20:40:27 +03004323 adjusted_mode->hdisplay, adjusted_mode->vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004324}
4325
4326/**
4327 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4328 *
4329 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004330 * @plane_state: atomic plane state to update
4331 *
4332 * Return
4333 * 0 - scaler_usage updated successfully
4334 * error - requested scaling cannot be supported or other error condition
4335 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004336static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4337 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004338{
4339
4340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004341 struct intel_plane *intel_plane =
4342 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004343 struct drm_framebuffer *fb = plane_state->base.fb;
4344 int ret;
4345
4346 bool force_detach = !fb || !plane_state->visible;
4347
4348 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4349 intel_plane->base.base.id, intel_crtc->pipe,
4350 drm_plane_index(&intel_plane->base));
4351
4352 ret = skl_update_scaler(crtc_state, force_detach,
4353 drm_plane_index(&intel_plane->base),
4354 &plane_state->scaler_id,
4355 plane_state->base.rotation,
4356 drm_rect_width(&plane_state->src) >> 16,
4357 drm_rect_height(&plane_state->src) >> 16,
4358 drm_rect_width(&plane_state->dst),
4359 drm_rect_height(&plane_state->dst));
4360
4361 if (ret || plane_state->scaler_id < 0)
4362 return ret;
4363
Chandra Kondurua1b22782015-04-07 15:28:45 -07004364 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004365 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004366 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004367 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004368 return -EINVAL;
4369 }
4370
4371 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004372 switch (fb->pixel_format) {
4373 case DRM_FORMAT_RGB565:
4374 case DRM_FORMAT_XBGR8888:
4375 case DRM_FORMAT_XRGB8888:
4376 case DRM_FORMAT_ABGR8888:
4377 case DRM_FORMAT_ARGB8888:
4378 case DRM_FORMAT_XRGB2101010:
4379 case DRM_FORMAT_XBGR2101010:
4380 case DRM_FORMAT_YUYV:
4381 case DRM_FORMAT_YVYU:
4382 case DRM_FORMAT_UYVY:
4383 case DRM_FORMAT_VYUY:
4384 break;
4385 default:
4386 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4387 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4388 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004389 }
4390
Chandra Kondurua1b22782015-04-07 15:28:45 -07004391 return 0;
4392}
4393
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004394static void skylake_scaler_disable(struct intel_crtc *crtc)
4395{
4396 int i;
4397
4398 for (i = 0; i < crtc->num_scalers; i++)
4399 skl_detach_scaler(crtc, i);
4400}
4401
4402static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004403{
4404 struct drm_device *dev = crtc->base.dev;
4405 struct drm_i915_private *dev_priv = dev->dev_private;
4406 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004407 struct intel_crtc_scaler_state *scaler_state =
4408 &crtc->config->scaler_state;
4409
4410 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4411
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004412 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004413 int id;
4414
4415 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4416 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4417 return;
4418 }
4419
4420 id = scaler_state->scaler_id;
4421 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4422 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4423 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4424 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4425
4426 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004427 }
4428}
4429
Jesse Barnesb074cec2013-04-25 12:55:02 -07004430static void ironlake_pfit_enable(struct intel_crtc *crtc)
4431{
4432 struct drm_device *dev = crtc->base.dev;
4433 struct drm_i915_private *dev_priv = dev->dev_private;
4434 int pipe = crtc->pipe;
4435
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004436 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004437 /* Force use of hard-coded filter coefficients
4438 * as some pre-programmed values are broken,
4439 * e.g. x201.
4440 */
4441 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4442 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4443 PF_PIPE_SEL_IVB(pipe));
4444 else
4445 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004446 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4447 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004448 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004449}
4450
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004451void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004452{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004453 struct drm_device *dev = crtc->base.dev;
4454 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004455
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004456 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004457 return;
4458
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004459 /* We can only enable IPS after we enable a plane and wait for a vblank */
4460 intel_wait_for_vblank(dev, crtc->pipe);
4461
Paulo Zanonid77e4532013-09-24 13:52:55 -03004462 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004463 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004464 mutex_lock(&dev_priv->rps.hw_lock);
4465 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4466 mutex_unlock(&dev_priv->rps.hw_lock);
4467 /* Quoting Art Runyan: "its not safe to expect any particular
4468 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004469 * mailbox." Moreover, the mailbox may return a bogus state,
4470 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004471 */
4472 } else {
4473 I915_WRITE(IPS_CTL, IPS_ENABLE);
4474 /* The bit only becomes 1 in the next vblank, so this wait here
4475 * is essentially intel_wait_for_vblank. If we don't have this
4476 * and don't wait for vblanks until the end of crtc_enable, then
4477 * the HW state readout code will complain that the expected
4478 * IPS_CTL value is not the one we read. */
4479 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4480 DRM_ERROR("Timed out waiting for IPS enable\n");
4481 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004482}
4483
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004484void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004485{
4486 struct drm_device *dev = crtc->base.dev;
4487 struct drm_i915_private *dev_priv = dev->dev_private;
4488
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004489 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004490 return;
4491
4492 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004493 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004494 mutex_lock(&dev_priv->rps.hw_lock);
4495 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4496 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004497 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4498 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4499 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004500 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004501 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004502 POSTING_READ(IPS_CTL);
4503 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004504
4505 /* We need to wait for a vblank before we can disable the plane. */
4506 intel_wait_for_vblank(dev, crtc->pipe);
4507}
4508
4509/** Loads the palette/gamma unit for the CRTC with the prepared values */
4510static void intel_crtc_load_lut(struct drm_crtc *crtc)
4511{
4512 struct drm_device *dev = crtc->dev;
4513 struct drm_i915_private *dev_priv = dev->dev_private;
4514 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4515 enum pipe pipe = intel_crtc->pipe;
4516 int palreg = PALETTE(pipe);
4517 int i;
4518 bool reenable_ips = false;
4519
4520 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004521 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004522 return;
4523
Imre Deak50360402015-01-16 00:55:16 -08004524 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004525 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004526 assert_dsi_pll_enabled(dev_priv);
4527 else
4528 assert_pll_enabled(dev_priv, pipe);
4529 }
4530
4531 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304532 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004533 palreg = LGC_PALETTE(pipe);
4534
4535 /* Workaround : Do not read or write the pipe palette/gamma data while
4536 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4537 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004538 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004539 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4540 GAMMA_MODE_MODE_SPLIT)) {
4541 hsw_disable_ips(intel_crtc);
4542 reenable_ips = true;
4543 }
4544
4545 for (i = 0; i < 256; i++) {
4546 I915_WRITE(palreg + 4 * i,
4547 (intel_crtc->lut_r[i] << 16) |
4548 (intel_crtc->lut_g[i] << 8) |
4549 intel_crtc->lut_b[i]);
4550 }
4551
4552 if (reenable_ips)
4553 hsw_enable_ips(intel_crtc);
4554}
4555
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004556static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004557{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004558 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004559 struct drm_device *dev = intel_crtc->base.dev;
4560 struct drm_i915_private *dev_priv = dev->dev_private;
4561
4562 mutex_lock(&dev->struct_mutex);
4563 dev_priv->mm.interruptible = false;
4564 (void) intel_overlay_switch_off(intel_crtc->overlay);
4565 dev_priv->mm.interruptible = true;
4566 mutex_unlock(&dev->struct_mutex);
4567 }
4568
4569 /* Let userspace switch the overlay on again. In most cases userspace
4570 * has to recompute where to put it anyway.
4571 */
4572}
4573
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004574/**
4575 * intel_post_enable_primary - Perform operations after enabling primary plane
4576 * @crtc: the CRTC whose primary plane was just enabled
4577 *
4578 * Performs potentially sleeping operations that must be done after the primary
4579 * plane is enabled, such as updating FBC and IPS. Note that this may be
4580 * called due to an explicit primary plane update, or due to an implicit
4581 * re-enable that is caused when a sprite plane is updated to no longer
4582 * completely hide the primary plane.
4583 */
4584static void
4585intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004586{
4587 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004588 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004589 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4590 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004591
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004592 /*
4593 * BDW signals flip done immediately if the plane
4594 * is disabled, even if the plane enable is already
4595 * armed to occur at the next vblank :(
4596 */
4597 if (IS_BROADWELL(dev))
4598 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004599
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004600 /*
4601 * FIXME IPS should be fine as long as one plane is
4602 * enabled, but in practice it seems to have problems
4603 * when going from primary only to sprite only and vice
4604 * versa.
4605 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004606 hsw_enable_ips(intel_crtc);
4607
Daniel Vetterf99d7062014-06-19 16:01:59 +02004608 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004609 * Gen2 reports pipe underruns whenever all planes are disabled.
4610 * So don't enable underrun reporting before at least some planes
4611 * are enabled.
4612 * FIXME: Need to fix the logic to work when we turn off all planes
4613 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004614 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004615 if (IS_GEN2(dev))
4616 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4617
4618 /* Underruns don't raise interrupts, so check manually. */
4619 if (HAS_GMCH_DISPLAY(dev))
4620 i9xx_check_fifo_underruns(dev_priv);
4621}
4622
4623/**
4624 * intel_pre_disable_primary - Perform operations before disabling primary plane
4625 * @crtc: the CRTC whose primary plane is to be disabled
4626 *
4627 * Performs potentially sleeping operations that must be done before the
4628 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4629 * be called due to an explicit primary plane update, or due to an implicit
4630 * disable that is caused when a sprite plane completely hides the primary
4631 * plane.
4632 */
4633static void
4634intel_pre_disable_primary(struct drm_crtc *crtc)
4635{
4636 struct drm_device *dev = crtc->dev;
4637 struct drm_i915_private *dev_priv = dev->dev_private;
4638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4639 int pipe = intel_crtc->pipe;
4640
4641 /*
4642 * Gen2 reports pipe underruns whenever all planes are disabled.
4643 * So diasble underrun reporting before all the planes get disabled.
4644 * FIXME: Need to fix the logic to work when we turn off all planes
4645 * but leave the pipe running.
4646 */
4647 if (IS_GEN2(dev))
4648 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4649
4650 /*
4651 * Vblank time updates from the shadow to live plane control register
4652 * are blocked if the memory self-refresh mode is active at that
4653 * moment. So to make sure the plane gets truly disabled, disable
4654 * first the self-refresh mode. The self-refresh enable bit in turn
4655 * will be checked/applied by the HW only at the next frame start
4656 * event which is after the vblank start event, so we need to have a
4657 * wait-for-vblank between disabling the plane and the pipe.
4658 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004659 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004660 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004661 dev_priv->wm.vlv.cxsr = false;
4662 intel_wait_for_vblank(dev, pipe);
4663 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004664
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004665 /*
4666 * FIXME IPS should be fine as long as one plane is
4667 * enabled, but in practice it seems to have problems
4668 * when going from primary only to sprite only and vice
4669 * versa.
4670 */
4671 hsw_disable_ips(intel_crtc);
4672}
4673
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004674static void intel_post_plane_update(struct intel_crtc *crtc)
4675{
4676 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4677 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni7733b492015-07-07 15:26:04 -03004678 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004679 struct drm_plane *plane;
4680
4681 if (atomic->wait_vblank)
4682 intel_wait_for_vblank(dev, crtc->pipe);
4683
4684 intel_frontbuffer_flip(dev, atomic->fb_bits);
4685
Ville Syrjälä852eb002015-06-24 22:00:07 +03004686 if (atomic->disable_cxsr)
4687 crtc->wm.cxsr_allowed = true;
4688
Ville Syrjäläf015c552015-06-24 22:00:02 +03004689 if (crtc->atomic.update_wm_post)
4690 intel_update_watermarks(&crtc->base);
4691
Paulo Zanonic80ac852015-07-02 19:25:13 -03004692 if (atomic->update_fbc)
Paulo Zanoni7733b492015-07-07 15:26:04 -03004693 intel_fbc_update(dev_priv);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004694
4695 if (atomic->post_enable_primary)
4696 intel_post_enable_primary(&crtc->base);
4697
4698 drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4699 intel_update_sprite_watermarks(plane, &crtc->base,
4700 0, 0, 0, false, false);
4701
4702 memset(atomic, 0, sizeof(*atomic));
4703}
4704
4705static void intel_pre_plane_update(struct intel_crtc *crtc)
4706{
4707 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004708 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004709 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4710 struct drm_plane *p;
4711
4712 /* Track fb's for any planes being disabled */
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004713 drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4714 struct intel_plane *plane = to_intel_plane(p);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004715
4716 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03004717 i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
4718 plane->frontbuffer_bit);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004719 mutex_unlock(&dev->struct_mutex);
4720 }
4721
4722 if (atomic->wait_for_flips)
4723 intel_crtc_wait_for_pending_flips(&crtc->base);
4724
Paulo Zanonic80ac852015-07-02 19:25:13 -03004725 if (atomic->disable_fbc)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03004726 intel_fbc_disable_crtc(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004727
Rodrigo Vivi066cf55b2015-06-26 13:55:54 -07004728 if (crtc->atomic.disable_ips)
4729 hsw_disable_ips(crtc);
4730
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004731 if (atomic->pre_disable_primary)
4732 intel_pre_disable_primary(&crtc->base);
Ville Syrjälä852eb002015-06-24 22:00:07 +03004733
4734 if (atomic->disable_cxsr) {
4735 crtc->wm.cxsr_allowed = false;
4736 intel_set_memory_cxsr(dev_priv, false);
4737 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004738}
4739
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004740static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004741{
4742 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004744 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004745 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004746
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004747 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004748
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004749 drm_for_each_plane_mask(p, dev, plane_mask)
4750 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004751
Daniel Vetterf99d7062014-06-19 16:01:59 +02004752 /*
4753 * FIXME: Once we grow proper nuclear flip support out of this we need
4754 * to compute the mask of flip planes precisely. For the time being
4755 * consider this a flip to a NULL plane.
4756 */
4757 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004758}
4759
Jesse Barnesf67a5592011-01-05 10:31:48 -08004760static void ironlake_crtc_enable(struct drm_crtc *crtc)
4761{
4762 struct drm_device *dev = crtc->dev;
4763 struct drm_i915_private *dev_priv = dev->dev_private;
4764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004765 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004766 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004767
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004768 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004769 return;
4770
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004771 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004772 intel_prepare_shared_dpll(intel_crtc);
4773
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004774 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304775 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004776
4777 intel_set_pipe_timings(intel_crtc);
4778
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004779 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004780 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004781 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004782 }
4783
4784 ironlake_set_pipeconf(crtc);
4785
Jesse Barnesf67a5592011-01-05 10:31:48 -08004786 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004787
Daniel Vettera72e4c92014-09-30 10:56:47 +02004788 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4789 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004790
Daniel Vetterf6736a12013-06-05 13:34:30 +02004791 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004792 if (encoder->pre_enable)
4793 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004794
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004795 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004796 /* Note: FDI PLL enabling _must_ be done before we enable the
4797 * cpu pipes, hence this is separate from all the other fdi/pch
4798 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004799 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004800 } else {
4801 assert_fdi_tx_disabled(dev_priv, pipe);
4802 assert_fdi_rx_disabled(dev_priv, pipe);
4803 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004804
Jesse Barnesb074cec2013-04-25 12:55:02 -07004805 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004806
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004807 /*
4808 * On ILK+ LUT must be loaded before the pipe is running but with
4809 * clocks enabled
4810 */
4811 intel_crtc_load_lut(crtc);
4812
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004813 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004814 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004815
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004816 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004817 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004818
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004819 assert_vblank_disabled(crtc);
4820 drm_crtc_vblank_on(crtc);
4821
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004822 for_each_encoder_on_crtc(dev, crtc, encoder)
4823 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004824
4825 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004826 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004827}
4828
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004829/* IPS only exists on ULT machines and is tied to pipe A. */
4830static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4831{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004832 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004833}
4834
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004835static void haswell_crtc_enable(struct drm_crtc *crtc)
4836{
4837 struct drm_device *dev = crtc->dev;
4838 struct drm_i915_private *dev_priv = dev->dev_private;
4839 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4840 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004841 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4842 struct intel_crtc_state *pipe_config =
4843 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004844
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004845 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004846 return;
4847
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004848 if (intel_crtc_to_shared_dpll(intel_crtc))
4849 intel_enable_shared_dpll(intel_crtc);
4850
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004851 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304852 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004853
4854 intel_set_pipe_timings(intel_crtc);
4855
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004856 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4857 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4858 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004859 }
4860
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004861 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004862 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004863 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004864 }
4865
4866 haswell_set_pipeconf(crtc);
4867
4868 intel_set_pipe_csc(crtc);
4869
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004870 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004871
Daniel Vettera72e4c92014-09-30 10:56:47 +02004872 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004873 for_each_encoder_on_crtc(dev, crtc, encoder)
4874 if (encoder->pre_enable)
4875 encoder->pre_enable(encoder);
4876
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004877 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004878 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4879 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004880 dev_priv->display.fdi_link_train(crtc);
4881 }
4882
Paulo Zanoni1f544382012-10-24 11:32:00 -02004883 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004884
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004885 if (INTEL_INFO(dev)->gen == 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004886 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004887 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004888 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004889 else
4890 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004891
4892 /*
4893 * On ILK+ LUT must be loaded before the pipe is running but with
4894 * clocks enabled
4895 */
4896 intel_crtc_load_lut(crtc);
4897
Paulo Zanoni1f544382012-10-24 11:32:00 -02004898 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004899 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004900
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004901 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004902 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004903
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004904 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004905 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004906
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004907 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004908 intel_ddi_set_vc_payload_alloc(crtc, true);
4909
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004910 assert_vblank_disabled(crtc);
4911 drm_crtc_vblank_on(crtc);
4912
Jani Nikula8807e552013-08-30 19:40:32 +03004913 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004914 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004915 intel_opregion_notify_encoder(encoder, true);
4916 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004917
Paulo Zanonie4916942013-09-20 16:21:19 -03004918 /* If we change the relative order between pipe/planes enabling, we need
4919 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004920 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4921 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4922 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4923 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4924 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004925}
4926
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004927static void ironlake_pfit_disable(struct intel_crtc *crtc)
4928{
4929 struct drm_device *dev = crtc->base.dev;
4930 struct drm_i915_private *dev_priv = dev->dev_private;
4931 int pipe = crtc->pipe;
4932
4933 /* To avoid upsetting the power well on haswell only disable the pfit if
4934 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004935 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004936 I915_WRITE(PF_CTL(pipe), 0);
4937 I915_WRITE(PF_WIN_POS(pipe), 0);
4938 I915_WRITE(PF_WIN_SZ(pipe), 0);
4939 }
4940}
4941
Jesse Barnes6be4a602010-09-10 10:26:01 -07004942static void ironlake_crtc_disable(struct drm_crtc *crtc)
4943{
4944 struct drm_device *dev = crtc->dev;
4945 struct drm_i915_private *dev_priv = dev->dev_private;
4946 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004947 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004948 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004949 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004950
Daniel Vetterea9d7582012-07-10 10:42:52 +02004951 for_each_encoder_on_crtc(dev, crtc, encoder)
4952 encoder->disable(encoder);
4953
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004954 drm_crtc_vblank_off(crtc);
4955 assert_vblank_disabled(crtc);
4956
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004957 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004958 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004959
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004960 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004961
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004962 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004963
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004964 if (intel_crtc->config->has_pch_encoder)
4965 ironlake_fdi_disable(crtc);
4966
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004967 for_each_encoder_on_crtc(dev, crtc, encoder)
4968 if (encoder->post_disable)
4969 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004970
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004971 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004972 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004973
Daniel Vetterd925c592013-06-05 13:34:04 +02004974 if (HAS_PCH_CPT(dev)) {
4975 /* disable TRANS_DP_CTL */
4976 reg = TRANS_DP_CTL(pipe);
4977 temp = I915_READ(reg);
4978 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4979 TRANS_DP_PORT_SEL_MASK);
4980 temp |= TRANS_DP_PORT_SEL_NONE;
4981 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004982
Daniel Vetterd925c592013-06-05 13:34:04 +02004983 /* disable DPLL_SEL */
4984 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004985 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004986 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004987 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004988
Daniel Vetterd925c592013-06-05 13:34:04 +02004989 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004990 }
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02004991
4992 intel_crtc->active = false;
4993 intel_update_watermarks(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004994}
4995
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004996static void haswell_crtc_disable(struct drm_crtc *crtc)
4997{
4998 struct drm_device *dev = crtc->dev;
4999 struct drm_i915_private *dev_priv = dev->dev_private;
5000 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5001 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005002 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005003
Jani Nikula8807e552013-08-30 19:40:32 +03005004 for_each_encoder_on_crtc(dev, crtc, encoder) {
5005 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005006 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005007 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005008
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005009 drm_crtc_vblank_off(crtc);
5010 assert_vblank_disabled(crtc);
5011
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005012 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005013 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5014 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005015 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005016
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005017 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005018 intel_ddi_set_vc_payload_alloc(crtc, false);
5019
Paulo Zanoniad80a812012-10-24 16:06:19 -02005020 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005021
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005022 if (INTEL_INFO(dev)->gen == 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005023 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005024 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005025 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005026 else
5027 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005028
Paulo Zanoni1f544382012-10-24 11:32:00 -02005029 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005030
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005031 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005032 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005033 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005034 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005035
Imre Deak97b040a2014-06-25 22:01:50 +03005036 for_each_encoder_on_crtc(dev, crtc, encoder)
5037 if (encoder->post_disable)
5038 encoder->post_disable(encoder);
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02005039
5040 intel_crtc->active = false;
5041 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005042}
5043
Jesse Barnes2dd24552013-04-25 12:55:01 -07005044static void i9xx_pfit_enable(struct intel_crtc *crtc)
5045{
5046 struct drm_device *dev = crtc->base.dev;
5047 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005048 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005049
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005050 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005051 return;
5052
Daniel Vetterc0b03412013-05-28 12:05:54 +02005053 /*
5054 * The panel fitter should only be adjusted whilst the pipe is disabled,
5055 * according to register description and PRM.
5056 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005057 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5058 assert_pipe_disabled(dev_priv, crtc->pipe);
5059
Jesse Barnesb074cec2013-04-25 12:55:02 -07005060 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5061 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005062
5063 /* Border color in case we don't scale up to the full screen. Black by
5064 * default, change to something else for debugging. */
5065 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005066}
5067
Dave Airlied05410f2014-06-05 13:22:59 +10005068static enum intel_display_power_domain port_to_power_domain(enum port port)
5069{
5070 switch (port) {
5071 case PORT_A:
Rodrigo Vivia513e3d752015-08-06 15:51:37 +08005072 case PORT_E:
Dave Airlied05410f2014-06-05 13:22:59 +10005073 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5074 case PORT_B:
5075 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5076 case PORT_C:
5077 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5078 case PORT_D:
5079 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5080 default:
5081 WARN_ON_ONCE(1);
5082 return POWER_DOMAIN_PORT_OTHER;
5083 }
5084}
5085
Imre Deak77d22dc2014-03-05 16:20:52 +02005086#define for_each_power_domain(domain, mask) \
5087 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5088 if ((1 << (domain)) & (mask))
5089
Imre Deak319be8a2014-03-04 19:22:57 +02005090enum intel_display_power_domain
5091intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005092{
Imre Deak319be8a2014-03-04 19:22:57 +02005093 struct drm_device *dev = intel_encoder->base.dev;
5094 struct intel_digital_port *intel_dig_port;
5095
5096 switch (intel_encoder->type) {
5097 case INTEL_OUTPUT_UNKNOWN:
5098 /* Only DDI platforms should ever use this output type */
5099 WARN_ON_ONCE(!HAS_DDI(dev));
5100 case INTEL_OUTPUT_DISPLAYPORT:
5101 case INTEL_OUTPUT_HDMI:
5102 case INTEL_OUTPUT_EDP:
5103 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005104 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005105 case INTEL_OUTPUT_DP_MST:
5106 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5107 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005108 case INTEL_OUTPUT_ANALOG:
5109 return POWER_DOMAIN_PORT_CRT;
5110 case INTEL_OUTPUT_DSI:
5111 return POWER_DOMAIN_PORT_DSI;
5112 default:
5113 return POWER_DOMAIN_PORT_OTHER;
5114 }
5115}
5116
5117static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5118{
5119 struct drm_device *dev = crtc->dev;
5120 struct intel_encoder *intel_encoder;
5121 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5122 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005123 unsigned long mask;
5124 enum transcoder transcoder;
5125
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005126 if (!crtc->state->active)
5127 return 0;
5128
Imre Deak77d22dc2014-03-05 16:20:52 +02005129 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5130
5131 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5132 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005133 if (intel_crtc->config->pch_pfit.enabled ||
5134 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005135 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5136
Imre Deak319be8a2014-03-04 19:22:57 +02005137 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5138 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5139
Imre Deak77d22dc2014-03-05 16:20:52 +02005140 return mask;
5141}
5142
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005143static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
5144{
5145 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5146 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5147 enum intel_display_power_domain domain;
5148 unsigned long domains, new_domains, old_domains;
5149
5150 old_domains = intel_crtc->enabled_power_domains;
5151 intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
5152
5153 domains = new_domains & ~old_domains;
5154
5155 for_each_power_domain(domain, domains)
5156 intel_display_power_get(dev_priv, domain);
5157
5158 return old_domains & ~new_domains;
5159}
5160
5161static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5162 unsigned long domains)
5163{
5164 enum intel_display_power_domain domain;
5165
5166 for_each_power_domain(domain, domains)
5167 intel_display_power_put(dev_priv, domain);
5168}
5169
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005170static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005171{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005172 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005173 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005174 unsigned long put_domains[I915_MAX_PIPES] = {};
5175 struct drm_crtc_state *crtc_state;
5176 struct drm_crtc *crtc;
5177 int i;
Imre Deak77d22dc2014-03-05 16:20:52 +02005178
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005179 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5180 if (needs_modeset(crtc->state))
5181 put_domains[to_intel_crtc(crtc)->pipe] =
5182 modeset_get_crtc_power_domains(crtc);
Imre Deak77d22dc2014-03-05 16:20:52 +02005183 }
5184
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005185 if (dev_priv->display.modeset_commit_cdclk) {
5186 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5187
5188 if (cdclk != dev_priv->cdclk_freq &&
5189 !WARN_ON(!state->allow_modeset))
5190 dev_priv->display.modeset_commit_cdclk(state);
5191 }
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005192
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005193 for (i = 0; i < I915_MAX_PIPES; i++)
5194 if (put_domains[i])
5195 modeset_put_power_domains(dev_priv, put_domains[i]);
Imre Deak77d22dc2014-03-05 16:20:52 +02005196}
5197
Mika Kaholaadafdc62015-08-18 14:36:59 +03005198static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5199{
5200 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5201
5202 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5203 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5204 return max_cdclk_freq;
5205 else if (IS_CHERRYVIEW(dev_priv))
5206 return max_cdclk_freq*95/100;
5207 else if (INTEL_INFO(dev_priv)->gen < 4)
5208 return 2*max_cdclk_freq*90/100;
5209 else
5210 return max_cdclk_freq*90/100;
5211}
5212
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005213static void intel_update_max_cdclk(struct drm_device *dev)
5214{
5215 struct drm_i915_private *dev_priv = dev->dev_private;
5216
5217 if (IS_SKYLAKE(dev)) {
5218 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5219
5220 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5221 dev_priv->max_cdclk_freq = 675000;
5222 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5223 dev_priv->max_cdclk_freq = 540000;
5224 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5225 dev_priv->max_cdclk_freq = 450000;
5226 else
5227 dev_priv->max_cdclk_freq = 337500;
5228 } else if (IS_BROADWELL(dev)) {
5229 /*
5230 * FIXME with extra cooling we can allow
5231 * 540 MHz for ULX and 675 Mhz for ULT.
5232 * How can we know if extra cooling is
5233 * available? PCI ID, VTB, something else?
5234 */
5235 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5236 dev_priv->max_cdclk_freq = 450000;
5237 else if (IS_BDW_ULX(dev))
5238 dev_priv->max_cdclk_freq = 450000;
5239 else if (IS_BDW_ULT(dev))
5240 dev_priv->max_cdclk_freq = 540000;
5241 else
5242 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005243 } else if (IS_CHERRYVIEW(dev)) {
5244 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005245 } else if (IS_VALLEYVIEW(dev)) {
5246 dev_priv->max_cdclk_freq = 400000;
5247 } else {
5248 /* otherwise assume cdclk is fixed */
5249 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5250 }
5251
Mika Kaholaadafdc62015-08-18 14:36:59 +03005252 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5253
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005254 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5255 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005256
5257 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5258 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005259}
5260
5261static void intel_update_cdclk(struct drm_device *dev)
5262{
5263 struct drm_i915_private *dev_priv = dev->dev_private;
5264
5265 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5266 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5267 dev_priv->cdclk_freq);
5268
5269 /*
5270 * Program the gmbus_freq based on the cdclk frequency.
5271 * BSpec erroneously claims we should aim for 4MHz, but
5272 * in fact 1MHz is the correct frequency.
5273 */
5274 if (IS_VALLEYVIEW(dev)) {
5275 /*
5276 * Program the gmbus_freq based on the cdclk frequency.
5277 * BSpec erroneously claims we should aim for 4MHz, but
5278 * in fact 1MHz is the correct frequency.
5279 */
5280 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5281 }
5282
5283 if (dev_priv->max_cdclk_freq == 0)
5284 intel_update_max_cdclk(dev);
5285}
5286
Damien Lespiau70d0c572015-06-04 18:21:29 +01005287static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305288{
5289 struct drm_i915_private *dev_priv = dev->dev_private;
5290 uint32_t divider;
5291 uint32_t ratio;
5292 uint32_t current_freq;
5293 int ret;
5294
5295 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5296 switch (frequency) {
5297 case 144000:
5298 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5299 ratio = BXT_DE_PLL_RATIO(60);
5300 break;
5301 case 288000:
5302 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5303 ratio = BXT_DE_PLL_RATIO(60);
5304 break;
5305 case 384000:
5306 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5307 ratio = BXT_DE_PLL_RATIO(60);
5308 break;
5309 case 576000:
5310 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5311 ratio = BXT_DE_PLL_RATIO(60);
5312 break;
5313 case 624000:
5314 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5315 ratio = BXT_DE_PLL_RATIO(65);
5316 break;
5317 case 19200:
5318 /*
5319 * Bypass frequency with DE PLL disabled. Init ratio, divider
5320 * to suppress GCC warning.
5321 */
5322 ratio = 0;
5323 divider = 0;
5324 break;
5325 default:
5326 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5327
5328 return;
5329 }
5330
5331 mutex_lock(&dev_priv->rps.hw_lock);
5332 /* Inform power controller of upcoming frequency change */
5333 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5334 0x80000000);
5335 mutex_unlock(&dev_priv->rps.hw_lock);
5336
5337 if (ret) {
5338 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5339 ret, frequency);
5340 return;
5341 }
5342
5343 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5344 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5345 current_freq = current_freq * 500 + 1000;
5346
5347 /*
5348 * DE PLL has to be disabled when
5349 * - setting to 19.2MHz (bypass, PLL isn't used)
5350 * - before setting to 624MHz (PLL needs toggling)
5351 * - before setting to any frequency from 624MHz (PLL needs toggling)
5352 */
5353 if (frequency == 19200 || frequency == 624000 ||
5354 current_freq == 624000) {
5355 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5356 /* Timeout 200us */
5357 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5358 1))
5359 DRM_ERROR("timout waiting for DE PLL unlock\n");
5360 }
5361
5362 if (frequency != 19200) {
5363 uint32_t val;
5364
5365 val = I915_READ(BXT_DE_PLL_CTL);
5366 val &= ~BXT_DE_PLL_RATIO_MASK;
5367 val |= ratio;
5368 I915_WRITE(BXT_DE_PLL_CTL, val);
5369
5370 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5371 /* Timeout 200us */
5372 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5373 DRM_ERROR("timeout waiting for DE PLL lock\n");
5374
5375 val = I915_READ(CDCLK_CTL);
5376 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5377 val |= divider;
5378 /*
5379 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5380 * enable otherwise.
5381 */
5382 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5383 if (frequency >= 500000)
5384 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5385
5386 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5387 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5388 val |= (frequency - 1000) / 500;
5389 I915_WRITE(CDCLK_CTL, val);
5390 }
5391
5392 mutex_lock(&dev_priv->rps.hw_lock);
5393 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5394 DIV_ROUND_UP(frequency, 25000));
5395 mutex_unlock(&dev_priv->rps.hw_lock);
5396
5397 if (ret) {
5398 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5399 ret, frequency);
5400 return;
5401 }
5402
Damien Lespiaua47871b2015-06-04 18:21:34 +01005403 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305404}
5405
5406void broxton_init_cdclk(struct drm_device *dev)
5407{
5408 struct drm_i915_private *dev_priv = dev->dev_private;
5409 uint32_t val;
5410
5411 /*
5412 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5413 * or else the reset will hang because there is no PCH to respond.
5414 * Move the handshake programming to initialization sequence.
5415 * Previously was left up to BIOS.
5416 */
5417 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5418 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5419 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5420
5421 /* Enable PG1 for cdclk */
5422 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5423
5424 /* check if cd clock is enabled */
5425 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5426 DRM_DEBUG_KMS("Display already initialized\n");
5427 return;
5428 }
5429
5430 /*
5431 * FIXME:
5432 * - The initial CDCLK needs to be read from VBT.
5433 * Need to make this change after VBT has changes for BXT.
5434 * - check if setting the max (or any) cdclk freq is really necessary
5435 * here, it belongs to modeset time
5436 */
5437 broxton_set_cdclk(dev, 624000);
5438
5439 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005440 POSTING_READ(DBUF_CTL);
5441
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305442 udelay(10);
5443
5444 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5445 DRM_ERROR("DBuf power enable timeout!\n");
5446}
5447
5448void broxton_uninit_cdclk(struct drm_device *dev)
5449{
5450 struct drm_i915_private *dev_priv = dev->dev_private;
5451
5452 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005453 POSTING_READ(DBUF_CTL);
5454
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305455 udelay(10);
5456
5457 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5458 DRM_ERROR("DBuf power disable timeout!\n");
5459
5460 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5461 broxton_set_cdclk(dev, 19200);
5462
5463 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5464}
5465
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005466static const struct skl_cdclk_entry {
5467 unsigned int freq;
5468 unsigned int vco;
5469} skl_cdclk_frequencies[] = {
5470 { .freq = 308570, .vco = 8640 },
5471 { .freq = 337500, .vco = 8100 },
5472 { .freq = 432000, .vco = 8640 },
5473 { .freq = 450000, .vco = 8100 },
5474 { .freq = 540000, .vco = 8100 },
5475 { .freq = 617140, .vco = 8640 },
5476 { .freq = 675000, .vco = 8100 },
5477};
5478
5479static unsigned int skl_cdclk_decimal(unsigned int freq)
5480{
5481 return (freq - 1000) / 500;
5482}
5483
5484static unsigned int skl_cdclk_get_vco(unsigned int freq)
5485{
5486 unsigned int i;
5487
5488 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5489 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5490
5491 if (e->freq == freq)
5492 return e->vco;
5493 }
5494
5495 return 8100;
5496}
5497
5498static void
5499skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5500{
5501 unsigned int min_freq;
5502 u32 val;
5503
5504 /* select the minimum CDCLK before enabling DPLL 0 */
5505 val = I915_READ(CDCLK_CTL);
5506 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5507 val |= CDCLK_FREQ_337_308;
5508
5509 if (required_vco == 8640)
5510 min_freq = 308570;
5511 else
5512 min_freq = 337500;
5513
5514 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5515
5516 I915_WRITE(CDCLK_CTL, val);
5517 POSTING_READ(CDCLK_CTL);
5518
5519 /*
5520 * We always enable DPLL0 with the lowest link rate possible, but still
5521 * taking into account the VCO required to operate the eDP panel at the
5522 * desired frequency. The usual DP link rates operate with a VCO of
5523 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5524 * The modeset code is responsible for the selection of the exact link
5525 * rate later on, with the constraint of choosing a frequency that
5526 * works with required_vco.
5527 */
5528 val = I915_READ(DPLL_CTRL1);
5529
5530 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5531 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5532 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5533 if (required_vco == 8640)
5534 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5535 SKL_DPLL0);
5536 else
5537 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5538 SKL_DPLL0);
5539
5540 I915_WRITE(DPLL_CTRL1, val);
5541 POSTING_READ(DPLL_CTRL1);
5542
5543 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5544
5545 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5546 DRM_ERROR("DPLL0 not locked\n");
5547}
5548
5549static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5550{
5551 int ret;
5552 u32 val;
5553
5554 /* inform PCU we want to change CDCLK */
5555 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5556 mutex_lock(&dev_priv->rps.hw_lock);
5557 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5558 mutex_unlock(&dev_priv->rps.hw_lock);
5559
5560 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5561}
5562
5563static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5564{
5565 unsigned int i;
5566
5567 for (i = 0; i < 15; i++) {
5568 if (skl_cdclk_pcu_ready(dev_priv))
5569 return true;
5570 udelay(10);
5571 }
5572
5573 return false;
5574}
5575
5576static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5577{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005578 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005579 u32 freq_select, pcu_ack;
5580
5581 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5582
5583 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5584 DRM_ERROR("failed to inform PCU about cdclk change\n");
5585 return;
5586 }
5587
5588 /* set CDCLK_CTL */
5589 switch(freq) {
5590 case 450000:
5591 case 432000:
5592 freq_select = CDCLK_FREQ_450_432;
5593 pcu_ack = 1;
5594 break;
5595 case 540000:
5596 freq_select = CDCLK_FREQ_540;
5597 pcu_ack = 2;
5598 break;
5599 case 308570:
5600 case 337500:
5601 default:
5602 freq_select = CDCLK_FREQ_337_308;
5603 pcu_ack = 0;
5604 break;
5605 case 617140:
5606 case 675000:
5607 freq_select = CDCLK_FREQ_675_617;
5608 pcu_ack = 3;
5609 break;
5610 }
5611
5612 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5613 POSTING_READ(CDCLK_CTL);
5614
5615 /* inform PCU of the change */
5616 mutex_lock(&dev_priv->rps.hw_lock);
5617 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5618 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005619
5620 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005621}
5622
5623void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5624{
5625 /* disable DBUF power */
5626 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5627 POSTING_READ(DBUF_CTL);
5628
5629 udelay(10);
5630
5631 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5632 DRM_ERROR("DBuf power disable timeout\n");
5633
5634 /* disable DPLL0 */
5635 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5636 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5637 DRM_ERROR("Couldn't disable DPLL0\n");
5638
5639 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5640}
5641
5642void skl_init_cdclk(struct drm_i915_private *dev_priv)
5643{
5644 u32 val;
5645 unsigned int required_vco;
5646
5647 /* enable PCH reset handshake */
5648 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5649 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5650
5651 /* enable PG1 and Misc I/O */
5652 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5653
5654 /* DPLL0 already enabed !? */
5655 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5656 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5657 return;
5658 }
5659
5660 /* enable DPLL0 */
5661 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5662 skl_dpll0_enable(dev_priv, required_vco);
5663
5664 /* set CDCLK to the frequency the BIOS chose */
5665 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5666
5667 /* enable DBUF power */
5668 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5669 POSTING_READ(DBUF_CTL);
5670
5671 udelay(10);
5672
5673 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5674 DRM_ERROR("DBuf power enable timeout\n");
5675}
5676
Ville Syrjälädfcab172014-06-13 13:37:47 +03005677/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005678static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005679{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005680 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005681
Jesse Barnes586f49d2013-11-04 16:06:59 -08005682 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005683 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005684 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5685 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005686 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005687
Ville Syrjälädfcab172014-06-13 13:37:47 +03005688 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005689}
5690
5691/* Adjust CDclk dividers to allow high res or save power if possible */
5692static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5693{
5694 struct drm_i915_private *dev_priv = dev->dev_private;
5695 u32 val, cmd;
5696
Vandana Kannan164dfd22014-11-24 13:37:41 +05305697 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5698 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005699
Ville Syrjälädfcab172014-06-13 13:37:47 +03005700 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005701 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005702 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005703 cmd = 1;
5704 else
5705 cmd = 0;
5706
5707 mutex_lock(&dev_priv->rps.hw_lock);
5708 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5709 val &= ~DSPFREQGUAR_MASK;
5710 val |= (cmd << DSPFREQGUAR_SHIFT);
5711 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5712 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5713 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5714 50)) {
5715 DRM_ERROR("timed out waiting for CDclk change\n");
5716 }
5717 mutex_unlock(&dev_priv->rps.hw_lock);
5718
Ville Syrjälä54433e92015-05-26 20:42:31 +03005719 mutex_lock(&dev_priv->sb_lock);
5720
Ville Syrjälädfcab172014-06-13 13:37:47 +03005721 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005722 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005723
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005724 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005725
Jesse Barnes30a970c2013-11-04 13:48:12 -08005726 /* adjust cdclk divider */
5727 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005728 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005729 val |= divider;
5730 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005731
5732 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5733 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5734 50))
5735 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005736 }
5737
Jesse Barnes30a970c2013-11-04 13:48:12 -08005738 /* adjust self-refresh exit latency value */
5739 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5740 val &= ~0x7f;
5741
5742 /*
5743 * For high bandwidth configs, we set a higher latency in the bunit
5744 * so that the core display fetch happens in time to avoid underruns.
5745 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005746 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005747 val |= 4500 / 250; /* 4.5 usec */
5748 else
5749 val |= 3000 / 250; /* 3.0 usec */
5750 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005751
Ville Syrjäläa5805162015-05-26 20:42:30 +03005752 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005753
Ville Syrjäläb6283052015-06-03 15:45:07 +03005754 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005755}
5756
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005757static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5758{
5759 struct drm_i915_private *dev_priv = dev->dev_private;
5760 u32 val, cmd;
5761
Vandana Kannan164dfd22014-11-24 13:37:41 +05305762 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5763 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005764
5765 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005766 case 333333:
5767 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005768 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005769 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005770 break;
5771 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005772 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005773 return;
5774 }
5775
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005776 /*
5777 * Specs are full of misinformation, but testing on actual
5778 * hardware has shown that we just need to write the desired
5779 * CCK divider into the Punit register.
5780 */
5781 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5782
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005783 mutex_lock(&dev_priv->rps.hw_lock);
5784 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5785 val &= ~DSPFREQGUAR_MASK_CHV;
5786 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5787 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5788 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5789 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5790 50)) {
5791 DRM_ERROR("timed out waiting for CDclk change\n");
5792 }
5793 mutex_unlock(&dev_priv->rps.hw_lock);
5794
Ville Syrjäläb6283052015-06-03 15:45:07 +03005795 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005796}
5797
Jesse Barnes30a970c2013-11-04 13:48:12 -08005798static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5799 int max_pixclk)
5800{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005801 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005802 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005803
Jesse Barnes30a970c2013-11-04 13:48:12 -08005804 /*
5805 * Really only a few cases to deal with, as only 4 CDclks are supported:
5806 * 200MHz
5807 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005808 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005809 * 400MHz (VLV only)
5810 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5811 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005812 *
5813 * We seem to get an unstable or solid color picture at 200MHz.
5814 * Not sure what's wrong. For now use 200MHz only when all pipes
5815 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005816 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005817 if (!IS_CHERRYVIEW(dev_priv) &&
5818 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005819 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005820 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005821 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005822 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005823 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005824 else
5825 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005826}
5827
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305828static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5829 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005830{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305831 /*
5832 * FIXME:
5833 * - remove the guardband, it's not needed on BXT
5834 * - set 19.2MHz bypass frequency if there are no active pipes
5835 */
5836 if (max_pixclk > 576000*9/10)
5837 return 624000;
5838 else if (max_pixclk > 384000*9/10)
5839 return 576000;
5840 else if (max_pixclk > 288000*9/10)
5841 return 384000;
5842 else if (max_pixclk > 144000*9/10)
5843 return 288000;
5844 else
5845 return 144000;
5846}
5847
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005848/* Compute the max pixel clock for new configuration. Uses atomic state if
5849 * that's non-NULL, look at current state otherwise. */
5850static int intel_mode_max_pixclk(struct drm_device *dev,
5851 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005852{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005853 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005854 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005855 int max_pixclk = 0;
5856
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005857 for_each_intel_crtc(dev, intel_crtc) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005858 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005859 if (IS_ERR(crtc_state))
5860 return PTR_ERR(crtc_state);
5861
5862 if (!crtc_state->base.enable)
5863 continue;
5864
5865 max_pixclk = max(max_pixclk,
5866 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005867 }
5868
5869 return max_pixclk;
5870}
5871
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005872static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005873{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005874 struct drm_device *dev = state->dev;
5875 struct drm_i915_private *dev_priv = dev->dev_private;
5876 int max_pixclk = intel_mode_max_pixclk(dev, state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005877
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005878 if (max_pixclk < 0)
5879 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005880
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005881 to_intel_atomic_state(state)->cdclk =
5882 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305883
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005884 return 0;
5885}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005886
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005887static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5888{
5889 struct drm_device *dev = state->dev;
5890 struct drm_i915_private *dev_priv = dev->dev_private;
5891 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005892
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005893 if (max_pixclk < 0)
5894 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005895
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005896 to_intel_atomic_state(state)->cdclk =
5897 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005898
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005899 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005900}
5901
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005902static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5903{
5904 unsigned int credits, default_credits;
5905
5906 if (IS_CHERRYVIEW(dev_priv))
5907 default_credits = PFI_CREDIT(12);
5908 else
5909 default_credits = PFI_CREDIT(8);
5910
Vandana Kannan164dfd22014-11-24 13:37:41 +05305911 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005912 /* CHV suggested value is 31 or 63 */
5913 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005914 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005915 else
5916 credits = PFI_CREDIT(15);
5917 } else {
5918 credits = default_credits;
5919 }
5920
5921 /*
5922 * WA - write default credits before re-programming
5923 * FIXME: should we also set the resend bit here?
5924 */
5925 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5926 default_credits);
5927
5928 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5929 credits | PFI_CREDIT_RESEND);
5930
5931 /*
5932 * FIXME is this guaranteed to clear
5933 * immediately or should we poll for it?
5934 */
5935 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5936}
5937
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005938static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005939{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005940 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005941 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005942 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005943
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005944 /*
5945 * FIXME: We can end up here with all power domains off, yet
5946 * with a CDCLK frequency other than the minimum. To account
5947 * for this take the PIPE-A power domain, which covers the HW
5948 * blocks needed for the following programming. This can be
5949 * removed once it's guaranteed that we get here either with
5950 * the minimum CDCLK set, or the required power domains
5951 * enabled.
5952 */
5953 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005954
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005955 if (IS_CHERRYVIEW(dev))
5956 cherryview_set_cdclk(dev, req_cdclk);
5957 else
5958 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005959
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005960 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02005961
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005962 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005963}
5964
Jesse Barnes89b667f2013-04-18 14:51:36 -07005965static void valleyview_crtc_enable(struct drm_crtc *crtc)
5966{
5967 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005968 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005969 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5970 struct intel_encoder *encoder;
5971 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005972 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005973
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005974 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07005975 return;
5976
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005977 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305978
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005979 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305980 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005981
5982 intel_set_pipe_timings(intel_crtc);
5983
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005984 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5985 struct drm_i915_private *dev_priv = dev->dev_private;
5986
5987 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5988 I915_WRITE(CHV_CANVAS(pipe), 0);
5989 }
5990
Daniel Vetter5b18e572014-04-24 23:55:06 +02005991 i9xx_set_pipeconf(intel_crtc);
5992
Jesse Barnes89b667f2013-04-18 14:51:36 -07005993 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005994
Daniel Vettera72e4c92014-09-30 10:56:47 +02005995 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005996
Jesse Barnes89b667f2013-04-18 14:51:36 -07005997 for_each_encoder_on_crtc(dev, crtc, encoder)
5998 if (encoder->pre_pll_enable)
5999 encoder->pre_pll_enable(encoder);
6000
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006001 if (!is_dsi) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006002 if (IS_CHERRYVIEW(dev)) {
6003 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006004 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006005 } else {
6006 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006007 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006008 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006009 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006010
6011 for_each_encoder_on_crtc(dev, crtc, encoder)
6012 if (encoder->pre_enable)
6013 encoder->pre_enable(encoder);
6014
Jesse Barnes2dd24552013-04-25 12:55:01 -07006015 i9xx_pfit_enable(intel_crtc);
6016
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006017 intel_crtc_load_lut(crtc);
6018
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006019 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006020
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006021 assert_vblank_disabled(crtc);
6022 drm_crtc_vblank_on(crtc);
6023
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006024 for_each_encoder_on_crtc(dev, crtc, encoder)
6025 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006026}
6027
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006028static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6029{
6030 struct drm_device *dev = crtc->base.dev;
6031 struct drm_i915_private *dev_priv = dev->dev_private;
6032
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006033 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6034 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006035}
6036
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006037static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006038{
6039 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006040 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006042 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006043 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006044
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006045 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006046 return;
6047
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006048 i9xx_set_pll_dividers(intel_crtc);
6049
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006050 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306051 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006052
6053 intel_set_pipe_timings(intel_crtc);
6054
Daniel Vetter5b18e572014-04-24 23:55:06 +02006055 i9xx_set_pipeconf(intel_crtc);
6056
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006057 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006058
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006059 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006060 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006061
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006062 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006063 if (encoder->pre_enable)
6064 encoder->pre_enable(encoder);
6065
Daniel Vetterf6736a12013-06-05 13:34:30 +02006066 i9xx_enable_pll(intel_crtc);
6067
Jesse Barnes2dd24552013-04-25 12:55:01 -07006068 i9xx_pfit_enable(intel_crtc);
6069
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006070 intel_crtc_load_lut(crtc);
6071
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006072 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006073 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006074
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006075 assert_vblank_disabled(crtc);
6076 drm_crtc_vblank_on(crtc);
6077
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006078 for_each_encoder_on_crtc(dev, crtc, encoder)
6079 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006080}
6081
Daniel Vetter87476d62013-04-11 16:29:06 +02006082static void i9xx_pfit_disable(struct intel_crtc *crtc)
6083{
6084 struct drm_device *dev = crtc->base.dev;
6085 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006086
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006087 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006088 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006089
6090 assert_pipe_disabled(dev_priv, crtc->pipe);
6091
Daniel Vetter328d8e82013-05-08 10:36:31 +02006092 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6093 I915_READ(PFIT_CONTROL));
6094 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006095}
6096
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006097static void i9xx_crtc_disable(struct drm_crtc *crtc)
6098{
6099 struct drm_device *dev = crtc->dev;
6100 struct drm_i915_private *dev_priv = dev->dev_private;
6101 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006102 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006103 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006104
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006105 /*
6106 * On gen2 planes are double buffered but the pipe isn't, so we must
6107 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006108 * We also need to wait on all gmch platforms because of the
6109 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006110 */
Imre Deak564ed192014-06-13 14:54:21 +03006111 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006112
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006113 for_each_encoder_on_crtc(dev, crtc, encoder)
6114 encoder->disable(encoder);
6115
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006116 drm_crtc_vblank_off(crtc);
6117 assert_vblank_disabled(crtc);
6118
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006119 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006120
Daniel Vetter87476d62013-04-11 16:29:06 +02006121 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006122
Jesse Barnes89b667f2013-04-18 14:51:36 -07006123 for_each_encoder_on_crtc(dev, crtc, encoder)
6124 if (encoder->post_disable)
6125 encoder->post_disable(encoder);
6126
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006127 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006128 if (IS_CHERRYVIEW(dev))
6129 chv_disable_pll(dev_priv, pipe);
6130 else if (IS_VALLEYVIEW(dev))
6131 vlv_disable_pll(dev_priv, pipe);
6132 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006133 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006134 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006135
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006136 for_each_encoder_on_crtc(dev, crtc, encoder)
6137 if (encoder->post_pll_disable)
6138 encoder->post_pll_disable(encoder);
6139
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006140 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006141 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02006142
6143 intel_crtc->active = false;
6144 intel_update_watermarks(crtc);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006145}
6146
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006147static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006148{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006149 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006150 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006151 enum intel_display_power_domain domain;
6152 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006153
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006154 if (!intel_crtc->active)
6155 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006156
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006157 if (to_intel_plane_state(crtc->primary->state)->visible) {
6158 intel_crtc_wait_for_pending_flips(crtc);
6159 intel_pre_disable_primary(crtc);
6160 }
6161
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02006162 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006163 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006164 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006165
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006166 domains = intel_crtc->enabled_power_domains;
6167 for_each_power_domain(domain, domains)
6168 intel_display_power_put(dev_priv, domain);
6169 intel_crtc->enabled_power_domains = 0;
6170}
6171
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006172/*
6173 * turn all crtc's off, but do not adjust state
6174 * This has to be paired with a call to intel_modeset_setup_hw_state.
6175 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006176int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006177{
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006178 struct drm_mode_config *config = &dev->mode_config;
6179 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
6180 struct drm_atomic_state *state;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006181 struct drm_crtc *crtc;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006182 unsigned crtc_mask = 0;
6183 int ret = 0;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006184
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006185 if (WARN_ON(!ctx))
6186 return 0;
6187
6188 lockdep_assert_held(&ctx->ww_ctx);
6189 state = drm_atomic_state_alloc(dev);
6190 if (WARN_ON(!state))
6191 return -ENOMEM;
6192
6193 state->acquire_ctx = ctx;
6194 state->allow_modeset = true;
6195
6196 for_each_crtc(dev, crtc) {
6197 struct drm_crtc_state *crtc_state =
6198 drm_atomic_get_crtc_state(state, crtc);
6199
6200 ret = PTR_ERR_OR_ZERO(crtc_state);
6201 if (ret)
6202 goto free;
6203
6204 if (!crtc_state->active)
6205 continue;
6206
6207 crtc_state->active = false;
6208 crtc_mask |= 1 << drm_crtc_index(crtc);
6209 }
6210
6211 if (crtc_mask) {
Maarten Lankhorst74c090b2015-07-13 16:30:30 +02006212 ret = drm_atomic_commit(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006213
6214 if (!ret) {
6215 for_each_crtc(dev, crtc)
6216 if (crtc_mask & (1 << drm_crtc_index(crtc)))
6217 crtc->state->active = true;
6218
6219 return ret;
6220 }
6221 }
6222
6223free:
6224 if (ret)
6225 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
6226 drm_atomic_state_free(state);
6227 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006228}
6229
Chris Wilsonea5b2132010-08-04 13:50:23 +01006230void intel_encoder_destroy(struct drm_encoder *encoder)
6231{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006232 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006233
Chris Wilsonea5b2132010-08-04 13:50:23 +01006234 drm_encoder_cleanup(encoder);
6235 kfree(intel_encoder);
6236}
6237
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006238/* Cross check the actual hw state with our own modeset state tracking (and it's
6239 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006240static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006241{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006242 struct drm_crtc *crtc = connector->base.state->crtc;
6243
6244 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6245 connector->base.base.id,
6246 connector->base.name);
6247
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006248 if (connector->get_hw_state(connector)) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006249 struct drm_encoder *encoder = &connector->encoder->base;
6250 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006251
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006252 I915_STATE_WARN(!crtc,
6253 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006254
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006255 if (!crtc)
6256 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006257
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006258 I915_STATE_WARN(!crtc->state->active,
6259 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006260
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006261 if (!encoder)
6262 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006263
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006264 I915_STATE_WARN(conn_state->best_encoder != encoder,
6265 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006266
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006267 I915_STATE_WARN(conn_state->crtc != encoder->crtc,
6268 "attached encoder crtc differs from connector crtc\n");
6269 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006270 I915_STATE_WARN(crtc && crtc->state->active,
6271 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006272 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6273 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006274 }
6275}
6276
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006277int intel_connector_init(struct intel_connector *connector)
6278{
6279 struct drm_connector_state *connector_state;
6280
6281 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6282 if (!connector_state)
6283 return -ENOMEM;
6284
6285 connector->base.state = connector_state;
6286 return 0;
6287}
6288
6289struct intel_connector *intel_connector_alloc(void)
6290{
6291 struct intel_connector *connector;
6292
6293 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6294 if (!connector)
6295 return NULL;
6296
6297 if (intel_connector_init(connector) < 0) {
6298 kfree(connector);
6299 return NULL;
6300 }
6301
6302 return connector;
6303}
6304
Daniel Vetterf0947c32012-07-02 13:10:34 +02006305/* Simple connector->get_hw_state implementation for encoders that support only
6306 * one connector and no cloning and hence the encoder state determines the state
6307 * of the connector. */
6308bool intel_connector_get_hw_state(struct intel_connector *connector)
6309{
Daniel Vetter24929352012-07-02 20:28:59 +02006310 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006311 struct intel_encoder *encoder = connector->encoder;
6312
6313 return encoder->get_hw_state(encoder, &pipe);
6314}
6315
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006316static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006317{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006318 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6319 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006320
6321 return 0;
6322}
6323
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006324static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006325 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006326{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006327 struct drm_atomic_state *state = pipe_config->base.state;
6328 struct intel_crtc *other_crtc;
6329 struct intel_crtc_state *other_crtc_state;
6330
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006331 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6332 pipe_name(pipe), pipe_config->fdi_lanes);
6333 if (pipe_config->fdi_lanes > 4) {
6334 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6335 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006336 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006337 }
6338
Paulo Zanonibafb6552013-11-02 21:07:44 -07006339 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006340 if (pipe_config->fdi_lanes > 2) {
6341 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6342 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006343 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006344 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006345 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006346 }
6347 }
6348
6349 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006350 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006351
6352 /* Ivybridge 3 pipe is really complicated */
6353 switch (pipe) {
6354 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006355 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006356 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006357 if (pipe_config->fdi_lanes <= 2)
6358 return 0;
6359
6360 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6361 other_crtc_state =
6362 intel_atomic_get_crtc_state(state, other_crtc);
6363 if (IS_ERR(other_crtc_state))
6364 return PTR_ERR(other_crtc_state);
6365
6366 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006367 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6368 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006369 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006370 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006371 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006372 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006373 if (pipe_config->fdi_lanes > 2) {
6374 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6375 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006376 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006377 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006378
6379 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6380 other_crtc_state =
6381 intel_atomic_get_crtc_state(state, other_crtc);
6382 if (IS_ERR(other_crtc_state))
6383 return PTR_ERR(other_crtc_state);
6384
6385 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006386 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006387 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006388 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006389 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006390 default:
6391 BUG();
6392 }
6393}
6394
Daniel Vettere29c22c2013-02-21 00:00:16 +01006395#define RETRY 1
6396static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006397 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006398{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006399 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006400 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006401 int lane, link_bw, fdi_dotclock, ret;
6402 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006403
Daniel Vettere29c22c2013-02-21 00:00:16 +01006404retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006405 /* FDI is a binary signal running at ~2.7GHz, encoding
6406 * each output octet as 10 bits. The actual frequency
6407 * is stored as a divider into a 100MHz clock, and the
6408 * mode pixel clock is stored in units of 1KHz.
6409 * Hence the bw of each lane in terms of the mode signal
6410 * is:
6411 */
6412 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6413
Damien Lespiau241bfc32013-09-25 16:45:37 +01006414 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006415
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006416 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006417 pipe_config->pipe_bpp);
6418
6419 pipe_config->fdi_lanes = lane;
6420
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006421 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006422 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006423
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006424 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6425 intel_crtc->pipe, pipe_config);
6426 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006427 pipe_config->pipe_bpp -= 2*3;
6428 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6429 pipe_config->pipe_bpp);
6430 needs_recompute = true;
6431 pipe_config->bw_constrained = true;
6432
6433 goto retry;
6434 }
6435
6436 if (needs_recompute)
6437 return RETRY;
6438
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006439 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006440}
6441
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006442static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6443 struct intel_crtc_state *pipe_config)
6444{
6445 if (pipe_config->pipe_bpp > 24)
6446 return false;
6447
6448 /* HSW can handle pixel rate up to cdclk? */
6449 if (IS_HASWELL(dev_priv->dev))
6450 return true;
6451
6452 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006453 * We compare against max which means we must take
6454 * the increased cdclk requirement into account when
6455 * calculating the new cdclk.
6456 *
6457 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006458 */
6459 return ilk_pipe_pixel_rate(pipe_config) <=
6460 dev_priv->max_cdclk_freq * 95 / 100;
6461}
6462
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006463static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006464 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006465{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006466 struct drm_device *dev = crtc->base.dev;
6467 struct drm_i915_private *dev_priv = dev->dev_private;
6468
Jani Nikulad330a952014-01-21 11:24:25 +02006469 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006470 hsw_crtc_supports_ips(crtc) &&
6471 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006472}
6473
Daniel Vettera43f6e02013-06-07 23:10:32 +02006474static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006475 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006476{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006477 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006478 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006479 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006480
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006481 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006482 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006483 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006484
6485 /*
6486 * Enable pixel doubling when the dot clock
6487 * is > 90% of the (display) core speed.
6488 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006489 * GDG double wide on either pipe,
6490 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006491 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006492 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006493 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006494 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006495 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006496 }
6497
Damien Lespiau241bfc32013-09-25 16:45:37 +01006498 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006499 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006500 }
Chris Wilson89749352010-09-12 18:25:19 +01006501
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006502 /*
6503 * Pipe horizontal size must be even in:
6504 * - DVO ganged mode
6505 * - LVDS dual channel mode
6506 * - Double wide pipe
6507 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006508 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006509 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6510 pipe_config->pipe_src_w &= ~1;
6511
Damien Lespiau8693a822013-05-03 18:48:11 +01006512 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6513 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006514 */
6515 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6516 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006517 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006518
Damien Lespiauf5adf942013-06-24 18:29:34 +01006519 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006520 hsw_compute_ips_config(crtc, pipe_config);
6521
Daniel Vetter877d48d2013-04-19 11:24:43 +02006522 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006523 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006524
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006525 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006526}
6527
Ville Syrjälä1652d192015-03-31 14:12:01 +03006528static int skylake_get_display_clock_speed(struct drm_device *dev)
6529{
6530 struct drm_i915_private *dev_priv = to_i915(dev);
6531 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6532 uint32_t cdctl = I915_READ(CDCLK_CTL);
6533 uint32_t linkrate;
6534
Damien Lespiau414355a2015-06-04 18:21:31 +01006535 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006536 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006537
6538 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6539 return 540000;
6540
6541 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006542 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006543
Damien Lespiau71cd8422015-04-30 16:39:17 +01006544 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6545 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006546 /* vco 8640 */
6547 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6548 case CDCLK_FREQ_450_432:
6549 return 432000;
6550 case CDCLK_FREQ_337_308:
6551 return 308570;
6552 case CDCLK_FREQ_675_617:
6553 return 617140;
6554 default:
6555 WARN(1, "Unknown cd freq selection\n");
6556 }
6557 } else {
6558 /* vco 8100 */
6559 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6560 case CDCLK_FREQ_450_432:
6561 return 450000;
6562 case CDCLK_FREQ_337_308:
6563 return 337500;
6564 case CDCLK_FREQ_675_617:
6565 return 675000;
6566 default:
6567 WARN(1, "Unknown cd freq selection\n");
6568 }
6569 }
6570
6571 /* error case, do as if DPLL0 isn't enabled */
6572 return 24000;
6573}
6574
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006575static int broxton_get_display_clock_speed(struct drm_device *dev)
6576{
6577 struct drm_i915_private *dev_priv = to_i915(dev);
6578 uint32_t cdctl = I915_READ(CDCLK_CTL);
6579 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6580 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6581 int cdclk;
6582
6583 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6584 return 19200;
6585
6586 cdclk = 19200 * pll_ratio / 2;
6587
6588 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6589 case BXT_CDCLK_CD2X_DIV_SEL_1:
6590 return cdclk; /* 576MHz or 624MHz */
6591 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6592 return cdclk * 2 / 3; /* 384MHz */
6593 case BXT_CDCLK_CD2X_DIV_SEL_2:
6594 return cdclk / 2; /* 288MHz */
6595 case BXT_CDCLK_CD2X_DIV_SEL_4:
6596 return cdclk / 4; /* 144MHz */
6597 }
6598
6599 /* error case, do as if DE PLL isn't enabled */
6600 return 19200;
6601}
6602
Ville Syrjälä1652d192015-03-31 14:12:01 +03006603static int broadwell_get_display_clock_speed(struct drm_device *dev)
6604{
6605 struct drm_i915_private *dev_priv = dev->dev_private;
6606 uint32_t lcpll = I915_READ(LCPLL_CTL);
6607 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6608
6609 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6610 return 800000;
6611 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6612 return 450000;
6613 else if (freq == LCPLL_CLK_FREQ_450)
6614 return 450000;
6615 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6616 return 540000;
6617 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6618 return 337500;
6619 else
6620 return 675000;
6621}
6622
6623static int haswell_get_display_clock_speed(struct drm_device *dev)
6624{
6625 struct drm_i915_private *dev_priv = dev->dev_private;
6626 uint32_t lcpll = I915_READ(LCPLL_CTL);
6627 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6628
6629 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6630 return 800000;
6631 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6632 return 450000;
6633 else if (freq == LCPLL_CLK_FREQ_450)
6634 return 450000;
6635 else if (IS_HSW_ULT(dev))
6636 return 337500;
6637 else
6638 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006639}
6640
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006641static int valleyview_get_display_clock_speed(struct drm_device *dev)
6642{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006643 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006644 u32 val;
6645 int divider;
6646
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006647 if (dev_priv->hpll_freq == 0)
6648 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6649
Ville Syrjäläa5805162015-05-26 20:42:30 +03006650 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006651 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006652 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006653
6654 divider = val & DISPLAY_FREQUENCY_VALUES;
6655
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006656 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6657 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6658 "cdclk change in progress\n");
6659
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006660 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006661}
6662
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006663static int ilk_get_display_clock_speed(struct drm_device *dev)
6664{
6665 return 450000;
6666}
6667
Jesse Barnese70236a2009-09-21 10:42:27 -07006668static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006669{
Jesse Barnese70236a2009-09-21 10:42:27 -07006670 return 400000;
6671}
Jesse Barnes79e53942008-11-07 14:24:08 -08006672
Jesse Barnese70236a2009-09-21 10:42:27 -07006673static int i915_get_display_clock_speed(struct drm_device *dev)
6674{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006675 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006676}
Jesse Barnes79e53942008-11-07 14:24:08 -08006677
Jesse Barnese70236a2009-09-21 10:42:27 -07006678static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6679{
6680 return 200000;
6681}
Jesse Barnes79e53942008-11-07 14:24:08 -08006682
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006683static int pnv_get_display_clock_speed(struct drm_device *dev)
6684{
6685 u16 gcfgc = 0;
6686
6687 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6688
6689 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6690 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006691 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006692 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006693 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006694 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006695 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006696 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6697 return 200000;
6698 default:
6699 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6700 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006701 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006702 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006703 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006704 }
6705}
6706
Jesse Barnese70236a2009-09-21 10:42:27 -07006707static int i915gm_get_display_clock_speed(struct drm_device *dev)
6708{
6709 u16 gcfgc = 0;
6710
6711 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6712
6713 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006714 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006715 else {
6716 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6717 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006718 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006719 default:
6720 case GC_DISPLAY_CLOCK_190_200_MHZ:
6721 return 190000;
6722 }
6723 }
6724}
Jesse Barnes79e53942008-11-07 14:24:08 -08006725
Jesse Barnese70236a2009-09-21 10:42:27 -07006726static int i865_get_display_clock_speed(struct drm_device *dev)
6727{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006728 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006729}
6730
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006731static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006732{
6733 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006734
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006735 /*
6736 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6737 * encoding is different :(
6738 * FIXME is this the right way to detect 852GM/852GMV?
6739 */
6740 if (dev->pdev->revision == 0x1)
6741 return 133333;
6742
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006743 pci_bus_read_config_word(dev->pdev->bus,
6744 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6745
Jesse Barnese70236a2009-09-21 10:42:27 -07006746 /* Assume that the hardware is in the high speed state. This
6747 * should be the default.
6748 */
6749 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6750 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006751 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006752 case GC_CLOCK_100_200:
6753 return 200000;
6754 case GC_CLOCK_166_250:
6755 return 250000;
6756 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006757 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006758 case GC_CLOCK_133_266:
6759 case GC_CLOCK_133_266_2:
6760 case GC_CLOCK_166_266:
6761 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006762 }
6763
6764 /* Shouldn't happen */
6765 return 0;
6766}
6767
6768static int i830_get_display_clock_speed(struct drm_device *dev)
6769{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006770 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006771}
6772
Ville Syrjälä34edce22015-05-22 11:22:33 +03006773static unsigned int intel_hpll_vco(struct drm_device *dev)
6774{
6775 struct drm_i915_private *dev_priv = dev->dev_private;
6776 static const unsigned int blb_vco[8] = {
6777 [0] = 3200000,
6778 [1] = 4000000,
6779 [2] = 5333333,
6780 [3] = 4800000,
6781 [4] = 6400000,
6782 };
6783 static const unsigned int pnv_vco[8] = {
6784 [0] = 3200000,
6785 [1] = 4000000,
6786 [2] = 5333333,
6787 [3] = 4800000,
6788 [4] = 2666667,
6789 };
6790 static const unsigned int cl_vco[8] = {
6791 [0] = 3200000,
6792 [1] = 4000000,
6793 [2] = 5333333,
6794 [3] = 6400000,
6795 [4] = 3333333,
6796 [5] = 3566667,
6797 [6] = 4266667,
6798 };
6799 static const unsigned int elk_vco[8] = {
6800 [0] = 3200000,
6801 [1] = 4000000,
6802 [2] = 5333333,
6803 [3] = 4800000,
6804 };
6805 static const unsigned int ctg_vco[8] = {
6806 [0] = 3200000,
6807 [1] = 4000000,
6808 [2] = 5333333,
6809 [3] = 6400000,
6810 [4] = 2666667,
6811 [5] = 4266667,
6812 };
6813 const unsigned int *vco_table;
6814 unsigned int vco;
6815 uint8_t tmp = 0;
6816
6817 /* FIXME other chipsets? */
6818 if (IS_GM45(dev))
6819 vco_table = ctg_vco;
6820 else if (IS_G4X(dev))
6821 vco_table = elk_vco;
6822 else if (IS_CRESTLINE(dev))
6823 vco_table = cl_vco;
6824 else if (IS_PINEVIEW(dev))
6825 vco_table = pnv_vco;
6826 else if (IS_G33(dev))
6827 vco_table = blb_vco;
6828 else
6829 return 0;
6830
6831 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6832
6833 vco = vco_table[tmp & 0x7];
6834 if (vco == 0)
6835 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6836 else
6837 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6838
6839 return vco;
6840}
6841
6842static int gm45_get_display_clock_speed(struct drm_device *dev)
6843{
6844 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6845 uint16_t tmp = 0;
6846
6847 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6848
6849 cdclk_sel = (tmp >> 12) & 0x1;
6850
6851 switch (vco) {
6852 case 2666667:
6853 case 4000000:
6854 case 5333333:
6855 return cdclk_sel ? 333333 : 222222;
6856 case 3200000:
6857 return cdclk_sel ? 320000 : 228571;
6858 default:
6859 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6860 return 222222;
6861 }
6862}
6863
6864static int i965gm_get_display_clock_speed(struct drm_device *dev)
6865{
6866 static const uint8_t div_3200[] = { 16, 10, 8 };
6867 static const uint8_t div_4000[] = { 20, 12, 10 };
6868 static const uint8_t div_5333[] = { 24, 16, 14 };
6869 const uint8_t *div_table;
6870 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6871 uint16_t tmp = 0;
6872
6873 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6874
6875 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6876
6877 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6878 goto fail;
6879
6880 switch (vco) {
6881 case 3200000:
6882 div_table = div_3200;
6883 break;
6884 case 4000000:
6885 div_table = div_4000;
6886 break;
6887 case 5333333:
6888 div_table = div_5333;
6889 break;
6890 default:
6891 goto fail;
6892 }
6893
6894 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6895
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006896fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006897 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6898 return 200000;
6899}
6900
6901static int g33_get_display_clock_speed(struct drm_device *dev)
6902{
6903 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6904 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6905 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6906 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6907 const uint8_t *div_table;
6908 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6909 uint16_t tmp = 0;
6910
6911 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6912
6913 cdclk_sel = (tmp >> 4) & 0x7;
6914
6915 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6916 goto fail;
6917
6918 switch (vco) {
6919 case 3200000:
6920 div_table = div_3200;
6921 break;
6922 case 4000000:
6923 div_table = div_4000;
6924 break;
6925 case 4800000:
6926 div_table = div_4800;
6927 break;
6928 case 5333333:
6929 div_table = div_5333;
6930 break;
6931 default:
6932 goto fail;
6933 }
6934
6935 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6936
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006937fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006938 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6939 return 190476;
6940}
6941
Zhenyu Wang2c072452009-06-05 15:38:42 +08006942static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006943intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006944{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006945 while (*num > DATA_LINK_M_N_MASK ||
6946 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006947 *num >>= 1;
6948 *den >>= 1;
6949 }
6950}
6951
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006952static void compute_m_n(unsigned int m, unsigned int n,
6953 uint32_t *ret_m, uint32_t *ret_n)
6954{
6955 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6956 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6957 intel_reduce_m_n_ratio(ret_m, ret_n);
6958}
6959
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006960void
6961intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6962 int pixel_clock, int link_clock,
6963 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006964{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006965 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006966
6967 compute_m_n(bits_per_pixel * pixel_clock,
6968 link_clock * nlanes * 8,
6969 &m_n->gmch_m, &m_n->gmch_n);
6970
6971 compute_m_n(pixel_clock, link_clock,
6972 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006973}
6974
Chris Wilsona7615032011-01-12 17:04:08 +00006975static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6976{
Jani Nikulad330a952014-01-21 11:24:25 +02006977 if (i915.panel_use_ssc >= 0)
6978 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006979 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006980 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006981}
6982
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006983static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6984 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006985{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006986 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006987 struct drm_i915_private *dev_priv = dev->dev_private;
6988 int refclk;
6989
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006990 WARN_ON(!crtc_state->base.state);
6991
Imre Deak5ab7b0b2015-03-06 03:29:25 +02006992 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006993 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006994 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006995 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006996 refclk = dev_priv->vbt.lvds_ssc_freq;
6997 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006998 } else if (!IS_GEN2(dev)) {
6999 refclk = 96000;
7000 } else {
7001 refclk = 48000;
7002 }
7003
7004 return refclk;
7005}
7006
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007007static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007008{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007009 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007010}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007011
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007012static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7013{
7014 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007015}
7016
Daniel Vetterf47709a2013-03-28 10:42:02 +01007017static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007018 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007019 intel_clock_t *reduced_clock)
7020{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007021 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007022 u32 fp, fp2 = 0;
7023
7024 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007025 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007026 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007027 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007028 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007029 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007030 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007031 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007032 }
7033
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007034 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007035
Daniel Vetterf47709a2013-03-28 10:42:02 +01007036 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007037 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007038 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007039 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007040 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007041 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007042 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007043 }
7044}
7045
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007046static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7047 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007048{
7049 u32 reg_val;
7050
7051 /*
7052 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7053 * and set it to a reasonable value instead.
7054 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007055 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007056 reg_val &= 0xffffff00;
7057 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007058 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007059
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007060 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007061 reg_val &= 0x8cffffff;
7062 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007063 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007064
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007065 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007066 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007067 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007068
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007069 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007070 reg_val &= 0x00ffffff;
7071 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007072 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007073}
7074
Daniel Vetterb5518422013-05-03 11:49:48 +02007075static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7076 struct intel_link_m_n *m_n)
7077{
7078 struct drm_device *dev = crtc->base.dev;
7079 struct drm_i915_private *dev_priv = dev->dev_private;
7080 int pipe = crtc->pipe;
7081
Daniel Vettere3b95f12013-05-03 11:49:49 +02007082 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7083 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7084 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7085 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007086}
7087
7088static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007089 struct intel_link_m_n *m_n,
7090 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007091{
7092 struct drm_device *dev = crtc->base.dev;
7093 struct drm_i915_private *dev_priv = dev->dev_private;
7094 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007095 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007096
7097 if (INTEL_INFO(dev)->gen >= 5) {
7098 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7099 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7100 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7101 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007102 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7103 * for gen < 8) and if DRRS is supported (to make sure the
7104 * registers are not unnecessarily accessed).
7105 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307106 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007107 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007108 I915_WRITE(PIPE_DATA_M2(transcoder),
7109 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7110 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7111 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7112 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7113 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007114 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007115 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7116 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7117 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7118 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007119 }
7120}
7121
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307122void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007123{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307124 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7125
7126 if (m_n == M1_N1) {
7127 dp_m_n = &crtc->config->dp_m_n;
7128 dp_m2_n2 = &crtc->config->dp_m2_n2;
7129 } else if (m_n == M2_N2) {
7130
7131 /*
7132 * M2_N2 registers are not supported. Hence m2_n2 divider value
7133 * needs to be programmed into M1_N1.
7134 */
7135 dp_m_n = &crtc->config->dp_m2_n2;
7136 } else {
7137 DRM_ERROR("Unsupported divider value\n");
7138 return;
7139 }
7140
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007141 if (crtc->config->has_pch_encoder)
7142 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007143 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307144 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007145}
7146
Daniel Vetter251ac862015-06-18 10:30:24 +02007147static void vlv_compute_dpll(struct intel_crtc *crtc,
7148 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007149{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007150 u32 dpll, dpll_md;
7151
7152 /*
7153 * Enable DPIO clock input. We should never disable the reference
7154 * clock for pipe B, since VGA hotplug / manual detection depends
7155 * on it.
7156 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007157 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7158 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007159 /* We should never disable this, set it here for state tracking */
7160 if (crtc->pipe == PIPE_B)
7161 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7162 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007163 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007164
Ville Syrjäläd288f652014-10-28 13:20:22 +02007165 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007166 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007167 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007168}
7169
Ville Syrjäläd288f652014-10-28 13:20:22 +02007170static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007171 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007172{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007173 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007174 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007175 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007176 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007177 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007178 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007179
Ville Syrjäläa5805162015-05-26 20:42:30 +03007180 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007181
Ville Syrjäläd288f652014-10-28 13:20:22 +02007182 bestn = pipe_config->dpll.n;
7183 bestm1 = pipe_config->dpll.m1;
7184 bestm2 = pipe_config->dpll.m2;
7185 bestp1 = pipe_config->dpll.p1;
7186 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007187
Jesse Barnes89b667f2013-04-18 14:51:36 -07007188 /* See eDP HDMI DPIO driver vbios notes doc */
7189
7190 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007191 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007192 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007193
7194 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007195 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007196
7197 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007198 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007199 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007200 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007201
7202 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007203 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007204
7205 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007206 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7207 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7208 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007209 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007210
7211 /*
7212 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7213 * but we don't support that).
7214 * Note: don't use the DAC post divider as it seems unstable.
7215 */
7216 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007217 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007218
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007219 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007220 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007221
Jesse Barnes89b667f2013-04-18 14:51:36 -07007222 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007223 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007224 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7225 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007226 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007227 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007228 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007229 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007230 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007231
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007232 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007233 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007234 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007235 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007236 0x0df40000);
7237 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007238 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007239 0x0df70000);
7240 } else { /* HDMI or VGA */
7241 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007242 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007243 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007244 0x0df70000);
7245 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007246 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007247 0x0df40000);
7248 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007249
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007250 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007251 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007252 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7253 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007254 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007255 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007256
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007257 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007258 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007259}
7260
Daniel Vetter251ac862015-06-18 10:30:24 +02007261static void chv_compute_dpll(struct intel_crtc *crtc,
7262 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007263{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007264 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7265 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007266 DPLL_VCO_ENABLE;
7267 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007268 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007269
Ville Syrjäläd288f652014-10-28 13:20:22 +02007270 pipe_config->dpll_hw_state.dpll_md =
7271 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007272}
7273
Ville Syrjäläd288f652014-10-28 13:20:22 +02007274static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007275 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007276{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007277 struct drm_device *dev = crtc->base.dev;
7278 struct drm_i915_private *dev_priv = dev->dev_private;
7279 int pipe = crtc->pipe;
7280 int dpll_reg = DPLL(crtc->pipe);
7281 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307282 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007283 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307284 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307285 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007286
Ville Syrjäläd288f652014-10-28 13:20:22 +02007287 bestn = pipe_config->dpll.n;
7288 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7289 bestm1 = pipe_config->dpll.m1;
7290 bestm2 = pipe_config->dpll.m2 >> 22;
7291 bestp1 = pipe_config->dpll.p1;
7292 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307293 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307294 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307295 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007296
7297 /*
7298 * Enable Refclk and SSC
7299 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007300 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007301 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007302
Ville Syrjäläa5805162015-05-26 20:42:30 +03007303 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007304
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007305 /* p1 and p2 divider */
7306 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7307 5 << DPIO_CHV_S1_DIV_SHIFT |
7308 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7309 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7310 1 << DPIO_CHV_K_DIV_SHIFT);
7311
7312 /* Feedback post-divider - m2 */
7313 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7314
7315 /* Feedback refclk divider - n and m1 */
7316 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7317 DPIO_CHV_M1_DIV_BY_2 |
7318 1 << DPIO_CHV_N_DIV_SHIFT);
7319
7320 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007321 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007322
7323 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307324 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7325 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7326 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7327 if (bestm2_frac)
7328 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7329 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007330
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307331 /* Program digital lock detect threshold */
7332 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7333 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7334 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7335 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7336 if (!bestm2_frac)
7337 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7338 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7339
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007340 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307341 if (vco == 5400000) {
7342 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7343 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7344 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7345 tribuf_calcntr = 0x9;
7346 } else if (vco <= 6200000) {
7347 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7348 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7349 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7350 tribuf_calcntr = 0x9;
7351 } else if (vco <= 6480000) {
7352 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7353 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7354 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7355 tribuf_calcntr = 0x8;
7356 } else {
7357 /* Not supported. Apply the same limits as in the max case */
7358 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7359 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7360 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7361 tribuf_calcntr = 0;
7362 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007363 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7364
Ville Syrjälä968040b2015-03-11 22:52:08 +02007365 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307366 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7367 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7368 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7369
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007370 /* AFC Recal */
7371 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7372 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7373 DPIO_AFC_RECAL);
7374
Ville Syrjäläa5805162015-05-26 20:42:30 +03007375 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007376}
7377
Ville Syrjäläd288f652014-10-28 13:20:22 +02007378/**
7379 * vlv_force_pll_on - forcibly enable just the PLL
7380 * @dev_priv: i915 private structure
7381 * @pipe: pipe PLL to enable
7382 * @dpll: PLL configuration
7383 *
7384 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7385 * in cases where we need the PLL enabled even when @pipe is not going to
7386 * be enabled.
7387 */
7388void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7389 const struct dpll *dpll)
7390{
7391 struct intel_crtc *crtc =
7392 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007393 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007394 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007395 .pixel_multiplier = 1,
7396 .dpll = *dpll,
7397 };
7398
7399 if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007400 chv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007401 chv_prepare_pll(crtc, &pipe_config);
7402 chv_enable_pll(crtc, &pipe_config);
7403 } else {
Daniel Vetter251ac862015-06-18 10:30:24 +02007404 vlv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007405 vlv_prepare_pll(crtc, &pipe_config);
7406 vlv_enable_pll(crtc, &pipe_config);
7407 }
7408}
7409
7410/**
7411 * vlv_force_pll_off - forcibly disable just the PLL
7412 * @dev_priv: i915 private structure
7413 * @pipe: pipe PLL to disable
7414 *
7415 * Disable the PLL for @pipe. To be used in cases where we need
7416 * the PLL enabled even when @pipe is not going to be enabled.
7417 */
7418void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7419{
7420 if (IS_CHERRYVIEW(dev))
7421 chv_disable_pll(to_i915(dev), pipe);
7422 else
7423 vlv_disable_pll(to_i915(dev), pipe);
7424}
7425
Daniel Vetter251ac862015-06-18 10:30:24 +02007426static void i9xx_compute_dpll(struct intel_crtc *crtc,
7427 struct intel_crtc_state *crtc_state,
7428 intel_clock_t *reduced_clock,
7429 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007430{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007431 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007432 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007433 u32 dpll;
7434 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007435 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007436
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007437 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307438
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007439 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7440 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007441
7442 dpll = DPLL_VGA_MODE_DIS;
7443
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007444 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007445 dpll |= DPLLB_MODE_LVDS;
7446 else
7447 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007448
Daniel Vetteref1b4602013-06-01 17:17:04 +02007449 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007450 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007451 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007452 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007453
7454 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007455 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007456
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007457 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007458 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007459
7460 /* compute bitmask from p1 value */
7461 if (IS_PINEVIEW(dev))
7462 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7463 else {
7464 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7465 if (IS_G4X(dev) && reduced_clock)
7466 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7467 }
7468 switch (clock->p2) {
7469 case 5:
7470 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7471 break;
7472 case 7:
7473 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7474 break;
7475 case 10:
7476 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7477 break;
7478 case 14:
7479 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7480 break;
7481 }
7482 if (INTEL_INFO(dev)->gen >= 4)
7483 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7484
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007485 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007486 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007487 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007488 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7489 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7490 else
7491 dpll |= PLL_REF_INPUT_DREFCLK;
7492
7493 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007494 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007495
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007496 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007497 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007498 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007499 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007500 }
7501}
7502
Daniel Vetter251ac862015-06-18 10:30:24 +02007503static void i8xx_compute_dpll(struct intel_crtc *crtc,
7504 struct intel_crtc_state *crtc_state,
7505 intel_clock_t *reduced_clock,
7506 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007507{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007508 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007509 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007510 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007511 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007512
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007513 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307514
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007515 dpll = DPLL_VGA_MODE_DIS;
7516
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007517 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007518 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7519 } else {
7520 if (clock->p1 == 2)
7521 dpll |= PLL_P1_DIVIDE_BY_TWO;
7522 else
7523 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7524 if (clock->p2 == 4)
7525 dpll |= PLL_P2_DIVIDE_BY_4;
7526 }
7527
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007528 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007529 dpll |= DPLL_DVO_2X_MODE;
7530
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007531 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007532 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7533 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7534 else
7535 dpll |= PLL_REF_INPUT_DREFCLK;
7536
7537 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007538 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007539}
7540
Daniel Vetter8a654f32013-06-01 17:16:22 +02007541static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007542{
7543 struct drm_device *dev = intel_crtc->base.dev;
7544 struct drm_i915_private *dev_priv = dev->dev_private;
7545 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007546 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007547 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007548 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007549 uint32_t crtc_vtotal, crtc_vblank_end;
7550 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007551
7552 /* We need to be careful not to changed the adjusted mode, for otherwise
7553 * the hw state checker will get angry at the mismatch. */
7554 crtc_vtotal = adjusted_mode->crtc_vtotal;
7555 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007556
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007557 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007558 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007559 crtc_vtotal -= 1;
7560 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007561
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007562 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007563 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7564 else
7565 vsyncshift = adjusted_mode->crtc_hsync_start -
7566 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007567 if (vsyncshift < 0)
7568 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007569 }
7570
7571 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007572 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007573
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007574 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007575 (adjusted_mode->crtc_hdisplay - 1) |
7576 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007577 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007578 (adjusted_mode->crtc_hblank_start - 1) |
7579 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007580 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007581 (adjusted_mode->crtc_hsync_start - 1) |
7582 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7583
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007584 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007585 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007586 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007587 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007588 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007589 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007590 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007591 (adjusted_mode->crtc_vsync_start - 1) |
7592 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7593
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007594 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7595 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7596 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7597 * bits. */
7598 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7599 (pipe == PIPE_B || pipe == PIPE_C))
7600 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7601
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007602 /* pipesrc controls the size that is scaled from, which should
7603 * always be the user's requested size.
7604 */
7605 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007606 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7607 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007608}
7609
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007610static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007611 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007612{
7613 struct drm_device *dev = crtc->base.dev;
7614 struct drm_i915_private *dev_priv = dev->dev_private;
7615 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7616 uint32_t tmp;
7617
7618 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007619 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7620 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007621 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007622 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7623 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007624 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007625 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7626 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007627
7628 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007629 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7630 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007631 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007632 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7633 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007634 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007635 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7636 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007637
7638 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007639 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7640 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7641 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007642 }
7643
7644 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007645 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7646 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7647
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007648 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7649 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007650}
7651
Daniel Vetterf6a83282014-02-11 15:28:57 -08007652void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007653 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007654{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007655 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7656 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7657 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7658 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007659
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007660 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7661 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7662 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7663 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007664
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007665 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007666 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007667
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007668 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7669 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007670
7671 mode->hsync = drm_mode_hsync(mode);
7672 mode->vrefresh = drm_mode_vrefresh(mode);
7673 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007674}
7675
Daniel Vetter84b046f2013-02-19 18:48:54 +01007676static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7677{
7678 struct drm_device *dev = intel_crtc->base.dev;
7679 struct drm_i915_private *dev_priv = dev->dev_private;
7680 uint32_t pipeconf;
7681
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007682 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007683
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007684 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7685 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7686 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007687
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007688 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007689 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007690
Daniel Vetterff9ce462013-04-24 14:57:17 +02007691 /* only g4x and later have fancy bpc/dither controls */
7692 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007693 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007694 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007695 pipeconf |= PIPECONF_DITHER_EN |
7696 PIPECONF_DITHER_TYPE_SP;
7697
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007698 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007699 case 18:
7700 pipeconf |= PIPECONF_6BPC;
7701 break;
7702 case 24:
7703 pipeconf |= PIPECONF_8BPC;
7704 break;
7705 case 30:
7706 pipeconf |= PIPECONF_10BPC;
7707 break;
7708 default:
7709 /* Case prevented by intel_choose_pipe_bpp_dither. */
7710 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007711 }
7712 }
7713
7714 if (HAS_PIPE_CXSR(dev)) {
7715 if (intel_crtc->lowfreq_avail) {
7716 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7717 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7718 } else {
7719 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007720 }
7721 }
7722
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007723 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007724 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007725 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007726 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7727 else
7728 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7729 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007730 pipeconf |= PIPECONF_PROGRESSIVE;
7731
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007732 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007733 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007734
Daniel Vetter84b046f2013-02-19 18:48:54 +01007735 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7736 POSTING_READ(PIPECONF(intel_crtc->pipe));
7737}
7738
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007739static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7740 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007741{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007742 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007743 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007744 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007745 intel_clock_t clock;
7746 bool ok;
7747 bool is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007748 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007749 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007750 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007751 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007752 struct drm_connector_state *connector_state;
7753 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007754
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007755 memset(&crtc_state->dpll_hw_state, 0,
7756 sizeof(crtc_state->dpll_hw_state));
7757
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007758 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007759 if (connector_state->crtc != &crtc->base)
7760 continue;
7761
7762 encoder = to_intel_encoder(connector_state->best_encoder);
7763
Chris Wilson5eddb702010-09-11 13:48:45 +01007764 switch (encoder->type) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007765 case INTEL_OUTPUT_DSI:
7766 is_dsi = true;
7767 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007768 default:
7769 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007770 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007771
Eric Anholtc751ce42010-03-25 11:48:48 -07007772 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007773 }
7774
Jani Nikulaf2335332013-09-13 11:03:09 +03007775 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007776 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007777
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007778 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007779 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007780
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007781 /*
7782 * Returns a set of divisors for the desired target clock with
7783 * the given refclk, or FALSE. The returned values represent
7784 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7785 * 2) / p1 / p2.
7786 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007787 limit = intel_limit(crtc_state, refclk);
7788 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007789 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007790 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007791 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007792 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7793 return -EINVAL;
7794 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007795
Jani Nikulaf2335332013-09-13 11:03:09 +03007796 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007797 crtc_state->dpll.n = clock.n;
7798 crtc_state->dpll.m1 = clock.m1;
7799 crtc_state->dpll.m2 = clock.m2;
7800 crtc_state->dpll.p1 = clock.p1;
7801 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007802 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007803
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007804 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007805 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007806 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007807 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007808 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007809 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007810 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007811 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007812 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007813 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007814 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007815
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007816 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007817}
7818
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007819static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007820 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007821{
7822 struct drm_device *dev = crtc->base.dev;
7823 struct drm_i915_private *dev_priv = dev->dev_private;
7824 uint32_t tmp;
7825
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007826 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7827 return;
7828
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007829 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007830 if (!(tmp & PFIT_ENABLE))
7831 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007832
Daniel Vetter06922822013-07-11 13:35:40 +02007833 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007834 if (INTEL_INFO(dev)->gen < 4) {
7835 if (crtc->pipe != PIPE_B)
7836 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007837 } else {
7838 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7839 return;
7840 }
7841
Daniel Vetter06922822013-07-11 13:35:40 +02007842 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007843 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7844 if (INTEL_INFO(dev)->gen < 5)
7845 pipe_config->gmch_pfit.lvds_border_bits =
7846 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7847}
7848
Jesse Barnesacbec812013-09-20 11:29:32 -07007849static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007850 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007851{
7852 struct drm_device *dev = crtc->base.dev;
7853 struct drm_i915_private *dev_priv = dev->dev_private;
7854 int pipe = pipe_config->cpu_transcoder;
7855 intel_clock_t clock;
7856 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007857 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007858
Shobhit Kumarf573de52014-07-30 20:32:37 +05307859 /* In case of MIPI DPLL will not even be used */
7860 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7861 return;
7862
Ville Syrjäläa5805162015-05-26 20:42:30 +03007863 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007864 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007865 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007866
7867 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7868 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7869 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7870 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7871 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7872
Imre Deakdccbea32015-06-22 23:35:51 +03007873 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007874}
7875
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007876static void
7877i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7878 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007879{
7880 struct drm_device *dev = crtc->base.dev;
7881 struct drm_i915_private *dev_priv = dev->dev_private;
7882 u32 val, base, offset;
7883 int pipe = crtc->pipe, plane = crtc->plane;
7884 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007885 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007886 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007887 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007888
Damien Lespiau42a7b082015-02-05 19:35:13 +00007889 val = I915_READ(DSPCNTR(plane));
7890 if (!(val & DISPLAY_PLANE_ENABLE))
7891 return;
7892
Damien Lespiaud9806c92015-01-21 14:07:19 +00007893 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007894 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007895 DRM_DEBUG_KMS("failed to alloc fb\n");
7896 return;
7897 }
7898
Damien Lespiau1b842c82015-01-21 13:50:54 +00007899 fb = &intel_fb->base;
7900
Daniel Vetter18c52472015-02-10 17:16:09 +00007901 if (INTEL_INFO(dev)->gen >= 4) {
7902 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007903 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007904 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7905 }
7906 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007907
7908 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007909 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007910 fb->pixel_format = fourcc;
7911 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007912
7913 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007914 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007915 offset = I915_READ(DSPTILEOFF(plane));
7916 else
7917 offset = I915_READ(DSPLINOFF(plane));
7918 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7919 } else {
7920 base = I915_READ(DSPADDR(plane));
7921 }
7922 plane_config->base = base;
7923
7924 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007925 fb->width = ((val >> 16) & 0xfff) + 1;
7926 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007927
7928 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007929 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007930
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007931 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007932 fb->pixel_format,
7933 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007934
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007935 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007936
Damien Lespiau2844a922015-01-20 12:51:48 +00007937 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7938 pipe_name(pipe), plane, fb->width, fb->height,
7939 fb->bits_per_pixel, base, fb->pitches[0],
7940 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007941
Damien Lespiau2d140302015-02-05 17:22:18 +00007942 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007943}
7944
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007945static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007946 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007947{
7948 struct drm_device *dev = crtc->base.dev;
7949 struct drm_i915_private *dev_priv = dev->dev_private;
7950 int pipe = pipe_config->cpu_transcoder;
7951 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7952 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007953 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007954 int refclk = 100000;
7955
Ville Syrjäläa5805162015-05-26 20:42:30 +03007956 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007957 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7958 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7959 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7960 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03007961 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007962 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007963
7964 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007965 clock.m2 = (pll_dw0 & 0xff) << 22;
7966 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7967 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007968 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7969 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7970 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7971
Imre Deakdccbea32015-06-22 23:35:51 +03007972 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007973}
7974
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007975static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007976 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007977{
7978 struct drm_device *dev = crtc->base.dev;
7979 struct drm_i915_private *dev_priv = dev->dev_private;
7980 uint32_t tmp;
7981
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007982 if (!intel_display_power_is_enabled(dev_priv,
7983 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007984 return false;
7985
Daniel Vettere143a212013-07-04 12:01:15 +02007986 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007987 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007988
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007989 tmp = I915_READ(PIPECONF(crtc->pipe));
7990 if (!(tmp & PIPECONF_ENABLE))
7991 return false;
7992
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007993 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7994 switch (tmp & PIPECONF_BPC_MASK) {
7995 case PIPECONF_6BPC:
7996 pipe_config->pipe_bpp = 18;
7997 break;
7998 case PIPECONF_8BPC:
7999 pipe_config->pipe_bpp = 24;
8000 break;
8001 case PIPECONF_10BPC:
8002 pipe_config->pipe_bpp = 30;
8003 break;
8004 default:
8005 break;
8006 }
8007 }
8008
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008009 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8010 pipe_config->limited_color_range = true;
8011
Ville Syrjälä282740f2013-09-04 18:30:03 +03008012 if (INTEL_INFO(dev)->gen < 4)
8013 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8014
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008015 intel_get_pipe_timings(crtc, pipe_config);
8016
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008017 i9xx_get_pfit_config(crtc, pipe_config);
8018
Daniel Vetter6c49f242013-06-06 12:45:25 +02008019 if (INTEL_INFO(dev)->gen >= 4) {
8020 tmp = I915_READ(DPLL_MD(crtc->pipe));
8021 pipe_config->pixel_multiplier =
8022 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8023 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008024 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008025 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8026 tmp = I915_READ(DPLL(crtc->pipe));
8027 pipe_config->pixel_multiplier =
8028 ((tmp & SDVO_MULTIPLIER_MASK)
8029 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8030 } else {
8031 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8032 * port and will be fixed up in the encoder->get_config
8033 * function. */
8034 pipe_config->pixel_multiplier = 1;
8035 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008036 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8037 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008038 /*
8039 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8040 * on 830. Filter it out here so that we don't
8041 * report errors due to that.
8042 */
8043 if (IS_I830(dev))
8044 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8045
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008046 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8047 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008048 } else {
8049 /* Mask out read-only status bits. */
8050 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8051 DPLL_PORTC_READY_MASK |
8052 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008053 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008054
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008055 if (IS_CHERRYVIEW(dev))
8056 chv_crtc_clock_get(crtc, pipe_config);
8057 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008058 vlv_crtc_clock_get(crtc, pipe_config);
8059 else
8060 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008061
Ville Syrjälä0f646142015-08-26 19:39:18 +03008062 /*
8063 * Normally the dotclock is filled in by the encoder .get_config()
8064 * but in case the pipe is enabled w/o any ports we need a sane
8065 * default.
8066 */
8067 pipe_config->base.adjusted_mode.crtc_clock =
8068 pipe_config->port_clock / pipe_config->pixel_multiplier;
8069
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008070 return true;
8071}
8072
Paulo Zanonidde86e22012-12-01 12:04:25 -02008073static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008074{
8075 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008076 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008077 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008078 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008079 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008080 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008081 bool has_ck505 = false;
8082 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008083
8084 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008085 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008086 switch (encoder->type) {
8087 case INTEL_OUTPUT_LVDS:
8088 has_panel = true;
8089 has_lvds = true;
8090 break;
8091 case INTEL_OUTPUT_EDP:
8092 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008093 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008094 has_cpu_edp = true;
8095 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008096 default:
8097 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008098 }
8099 }
8100
Keith Packard99eb6a02011-09-26 14:29:12 -07008101 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008102 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008103 can_ssc = has_ck505;
8104 } else {
8105 has_ck505 = false;
8106 can_ssc = true;
8107 }
8108
Imre Deak2de69052013-05-08 13:14:04 +03008109 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8110 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008111
8112 /* Ironlake: try to setup display ref clock before DPLL
8113 * enabling. This is only under driver's control after
8114 * PCH B stepping, previous chipset stepping should be
8115 * ignoring this setting.
8116 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008117 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008118
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008119 /* As we must carefully and slowly disable/enable each source in turn,
8120 * compute the final state we want first and check if we need to
8121 * make any changes at all.
8122 */
8123 final = val;
8124 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008125 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008126 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008127 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008128 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8129
8130 final &= ~DREF_SSC_SOURCE_MASK;
8131 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8132 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008133
Keith Packard199e5d72011-09-22 12:01:57 -07008134 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008135 final |= DREF_SSC_SOURCE_ENABLE;
8136
8137 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8138 final |= DREF_SSC1_ENABLE;
8139
8140 if (has_cpu_edp) {
8141 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8142 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8143 else
8144 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8145 } else
8146 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8147 } else {
8148 final |= DREF_SSC_SOURCE_DISABLE;
8149 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8150 }
8151
8152 if (final == val)
8153 return;
8154
8155 /* Always enable nonspread source */
8156 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8157
8158 if (has_ck505)
8159 val |= DREF_NONSPREAD_CK505_ENABLE;
8160 else
8161 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8162
8163 if (has_panel) {
8164 val &= ~DREF_SSC_SOURCE_MASK;
8165 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008166
Keith Packard199e5d72011-09-22 12:01:57 -07008167 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008168 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008169 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008170 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008171 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008172 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008173
8174 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008175 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008176 POSTING_READ(PCH_DREF_CONTROL);
8177 udelay(200);
8178
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008179 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008180
8181 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008182 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008183 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008184 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008185 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008186 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008187 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008188 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008189 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008190
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008191 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008192 POSTING_READ(PCH_DREF_CONTROL);
8193 udelay(200);
8194 } else {
8195 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8196
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008197 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008198
8199 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008200 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008201
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008202 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008203 POSTING_READ(PCH_DREF_CONTROL);
8204 udelay(200);
8205
8206 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008207 val &= ~DREF_SSC_SOURCE_MASK;
8208 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008209
8210 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008211 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008212
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008213 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008214 POSTING_READ(PCH_DREF_CONTROL);
8215 udelay(200);
8216 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008217
8218 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008219}
8220
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008221static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008222{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008223 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008224
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008225 tmp = I915_READ(SOUTH_CHICKEN2);
8226 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8227 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008228
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008229 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8230 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8231 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008232
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008233 tmp = I915_READ(SOUTH_CHICKEN2);
8234 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8235 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008236
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008237 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8238 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8239 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008240}
8241
8242/* WaMPhyProgramming:hsw */
8243static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8244{
8245 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008246
8247 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8248 tmp &= ~(0xFF << 24);
8249 tmp |= (0x12 << 24);
8250 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8251
Paulo Zanonidde86e22012-12-01 12:04:25 -02008252 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8253 tmp |= (1 << 11);
8254 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8255
8256 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8257 tmp |= (1 << 11);
8258 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8259
Paulo Zanonidde86e22012-12-01 12:04:25 -02008260 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8261 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8262 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8263
8264 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8265 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8266 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8267
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008268 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8269 tmp &= ~(7 << 13);
8270 tmp |= (5 << 13);
8271 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008272
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008273 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8274 tmp &= ~(7 << 13);
8275 tmp |= (5 << 13);
8276 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008277
8278 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8279 tmp &= ~0xFF;
8280 tmp |= 0x1C;
8281 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8282
8283 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8284 tmp &= ~0xFF;
8285 tmp |= 0x1C;
8286 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8287
8288 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8289 tmp &= ~(0xFF << 16);
8290 tmp |= (0x1C << 16);
8291 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8292
8293 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8294 tmp &= ~(0xFF << 16);
8295 tmp |= (0x1C << 16);
8296 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8297
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008298 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8299 tmp |= (1 << 27);
8300 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008301
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008302 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8303 tmp |= (1 << 27);
8304 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008305
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008306 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8307 tmp &= ~(0xF << 28);
8308 tmp |= (4 << 28);
8309 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008310
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008311 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8312 tmp &= ~(0xF << 28);
8313 tmp |= (4 << 28);
8314 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008315}
8316
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008317/* Implements 3 different sequences from BSpec chapter "Display iCLK
8318 * Programming" based on the parameters passed:
8319 * - Sequence to enable CLKOUT_DP
8320 * - Sequence to enable CLKOUT_DP without spread
8321 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8322 */
8323static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8324 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008325{
8326 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008327 uint32_t reg, tmp;
8328
8329 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8330 with_spread = true;
8331 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8332 with_fdi, "LP PCH doesn't have FDI\n"))
8333 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008334
Ville Syrjäläa5805162015-05-26 20:42:30 +03008335 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008336
8337 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8338 tmp &= ~SBI_SSCCTL_DISABLE;
8339 tmp |= SBI_SSCCTL_PATHALT;
8340 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8341
8342 udelay(24);
8343
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008344 if (with_spread) {
8345 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8346 tmp &= ~SBI_SSCCTL_PATHALT;
8347 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008348
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008349 if (with_fdi) {
8350 lpt_reset_fdi_mphy(dev_priv);
8351 lpt_program_fdi_mphy(dev_priv);
8352 }
8353 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008354
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008355 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8356 SBI_GEN0 : SBI_DBUFF0;
8357 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8358 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8359 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008360
Ville Syrjäläa5805162015-05-26 20:42:30 +03008361 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008362}
8363
Paulo Zanoni47701c32013-07-23 11:19:25 -03008364/* Sequence to disable CLKOUT_DP */
8365static void lpt_disable_clkout_dp(struct drm_device *dev)
8366{
8367 struct drm_i915_private *dev_priv = dev->dev_private;
8368 uint32_t reg, tmp;
8369
Ville Syrjäläa5805162015-05-26 20:42:30 +03008370 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008371
8372 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8373 SBI_GEN0 : SBI_DBUFF0;
8374 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8375 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8376 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8377
8378 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8379 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8380 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8381 tmp |= SBI_SSCCTL_PATHALT;
8382 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8383 udelay(32);
8384 }
8385 tmp |= SBI_SSCCTL_DISABLE;
8386 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8387 }
8388
Ville Syrjäläa5805162015-05-26 20:42:30 +03008389 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008390}
8391
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008392static void lpt_init_pch_refclk(struct drm_device *dev)
8393{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008394 struct intel_encoder *encoder;
8395 bool has_vga = false;
8396
Damien Lespiaub2784e12014-08-05 11:29:37 +01008397 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008398 switch (encoder->type) {
8399 case INTEL_OUTPUT_ANALOG:
8400 has_vga = true;
8401 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008402 default:
8403 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008404 }
8405 }
8406
Paulo Zanoni47701c32013-07-23 11:19:25 -03008407 if (has_vga)
8408 lpt_enable_clkout_dp(dev, true, true);
8409 else
8410 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008411}
8412
Paulo Zanonidde86e22012-12-01 12:04:25 -02008413/*
8414 * Initialize reference clocks when the driver loads
8415 */
8416void intel_init_pch_refclk(struct drm_device *dev)
8417{
8418 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8419 ironlake_init_pch_refclk(dev);
8420 else if (HAS_PCH_LPT(dev))
8421 lpt_init_pch_refclk(dev);
8422}
8423
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008424static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008425{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008426 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008427 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008428 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008429 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008430 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008431 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008432 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008433 bool is_lvds = false;
8434
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008435 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008436 if (connector_state->crtc != crtc_state->base.crtc)
8437 continue;
8438
8439 encoder = to_intel_encoder(connector_state->best_encoder);
8440
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008441 switch (encoder->type) {
8442 case INTEL_OUTPUT_LVDS:
8443 is_lvds = true;
8444 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008445 default:
8446 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008447 }
8448 num_connectors++;
8449 }
8450
8451 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008452 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008453 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008454 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008455 }
8456
8457 return 120000;
8458}
8459
Daniel Vetter6ff93602013-04-19 11:24:36 +02008460static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008461{
8462 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8464 int pipe = intel_crtc->pipe;
8465 uint32_t val;
8466
Daniel Vetter78114072013-06-13 00:54:57 +02008467 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008468
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008469 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008470 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008471 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008472 break;
8473 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008474 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008475 break;
8476 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008477 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008478 break;
8479 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008480 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008481 break;
8482 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008483 /* Case prevented by intel_choose_pipe_bpp_dither. */
8484 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008485 }
8486
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008487 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008488 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8489
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008490 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008491 val |= PIPECONF_INTERLACED_ILK;
8492 else
8493 val |= PIPECONF_PROGRESSIVE;
8494
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008495 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008496 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008497
Paulo Zanonic8203562012-09-12 10:06:29 -03008498 I915_WRITE(PIPECONF(pipe), val);
8499 POSTING_READ(PIPECONF(pipe));
8500}
8501
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008502/*
8503 * Set up the pipe CSC unit.
8504 *
8505 * Currently only full range RGB to limited range RGB conversion
8506 * is supported, but eventually this should handle various
8507 * RGB<->YCbCr scenarios as well.
8508 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008509static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008510{
8511 struct drm_device *dev = crtc->dev;
8512 struct drm_i915_private *dev_priv = dev->dev_private;
8513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8514 int pipe = intel_crtc->pipe;
8515 uint16_t coeff = 0x7800; /* 1.0 */
8516
8517 /*
8518 * TODO: Check what kind of values actually come out of the pipe
8519 * with these coeff/postoff values and adjust to get the best
8520 * accuracy. Perhaps we even need to take the bpc value into
8521 * consideration.
8522 */
8523
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008524 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008525 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8526
8527 /*
8528 * GY/GU and RY/RU should be the other way around according
8529 * to BSpec, but reality doesn't agree. Just set them up in
8530 * a way that results in the correct picture.
8531 */
8532 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8533 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8534
8535 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8536 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8537
8538 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8539 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8540
8541 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8542 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8543 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8544
8545 if (INTEL_INFO(dev)->gen > 6) {
8546 uint16_t postoff = 0;
8547
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008548 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008549 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008550
8551 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8552 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8553 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8554
8555 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8556 } else {
8557 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8558
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008559 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008560 mode |= CSC_BLACK_SCREEN_OFFSET;
8561
8562 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8563 }
8564}
8565
Daniel Vetter6ff93602013-04-19 11:24:36 +02008566static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008567{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008568 struct drm_device *dev = crtc->dev;
8569 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008570 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008571 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008572 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008573 uint32_t val;
8574
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008575 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008576
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008577 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008578 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8579
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008580 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008581 val |= PIPECONF_INTERLACED_ILK;
8582 else
8583 val |= PIPECONF_PROGRESSIVE;
8584
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008585 I915_WRITE(PIPECONF(cpu_transcoder), val);
8586 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008587
8588 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8589 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008590
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308591 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008592 val = 0;
8593
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008594 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008595 case 18:
8596 val |= PIPEMISC_DITHER_6_BPC;
8597 break;
8598 case 24:
8599 val |= PIPEMISC_DITHER_8_BPC;
8600 break;
8601 case 30:
8602 val |= PIPEMISC_DITHER_10_BPC;
8603 break;
8604 case 36:
8605 val |= PIPEMISC_DITHER_12_BPC;
8606 break;
8607 default:
8608 /* Case prevented by pipe_config_set_bpp. */
8609 BUG();
8610 }
8611
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008612 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008613 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8614
8615 I915_WRITE(PIPEMISC(pipe), val);
8616 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008617}
8618
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008619static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008620 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008621 intel_clock_t *clock,
8622 bool *has_reduced_clock,
8623 intel_clock_t *reduced_clock)
8624{
8625 struct drm_device *dev = crtc->dev;
8626 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008627 int refclk;
8628 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008629 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008630
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008631 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008632
8633 /*
8634 * Returns a set of divisors for the desired target clock with the given
8635 * refclk, or FALSE. The returned values represent the clock equation:
8636 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8637 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008638 limit = intel_limit(crtc_state, refclk);
8639 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008640 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008641 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008642 if (!ret)
8643 return false;
8644
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008645 return true;
8646}
8647
Paulo Zanonid4b19312012-11-29 11:29:32 -02008648int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8649{
8650 /*
8651 * Account for spread spectrum to avoid
8652 * oversubscribing the link. Max center spread
8653 * is 2.5%; use 5% for safety's sake.
8654 */
8655 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008656 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008657}
8658
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008659static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008660{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008661 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008662}
8663
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008664static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008665 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008666 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008667 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008668{
8669 struct drm_crtc *crtc = &intel_crtc->base;
8670 struct drm_device *dev = crtc->dev;
8671 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008672 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008673 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008674 struct drm_connector_state *connector_state;
8675 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008676 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008677 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008678 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008679
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008680 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008681 if (connector_state->crtc != crtc_state->base.crtc)
8682 continue;
8683
8684 encoder = to_intel_encoder(connector_state->best_encoder);
8685
8686 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008687 case INTEL_OUTPUT_LVDS:
8688 is_lvds = true;
8689 break;
8690 case INTEL_OUTPUT_SDVO:
8691 case INTEL_OUTPUT_HDMI:
8692 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008693 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008694 default:
8695 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008696 }
8697
8698 num_connectors++;
8699 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008700
Chris Wilsonc1858122010-12-03 21:35:48 +00008701 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008702 factor = 21;
8703 if (is_lvds) {
8704 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008705 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008706 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008707 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008708 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008709 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008710
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008711 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008712 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008713
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008714 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8715 *fp2 |= FP_CB_TUNE;
8716
Chris Wilson5eddb702010-09-11 13:48:45 +01008717 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008718
Eric Anholta07d6782011-03-30 13:01:08 -07008719 if (is_lvds)
8720 dpll |= DPLLB_MODE_LVDS;
8721 else
8722 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008723
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008724 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008725 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008726
8727 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008728 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008729 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008730 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008731
Eric Anholta07d6782011-03-30 13:01:08 -07008732 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008733 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008734 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008735 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008736
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008737 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008738 case 5:
8739 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8740 break;
8741 case 7:
8742 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8743 break;
8744 case 10:
8745 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8746 break;
8747 case 14:
8748 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8749 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008750 }
8751
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008752 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008753 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008754 else
8755 dpll |= PLL_REF_INPUT_DREFCLK;
8756
Daniel Vetter959e16d2013-06-05 13:34:21 +02008757 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008758}
8759
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008760static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8761 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008762{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008763 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008764 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008765 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008766 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008767 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008768 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008769
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008770 memset(&crtc_state->dpll_hw_state, 0,
8771 sizeof(crtc_state->dpll_hw_state));
8772
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008773 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008774
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008775 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8776 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8777
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008778 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008779 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008780 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008781 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8782 return -EINVAL;
8783 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008784 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008785 if (!crtc_state->clock_set) {
8786 crtc_state->dpll.n = clock.n;
8787 crtc_state->dpll.m1 = clock.m1;
8788 crtc_state->dpll.m2 = clock.m2;
8789 crtc_state->dpll.p1 = clock.p1;
8790 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008791 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008792
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008793 /* 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 +02008794 if (crtc_state->has_pch_encoder) {
8795 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008796 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008797 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008798
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008799 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008800 &fp, &reduced_clock,
8801 has_reduced_clock ? &fp2 : NULL);
8802
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008803 crtc_state->dpll_hw_state.dpll = dpll;
8804 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008805 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008806 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008807 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008808 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008809
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008810 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008811 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008812 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008813 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008814 return -EINVAL;
8815 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008816 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008817
Rodrigo Viviab585de2015-03-24 12:40:09 -07008818 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008819 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008820 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008821 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008822
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008823 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008824}
8825
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008826static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8827 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008828{
8829 struct drm_device *dev = crtc->base.dev;
8830 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008831 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008832
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008833 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8834 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8835 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8836 & ~TU_SIZE_MASK;
8837 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8838 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8839 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8840}
8841
8842static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8843 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008844 struct intel_link_m_n *m_n,
8845 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008846{
8847 struct drm_device *dev = crtc->base.dev;
8848 struct drm_i915_private *dev_priv = dev->dev_private;
8849 enum pipe pipe = crtc->pipe;
8850
8851 if (INTEL_INFO(dev)->gen >= 5) {
8852 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8853 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8854 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8855 & ~TU_SIZE_MASK;
8856 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8857 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8858 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008859 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8860 * gen < 8) and if DRRS is supported (to make sure the
8861 * registers are not unnecessarily read).
8862 */
8863 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008864 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008865 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8866 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8867 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8868 & ~TU_SIZE_MASK;
8869 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8870 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8871 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8872 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008873 } else {
8874 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8875 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8876 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8877 & ~TU_SIZE_MASK;
8878 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8879 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8880 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8881 }
8882}
8883
8884void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008885 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008886{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008887 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008888 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8889 else
8890 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008891 &pipe_config->dp_m_n,
8892 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008893}
8894
Daniel Vetter72419202013-04-04 13:28:53 +02008895static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008896 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008897{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008898 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008899 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008900}
8901
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008902static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008903 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008904{
8905 struct drm_device *dev = crtc->base.dev;
8906 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008907 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8908 uint32_t ps_ctrl = 0;
8909 int id = -1;
8910 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008911
Chandra Kondurua1b22782015-04-07 15:28:45 -07008912 /* find scaler attached to this pipe */
8913 for (i = 0; i < crtc->num_scalers; i++) {
8914 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8915 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8916 id = i;
8917 pipe_config->pch_pfit.enabled = true;
8918 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8919 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8920 break;
8921 }
8922 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008923
Chandra Kondurua1b22782015-04-07 15:28:45 -07008924 scaler_state->scaler_id = id;
8925 if (id >= 0) {
8926 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8927 } else {
8928 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008929 }
8930}
8931
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008932static void
8933skylake_get_initial_plane_config(struct intel_crtc *crtc,
8934 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008935{
8936 struct drm_device *dev = crtc->base.dev;
8937 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008938 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008939 int pipe = crtc->pipe;
8940 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008941 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008942 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008943 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008944
Damien Lespiaud9806c92015-01-21 14:07:19 +00008945 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008946 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008947 DRM_DEBUG_KMS("failed to alloc fb\n");
8948 return;
8949 }
8950
Damien Lespiau1b842c82015-01-21 13:50:54 +00008951 fb = &intel_fb->base;
8952
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008953 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008954 if (!(val & PLANE_CTL_ENABLE))
8955 goto error;
8956
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008957 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8958 fourcc = skl_format_to_fourcc(pixel_format,
8959 val & PLANE_CTL_ORDER_RGBX,
8960 val & PLANE_CTL_ALPHA_MASK);
8961 fb->pixel_format = fourcc;
8962 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8963
Damien Lespiau40f46282015-02-27 11:15:21 +00008964 tiling = val & PLANE_CTL_TILED_MASK;
8965 switch (tiling) {
8966 case PLANE_CTL_TILED_LINEAR:
8967 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8968 break;
8969 case PLANE_CTL_TILED_X:
8970 plane_config->tiling = I915_TILING_X;
8971 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8972 break;
8973 case PLANE_CTL_TILED_Y:
8974 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8975 break;
8976 case PLANE_CTL_TILED_YF:
8977 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8978 break;
8979 default:
8980 MISSING_CASE(tiling);
8981 goto error;
8982 }
8983
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008984 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8985 plane_config->base = base;
8986
8987 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8988
8989 val = I915_READ(PLANE_SIZE(pipe, 0));
8990 fb->height = ((val >> 16) & 0xfff) + 1;
8991 fb->width = ((val >> 0) & 0x1fff) + 1;
8992
8993 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008994 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8995 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008996 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8997
8998 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008999 fb->pixel_format,
9000 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009001
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009002 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009003
9004 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9005 pipe_name(pipe), fb->width, fb->height,
9006 fb->bits_per_pixel, base, fb->pitches[0],
9007 plane_config->size);
9008
Damien Lespiau2d140302015-02-05 17:22:18 +00009009 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009010 return;
9011
9012error:
9013 kfree(fb);
9014}
9015
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009016static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009017 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009018{
9019 struct drm_device *dev = crtc->base.dev;
9020 struct drm_i915_private *dev_priv = dev->dev_private;
9021 uint32_t tmp;
9022
9023 tmp = I915_READ(PF_CTL(crtc->pipe));
9024
9025 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009026 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009027 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9028 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009029
9030 /* We currently do not free assignements of panel fitters on
9031 * ivb/hsw (since we don't use the higher upscaling modes which
9032 * differentiates them) so just WARN about this case for now. */
9033 if (IS_GEN7(dev)) {
9034 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9035 PF_PIPE_SEL_IVB(crtc->pipe));
9036 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009037 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009038}
9039
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009040static void
9041ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9042 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009043{
9044 struct drm_device *dev = crtc->base.dev;
9045 struct drm_i915_private *dev_priv = dev->dev_private;
9046 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009047 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009048 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009049 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009050 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009051 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009052
Damien Lespiau42a7b082015-02-05 19:35:13 +00009053 val = I915_READ(DSPCNTR(pipe));
9054 if (!(val & DISPLAY_PLANE_ENABLE))
9055 return;
9056
Damien Lespiaud9806c92015-01-21 14:07:19 +00009057 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009058 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009059 DRM_DEBUG_KMS("failed to alloc fb\n");
9060 return;
9061 }
9062
Damien Lespiau1b842c82015-01-21 13:50:54 +00009063 fb = &intel_fb->base;
9064
Daniel Vetter18c52472015-02-10 17:16:09 +00009065 if (INTEL_INFO(dev)->gen >= 4) {
9066 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009067 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009068 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9069 }
9070 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009071
9072 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009073 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009074 fb->pixel_format = fourcc;
9075 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009076
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009077 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009078 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009079 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009080 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009081 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009082 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009083 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009084 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009085 }
9086 plane_config->base = base;
9087
9088 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009089 fb->width = ((val >> 16) & 0xfff) + 1;
9090 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009091
9092 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009093 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009094
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009095 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009096 fb->pixel_format,
9097 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009098
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009099 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009100
Damien Lespiau2844a922015-01-20 12:51:48 +00009101 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9102 pipe_name(pipe), fb->width, fb->height,
9103 fb->bits_per_pixel, base, fb->pitches[0],
9104 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009105
Damien Lespiau2d140302015-02-05 17:22:18 +00009106 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009107}
9108
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009109static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009110 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009111{
9112 struct drm_device *dev = crtc->base.dev;
9113 struct drm_i915_private *dev_priv = dev->dev_private;
9114 uint32_t tmp;
9115
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009116 if (!intel_display_power_is_enabled(dev_priv,
9117 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009118 return false;
9119
Daniel Vettere143a212013-07-04 12:01:15 +02009120 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009121 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009122
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009123 tmp = I915_READ(PIPECONF(crtc->pipe));
9124 if (!(tmp & PIPECONF_ENABLE))
9125 return false;
9126
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009127 switch (tmp & PIPECONF_BPC_MASK) {
9128 case PIPECONF_6BPC:
9129 pipe_config->pipe_bpp = 18;
9130 break;
9131 case PIPECONF_8BPC:
9132 pipe_config->pipe_bpp = 24;
9133 break;
9134 case PIPECONF_10BPC:
9135 pipe_config->pipe_bpp = 30;
9136 break;
9137 case PIPECONF_12BPC:
9138 pipe_config->pipe_bpp = 36;
9139 break;
9140 default:
9141 break;
9142 }
9143
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009144 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9145 pipe_config->limited_color_range = true;
9146
Daniel Vetterab9412b2013-05-03 11:49:46 +02009147 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009148 struct intel_shared_dpll *pll;
9149
Daniel Vetter88adfff2013-03-28 10:42:01 +01009150 pipe_config->has_pch_encoder = true;
9151
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009152 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9153 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9154 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009155
9156 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009157
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009158 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009159 pipe_config->shared_dpll =
9160 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009161 } else {
9162 tmp = I915_READ(PCH_DPLL_SEL);
9163 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9164 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9165 else
9166 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9167 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009168
9169 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9170
9171 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9172 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009173
9174 tmp = pipe_config->dpll_hw_state.dpll;
9175 pipe_config->pixel_multiplier =
9176 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9177 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009178
9179 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009180 } else {
9181 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009182 }
9183
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009184 intel_get_pipe_timings(crtc, pipe_config);
9185
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009186 ironlake_get_pfit_config(crtc, pipe_config);
9187
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009188 return true;
9189}
9190
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009191static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9192{
9193 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009194 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009195
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009196 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009197 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009198 pipe_name(crtc->pipe));
9199
Rob Clarke2c719b2014-12-15 13:56:32 -05009200 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9201 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9202 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9203 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9204 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9205 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009206 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009207 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009208 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009209 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009210 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009211 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009212 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009213 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009214 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009215
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009216 /*
9217 * In theory we can still leave IRQs enabled, as long as only the HPD
9218 * interrupts remain enabled. We used to check for that, but since it's
9219 * gen-specific and since we only disable LCPLL after we fully disable
9220 * the interrupts, the check below should be enough.
9221 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009222 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009223}
9224
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009225static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9226{
9227 struct drm_device *dev = dev_priv->dev;
9228
9229 if (IS_HASWELL(dev))
9230 return I915_READ(D_COMP_HSW);
9231 else
9232 return I915_READ(D_COMP_BDW);
9233}
9234
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009235static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9236{
9237 struct drm_device *dev = dev_priv->dev;
9238
9239 if (IS_HASWELL(dev)) {
9240 mutex_lock(&dev_priv->rps.hw_lock);
9241 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9242 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009243 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009244 mutex_unlock(&dev_priv->rps.hw_lock);
9245 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009246 I915_WRITE(D_COMP_BDW, val);
9247 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009248 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009249}
9250
9251/*
9252 * This function implements pieces of two sequences from BSpec:
9253 * - Sequence for display software to disable LCPLL
9254 * - Sequence for display software to allow package C8+
9255 * The steps implemented here are just the steps that actually touch the LCPLL
9256 * register. Callers should take care of disabling all the display engine
9257 * functions, doing the mode unset, fixing interrupts, etc.
9258 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009259static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9260 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009261{
9262 uint32_t val;
9263
9264 assert_can_disable_lcpll(dev_priv);
9265
9266 val = I915_READ(LCPLL_CTL);
9267
9268 if (switch_to_fclk) {
9269 val |= LCPLL_CD_SOURCE_FCLK;
9270 I915_WRITE(LCPLL_CTL, val);
9271
9272 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9273 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9274 DRM_ERROR("Switching to FCLK failed\n");
9275
9276 val = I915_READ(LCPLL_CTL);
9277 }
9278
9279 val |= LCPLL_PLL_DISABLE;
9280 I915_WRITE(LCPLL_CTL, val);
9281 POSTING_READ(LCPLL_CTL);
9282
9283 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9284 DRM_ERROR("LCPLL still locked\n");
9285
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009286 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009287 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009288 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009289 ndelay(100);
9290
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009291 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9292 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009293 DRM_ERROR("D_COMP RCOMP still in progress\n");
9294
9295 if (allow_power_down) {
9296 val = I915_READ(LCPLL_CTL);
9297 val |= LCPLL_POWER_DOWN_ALLOW;
9298 I915_WRITE(LCPLL_CTL, val);
9299 POSTING_READ(LCPLL_CTL);
9300 }
9301}
9302
9303/*
9304 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9305 * source.
9306 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009307static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009308{
9309 uint32_t val;
9310
9311 val = I915_READ(LCPLL_CTL);
9312
9313 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9314 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9315 return;
9316
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009317 /*
9318 * Make sure we're not on PC8 state before disabling PC8, otherwise
9319 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009320 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009321 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009322
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009323 if (val & LCPLL_POWER_DOWN_ALLOW) {
9324 val &= ~LCPLL_POWER_DOWN_ALLOW;
9325 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009326 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009327 }
9328
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009329 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009330 val |= D_COMP_COMP_FORCE;
9331 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009332 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009333
9334 val = I915_READ(LCPLL_CTL);
9335 val &= ~LCPLL_PLL_DISABLE;
9336 I915_WRITE(LCPLL_CTL, val);
9337
9338 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9339 DRM_ERROR("LCPLL not locked yet\n");
9340
9341 if (val & LCPLL_CD_SOURCE_FCLK) {
9342 val = I915_READ(LCPLL_CTL);
9343 val &= ~LCPLL_CD_SOURCE_FCLK;
9344 I915_WRITE(LCPLL_CTL, val);
9345
9346 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9347 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9348 DRM_ERROR("Switching back to LCPLL failed\n");
9349 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009350
Mika Kuoppala59bad942015-01-16 11:34:40 +02009351 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009352 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009353}
9354
Paulo Zanoni765dab672014-03-07 20:08:18 -03009355/*
9356 * Package states C8 and deeper are really deep PC states that can only be
9357 * reached when all the devices on the system allow it, so even if the graphics
9358 * device allows PC8+, it doesn't mean the system will actually get to these
9359 * states. Our driver only allows PC8+ when going into runtime PM.
9360 *
9361 * The requirements for PC8+ are that all the outputs are disabled, the power
9362 * well is disabled and most interrupts are disabled, and these are also
9363 * requirements for runtime PM. When these conditions are met, we manually do
9364 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9365 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9366 * hang the machine.
9367 *
9368 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9369 * the state of some registers, so when we come back from PC8+ we need to
9370 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9371 * need to take care of the registers kept by RC6. Notice that this happens even
9372 * if we don't put the device in PCI D3 state (which is what currently happens
9373 * because of the runtime PM support).
9374 *
9375 * For more, read "Display Sequences for Package C8" on the hardware
9376 * documentation.
9377 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009378void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009379{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009380 struct drm_device *dev = dev_priv->dev;
9381 uint32_t val;
9382
Paulo Zanonic67a4702013-08-19 13:18:09 -03009383 DRM_DEBUG_KMS("Enabling package C8+\n");
9384
Paulo Zanonic67a4702013-08-19 13:18:09 -03009385 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9386 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9387 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9388 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9389 }
9390
9391 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009392 hsw_disable_lcpll(dev_priv, true, true);
9393}
9394
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009395void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009396{
9397 struct drm_device *dev = dev_priv->dev;
9398 uint32_t val;
9399
Paulo Zanonic67a4702013-08-19 13:18:09 -03009400 DRM_DEBUG_KMS("Disabling package C8+\n");
9401
9402 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009403 lpt_init_pch_refclk(dev);
9404
9405 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9406 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9407 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9408 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9409 }
9410
9411 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009412}
9413
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009414static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309415{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009416 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009417 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309418
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009419 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309420}
9421
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009422/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009423static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009424{
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009425 struct intel_crtc *intel_crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009426 struct intel_crtc_state *crtc_state;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009427 int max_pixel_rate = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009428
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009429 for_each_intel_crtc(state->dev, intel_crtc) {
9430 int pixel_rate;
9431
9432 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9433 if (IS_ERR(crtc_state))
9434 return PTR_ERR(crtc_state);
9435
9436 if (!crtc_state->base.enable)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009437 continue;
9438
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009439 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009440
9441 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009442 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009443 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9444
9445 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9446 }
9447
9448 return max_pixel_rate;
9449}
9450
9451static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9452{
9453 struct drm_i915_private *dev_priv = dev->dev_private;
9454 uint32_t val, data;
9455 int ret;
9456
9457 if (WARN((I915_READ(LCPLL_CTL) &
9458 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9459 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9460 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9461 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9462 "trying to change cdclk frequency with cdclk not enabled\n"))
9463 return;
9464
9465 mutex_lock(&dev_priv->rps.hw_lock);
9466 ret = sandybridge_pcode_write(dev_priv,
9467 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9468 mutex_unlock(&dev_priv->rps.hw_lock);
9469 if (ret) {
9470 DRM_ERROR("failed to inform pcode about cdclk change\n");
9471 return;
9472 }
9473
9474 val = I915_READ(LCPLL_CTL);
9475 val |= LCPLL_CD_SOURCE_FCLK;
9476 I915_WRITE(LCPLL_CTL, val);
9477
9478 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9479 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9480 DRM_ERROR("Switching to FCLK failed\n");
9481
9482 val = I915_READ(LCPLL_CTL);
9483 val &= ~LCPLL_CLK_FREQ_MASK;
9484
9485 switch (cdclk) {
9486 case 450000:
9487 val |= LCPLL_CLK_FREQ_450;
9488 data = 0;
9489 break;
9490 case 540000:
9491 val |= LCPLL_CLK_FREQ_54O_BDW;
9492 data = 1;
9493 break;
9494 case 337500:
9495 val |= LCPLL_CLK_FREQ_337_5_BDW;
9496 data = 2;
9497 break;
9498 case 675000:
9499 val |= LCPLL_CLK_FREQ_675_BDW;
9500 data = 3;
9501 break;
9502 default:
9503 WARN(1, "invalid cdclk frequency\n");
9504 return;
9505 }
9506
9507 I915_WRITE(LCPLL_CTL, val);
9508
9509 val = I915_READ(LCPLL_CTL);
9510 val &= ~LCPLL_CD_SOURCE_FCLK;
9511 I915_WRITE(LCPLL_CTL, val);
9512
9513 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9514 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9515 DRM_ERROR("Switching back to LCPLL failed\n");
9516
9517 mutex_lock(&dev_priv->rps.hw_lock);
9518 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9519 mutex_unlock(&dev_priv->rps.hw_lock);
9520
9521 intel_update_cdclk(dev);
9522
9523 WARN(cdclk != dev_priv->cdclk_freq,
9524 "cdclk requested %d kHz but got %d kHz\n",
9525 cdclk, dev_priv->cdclk_freq);
9526}
9527
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009528static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009529{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009530 struct drm_i915_private *dev_priv = to_i915(state->dev);
9531 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009532 int cdclk;
9533
9534 /*
9535 * FIXME should also account for plane ratio
9536 * once 64bpp pixel formats are supported.
9537 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009538 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009539 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009540 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009541 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009542 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009543 cdclk = 450000;
9544 else
9545 cdclk = 337500;
9546
9547 /*
9548 * FIXME move the cdclk caclulation to
9549 * compute_config() so we can fail gracegully.
9550 */
9551 if (cdclk > dev_priv->max_cdclk_freq) {
9552 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9553 cdclk, dev_priv->max_cdclk_freq);
9554 cdclk = dev_priv->max_cdclk_freq;
9555 }
9556
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009557 to_intel_atomic_state(state)->cdclk = cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009558
9559 return 0;
9560}
9561
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009562static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009563{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009564 struct drm_device *dev = old_state->dev;
9565 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009566
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009567 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009568}
9569
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009570static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9571 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009572{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009573 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009574 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009575
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009576 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009577
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009578 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009579}
9580
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309581static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9582 enum port port,
9583 struct intel_crtc_state *pipe_config)
9584{
9585 switch (port) {
9586 case PORT_A:
9587 pipe_config->ddi_pll_sel = SKL_DPLL0;
9588 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9589 break;
9590 case PORT_B:
9591 pipe_config->ddi_pll_sel = SKL_DPLL1;
9592 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9593 break;
9594 case PORT_C:
9595 pipe_config->ddi_pll_sel = SKL_DPLL2;
9596 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9597 break;
9598 default:
9599 DRM_ERROR("Incorrect port type\n");
9600 }
9601}
9602
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009603static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9604 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009605 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009606{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009607 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009608
9609 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9610 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9611
9612 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009613 case SKL_DPLL0:
9614 /*
9615 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9616 * of the shared DPLL framework and thus needs to be read out
9617 * separately
9618 */
9619 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9620 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9621 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009622 case SKL_DPLL1:
9623 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9624 break;
9625 case SKL_DPLL2:
9626 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9627 break;
9628 case SKL_DPLL3:
9629 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9630 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009631 }
9632}
9633
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009634static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9635 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009636 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009637{
9638 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9639
9640 switch (pipe_config->ddi_pll_sel) {
9641 case PORT_CLK_SEL_WRPLL1:
9642 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9643 break;
9644 case PORT_CLK_SEL_WRPLL2:
9645 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9646 break;
9647 }
9648}
9649
Daniel Vetter26804af2014-06-25 22:01:55 +03009650static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009651 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009652{
9653 struct drm_device *dev = crtc->base.dev;
9654 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009655 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009656 enum port port;
9657 uint32_t tmp;
9658
9659 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9660
9661 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9662
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009663 if (IS_SKYLAKE(dev))
9664 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309665 else if (IS_BROXTON(dev))
9666 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009667 else
9668 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009669
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009670 if (pipe_config->shared_dpll >= 0) {
9671 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9672
9673 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9674 &pipe_config->dpll_hw_state));
9675 }
9676
Daniel Vetter26804af2014-06-25 22:01:55 +03009677 /*
9678 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9679 * DDI E. So just check whether this pipe is wired to DDI E and whether
9680 * the PCH transcoder is on.
9681 */
Damien Lespiauca370452013-12-03 13:56:24 +00009682 if (INTEL_INFO(dev)->gen < 9 &&
9683 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009684 pipe_config->has_pch_encoder = true;
9685
9686 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9687 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9688 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9689
9690 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9691 }
9692}
9693
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009694static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009695 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009696{
9697 struct drm_device *dev = crtc->base.dev;
9698 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009699 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009700 uint32_t tmp;
9701
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009702 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009703 POWER_DOMAIN_PIPE(crtc->pipe)))
9704 return false;
9705
Daniel Vettere143a212013-07-04 12:01:15 +02009706 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009707 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9708
Daniel Vettereccb1402013-05-22 00:50:22 +02009709 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9710 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9711 enum pipe trans_edp_pipe;
9712 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9713 default:
9714 WARN(1, "unknown pipe linked to edp transcoder\n");
9715 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9716 case TRANS_DDI_EDP_INPUT_A_ON:
9717 trans_edp_pipe = PIPE_A;
9718 break;
9719 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9720 trans_edp_pipe = PIPE_B;
9721 break;
9722 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9723 trans_edp_pipe = PIPE_C;
9724 break;
9725 }
9726
9727 if (trans_edp_pipe == crtc->pipe)
9728 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9729 }
9730
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009731 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009732 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009733 return false;
9734
Daniel Vettereccb1402013-05-22 00:50:22 +02009735 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009736 if (!(tmp & PIPECONF_ENABLE))
9737 return false;
9738
Daniel Vetter26804af2014-06-25 22:01:55 +03009739 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009740
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009741 intel_get_pipe_timings(crtc, pipe_config);
9742
Chandra Kondurua1b22782015-04-07 15:28:45 -07009743 if (INTEL_INFO(dev)->gen >= 9) {
9744 skl_init_scalers(dev, crtc, pipe_config);
9745 }
9746
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009747 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ceda2015-05-11 14:35:47 -07009748
9749 if (INTEL_INFO(dev)->gen >= 9) {
9750 pipe_config->scaler_state.scaler_id = -1;
9751 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9752 }
9753
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009754 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009755 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009756 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009757 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009758 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009759 else
9760 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009761 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009762
Jesse Barnese59150d2014-01-07 13:30:45 -08009763 if (IS_HASWELL(dev))
9764 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9765 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009766
Clint Taylorebb69c92014-09-30 10:30:22 -07009767 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9768 pipe_config->pixel_multiplier =
9769 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9770 } else {
9771 pipe_config->pixel_multiplier = 1;
9772 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009773
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009774 return true;
9775}
9776
Chris Wilson560b85b2010-08-07 11:01:38 +01009777static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9778{
9779 struct drm_device *dev = crtc->dev;
9780 struct drm_i915_private *dev_priv = dev->dev_private;
9781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009782 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009783
Ville Syrjälädc41c152014-08-13 11:57:05 +03009784 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009785 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9786 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009787 unsigned int stride = roundup_pow_of_two(width) * 4;
9788
9789 switch (stride) {
9790 default:
9791 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9792 width, stride);
9793 stride = 256;
9794 /* fallthrough */
9795 case 256:
9796 case 512:
9797 case 1024:
9798 case 2048:
9799 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009800 }
9801
Ville Syrjälädc41c152014-08-13 11:57:05 +03009802 cntl |= CURSOR_ENABLE |
9803 CURSOR_GAMMA_ENABLE |
9804 CURSOR_FORMAT_ARGB |
9805 CURSOR_STRIDE(stride);
9806
9807 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009808 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009809
Ville Syrjälädc41c152014-08-13 11:57:05 +03009810 if (intel_crtc->cursor_cntl != 0 &&
9811 (intel_crtc->cursor_base != base ||
9812 intel_crtc->cursor_size != size ||
9813 intel_crtc->cursor_cntl != cntl)) {
9814 /* On these chipsets we can only modify the base/size/stride
9815 * whilst the cursor is disabled.
9816 */
9817 I915_WRITE(_CURACNTR, 0);
9818 POSTING_READ(_CURACNTR);
9819 intel_crtc->cursor_cntl = 0;
9820 }
9821
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009822 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009823 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009824 intel_crtc->cursor_base = base;
9825 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009826
9827 if (intel_crtc->cursor_size != size) {
9828 I915_WRITE(CURSIZE, size);
9829 intel_crtc->cursor_size = size;
9830 }
9831
Chris Wilson4b0e3332014-05-30 16:35:26 +03009832 if (intel_crtc->cursor_cntl != cntl) {
9833 I915_WRITE(_CURACNTR, cntl);
9834 POSTING_READ(_CURACNTR);
9835 intel_crtc->cursor_cntl = cntl;
9836 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009837}
9838
9839static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9840{
9841 struct drm_device *dev = crtc->dev;
9842 struct drm_i915_private *dev_priv = dev->dev_private;
9843 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9844 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009845 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009846
Chris Wilson4b0e3332014-05-30 16:35:26 +03009847 cntl = 0;
9848 if (base) {
9849 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009850 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309851 case 64:
9852 cntl |= CURSOR_MODE_64_ARGB_AX;
9853 break;
9854 case 128:
9855 cntl |= CURSOR_MODE_128_ARGB_AX;
9856 break;
9857 case 256:
9858 cntl |= CURSOR_MODE_256_ARGB_AX;
9859 break;
9860 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009861 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309862 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009863 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009864 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009865
9866 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9867 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009868 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009869
Matt Roper8e7d6882015-01-21 16:35:41 -08009870 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009871 cntl |= CURSOR_ROTATE_180;
9872
Chris Wilson4b0e3332014-05-30 16:35:26 +03009873 if (intel_crtc->cursor_cntl != cntl) {
9874 I915_WRITE(CURCNTR(pipe), cntl);
9875 POSTING_READ(CURCNTR(pipe));
9876 intel_crtc->cursor_cntl = cntl;
9877 }
9878
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009879 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009880 I915_WRITE(CURBASE(pipe), base);
9881 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009882
9883 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009884}
9885
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009886/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009887static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9888 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009889{
9890 struct drm_device *dev = crtc->dev;
9891 struct drm_i915_private *dev_priv = dev->dev_private;
9892 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9893 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009894 int x = crtc->cursor_x;
9895 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009896 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009897
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009898 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009899 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009900
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009901 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009902 base = 0;
9903
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009904 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009905 base = 0;
9906
9907 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009908 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009909 base = 0;
9910
9911 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9912 x = -x;
9913 }
9914 pos |= x << CURSOR_X_SHIFT;
9915
9916 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009917 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009918 base = 0;
9919
9920 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9921 y = -y;
9922 }
9923 pos |= y << CURSOR_Y_SHIFT;
9924
Chris Wilson4b0e3332014-05-30 16:35:26 +03009925 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009926 return;
9927
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009928 I915_WRITE(CURPOS(pipe), pos);
9929
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009930 /* ILK+ do this automagically */
9931 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009932 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009933 base += (intel_crtc->base.cursor->state->crtc_h *
9934 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009935 }
9936
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009937 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009938 i845_update_cursor(crtc, base);
9939 else
9940 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009941}
9942
Ville Syrjälädc41c152014-08-13 11:57:05 +03009943static bool cursor_size_ok(struct drm_device *dev,
9944 uint32_t width, uint32_t height)
9945{
9946 if (width == 0 || height == 0)
9947 return false;
9948
9949 /*
9950 * 845g/865g are special in that they are only limited by
9951 * the width of their cursors, the height is arbitrary up to
9952 * the precision of the register. Everything else requires
9953 * square cursors, limited to a few power-of-two sizes.
9954 */
9955 if (IS_845G(dev) || IS_I865G(dev)) {
9956 if ((width & 63) != 0)
9957 return false;
9958
9959 if (width > (IS_845G(dev) ? 64 : 512))
9960 return false;
9961
9962 if (height > 1023)
9963 return false;
9964 } else {
9965 switch (width | height) {
9966 case 256:
9967 case 128:
9968 if (IS_GEN2(dev))
9969 return false;
9970 case 64:
9971 break;
9972 default:
9973 return false;
9974 }
9975 }
9976
9977 return true;
9978}
9979
Jesse Barnes79e53942008-11-07 14:24:08 -08009980static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009981 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009982{
James Simmons72034252010-08-03 01:33:19 +01009983 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009984 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009985
James Simmons72034252010-08-03 01:33:19 +01009986 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009987 intel_crtc->lut_r[i] = red[i] >> 8;
9988 intel_crtc->lut_g[i] = green[i] >> 8;
9989 intel_crtc->lut_b[i] = blue[i] >> 8;
9990 }
9991
9992 intel_crtc_load_lut(crtc);
9993}
9994
Jesse Barnes79e53942008-11-07 14:24:08 -08009995/* VESA 640x480x72Hz mode to set on the pipe */
9996static struct drm_display_mode load_detect_mode = {
9997 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9998 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9999};
10000
Daniel Vettera8bb6812014-02-10 18:00:39 +010010001struct drm_framebuffer *
10002__intel_framebuffer_create(struct drm_device *dev,
10003 struct drm_mode_fb_cmd2 *mode_cmd,
10004 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010005{
10006 struct intel_framebuffer *intel_fb;
10007 int ret;
10008
10009 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10010 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010011 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010012 return ERR_PTR(-ENOMEM);
10013 }
10014
10015 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010016 if (ret)
10017 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010018
10019 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010020err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010021 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010022 kfree(intel_fb);
10023
10024 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010025}
10026
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010027static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010028intel_framebuffer_create(struct drm_device *dev,
10029 struct drm_mode_fb_cmd2 *mode_cmd,
10030 struct drm_i915_gem_object *obj)
10031{
10032 struct drm_framebuffer *fb;
10033 int ret;
10034
10035 ret = i915_mutex_lock_interruptible(dev);
10036 if (ret)
10037 return ERR_PTR(ret);
10038 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10039 mutex_unlock(&dev->struct_mutex);
10040
10041 return fb;
10042}
10043
Chris Wilsond2dff872011-04-19 08:36:26 +010010044static u32
10045intel_framebuffer_pitch_for_width(int width, int bpp)
10046{
10047 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10048 return ALIGN(pitch, 64);
10049}
10050
10051static u32
10052intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10053{
10054 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010055 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010056}
10057
10058static struct drm_framebuffer *
10059intel_framebuffer_create_for_mode(struct drm_device *dev,
10060 struct drm_display_mode *mode,
10061 int depth, int bpp)
10062{
10063 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010064 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010065
10066 obj = i915_gem_alloc_object(dev,
10067 intel_framebuffer_size_for_mode(mode, bpp));
10068 if (obj == NULL)
10069 return ERR_PTR(-ENOMEM);
10070
10071 mode_cmd.width = mode->hdisplay;
10072 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010073 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10074 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010075 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010076
10077 return intel_framebuffer_create(dev, &mode_cmd, obj);
10078}
10079
10080static struct drm_framebuffer *
10081mode_fits_in_fbdev(struct drm_device *dev,
10082 struct drm_display_mode *mode)
10083{
Daniel Vetter4520f532013-10-09 09:18:51 +020010084#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +010010085 struct drm_i915_private *dev_priv = dev->dev_private;
10086 struct drm_i915_gem_object *obj;
10087 struct drm_framebuffer *fb;
10088
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010089 if (!dev_priv->fbdev)
10090 return NULL;
10091
10092 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010093 return NULL;
10094
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010095 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010096 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010097
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010098 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010099 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10100 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010101 return NULL;
10102
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010103 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010104 return NULL;
10105
10106 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010107#else
10108 return NULL;
10109#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010110}
10111
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010112static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10113 struct drm_crtc *crtc,
10114 struct drm_display_mode *mode,
10115 struct drm_framebuffer *fb,
10116 int x, int y)
10117{
10118 struct drm_plane_state *plane_state;
10119 int hdisplay, vdisplay;
10120 int ret;
10121
10122 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10123 if (IS_ERR(plane_state))
10124 return PTR_ERR(plane_state);
10125
10126 if (mode)
10127 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10128 else
10129 hdisplay = vdisplay = 0;
10130
10131 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10132 if (ret)
10133 return ret;
10134 drm_atomic_set_fb_for_plane(plane_state, fb);
10135 plane_state->crtc_x = 0;
10136 plane_state->crtc_y = 0;
10137 plane_state->crtc_w = hdisplay;
10138 plane_state->crtc_h = vdisplay;
10139 plane_state->src_x = x << 16;
10140 plane_state->src_y = y << 16;
10141 plane_state->src_w = hdisplay << 16;
10142 plane_state->src_h = vdisplay << 16;
10143
10144 return 0;
10145}
10146
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010147bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010148 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010149 struct intel_load_detect_pipe *old,
10150 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010151{
10152 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010153 struct intel_encoder *intel_encoder =
10154 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010155 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010156 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010157 struct drm_crtc *crtc = NULL;
10158 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010159 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010160 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010161 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010162 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010163 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010164 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010165
Chris Wilsond2dff872011-04-19 08:36:26 +010010166 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010167 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010168 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010169
Rob Clark51fd3712013-11-19 12:10:12 -050010170retry:
10171 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10172 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010173 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010174
Jesse Barnes79e53942008-11-07 14:24:08 -080010175 /*
10176 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010177 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010178 * - if the connector already has an assigned crtc, use it (but make
10179 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010180 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010181 * - try to find the first unused crtc that can drive this connector,
10182 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010183 */
10184
10185 /* See if we already have a CRTC for this connector */
10186 if (encoder->crtc) {
10187 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010188
Rob Clark51fd3712013-11-19 12:10:12 -050010189 ret = drm_modeset_lock(&crtc->mutex, ctx);
10190 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010191 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010192 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10193 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010194 goto fail;
Daniel Vetter7b240562012-12-12 00:35:33 +010010195
Daniel Vetter24218aa2012-08-12 19:27:11 +020010196 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010197 old->load_detect_temp = false;
10198
10199 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010200 if (connector->dpms != DRM_MODE_DPMS_ON)
10201 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010202
Chris Wilson71731882011-04-19 23:10:58 +010010203 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010204 }
10205
10206 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010207 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010208 i++;
10209 if (!(encoder->possible_crtcs & (1 << i)))
10210 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010211 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010212 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +030010213
10214 crtc = possible_crtc;
10215 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010216 }
10217
10218 /*
10219 * If we didn't find an unused CRTC, don't use any.
10220 */
10221 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010222 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010223 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010224 }
10225
Rob Clark51fd3712013-11-19 12:10:12 -050010226 ret = drm_modeset_lock(&crtc->mutex, ctx);
10227 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010228 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010229 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10230 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010231 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010232
10233 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +020010234 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010235 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010236 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010237
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010238 state = drm_atomic_state_alloc(dev);
10239 if (!state)
10240 return false;
10241
10242 state->acquire_ctx = ctx;
10243
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010244 connector_state = drm_atomic_get_connector_state(state, connector);
10245 if (IS_ERR(connector_state)) {
10246 ret = PTR_ERR(connector_state);
10247 goto fail;
10248 }
10249
10250 connector_state->crtc = crtc;
10251 connector_state->best_encoder = &intel_encoder->base;
10252
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010253 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10254 if (IS_ERR(crtc_state)) {
10255 ret = PTR_ERR(crtc_state);
10256 goto fail;
10257 }
10258
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010259 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010260
Chris Wilson64927112011-04-20 07:25:26 +010010261 if (!mode)
10262 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010263
Chris Wilsond2dff872011-04-19 08:36:26 +010010264 /* We need a framebuffer large enough to accommodate all accesses
10265 * that the plane may generate whilst we perform load detection.
10266 * We can not rely on the fbcon either being present (we get called
10267 * during its initialisation to detect all boot displays, or it may
10268 * not even exist) or that it is large enough to satisfy the
10269 * requested mode.
10270 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010271 fb = mode_fits_in_fbdev(dev, mode);
10272 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010273 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010274 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10275 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010276 } else
10277 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010278 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010279 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010280 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010281 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010282
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010283 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10284 if (ret)
10285 goto fail;
10286
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010287 drm_mode_copy(&crtc_state->base.mode, mode);
10288
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020010289 if (drm_atomic_commit(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010290 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010291 if (old->release_fb)
10292 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010293 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010294 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010295 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010296
Jesse Barnes79e53942008-11-07 14:24:08 -080010297 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010298 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010299 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010300
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010301fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010302 drm_atomic_state_free(state);
10303 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010304
Rob Clark51fd3712013-11-19 12:10:12 -050010305 if (ret == -EDEADLK) {
10306 drm_modeset_backoff(ctx);
10307 goto retry;
10308 }
10309
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010310 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010311}
10312
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010313void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010314 struct intel_load_detect_pipe *old,
10315 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010316{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010317 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010318 struct intel_encoder *intel_encoder =
10319 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010320 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010321 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010322 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010323 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010324 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010325 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010326 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010327
Chris Wilsond2dff872011-04-19 08:36:26 +010010328 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010329 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010330 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010331
Chris Wilson8261b192011-04-19 23:18:09 +010010332 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010333 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010334 if (!state)
10335 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010336
10337 state->acquire_ctx = ctx;
10338
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010339 connector_state = drm_atomic_get_connector_state(state, connector);
10340 if (IS_ERR(connector_state))
10341 goto fail;
10342
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010343 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10344 if (IS_ERR(crtc_state))
10345 goto fail;
10346
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010347 connector_state->best_encoder = NULL;
10348 connector_state->crtc = NULL;
10349
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010350 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010351
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010352 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10353 0, 0);
10354 if (ret)
10355 goto fail;
10356
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020010357 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010358 if (ret)
10359 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010360
Daniel Vetter36206362012-12-10 20:42:17 +010010361 if (old->release_fb) {
10362 drm_framebuffer_unregister_private(old->release_fb);
10363 drm_framebuffer_unreference(old->release_fb);
10364 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010365
Chris Wilson0622a532011-04-21 09:32:11 +010010366 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010367 }
10368
Eric Anholtc751ce42010-03-25 11:48:48 -070010369 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010370 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10371 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010372
10373 return;
10374fail:
10375 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10376 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010377}
10378
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010379static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010380 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010381{
10382 struct drm_i915_private *dev_priv = dev->dev_private;
10383 u32 dpll = pipe_config->dpll_hw_state.dpll;
10384
10385 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010386 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010387 else if (HAS_PCH_SPLIT(dev))
10388 return 120000;
10389 else if (!IS_GEN2(dev))
10390 return 96000;
10391 else
10392 return 48000;
10393}
10394
Jesse Barnes79e53942008-11-07 14:24:08 -080010395/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010396static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010397 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010398{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010399 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010400 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010401 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010402 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010403 u32 fp;
10404 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010405 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010406 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010407
10408 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010409 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010410 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010411 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010412
10413 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010414 if (IS_PINEVIEW(dev)) {
10415 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10416 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010417 } else {
10418 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10419 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10420 }
10421
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010422 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010423 if (IS_PINEVIEW(dev))
10424 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10425 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010426 else
10427 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010428 DPLL_FPA01_P1_POST_DIV_SHIFT);
10429
10430 switch (dpll & DPLL_MODE_MASK) {
10431 case DPLLB_MODE_DAC_SERIAL:
10432 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10433 5 : 10;
10434 break;
10435 case DPLLB_MODE_LVDS:
10436 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10437 7 : 14;
10438 break;
10439 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010440 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010441 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010442 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010443 }
10444
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010445 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010446 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010447 else
Imre Deakdccbea32015-06-22 23:35:51 +030010448 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010449 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010450 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010451 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010452
10453 if (is_lvds) {
10454 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10455 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010456
10457 if (lvds & LVDS_CLKB_POWER_UP)
10458 clock.p2 = 7;
10459 else
10460 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010461 } else {
10462 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10463 clock.p1 = 2;
10464 else {
10465 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10466 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10467 }
10468 if (dpll & PLL_P2_DIVIDE_BY_4)
10469 clock.p2 = 4;
10470 else
10471 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010472 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010473
Imre Deakdccbea32015-06-22 23:35:51 +030010474 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010475 }
10476
Ville Syrjälä18442d02013-09-13 16:00:08 +030010477 /*
10478 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010479 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010480 * encoder's get_config() function.
10481 */
Imre Deakdccbea32015-06-22 23:35:51 +030010482 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010483}
10484
Ville Syrjälä6878da02013-09-13 15:59:11 +030010485int intel_dotclock_calculate(int link_freq,
10486 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010487{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010488 /*
10489 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010490 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010491 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010492 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010493 *
10494 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010495 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010496 */
10497
Ville Syrjälä6878da02013-09-13 15:59:11 +030010498 if (!m_n->link_n)
10499 return 0;
10500
10501 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10502}
10503
Ville Syrjälä18442d02013-09-13 16:00:08 +030010504static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010505 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010506{
10507 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010508
10509 /* read out port_clock from the DPLL */
10510 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010511
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010512 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010513 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010514 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010515 * agree once we know their relationship in the encoder's
10516 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010517 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010518 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010519 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10520 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010521}
10522
10523/** Returns the currently programmed mode of the given pipe. */
10524struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10525 struct drm_crtc *crtc)
10526{
Jesse Barnes548f2452011-02-17 10:40:53 -080010527 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010528 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010529 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010530 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010531 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010532 int htot = I915_READ(HTOTAL(cpu_transcoder));
10533 int hsync = I915_READ(HSYNC(cpu_transcoder));
10534 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10535 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010536 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010537
10538 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10539 if (!mode)
10540 return NULL;
10541
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010542 /*
10543 * Construct a pipe_config sufficient for getting the clock info
10544 * back out of crtc_clock_get.
10545 *
10546 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10547 * to use a real value here instead.
10548 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010549 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010550 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010551 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10552 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10553 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010554 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10555
Ville Syrjälä773ae032013-09-23 17:48:20 +030010556 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010557 mode->hdisplay = (htot & 0xffff) + 1;
10558 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10559 mode->hsync_start = (hsync & 0xffff) + 1;
10560 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10561 mode->vdisplay = (vtot & 0xffff) + 1;
10562 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10563 mode->vsync_start = (vsync & 0xffff) + 1;
10564 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10565
10566 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010567
10568 return mode;
10569}
10570
Chris Wilsonf047e392012-07-21 12:31:41 +010010571void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010572{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010573 struct drm_i915_private *dev_priv = dev->dev_private;
10574
Chris Wilsonf62a0072014-02-21 17:55:39 +000010575 if (dev_priv->mm.busy)
10576 return;
10577
Paulo Zanoni43694d62014-03-07 20:08:08 -030010578 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010579 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010580 if (INTEL_INFO(dev)->gen >= 6)
10581 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010582 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010583}
10584
10585void intel_mark_idle(struct drm_device *dev)
10586{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010587 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010588
Chris Wilsonf62a0072014-02-21 17:55:39 +000010589 if (!dev_priv->mm.busy)
10590 return;
10591
10592 dev_priv->mm.busy = false;
10593
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010594 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010595 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010596
Paulo Zanoni43694d62014-03-07 20:08:08 -030010597 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010598}
10599
Jesse Barnes79e53942008-11-07 14:24:08 -080010600static void intel_crtc_destroy(struct drm_crtc *crtc)
10601{
10602 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010603 struct drm_device *dev = crtc->dev;
10604 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010605
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010606 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010607 work = intel_crtc->unpin_work;
10608 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010609 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010610
10611 if (work) {
10612 cancel_work_sync(&work->work);
10613 kfree(work);
10614 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010615
10616 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010617
Jesse Barnes79e53942008-11-07 14:24:08 -080010618 kfree(intel_crtc);
10619}
10620
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010621static void intel_unpin_work_fn(struct work_struct *__work)
10622{
10623 struct intel_unpin_work *work =
10624 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010625 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10626 struct drm_device *dev = crtc->base.dev;
10627 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010628
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010629 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010630 intel_unpin_fb_obj(work->old_fb, primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010631 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010632
John Harrisonf06cc1b2014-11-24 18:49:37 +000010633 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010634 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010635 mutex_unlock(&dev->struct_mutex);
10636
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010637 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010638 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010639
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010640 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10641 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010642
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010643 kfree(work);
10644}
10645
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010646static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010647 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010648{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010649 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10650 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010651 unsigned long flags;
10652
10653 /* Ignore early vblank irqs */
10654 if (intel_crtc == NULL)
10655 return;
10656
Daniel Vetterf3260382014-09-15 14:55:23 +020010657 /*
10658 * This is called both by irq handlers and the reset code (to complete
10659 * lost pageflips) so needs the full irqsave spinlocks.
10660 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010661 spin_lock_irqsave(&dev->event_lock, flags);
10662 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010663
10664 /* Ensure we don't miss a work->pending update ... */
10665 smp_rmb();
10666
10667 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010668 spin_unlock_irqrestore(&dev->event_lock, flags);
10669 return;
10670 }
10671
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010672 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010673
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010674 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010675}
10676
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010677void intel_finish_page_flip(struct drm_device *dev, int pipe)
10678{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010679 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010680 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10681
Mario Kleiner49b14a52010-12-09 07:00:07 +010010682 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010683}
10684
10685void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10686{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010687 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010688 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10689
Mario Kleiner49b14a52010-12-09 07:00:07 +010010690 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010691}
10692
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010693/* Is 'a' after or equal to 'b'? */
10694static bool g4x_flip_count_after_eq(u32 a, u32 b)
10695{
10696 return !((a - b) & 0x80000000);
10697}
10698
10699static bool page_flip_finished(struct intel_crtc *crtc)
10700{
10701 struct drm_device *dev = crtc->base.dev;
10702 struct drm_i915_private *dev_priv = dev->dev_private;
10703
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010704 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10705 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10706 return true;
10707
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010708 /*
10709 * The relevant registers doen't exist on pre-ctg.
10710 * As the flip done interrupt doesn't trigger for mmio
10711 * flips on gmch platforms, a flip count check isn't
10712 * really needed there. But since ctg has the registers,
10713 * include it in the check anyway.
10714 */
10715 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10716 return true;
10717
10718 /*
10719 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10720 * used the same base address. In that case the mmio flip might
10721 * have completed, but the CS hasn't even executed the flip yet.
10722 *
10723 * A flip count check isn't enough as the CS might have updated
10724 * the base address just after start of vblank, but before we
10725 * managed to process the interrupt. This means we'd complete the
10726 * CS flip too soon.
10727 *
10728 * Combining both checks should get us a good enough result. It may
10729 * still happen that the CS flip has been executed, but has not
10730 * yet actually completed. But in case the base address is the same
10731 * anyway, we don't really care.
10732 */
10733 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10734 crtc->unpin_work->gtt_offset &&
10735 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10736 crtc->unpin_work->flip_count);
10737}
10738
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010739void intel_prepare_page_flip(struct drm_device *dev, int plane)
10740{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010741 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010742 struct intel_crtc *intel_crtc =
10743 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10744 unsigned long flags;
10745
Daniel Vetterf3260382014-09-15 14:55:23 +020010746
10747 /*
10748 * This is called both by irq handlers and the reset code (to complete
10749 * lost pageflips) so needs the full irqsave spinlocks.
10750 *
10751 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010752 * generate a page-flip completion irq, i.e. every modeset
10753 * is also accompanied by a spurious intel_prepare_page_flip().
10754 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010755 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010756 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010757 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010758 spin_unlock_irqrestore(&dev->event_lock, flags);
10759}
10760
Robin Schroereba905b2014-05-18 02:24:50 +020010761static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010762{
10763 /* Ensure that the work item is consistent when activating it ... */
10764 smp_wmb();
10765 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10766 /* and that it is marked active as soon as the irq could fire. */
10767 smp_wmb();
10768}
10769
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010770static int intel_gen2_queue_flip(struct drm_device *dev,
10771 struct drm_crtc *crtc,
10772 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010773 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010774 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010775 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010776{
John Harrison6258fbe2015-05-29 17:43:48 +010010777 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010778 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010779 u32 flip_mask;
10780 int ret;
10781
John Harrison5fb9de12015-05-29 17:44:07 +010010782 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010783 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010784 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010785
10786 /* Can't queue multiple flips, so wait for the previous
10787 * one to finish before executing the next.
10788 */
10789 if (intel_crtc->plane)
10790 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10791 else
10792 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010793 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10794 intel_ring_emit(ring, MI_NOOP);
10795 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10796 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10797 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010798 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010799 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010800
10801 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010802 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010803}
10804
10805static int intel_gen3_queue_flip(struct drm_device *dev,
10806 struct drm_crtc *crtc,
10807 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010808 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010809 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010810 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010811{
John Harrison6258fbe2015-05-29 17:43:48 +010010812 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010814 u32 flip_mask;
10815 int ret;
10816
John Harrison5fb9de12015-05-29 17:44:07 +010010817 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010818 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010819 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010820
10821 if (intel_crtc->plane)
10822 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10823 else
10824 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010825 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10826 intel_ring_emit(ring, MI_NOOP);
10827 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10828 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10829 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010830 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010831 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010832
Chris Wilsone7d841c2012-12-03 11:36:30 +000010833 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010834 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010835}
10836
10837static int intel_gen4_queue_flip(struct drm_device *dev,
10838 struct drm_crtc *crtc,
10839 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010840 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010841 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010842 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010843{
John Harrison6258fbe2015-05-29 17:43:48 +010010844 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010845 struct drm_i915_private *dev_priv = dev->dev_private;
10846 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10847 uint32_t pf, pipesrc;
10848 int ret;
10849
John Harrison5fb9de12015-05-29 17:44:07 +010010850 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010851 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010852 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010853
10854 /* i965+ uses the linear or tiled offsets from the
10855 * Display Registers (which do not change across a page-flip)
10856 * so we need only reprogram the base address.
10857 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010858 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10859 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10860 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010861 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010862 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010863
10864 /* XXX Enabling the panel-fitter across page-flip is so far
10865 * untested on non-native modes, so ignore it for now.
10866 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10867 */
10868 pf = 0;
10869 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010870 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010871
10872 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010873 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010874}
10875
10876static int intel_gen6_queue_flip(struct drm_device *dev,
10877 struct drm_crtc *crtc,
10878 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010879 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010880 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010881 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010882{
John Harrison6258fbe2015-05-29 17:43:48 +010010883 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010884 struct drm_i915_private *dev_priv = dev->dev_private;
10885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10886 uint32_t pf, pipesrc;
10887 int ret;
10888
John Harrison5fb9de12015-05-29 17:44:07 +010010889 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010890 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010891 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010892
Daniel Vetter6d90c952012-04-26 23:28:05 +020010893 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10894 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10895 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010896 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010897
Chris Wilson99d9acd2012-04-17 20:37:00 +010010898 /* Contrary to the suggestions in the documentation,
10899 * "Enable Panel Fitter" does not seem to be required when page
10900 * flipping with a non-native mode, and worse causes a normal
10901 * modeset to fail.
10902 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10903 */
10904 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010905 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010906 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010907
10908 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010909 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010910}
10911
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010912static int intel_gen7_queue_flip(struct drm_device *dev,
10913 struct drm_crtc *crtc,
10914 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010915 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010916 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010917 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010918{
John Harrison6258fbe2015-05-29 17:43:48 +010010919 struct intel_engine_cs *ring = req->ring;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010920 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010921 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010922 int len, ret;
10923
Robin Schroereba905b2014-05-18 02:24:50 +020010924 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010925 case PLANE_A:
10926 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10927 break;
10928 case PLANE_B:
10929 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10930 break;
10931 case PLANE_C:
10932 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10933 break;
10934 default:
10935 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010936 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010937 }
10938
Chris Wilsonffe74d72013-08-26 20:58:12 +010010939 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010940 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010941 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010942 /*
10943 * On Gen 8, SRM is now taking an extra dword to accommodate
10944 * 48bits addresses, and we need a NOOP for the batch size to
10945 * stay even.
10946 */
10947 if (IS_GEN8(dev))
10948 len += 2;
10949 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010950
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010951 /*
10952 * BSpec MI_DISPLAY_FLIP for IVB:
10953 * "The full packet must be contained within the same cache line."
10954 *
10955 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10956 * cacheline, if we ever start emitting more commands before
10957 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10958 * then do the cacheline alignment, and finally emit the
10959 * MI_DISPLAY_FLIP.
10960 */
John Harrisonbba09b12015-05-29 17:44:06 +010010961 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010962 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010963 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010964
John Harrison5fb9de12015-05-29 17:44:07 +010010965 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010966 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010967 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010968
Chris Wilsonffe74d72013-08-26 20:58:12 +010010969 /* Unmask the flip-done completion message. Note that the bspec says that
10970 * we should do this for both the BCS and RCS, and that we must not unmask
10971 * more than one flip event at any time (or ensure that one flip message
10972 * can be sent by waiting for flip-done prior to queueing new flips).
10973 * Experimentation says that BCS works despite DERRMR masking all
10974 * flip-done completion events and that unmasking all planes at once
10975 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10976 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10977 */
10978 if (ring->id == RCS) {
10979 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10980 intel_ring_emit(ring, DERRMR);
10981 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10982 DERRMR_PIPEB_PRI_FLIP_DONE |
10983 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010984 if (IS_GEN8(dev))
Arun Siluveryf1afe242015-08-04 16:22:20 +010010985 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010010986 MI_SRM_LRM_GLOBAL_GTT);
10987 else
Arun Siluveryf1afe242015-08-04 16:22:20 +010010988 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010010989 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010990 intel_ring_emit(ring, DERRMR);
10991 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010992 if (IS_GEN8(dev)) {
10993 intel_ring_emit(ring, 0);
10994 intel_ring_emit(ring, MI_NOOP);
10995 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010996 }
10997
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010998 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010999 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011000 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011001 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011002
11003 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011004 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011005}
11006
Sourab Gupta84c33a62014-06-02 16:47:17 +053011007static bool use_mmio_flip(struct intel_engine_cs *ring,
11008 struct drm_i915_gem_object *obj)
11009{
11010 /*
11011 * This is not being used for older platforms, because
11012 * non-availability of flip done interrupt forces us to use
11013 * CS flips. Older platforms derive flip done using some clever
11014 * tricks involving the flip_pending status bits and vblank irqs.
11015 * So using MMIO flips there would disrupt this mechanism.
11016 */
11017
Chris Wilson8e09bf82014-07-08 10:40:30 +010011018 if (ring == NULL)
11019 return true;
11020
Sourab Gupta84c33a62014-06-02 16:47:17 +053011021 if (INTEL_INFO(ring->dev)->gen < 5)
11022 return false;
11023
11024 if (i915.use_mmio_flip < 0)
11025 return false;
11026 else if (i915.use_mmio_flip > 0)
11027 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011028 else if (i915.enable_execlists)
11029 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011030 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011031 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011032}
11033
Damien Lespiauff944562014-11-20 14:58:16 +000011034static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11035{
11036 struct drm_device *dev = intel_crtc->base.dev;
11037 struct drm_i915_private *dev_priv = dev->dev_private;
11038 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011039 const enum pipe pipe = intel_crtc->pipe;
11040 u32 ctl, stride;
11041
11042 ctl = I915_READ(PLANE_CTL(pipe, 0));
11043 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011044 switch (fb->modifier[0]) {
11045 case DRM_FORMAT_MOD_NONE:
11046 break;
11047 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011048 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011049 break;
11050 case I915_FORMAT_MOD_Y_TILED:
11051 ctl |= PLANE_CTL_TILED_Y;
11052 break;
11053 case I915_FORMAT_MOD_Yf_TILED:
11054 ctl |= PLANE_CTL_TILED_YF;
11055 break;
11056 default:
11057 MISSING_CASE(fb->modifier[0]);
11058 }
Damien Lespiauff944562014-11-20 14:58:16 +000011059
11060 /*
11061 * The stride is either expressed as a multiple of 64 bytes chunks for
11062 * linear buffers or in number of tiles for tiled buffers.
11063 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011064 stride = fb->pitches[0] /
11065 intel_fb_stride_alignment(dev, fb->modifier[0],
11066 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011067
11068 /*
11069 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11070 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11071 */
11072 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11073 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11074
11075 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11076 POSTING_READ(PLANE_SURF(pipe, 0));
11077}
11078
11079static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011080{
11081 struct drm_device *dev = intel_crtc->base.dev;
11082 struct drm_i915_private *dev_priv = dev->dev_private;
11083 struct intel_framebuffer *intel_fb =
11084 to_intel_framebuffer(intel_crtc->base.primary->fb);
11085 struct drm_i915_gem_object *obj = intel_fb->obj;
11086 u32 dspcntr;
11087 u32 reg;
11088
Sourab Gupta84c33a62014-06-02 16:47:17 +053011089 reg = DSPCNTR(intel_crtc->plane);
11090 dspcntr = I915_READ(reg);
11091
Damien Lespiauc5d97472014-10-25 00:11:11 +010011092 if (obj->tiling_mode != I915_TILING_NONE)
11093 dspcntr |= DISPPLANE_TILED;
11094 else
11095 dspcntr &= ~DISPPLANE_TILED;
11096
Sourab Gupta84c33a62014-06-02 16:47:17 +053011097 I915_WRITE(reg, dspcntr);
11098
11099 I915_WRITE(DSPSURF(intel_crtc->plane),
11100 intel_crtc->unpin_work->gtt_offset);
11101 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011102
Damien Lespiauff944562014-11-20 14:58:16 +000011103}
11104
11105/*
11106 * XXX: This is the temporary way to update the plane registers until we get
11107 * around to using the usual plane update functions for MMIO flips
11108 */
11109static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11110{
11111 struct drm_device *dev = intel_crtc->base.dev;
Damien Lespiauff944562014-11-20 14:58:16 +000011112 u32 start_vbl_count;
11113
11114 intel_mark_page_flip_active(intel_crtc);
11115
Maarten Lankhorst8f539a82015-07-13 16:30:32 +020011116 intel_pipe_update_start(intel_crtc, &start_vbl_count);
Damien Lespiauff944562014-11-20 14:58:16 +000011117
11118 if (INTEL_INFO(dev)->gen >= 9)
11119 skl_do_mmio_flip(intel_crtc);
11120 else
11121 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11122 ilk_do_mmio_flip(intel_crtc);
11123
Maarten Lankhorst8f539a82015-07-13 16:30:32 +020011124 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011125}
11126
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011127static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011128{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011129 struct intel_mmio_flip *mmio_flip =
11130 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011131
Daniel Vettereed29a52015-05-21 14:21:25 +020011132 if (mmio_flip->req)
11133 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011134 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011135 false, NULL,
11136 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011137
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011138 intel_do_mmio_flip(mmio_flip->crtc);
11139
Daniel Vettereed29a52015-05-21 14:21:25 +020011140 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011141 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011142}
11143
11144static int intel_queue_mmio_flip(struct drm_device *dev,
11145 struct drm_crtc *crtc,
11146 struct drm_framebuffer *fb,
11147 struct drm_i915_gem_object *obj,
11148 struct intel_engine_cs *ring,
11149 uint32_t flags)
11150{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011151 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011152
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011153 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11154 if (mmio_flip == NULL)
11155 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011156
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011157 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011158 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011159 mmio_flip->crtc = to_intel_crtc(crtc);
11160
11161 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11162 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011163
Sourab Gupta84c33a62014-06-02 16:47:17 +053011164 return 0;
11165}
11166
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011167static int intel_default_queue_flip(struct drm_device *dev,
11168 struct drm_crtc *crtc,
11169 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011170 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011171 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011172 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011173{
11174 return -ENODEV;
11175}
11176
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011177static bool __intel_pageflip_stall_check(struct drm_device *dev,
11178 struct drm_crtc *crtc)
11179{
11180 struct drm_i915_private *dev_priv = dev->dev_private;
11181 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11182 struct intel_unpin_work *work = intel_crtc->unpin_work;
11183 u32 addr;
11184
11185 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11186 return true;
11187
Chris Wilson908565c2015-08-12 13:08:22 +010011188 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11189 return false;
11190
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011191 if (!work->enable_stall_check)
11192 return false;
11193
11194 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011195 if (work->flip_queued_req &&
11196 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011197 return false;
11198
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011199 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011200 }
11201
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011202 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011203 return false;
11204
11205 /* Potential stall - if we see that the flip has happened,
11206 * assume a missed interrupt. */
11207 if (INTEL_INFO(dev)->gen >= 4)
11208 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11209 else
11210 addr = I915_READ(DSPADDR(intel_crtc->plane));
11211
11212 /* There is a potential issue here with a false positive after a flip
11213 * to the same address. We could address this by checking for a
11214 * non-incrementing frame counter.
11215 */
11216 return addr == work->gtt_offset;
11217}
11218
11219void intel_check_page_flip(struct drm_device *dev, int pipe)
11220{
11221 struct drm_i915_private *dev_priv = dev->dev_private;
11222 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11223 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011224 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011225
Dave Gordon6c51d462015-03-06 15:34:26 +000011226 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011227
11228 if (crtc == NULL)
11229 return;
11230
Daniel Vetterf3260382014-09-15 14:55:23 +020011231 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011232 work = intel_crtc->unpin_work;
11233 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011234 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011235 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011236 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011237 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011238 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011239 if (work != NULL &&
11240 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11241 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011242 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011243}
11244
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011245static int intel_crtc_page_flip(struct drm_crtc *crtc,
11246 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011247 struct drm_pending_vblank_event *event,
11248 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011249{
11250 struct drm_device *dev = crtc->dev;
11251 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011252 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011253 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011255 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011256 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011257 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011258 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011259 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011260 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011261 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011262
Matt Roper2ff8fde2014-07-08 07:50:07 -070011263 /*
11264 * drm_mode_page_flip_ioctl() should already catch this, but double
11265 * check to be safe. In the future we may enable pageflipping from
11266 * a disabled primary plane.
11267 */
11268 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11269 return -EBUSY;
11270
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011271 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011272 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011273 return -EINVAL;
11274
11275 /*
11276 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11277 * Note that pitch changes could also affect these register.
11278 */
11279 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011280 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11281 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011282 return -EINVAL;
11283
Chris Wilsonf900db42014-02-20 09:26:13 +000011284 if (i915_terminally_wedged(&dev_priv->gpu_error))
11285 goto out_hang;
11286
Daniel Vetterb14c5672013-09-19 12:18:32 +020011287 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011288 if (work == NULL)
11289 return -ENOMEM;
11290
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011291 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011292 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011293 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011294 INIT_WORK(&work->work, intel_unpin_work_fn);
11295
Daniel Vetter87b6b102014-05-15 15:33:46 +020011296 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011297 if (ret)
11298 goto free_work;
11299
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011300 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011301 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011302 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011303 /* Before declaring the flip queue wedged, check if
11304 * the hardware completed the operation behind our backs.
11305 */
11306 if (__intel_pageflip_stall_check(dev, crtc)) {
11307 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11308 page_flip_completed(intel_crtc);
11309 } else {
11310 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011311 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011312
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011313 drm_crtc_vblank_put(crtc);
11314 kfree(work);
11315 return -EBUSY;
11316 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011317 }
11318 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011319 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011320
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011321 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11322 flush_workqueue(dev_priv->wq);
11323
Jesse Barnes75dfca82010-02-10 15:09:44 -080011324 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011325 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011326 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011327
Matt Roperf4510a22014-04-01 15:22:40 -070011328 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011329 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011330
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011331 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011332
Chris Wilson89ed88b2015-02-16 14:31:49 +000011333 ret = i915_mutex_lock_interruptible(dev);
11334 if (ret)
11335 goto cleanup;
11336
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011337 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011338 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011339
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011340 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011341 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011342
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011343 if (IS_VALLEYVIEW(dev)) {
11344 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011345 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011346 /* vlv: DISPLAY_FLIP fails to change tiling */
11347 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011348 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011349 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011350 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011351 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011352 if (ring == NULL || ring->id != RCS)
11353 ring = &dev_priv->ring[BCS];
11354 } else {
11355 ring = &dev_priv->ring[RCS];
11356 }
11357
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011358 mmio_flip = use_mmio_flip(ring, obj);
11359
11360 /* When using CS flips, we want to emit semaphores between rings.
11361 * However, when using mmio flips we will create a task to do the
11362 * synchronisation, so all we want here is to pin the framebuffer
11363 * into the display plane and skip any waits.
11364 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011365 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011366 crtc->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010011367 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011368 if (ret)
11369 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011370
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011371 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11372 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011373
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011374 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011375 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11376 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011377 if (ret)
11378 goto cleanup_unpin;
11379
John Harrisonf06cc1b2014-11-24 18:49:37 +000011380 i915_gem_request_assign(&work->flip_queued_req,
11381 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011382 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011383 if (!request) {
11384 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11385 if (ret)
11386 goto cleanup_unpin;
11387 }
11388
11389 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011390 page_flip_flags);
11391 if (ret)
11392 goto cleanup_unpin;
11393
John Harrison6258fbe2015-05-29 17:43:48 +010011394 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011395 }
11396
John Harrison91af1272015-06-18 13:14:56 +010011397 if (request)
John Harrison75289872015-05-29 17:43:49 +010011398 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011399
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011400 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011401 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011402
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011403 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011404 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011405 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011406
Paulo Zanoni4e1e26f2015-07-14 16:29:13 -030011407 intel_fbc_disable_crtc(intel_crtc);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011408 intel_frontbuffer_flip_prepare(dev,
11409 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011410
Jesse Barnese5510fa2010-07-01 16:48:37 -070011411 trace_i915_flip_request(intel_crtc->plane, obj);
11412
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011413 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011414
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011415cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011416 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011417cleanup_pending:
John Harrison91af1272015-06-18 13:14:56 +010011418 if (request)
11419 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011420 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011421 mutex_unlock(&dev->struct_mutex);
11422cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011423 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011424 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011425
Chris Wilson89ed88b2015-02-16 14:31:49 +000011426 drm_gem_object_unreference_unlocked(&obj->base);
11427 drm_framebuffer_unreference(work->old_fb);
11428
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011429 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011430 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011431 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011432
Daniel Vetter87b6b102014-05-15 15:33:46 +020011433 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011434free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011435 kfree(work);
11436
Chris Wilsonf900db42014-02-20 09:26:13 +000011437 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011438 struct drm_atomic_state *state;
11439 struct drm_plane_state *plane_state;
11440
Chris Wilsonf900db42014-02-20 09:26:13 +000011441out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011442 state = drm_atomic_state_alloc(dev);
11443 if (!state)
11444 return -ENOMEM;
11445 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11446
11447retry:
11448 plane_state = drm_atomic_get_plane_state(state, primary);
11449 ret = PTR_ERR_OR_ZERO(plane_state);
11450 if (!ret) {
11451 drm_atomic_set_fb_for_plane(plane_state, fb);
11452
11453 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11454 if (!ret)
11455 ret = drm_atomic_commit(state);
11456 }
11457
11458 if (ret == -EDEADLK) {
11459 drm_modeset_backoff(state->acquire_ctx);
11460 drm_atomic_state_clear(state);
11461 goto retry;
11462 }
11463
11464 if (ret)
11465 drm_atomic_state_free(state);
11466
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011467 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011468 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011469 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011470 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011471 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011472 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011473 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011474}
11475
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011476
11477/**
11478 * intel_wm_need_update - Check whether watermarks need updating
11479 * @plane: drm plane
11480 * @state: new plane state
11481 *
11482 * Check current plane state versus the new one to determine whether
11483 * watermarks need to be recalculated.
11484 *
11485 * Returns true or false.
11486 */
11487static bool intel_wm_need_update(struct drm_plane *plane,
11488 struct drm_plane_state *state)
11489{
11490 /* Update watermarks on tiling changes. */
11491 if (!plane->state->fb || !state->fb ||
11492 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11493 plane->state->rotation != state->rotation)
11494 return true;
11495
11496 if (plane->state->crtc_w != state->crtc_w)
11497 return true;
11498
11499 return false;
11500}
11501
11502int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11503 struct drm_plane_state *plane_state)
11504{
11505 struct drm_crtc *crtc = crtc_state->crtc;
11506 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11507 struct drm_plane *plane = plane_state->plane;
11508 struct drm_device *dev = crtc->dev;
11509 struct drm_i915_private *dev_priv = dev->dev_private;
11510 struct intel_plane_state *old_plane_state =
11511 to_intel_plane_state(plane->state);
11512 int idx = intel_crtc->base.base.id, ret;
11513 int i = drm_plane_index(plane);
11514 bool mode_changed = needs_modeset(crtc_state);
11515 bool was_crtc_enabled = crtc->state->active;
11516 bool is_crtc_enabled = crtc_state->active;
11517
11518 bool turn_off, turn_on, visible, was_visible;
11519 struct drm_framebuffer *fb = plane_state->fb;
11520
11521 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11522 plane->type != DRM_PLANE_TYPE_CURSOR) {
11523 ret = skl_update_scaler_plane(
11524 to_intel_crtc_state(crtc_state),
11525 to_intel_plane_state(plane_state));
11526 if (ret)
11527 return ret;
11528 }
11529
11530 /*
11531 * Disabling a plane is always okay; we just need to update
11532 * fb tracking in a special way since cleanup_fb() won't
11533 * get called by the plane helpers.
11534 */
11535 if (old_plane_state->base.fb && !fb)
11536 intel_crtc->atomic.disabled_planes |= 1 << i;
11537
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011538 was_visible = old_plane_state->visible;
11539 visible = to_intel_plane_state(plane_state)->visible;
11540
11541 if (!was_crtc_enabled && WARN_ON(was_visible))
11542 was_visible = false;
11543
11544 if (!is_crtc_enabled && WARN_ON(visible))
11545 visible = false;
11546
11547 if (!was_visible && !visible)
11548 return 0;
11549
11550 turn_off = was_visible && (!visible || mode_changed);
11551 turn_on = visible && (!was_visible || mode_changed);
11552
11553 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11554 plane->base.id, fb ? fb->base.id : -1);
11555
11556 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11557 plane->base.id, was_visible, visible,
11558 turn_off, turn_on, mode_changed);
11559
Ville Syrjälä852eb002015-06-24 22:00:07 +030011560 if (turn_on) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011561 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011562 /* must disable cxsr around plane enable/disable */
11563 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11564 intel_crtc->atomic.disable_cxsr = true;
11565 /* to potentially re-enable cxsr */
11566 intel_crtc->atomic.wait_vblank = true;
11567 intel_crtc->atomic.update_wm_post = true;
11568 }
11569 } else if (turn_off) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011570 intel_crtc->atomic.update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011571 /* must disable cxsr around plane enable/disable */
11572 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11573 if (is_crtc_enabled)
11574 intel_crtc->atomic.wait_vblank = true;
11575 intel_crtc->atomic.disable_cxsr = true;
11576 }
11577 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011578 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011579 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011580
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011581 if (visible || was_visible)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011582 intel_crtc->atomic.fb_bits |=
11583 to_intel_plane(plane)->frontbuffer_bit;
11584
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011585 switch (plane->type) {
11586 case DRM_PLANE_TYPE_PRIMARY:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011587 intel_crtc->atomic.wait_for_flips = true;
11588 intel_crtc->atomic.pre_disable_primary = turn_off;
11589 intel_crtc->atomic.post_enable_primary = turn_on;
11590
Rodrigo Vivi066cf55b2015-06-26 13:55:54 -070011591 if (turn_off) {
11592 /*
11593 * FIXME: Actually if we will still have any other
11594 * plane enabled on the pipe we could let IPS enabled
11595 * still, but for now lets consider that when we make
11596 * primary invisible by setting DSPCNTR to 0 on
11597 * update_primary_plane function IPS needs to be
11598 * disable.
11599 */
11600 intel_crtc->atomic.disable_ips = true;
11601
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011602 intel_crtc->atomic.disable_fbc = true;
Rodrigo Vivi066cf55b2015-06-26 13:55:54 -070011603 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011604
11605 /*
11606 * FBC does not work on some platforms for rotated
11607 * planes, so disable it when rotation is not 0 and
11608 * update it when rotation is set back to 0.
11609 *
11610 * FIXME: This is redundant with the fbc update done in
11611 * the primary plane enable function except that that
11612 * one is done too late. We eventually need to unify
11613 * this.
11614 */
11615
11616 if (visible &&
11617 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11618 dev_priv->fbc.crtc == intel_crtc &&
11619 plane_state->rotation != BIT(DRM_ROTATE_0))
11620 intel_crtc->atomic.disable_fbc = true;
11621
11622 /*
11623 * BDW signals flip done immediately if the plane
11624 * is disabled, even if the plane enable is already
11625 * armed to occur at the next vblank :(
11626 */
11627 if (turn_on && IS_BROADWELL(dev))
11628 intel_crtc->atomic.wait_vblank = true;
11629
11630 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11631 break;
11632 case DRM_PLANE_TYPE_CURSOR:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011633 break;
11634 case DRM_PLANE_TYPE_OVERLAY:
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011635 if (turn_off && !mode_changed) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011636 intel_crtc->atomic.wait_vblank = true;
11637 intel_crtc->atomic.update_sprite_watermarks |=
11638 1 << i;
11639 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011640 }
11641 return 0;
11642}
11643
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011644static bool encoders_cloneable(const struct intel_encoder *a,
11645 const struct intel_encoder *b)
11646{
11647 /* masks could be asymmetric, so check both ways */
11648 return a == b || (a->cloneable & (1 << b->type) &&
11649 b->cloneable & (1 << a->type));
11650}
11651
11652static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11653 struct intel_crtc *crtc,
11654 struct intel_encoder *encoder)
11655{
11656 struct intel_encoder *source_encoder;
11657 struct drm_connector *connector;
11658 struct drm_connector_state *connector_state;
11659 int i;
11660
11661 for_each_connector_in_state(state, connector, connector_state, i) {
11662 if (connector_state->crtc != &crtc->base)
11663 continue;
11664
11665 source_encoder =
11666 to_intel_encoder(connector_state->best_encoder);
11667 if (!encoders_cloneable(encoder, source_encoder))
11668 return false;
11669 }
11670
11671 return true;
11672}
11673
11674static bool check_encoder_cloning(struct drm_atomic_state *state,
11675 struct intel_crtc *crtc)
11676{
11677 struct intel_encoder *encoder;
11678 struct drm_connector *connector;
11679 struct drm_connector_state *connector_state;
11680 int i;
11681
11682 for_each_connector_in_state(state, connector, connector_state, i) {
11683 if (connector_state->crtc != &crtc->base)
11684 continue;
11685
11686 encoder = to_intel_encoder(connector_state->best_encoder);
11687 if (!check_single_encoder_cloning(state, crtc, encoder))
11688 return false;
11689 }
11690
11691 return true;
11692}
11693
11694static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11695 struct drm_crtc_state *crtc_state)
11696{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011697 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011698 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011699 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011700 struct intel_crtc_state *pipe_config =
11701 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011702 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011703 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011704 bool mode_changed = needs_modeset(crtc_state);
11705
11706 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11707 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11708 return -EINVAL;
11709 }
11710
Ville Syrjälä852eb002015-06-24 22:00:07 +030011711 if (mode_changed && !crtc_state->active)
11712 intel_crtc->atomic.update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011713
Maarten Lankhorstad421372015-06-15 12:33:42 +020011714 if (mode_changed && crtc_state->enable &&
11715 dev_priv->display.crtc_compute_clock &&
11716 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11717 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11718 pipe_config);
11719 if (ret)
11720 return ret;
11721 }
11722
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011723 ret = 0;
11724 if (INTEL_INFO(dev)->gen >= 9) {
11725 if (mode_changed)
11726 ret = skl_update_scaler_crtc(pipe_config);
11727
11728 if (!ret)
11729 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11730 pipe_config);
11731 }
11732
11733 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011734}
11735
Jani Nikula65b38e02015-04-13 11:26:56 +030011736static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011737 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11738 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011739 .atomic_begin = intel_begin_crtc_commit,
11740 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011741 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011742};
11743
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011744static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11745{
11746 struct intel_connector *connector;
11747
11748 for_each_intel_connector(dev, connector) {
11749 if (connector->base.encoder) {
11750 connector->base.state->best_encoder =
11751 connector->base.encoder;
11752 connector->base.state->crtc =
11753 connector->base.encoder->crtc;
11754 } else {
11755 connector->base.state->best_encoder = NULL;
11756 connector->base.state->crtc = NULL;
11757 }
11758 }
11759}
11760
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011761static void
Robin Schroereba905b2014-05-18 02:24:50 +020011762connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011763 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011764{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011765 int bpp = pipe_config->pipe_bpp;
11766
11767 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11768 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011769 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011770
11771 /* Don't use an invalid EDID bpc value */
11772 if (connector->base.display_info.bpc &&
11773 connector->base.display_info.bpc * 3 < bpp) {
11774 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11775 bpp, connector->base.display_info.bpc*3);
11776 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11777 }
11778
11779 /* Clamp bpp to 8 on screens without EDID 1.4 */
11780 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11781 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11782 bpp);
11783 pipe_config->pipe_bpp = 24;
11784 }
11785}
11786
11787static int
11788compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011789 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011790{
11791 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011792 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011793 struct drm_connector *connector;
11794 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011795 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011796
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011797 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011798 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011799 else if (INTEL_INFO(dev)->gen >= 5)
11800 bpp = 12*3;
11801 else
11802 bpp = 8*3;
11803
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011804
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011805 pipe_config->pipe_bpp = bpp;
11806
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011807 state = pipe_config->base.state;
11808
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011809 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011810 for_each_connector_in_state(state, connector, connector_state, i) {
11811 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011812 continue;
11813
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011814 connected_sink_compute_bpp(to_intel_connector(connector),
11815 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011816 }
11817
11818 return bpp;
11819}
11820
Daniel Vetter644db712013-09-19 14:53:58 +020011821static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11822{
11823 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11824 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011825 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011826 mode->crtc_hdisplay, mode->crtc_hsync_start,
11827 mode->crtc_hsync_end, mode->crtc_htotal,
11828 mode->crtc_vdisplay, mode->crtc_vsync_start,
11829 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11830}
11831
Daniel Vetterc0b03412013-05-28 12:05:54 +020011832static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011833 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011834 const char *context)
11835{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011836 struct drm_device *dev = crtc->base.dev;
11837 struct drm_plane *plane;
11838 struct intel_plane *intel_plane;
11839 struct intel_plane_state *state;
11840 struct drm_framebuffer *fb;
11841
11842 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11843 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011844
11845 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11846 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11847 pipe_config->pipe_bpp, pipe_config->dither);
11848 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11849 pipe_config->has_pch_encoder,
11850 pipe_config->fdi_lanes,
11851 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11852 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11853 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011854 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 +030011855 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011856 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011857 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11858 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11859 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011860
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011861 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 -070011862 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011863 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011864 pipe_config->dp_m2_n2.gmch_m,
11865 pipe_config->dp_m2_n2.gmch_n,
11866 pipe_config->dp_m2_n2.link_m,
11867 pipe_config->dp_m2_n2.link_n,
11868 pipe_config->dp_m2_n2.tu);
11869
Daniel Vetter55072d12014-11-20 16:10:28 +010011870 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11871 pipe_config->has_audio,
11872 pipe_config->has_infoframe);
11873
Daniel Vetterc0b03412013-05-28 12:05:54 +020011874 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011875 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011876 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011877 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11878 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011879 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011880 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11881 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011882 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11883 crtc->num_scalers,
11884 pipe_config->scaler_state.scaler_users,
11885 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011886 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11887 pipe_config->gmch_pfit.control,
11888 pipe_config->gmch_pfit.pgm_ratios,
11889 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011890 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011891 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011892 pipe_config->pch_pfit.size,
11893 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011894 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011895 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011896
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011897 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030011898 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011899 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030011900 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011901 pipe_config->ddi_pll_sel,
11902 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030011903 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011904 pipe_config->dpll_hw_state.pll0,
11905 pipe_config->dpll_hw_state.pll1,
11906 pipe_config->dpll_hw_state.pll2,
11907 pipe_config->dpll_hw_state.pll3,
11908 pipe_config->dpll_hw_state.pll6,
11909 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030011910 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030011911 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011912 pipe_config->dpll_hw_state.pcsdw12);
11913 } else if (IS_SKYLAKE(dev)) {
11914 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11915 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11916 pipe_config->ddi_pll_sel,
11917 pipe_config->dpll_hw_state.ctrl1,
11918 pipe_config->dpll_hw_state.cfgcr1,
11919 pipe_config->dpll_hw_state.cfgcr2);
11920 } else if (HAS_DDI(dev)) {
11921 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11922 pipe_config->ddi_pll_sel,
11923 pipe_config->dpll_hw_state.wrpll);
11924 } else {
11925 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11926 "fp0: 0x%x, fp1: 0x%x\n",
11927 pipe_config->dpll_hw_state.dpll,
11928 pipe_config->dpll_hw_state.dpll_md,
11929 pipe_config->dpll_hw_state.fp0,
11930 pipe_config->dpll_hw_state.fp1);
11931 }
11932
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011933 DRM_DEBUG_KMS("planes on this crtc\n");
11934 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11935 intel_plane = to_intel_plane(plane);
11936 if (intel_plane->pipe != crtc->pipe)
11937 continue;
11938
11939 state = to_intel_plane_state(plane->state);
11940 fb = state->base.fb;
11941 if (!fb) {
11942 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11943 "disabled, scaler_id = %d\n",
11944 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11945 plane->base.id, intel_plane->pipe,
11946 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11947 drm_plane_index(plane), state->scaler_id);
11948 continue;
11949 }
11950
11951 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11952 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11953 plane->base.id, intel_plane->pipe,
11954 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11955 drm_plane_index(plane));
11956 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11957 fb->base.id, fb->width, fb->height, fb->pixel_format);
11958 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11959 state->scaler_id,
11960 state->src.x1 >> 16, state->src.y1 >> 16,
11961 drm_rect_width(&state->src) >> 16,
11962 drm_rect_height(&state->src) >> 16,
11963 state->dst.x1, state->dst.y1,
11964 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11965 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011966}
11967
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011968static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011969{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011970 struct drm_device *dev = state->dev;
11971 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011972 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011973 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011974 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011975 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011976
11977 /*
11978 * Walk the connector list instead of the encoder
11979 * list to detect the problem on ddi platforms
11980 * where there's just one encoder per digital port.
11981 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011982 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011983 if (!connector_state->best_encoder)
11984 continue;
11985
11986 encoder = to_intel_encoder(connector_state->best_encoder);
11987
11988 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011989
11990 switch (encoder->type) {
11991 unsigned int port_mask;
11992 case INTEL_OUTPUT_UNKNOWN:
11993 if (WARN_ON(!HAS_DDI(dev)))
11994 break;
11995 case INTEL_OUTPUT_DISPLAYPORT:
11996 case INTEL_OUTPUT_HDMI:
11997 case INTEL_OUTPUT_EDP:
11998 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11999
12000 /* the same port mustn't appear more than once */
12001 if (used_ports & port_mask)
12002 return false;
12003
12004 used_ports |= port_mask;
12005 default:
12006 break;
12007 }
12008 }
12009
12010 return true;
12011}
12012
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012013static void
12014clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12015{
12016 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012017 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012018 struct intel_dpll_hw_state dpll_hw_state;
12019 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012020 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012021 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012022
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012023 /* FIXME: before the switch to atomic started, a new pipe_config was
12024 * kzalloc'd. Code that depends on any field being zero should be
12025 * fixed, so that the crtc_state can be safely duplicated. For now,
12026 * only fields that are know to not cause problems are preserved. */
12027
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012028 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012029 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012030 shared_dpll = crtc_state->shared_dpll;
12031 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012032 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012033 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012034
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012035 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012036
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012037 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012038 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012039 crtc_state->shared_dpll = shared_dpll;
12040 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012041 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012042 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012043}
12044
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012045static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012046intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012047 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012048{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012049 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012050 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012051 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012052 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012053 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012054 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012055 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012056
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012057 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012058
Daniel Vettere143a212013-07-04 12:01:15 +020012059 pipe_config->cpu_transcoder =
12060 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012061
Imre Deak2960bc92013-07-30 13:36:32 +030012062 /*
12063 * Sanitize sync polarity flags based on requested ones. If neither
12064 * positive or negative polarity is requested, treat this as meaning
12065 * negative polarity.
12066 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012067 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012068 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012069 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012070
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012071 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012072 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012073 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012074
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012075 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12076 * plane pixel format and any sink constraints into account. Returns the
12077 * source plane bpp so that dithering can be selected on mismatches
12078 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012079 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12080 pipe_config);
12081 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012082 goto fail;
12083
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012084 /*
12085 * Determine the real pipe dimensions. Note that stereo modes can
12086 * increase the actual pipe size due to the frame doubling and
12087 * insertion of additional space for blanks between the frame. This
12088 * is stored in the crtc timings. We use the requested mode to do this
12089 * computation to clearly distinguish it from the adjusted mode, which
12090 * can be changed by the connectors in the below retry loop.
12091 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012092 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012093 &pipe_config->pipe_src_w,
12094 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012095
Daniel Vettere29c22c2013-02-21 00:00:16 +010012096encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012097 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012098 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012099 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012100
Daniel Vetter135c81b2013-07-21 21:37:09 +020012101 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012102 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12103 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012104
Daniel Vetter7758a112012-07-08 19:40:39 +020012105 /* Pass our mode to the connectors and the CRTC to give them a chance to
12106 * adjust it according to limitations or connector properties, and also
12107 * a chance to reject the mode entirely.
12108 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012109 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012110 if (connector_state->crtc != crtc)
12111 continue;
12112
12113 encoder = to_intel_encoder(connector_state->best_encoder);
12114
Daniel Vetterefea6e82013-07-21 21:36:59 +020012115 if (!(encoder->compute_config(encoder, pipe_config))) {
12116 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012117 goto fail;
12118 }
12119 }
12120
Daniel Vetterff9a6752013-06-01 17:16:21 +020012121 /* Set default port clock if not overwritten by the encoder. Needs to be
12122 * done afterwards in case the encoder adjusts the mode. */
12123 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012124 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012125 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012126
Daniel Vettera43f6e02013-06-07 23:10:32 +020012127 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012128 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012129 DRM_DEBUG_KMS("CRTC fixup failed\n");
12130 goto fail;
12131 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012132
12133 if (ret == RETRY) {
12134 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12135 ret = -EINVAL;
12136 goto fail;
12137 }
12138
12139 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12140 retry = false;
12141 goto encoder_retry;
12142 }
12143
Daniel Vettere8fa4272015-08-12 11:43:34 +020012144 /* Dithering seems to not pass-through bits correctly when it should, so
12145 * only enable it on 6bpc panels. */
12146 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012147 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012148 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012149
Daniel Vetter7758a112012-07-08 19:40:39 +020012150fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012151 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012152}
12153
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012154static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012155intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012156{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012157 struct drm_crtc *crtc;
12158 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012159 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012160
Ville Syrjälä76688512014-01-10 11:28:06 +020012161 /* Double check state. */
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012162 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012163 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012164
12165 /* Update hwmode for vblank functions */
12166 if (crtc->state->active)
12167 crtc->hwmode = crtc->state->adjusted_mode;
12168 else
12169 crtc->hwmode.crtc_clock = 0;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012170 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012171}
12172
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012173static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012174{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012175 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012176
12177 if (clock1 == clock2)
12178 return true;
12179
12180 if (!clock1 || !clock2)
12181 return false;
12182
12183 diff = abs(clock1 - clock2);
12184
12185 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12186 return true;
12187
12188 return false;
12189}
12190
Daniel Vetter25c5b262012-07-08 22:08:04 +020012191#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12192 list_for_each_entry((intel_crtc), \
12193 &(dev)->mode_config.crtc_list, \
12194 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012195 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012196
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012197
12198static bool
12199intel_compare_m_n(unsigned int m, unsigned int n,
12200 unsigned int m2, unsigned int n2,
12201 bool exact)
12202{
12203 if (m == m2 && n == n2)
12204 return true;
12205
12206 if (exact || !m || !n || !m2 || !n2)
12207 return false;
12208
12209 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12210
12211 if (m > m2) {
12212 while (m > m2) {
12213 m2 <<= 1;
12214 n2 <<= 1;
12215 }
12216 } else if (m < m2) {
12217 while (m < m2) {
12218 m <<= 1;
12219 n <<= 1;
12220 }
12221 }
12222
12223 return m == m2 && n == n2;
12224}
12225
12226static bool
12227intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12228 struct intel_link_m_n *m2_n2,
12229 bool adjust)
12230{
12231 if (m_n->tu == m2_n2->tu &&
12232 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12233 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12234 intel_compare_m_n(m_n->link_m, m_n->link_n,
12235 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12236 if (adjust)
12237 *m2_n2 = *m_n;
12238
12239 return true;
12240 }
12241
12242 return false;
12243}
12244
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012245static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012246intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012247 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012248 struct intel_crtc_state *pipe_config,
12249 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012250{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012251 bool ret = true;
12252
12253#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12254 do { \
12255 if (!adjust) \
12256 DRM_ERROR(fmt, ##__VA_ARGS__); \
12257 else \
12258 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12259 } while (0)
12260
Daniel Vetter66e985c2013-06-05 13:34:20 +020012261#define PIPE_CONF_CHECK_X(name) \
12262 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012263 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012264 "(expected 0x%08x, found 0x%08x)\n", \
12265 current_config->name, \
12266 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012267 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012268 }
12269
Daniel Vetter08a24032013-04-19 11:25:34 +020012270#define PIPE_CONF_CHECK_I(name) \
12271 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012272 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012273 "(expected %i, found %i)\n", \
12274 current_config->name, \
12275 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012276 ret = false; \
12277 }
12278
12279#define PIPE_CONF_CHECK_M_N(name) \
12280 if (!intel_compare_link_m_n(&current_config->name, \
12281 &pipe_config->name,\
12282 adjust)) { \
12283 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12284 "(expected tu %i gmch %i/%i link %i/%i, " \
12285 "found tu %i, gmch %i/%i link %i/%i)\n", \
12286 current_config->name.tu, \
12287 current_config->name.gmch_m, \
12288 current_config->name.gmch_n, \
12289 current_config->name.link_m, \
12290 current_config->name.link_n, \
12291 pipe_config->name.tu, \
12292 pipe_config->name.gmch_m, \
12293 pipe_config->name.gmch_n, \
12294 pipe_config->name.link_m, \
12295 pipe_config->name.link_n); \
12296 ret = false; \
12297 }
12298
12299#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12300 if (!intel_compare_link_m_n(&current_config->name, \
12301 &pipe_config->name, adjust) && \
12302 !intel_compare_link_m_n(&current_config->alt_name, \
12303 &pipe_config->name, adjust)) { \
12304 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12305 "(expected tu %i gmch %i/%i link %i/%i, " \
12306 "or tu %i gmch %i/%i link %i/%i, " \
12307 "found tu %i, gmch %i/%i link %i/%i)\n", \
12308 current_config->name.tu, \
12309 current_config->name.gmch_m, \
12310 current_config->name.gmch_n, \
12311 current_config->name.link_m, \
12312 current_config->name.link_n, \
12313 current_config->alt_name.tu, \
12314 current_config->alt_name.gmch_m, \
12315 current_config->alt_name.gmch_n, \
12316 current_config->alt_name.link_m, \
12317 current_config->alt_name.link_n, \
12318 pipe_config->name.tu, \
12319 pipe_config->name.gmch_m, \
12320 pipe_config->name.gmch_n, \
12321 pipe_config->name.link_m, \
12322 pipe_config->name.link_n); \
12323 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012324 }
12325
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012326/* This is required for BDW+ where there is only one set of registers for
12327 * switching between high and low RR.
12328 * This macro can be used whenever a comparison has to be made between one
12329 * hw state and multiple sw state variables.
12330 */
12331#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12332 if ((current_config->name != pipe_config->name) && \
12333 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012334 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012335 "(expected %i or %i, found %i)\n", \
12336 current_config->name, \
12337 current_config->alt_name, \
12338 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012339 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012340 }
12341
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012342#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12343 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012344 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012345 "(expected %i, found %i)\n", \
12346 current_config->name & (mask), \
12347 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012348 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012349 }
12350
Ville Syrjälä5e550652013-09-06 23:29:07 +030012351#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12352 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012353 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012354 "(expected %i, found %i)\n", \
12355 current_config->name, \
12356 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012357 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012358 }
12359
Daniel Vetterbb760062013-06-06 14:55:52 +020012360#define PIPE_CONF_QUIRK(quirk) \
12361 ((current_config->quirks | pipe_config->quirks) & (quirk))
12362
Daniel Vettereccb1402013-05-22 00:50:22 +020012363 PIPE_CONF_CHECK_I(cpu_transcoder);
12364
Daniel Vetter08a24032013-04-19 11:25:34 +020012365 PIPE_CONF_CHECK_I(has_pch_encoder);
12366 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012367 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012368
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012369 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012370 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012371
12372 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012373 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012374
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012375 PIPE_CONF_CHECK_I(has_drrs);
12376 if (current_config->has_drrs)
12377 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12378 } else
12379 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012380
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012381 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12382 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12383 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12384 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12385 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12386 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012387
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012388 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12389 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12390 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12391 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12392 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12393 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012394
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012395 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012396 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012397 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12398 IS_VALLEYVIEW(dev))
12399 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012400 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012401
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012402 PIPE_CONF_CHECK_I(has_audio);
12403
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012404 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012405 DRM_MODE_FLAG_INTERLACE);
12406
Daniel Vetterbb760062013-06-06 14:55:52 +020012407 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012408 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012409 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012410 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012411 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012412 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012413 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012414 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012415 DRM_MODE_FLAG_NVSYNC);
12416 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012417
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012418 PIPE_CONF_CHECK_I(pipe_src_w);
12419 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012420
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012421 PIPE_CONF_CHECK_I(gmch_pfit.control);
12422 /* pfit ratios are autocomputed by the hw on gen4+ */
12423 if (INTEL_INFO(dev)->gen < 4)
12424 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12425 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012426
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012427 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12428 if (current_config->pch_pfit.enabled) {
12429 PIPE_CONF_CHECK_I(pch_pfit.pos);
12430 PIPE_CONF_CHECK_I(pch_pfit.size);
12431 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012432
Chandra Kondurua1b22782015-04-07 15:28:45 -070012433 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12434
Jesse Barnese59150d2014-01-07 13:30:45 -080012435 /* BDW+ don't expose a synchronous way to read the state */
12436 if (IS_HASWELL(dev))
12437 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012438
Ville Syrjälä282740f2013-09-04 18:30:03 +030012439 PIPE_CONF_CHECK_I(double_wide);
12440
Daniel Vetter26804af2014-06-25 22:01:55 +030012441 PIPE_CONF_CHECK_X(ddi_pll_sel);
12442
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012443 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012444 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012445 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012446 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12447 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012448 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012449 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12450 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12451 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012452
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012453 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12454 PIPE_CONF_CHECK_I(pipe_bpp);
12455
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012456 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012457 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012458
Daniel Vetter66e985c2013-06-05 13:34:20 +020012459#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012460#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012461#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012462#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012463#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012464#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012465#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012466
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012467 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012468}
12469
Damien Lespiau08db6652014-11-04 17:06:52 +000012470static void check_wm_state(struct drm_device *dev)
12471{
12472 struct drm_i915_private *dev_priv = dev->dev_private;
12473 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12474 struct intel_crtc *intel_crtc;
12475 int plane;
12476
12477 if (INTEL_INFO(dev)->gen < 9)
12478 return;
12479
12480 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12481 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12482
12483 for_each_intel_crtc(dev, intel_crtc) {
12484 struct skl_ddb_entry *hw_entry, *sw_entry;
12485 const enum pipe pipe = intel_crtc->pipe;
12486
12487 if (!intel_crtc->active)
12488 continue;
12489
12490 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012491 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012492 hw_entry = &hw_ddb.plane[pipe][plane];
12493 sw_entry = &sw_ddb->plane[pipe][plane];
12494
12495 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12496 continue;
12497
12498 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12499 "(expected (%u,%u), found (%u,%u))\n",
12500 pipe_name(pipe), plane + 1,
12501 sw_entry->start, sw_entry->end,
12502 hw_entry->start, hw_entry->end);
12503 }
12504
12505 /* cursor */
12506 hw_entry = &hw_ddb.cursor[pipe];
12507 sw_entry = &sw_ddb->cursor[pipe];
12508
12509 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12510 continue;
12511
12512 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12513 "(expected (%u,%u), found (%u,%u))\n",
12514 pipe_name(pipe),
12515 sw_entry->start, sw_entry->end,
12516 hw_entry->start, hw_entry->end);
12517 }
12518}
12519
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012520static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012521check_connector_state(struct drm_device *dev,
12522 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012523{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012524 struct drm_connector_state *old_conn_state;
12525 struct drm_connector *connector;
12526 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012527
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012528 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12529 struct drm_encoder *encoder = connector->encoder;
12530 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012531
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012532 /* This also checks the encoder/connector hw state with the
12533 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012534 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012535
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012536 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012537 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012538 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012539}
12540
12541static void
12542check_encoder_state(struct drm_device *dev)
12543{
12544 struct intel_encoder *encoder;
12545 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012546
Damien Lespiaub2784e12014-08-05 11:29:37 +010012547 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012548 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012549 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012550
12551 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12552 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012553 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012554
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012555 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012556 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012557 continue;
12558 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012559
12560 I915_STATE_WARN(connector->base.state->crtc !=
12561 encoder->base.crtc,
12562 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012563 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012564
Rob Clarke2c719b2014-12-15 13:56:32 -050012565 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012566 "encoder's enabled state mismatch "
12567 "(expected %i, found %i)\n",
12568 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012569
12570 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012571 bool active;
12572
12573 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012574 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012575 "encoder detached but still enabled on pipe %c.\n",
12576 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012577 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012578 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012579}
12580
12581static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012582check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012583{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012584 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012585 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012586 struct drm_crtc_state *old_crtc_state;
12587 struct drm_crtc *crtc;
12588 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012589
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012590 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12591 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12592 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020012593 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012594
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012595 if (!needs_modeset(crtc->state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012596 continue;
12597
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012598 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12599 pipe_config = to_intel_crtc_state(old_crtc_state);
12600 memset(pipe_config, 0, sizeof(*pipe_config));
12601 pipe_config->base.crtc = crtc;
12602 pipe_config->base.state = old_state;
12603
12604 DRM_DEBUG_KMS("[CRTC:%d]\n",
12605 crtc->base.id);
12606
12607 active = dev_priv->display.get_pipe_config(intel_crtc,
12608 pipe_config);
12609
12610 /* hw state is inconsistent with the pipe quirk */
12611 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12612 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12613 active = crtc->state->active;
12614
12615 I915_STATE_WARN(crtc->state->active != active,
12616 "crtc active state doesn't match with hw state "
12617 "(expected %i, found %i)\n", crtc->state->active, active);
12618
12619 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
12620 "transitional active state does not match atomic hw state "
12621 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12622
12623 for_each_encoder_on_crtc(dev, crtc, encoder) {
12624 enum pipe pipe;
12625
12626 active = encoder->get_hw_state(encoder, &pipe);
12627 I915_STATE_WARN(active != crtc->state->active,
12628 "[ENCODER:%i] active %i with crtc active %i\n",
12629 encoder->base.base.id, active, crtc->state->active);
12630
12631 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12632 "Encoder connected to wrong pipe %c\n",
12633 pipe_name(pipe));
12634
12635 if (active)
12636 encoder->get_config(encoder, pipe_config);
12637 }
12638
12639 if (!crtc->state->active)
12640 continue;
12641
12642 sw_config = to_intel_crtc_state(crtc->state);
12643 if (!intel_pipe_config_compare(dev, sw_config,
12644 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012645 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012646 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012647 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012648 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012649 "[sw state]");
12650 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012651 }
12652}
12653
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012654static void
12655check_shared_dpll_state(struct drm_device *dev)
12656{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012657 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012658 struct intel_crtc *crtc;
12659 struct intel_dpll_hw_state dpll_hw_state;
12660 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012661
12662 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12663 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12664 int enabled_crtcs = 0, active_crtcs = 0;
12665 bool active;
12666
12667 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12668
12669 DRM_DEBUG_KMS("%s\n", pll->name);
12670
12671 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12672
Rob Clarke2c719b2014-12-15 13:56:32 -050012673 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012674 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012675 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012676 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012677 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012678 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012679 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012680 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012681 "pll on state mismatch (expected %i, found %i)\n",
12682 pll->on, active);
12683
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012684 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012685 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012686 enabled_crtcs++;
12687 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12688 active_crtcs++;
12689 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012690 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012691 "pll active crtcs mismatch (expected %i, found %i)\n",
12692 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012693 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012694 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012695 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012696
Rob Clarke2c719b2014-12-15 13:56:32 -050012697 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012698 sizeof(dpll_hw_state)),
12699 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012700 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012701}
12702
Maarten Lankhorstee165b12015-08-05 12:37:00 +020012703static void
12704intel_modeset_check_state(struct drm_device *dev,
12705 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012706{
Damien Lespiau08db6652014-11-04 17:06:52 +000012707 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012708 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012709 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012710 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012711 check_shared_dpll_state(dev);
12712}
12713
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012714void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012715 int dotclock)
12716{
12717 /*
12718 * FDI already provided one idea for the dotclock.
12719 * Yell if the encoder disagrees.
12720 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012721 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012722 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012723 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012724}
12725
Ville Syrjälä80715b22014-05-15 20:23:23 +030012726static void update_scanline_offset(struct intel_crtc *crtc)
12727{
12728 struct drm_device *dev = crtc->base.dev;
12729
12730 /*
12731 * The scanline counter increments at the leading edge of hsync.
12732 *
12733 * On most platforms it starts counting from vtotal-1 on the
12734 * first active line. That means the scanline counter value is
12735 * always one less than what we would expect. Ie. just after
12736 * start of vblank, which also occurs at start of hsync (on the
12737 * last active line), the scanline counter will read vblank_start-1.
12738 *
12739 * On gen2 the scanline counter starts counting from 1 instead
12740 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12741 * to keep the value positive), instead of adding one.
12742 *
12743 * On HSW+ the behaviour of the scanline counter depends on the output
12744 * type. For DP ports it behaves like most other platforms, but on HDMI
12745 * there's an extra 1 line difference. So we need to add two instead of
12746 * one to the value.
12747 */
12748 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012749 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012750 int vtotal;
12751
12752 vtotal = mode->crtc_vtotal;
12753 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12754 vtotal /= 2;
12755
12756 crtc->scanline_offset = vtotal - 1;
12757 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012758 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012759 crtc->scanline_offset = 2;
12760 } else
12761 crtc->scanline_offset = 1;
12762}
12763
Maarten Lankhorstad421372015-06-15 12:33:42 +020012764static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012765{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012766 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012767 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012768 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012769 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012770 struct intel_crtc_state *intel_crtc_state;
12771 struct drm_crtc *crtc;
12772 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012773 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012774
12775 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012776 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012777
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012778 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012779 int dpll;
12780
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012781 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012782 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012783 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012784
Maarten Lankhorstad421372015-06-15 12:33:42 +020012785 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012786 continue;
12787
Maarten Lankhorstad421372015-06-15 12:33:42 +020012788 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012789
Maarten Lankhorstad421372015-06-15 12:33:42 +020012790 if (!shared_dpll)
12791 shared_dpll = intel_atomic_get_shared_dpll_state(state);
12792
12793 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012794 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012795}
12796
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012797/*
12798 * This implements the workaround described in the "notes" section of the mode
12799 * set sequence documentation. When going from no pipes or single pipe to
12800 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12801 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12802 */
12803static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12804{
12805 struct drm_crtc_state *crtc_state;
12806 struct intel_crtc *intel_crtc;
12807 struct drm_crtc *crtc;
12808 struct intel_crtc_state *first_crtc_state = NULL;
12809 struct intel_crtc_state *other_crtc_state = NULL;
12810 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12811 int i;
12812
12813 /* look at all crtc's that are going to be enabled in during modeset */
12814 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12815 intel_crtc = to_intel_crtc(crtc);
12816
12817 if (!crtc_state->active || !needs_modeset(crtc_state))
12818 continue;
12819
12820 if (first_crtc_state) {
12821 other_crtc_state = to_intel_crtc_state(crtc_state);
12822 break;
12823 } else {
12824 first_crtc_state = to_intel_crtc_state(crtc_state);
12825 first_pipe = intel_crtc->pipe;
12826 }
12827 }
12828
12829 /* No workaround needed? */
12830 if (!first_crtc_state)
12831 return 0;
12832
12833 /* w/a possibly needed, check how many crtc's are already enabled. */
12834 for_each_intel_crtc(state->dev, intel_crtc) {
12835 struct intel_crtc_state *pipe_config;
12836
12837 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12838 if (IS_ERR(pipe_config))
12839 return PTR_ERR(pipe_config);
12840
12841 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12842
12843 if (!pipe_config->base.active ||
12844 needs_modeset(&pipe_config->base))
12845 continue;
12846
12847 /* 2 or more enabled crtcs means no need for w/a */
12848 if (enabled_pipe != INVALID_PIPE)
12849 return 0;
12850
12851 enabled_pipe = intel_crtc->pipe;
12852 }
12853
12854 if (enabled_pipe != INVALID_PIPE)
12855 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12856 else if (other_crtc_state)
12857 other_crtc_state->hsw_workaround_pipe = first_pipe;
12858
12859 return 0;
12860}
12861
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012862static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12863{
12864 struct drm_crtc *crtc;
12865 struct drm_crtc_state *crtc_state;
12866 int ret = 0;
12867
12868 /* add all active pipes to the state */
12869 for_each_crtc(state->dev, crtc) {
12870 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12871 if (IS_ERR(crtc_state))
12872 return PTR_ERR(crtc_state);
12873
12874 if (!crtc_state->active || needs_modeset(crtc_state))
12875 continue;
12876
12877 crtc_state->mode_changed = true;
12878
12879 ret = drm_atomic_add_affected_connectors(state, crtc);
12880 if (ret)
12881 break;
12882
12883 ret = drm_atomic_add_affected_planes(state, crtc);
12884 if (ret)
12885 break;
12886 }
12887
12888 return ret;
12889}
12890
12891
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012892static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012893{
12894 struct drm_device *dev = state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012895 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012896 int ret;
12897
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012898 if (!check_digital_port_conflicts(state)) {
12899 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12900 return -EINVAL;
12901 }
12902
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012903 /*
12904 * See if the config requires any additional preparation, e.g.
12905 * to adjust global state with pipes off. We need to do this
12906 * here so we can get the modeset_pipe updated config for the new
12907 * mode set on this crtc. For other crtcs we need to use the
12908 * adjusted_mode bits in the crtc directly.
12909 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012910 if (dev_priv->display.modeset_calc_cdclk) {
12911 unsigned int cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030012912
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012913 ret = dev_priv->display.modeset_calc_cdclk(state);
12914
12915 cdclk = to_intel_atomic_state(state)->cdclk;
12916 if (!ret && cdclk != dev_priv->cdclk_freq)
12917 ret = intel_modeset_all_pipes(state);
12918
12919 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012920 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012921 } else
12922 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012923
Maarten Lankhorstad421372015-06-15 12:33:42 +020012924 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012925
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012926 if (IS_HASWELL(dev))
Maarten Lankhorstad421372015-06-15 12:33:42 +020012927 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012928
Maarten Lankhorstad421372015-06-15 12:33:42 +020012929 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012930}
12931
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012932/**
12933 * intel_atomic_check - validate state object
12934 * @dev: drm device
12935 * @state: state to validate
12936 */
12937static int intel_atomic_check(struct drm_device *dev,
12938 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012939{
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012940 struct drm_crtc *crtc;
12941 struct drm_crtc_state *crtc_state;
12942 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012943 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012944
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012945 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012946 if (ret)
12947 return ret;
12948
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012949 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012950 struct intel_crtc_state *pipe_config =
12951 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012952
12953 /* Catch I915_MODE_FLAG_INHERITED */
12954 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
12955 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012956
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012957 if (!crtc_state->enable) {
12958 if (needs_modeset(crtc_state))
12959 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012960 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012961 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012962
Daniel Vetter26495482015-07-15 14:15:52 +020012963 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012964 continue;
12965
Daniel Vetter26495482015-07-15 14:15:52 +020012966 /* FIXME: For only active_changed we shouldn't need to do any
12967 * state recomputation at all. */
12968
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012969 ret = drm_atomic_add_affected_connectors(state, crtc);
12970 if (ret)
12971 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012972
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012973 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012974 if (ret)
12975 return ret;
12976
Daniel Vetter26495482015-07-15 14:15:52 +020012977 if (i915.fastboot &&
12978 intel_pipe_config_compare(state->dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012979 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012980 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020012981 crtc_state->mode_changed = false;
12982 }
12983
12984 if (needs_modeset(crtc_state)) {
12985 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012986
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012987 ret = drm_atomic_add_affected_planes(state, crtc);
12988 if (ret)
12989 return ret;
12990 }
12991
Daniel Vetter26495482015-07-15 14:15:52 +020012992 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12993 needs_modeset(crtc_state) ?
12994 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012995 }
12996
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012997 if (any_ms) {
12998 ret = intel_modeset_checks(state);
12999
13000 if (ret)
13001 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013002 } else
13003 to_intel_atomic_state(state)->cdclk =
13004 to_i915(state->dev)->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013005
13006 return drm_atomic_helper_check_planes(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013007}
13008
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013009/**
13010 * intel_atomic_commit - commit validated state object
13011 * @dev: DRM device
13012 * @state: the top-level driver state object
13013 * @async: asynchronous commit
13014 *
13015 * This function commits a top-level state object that has been validated
13016 * with drm_atomic_helper_check().
13017 *
13018 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13019 * we can only handle plane-related operations and do not yet support
13020 * asynchronous commit.
13021 *
13022 * RETURNS
13023 * Zero for success or -errno.
13024 */
13025static int intel_atomic_commit(struct drm_device *dev,
13026 struct drm_atomic_state *state,
13027 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013028{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013029 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013030 struct drm_crtc *crtc;
13031 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013032 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013033 int i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013034 bool any_ms = false;
Daniel Vettera6778b32012-07-02 09:56:42 +020013035
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013036 if (async) {
13037 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13038 return -EINVAL;
13039 }
13040
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013041 ret = drm_atomic_helper_prepare_planes(dev, state);
13042 if (ret)
13043 return ret;
13044
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013045 drm_atomic_helper_swap_state(dev, state);
13046
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013047 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013048 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13049
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013050 if (!needs_modeset(crtc->state))
13051 continue;
13052
13053 any_ms = true;
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013054 intel_pre_plane_update(intel_crtc);
Daniel Vetter460da9162013-03-27 00:44:51 +010013055
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013056 if (crtc_state->active) {
13057 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13058 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013059 intel_crtc->active = false;
13060 intel_disable_shared_dpll(intel_crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013061 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013062 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013063
Daniel Vetterea9d7582012-07-10 10:42:52 +020013064 /* Only after disabling all output pipelines that will be changed can we
13065 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013066 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013067
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013068 if (any_ms) {
13069 intel_shared_dpll_commit(state);
13070
13071 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013072 modeset_update_crtc_power_domains(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013073 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013074
Daniel Vettera6778b32012-07-02 09:56:42 +020013075 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013076 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013077 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13078 bool modeset = needs_modeset(crtc->state);
13079
13080 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013081 update_scanline_offset(to_intel_crtc(crtc));
13082 dev_priv->display.crtc_enable(crtc);
13083 }
13084
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013085 if (!modeset)
13086 intel_pre_plane_update(intel_crtc);
13087
Maarten Lankhorst5ac1c4b2015-06-01 12:50:01 +020013088 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013089 intel_post_plane_update(intel_crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013090 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013091
Daniel Vettera6778b32012-07-02 09:56:42 +020013092 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013093
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013094 drm_atomic_helper_wait_for_vblanks(dev, state);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013095 drm_atomic_helper_cleanup_planes(dev, state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013096
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013097 if (any_ms)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013098 intel_modeset_check_state(dev, state);
13099
13100 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013101
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013102 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013103}
13104
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013105void intel_crtc_restore_mode(struct drm_crtc *crtc)
13106{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013107 struct drm_device *dev = crtc->dev;
13108 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013109 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013110 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013111
13112 state = drm_atomic_state_alloc(dev);
13113 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013114 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013115 crtc->base.id);
13116 return;
13117 }
13118
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013119 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013120
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013121retry:
13122 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13123 ret = PTR_ERR_OR_ZERO(crtc_state);
13124 if (!ret) {
13125 if (!crtc_state->active)
13126 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013127
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013128 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013129 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013130 }
13131
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013132 if (ret == -EDEADLK) {
13133 drm_atomic_state_clear(state);
13134 drm_modeset_backoff(state->acquire_ctx);
13135 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013136 }
13137
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013138 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013139out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013140 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013141}
13142
Daniel Vetter25c5b262012-07-08 22:08:04 +020013143#undef for_each_intel_crtc_masked
13144
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013145static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013146 .gamma_set = intel_crtc_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013147 .set_config = drm_atomic_helper_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013148 .destroy = intel_crtc_destroy,
13149 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013150 .atomic_duplicate_state = intel_crtc_duplicate_state,
13151 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013152};
13153
Daniel Vetter53589012013-06-05 13:34:16 +020013154static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13155 struct intel_shared_dpll *pll,
13156 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013157{
Daniel Vetter53589012013-06-05 13:34:16 +020013158 uint32_t val;
13159
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013160 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013161 return false;
13162
Daniel Vetter53589012013-06-05 13:34:16 +020013163 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013164 hw_state->dpll = val;
13165 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13166 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013167
13168 return val & DPLL_VCO_ENABLE;
13169}
13170
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013171static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13172 struct intel_shared_dpll *pll)
13173{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013174 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13175 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013176}
13177
Daniel Vettere7b903d2013-06-05 13:34:14 +020013178static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13179 struct intel_shared_dpll *pll)
13180{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013181 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013182 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013183
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013184 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013185
13186 /* Wait for the clocks to stabilize. */
13187 POSTING_READ(PCH_DPLL(pll->id));
13188 udelay(150);
13189
13190 /* The pixel multiplier can only be updated once the
13191 * DPLL is enabled and the clocks are stable.
13192 *
13193 * So write it again.
13194 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013195 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013196 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013197 udelay(200);
13198}
13199
13200static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13201 struct intel_shared_dpll *pll)
13202{
13203 struct drm_device *dev = dev_priv->dev;
13204 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013205
13206 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013207 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013208 if (intel_crtc_to_shared_dpll(crtc) == pll)
13209 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13210 }
13211
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013212 I915_WRITE(PCH_DPLL(pll->id), 0);
13213 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013214 udelay(200);
13215}
13216
Daniel Vetter46edb022013-06-05 13:34:12 +020013217static char *ibx_pch_dpll_names[] = {
13218 "PCH DPLL A",
13219 "PCH DPLL B",
13220};
13221
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013222static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013223{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013224 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013225 int i;
13226
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013227 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013228
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013229 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013230 dev_priv->shared_dplls[i].id = i;
13231 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013232 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013233 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13234 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013235 dev_priv->shared_dplls[i].get_hw_state =
13236 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013237 }
13238}
13239
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013240static void intel_shared_dpll_init(struct drm_device *dev)
13241{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013242 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013243
Ville Syrjäläb6283052015-06-03 15:45:07 +030013244 intel_update_cdclk(dev);
13245
Daniel Vetter9cd86932014-06-25 22:01:57 +030013246 if (HAS_DDI(dev))
13247 intel_ddi_pll_init(dev);
13248 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013249 ibx_pch_dpll_init(dev);
13250 else
13251 dev_priv->num_shared_dpll = 0;
13252
13253 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013254}
13255
Matt Roper6beb8c232014-12-01 15:40:14 -080013256/**
13257 * intel_prepare_plane_fb - Prepare fb for usage on plane
13258 * @plane: drm plane to prepare for
13259 * @fb: framebuffer to prepare for presentation
13260 *
13261 * Prepares a framebuffer for usage on a display plane. Generally this
13262 * involves pinning the underlying object and updating the frontbuffer tracking
13263 * bits. Some older platforms need special physical address handling for
13264 * cursor planes.
13265 *
13266 * Returns 0 on success, negative error code on failure.
13267 */
13268int
13269intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013270 struct drm_framebuffer *fb,
13271 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013272{
13273 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013274 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013275 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13276 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013277 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013278
Matt Roperea2c67b2014-12-23 10:41:52 -080013279 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013280 return 0;
13281
Matt Roper4c345742014-07-09 16:22:10 -070013282 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013283
Matt Roper6beb8c232014-12-01 15:40:14 -080013284 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13285 INTEL_INFO(dev)->cursor_needs_physical) {
13286 int align = IS_I830(dev) ? 16 * 1024 : 256;
13287 ret = i915_gem_object_attach_phys(obj, align);
13288 if (ret)
13289 DRM_DEBUG_KMS("failed to attach phys object\n");
13290 } else {
John Harrison91af1272015-06-18 13:14:56 +010013291 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013292 }
13293
13294 if (ret == 0)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013295 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Matt Roper6beb8c232014-12-01 15:40:14 -080013296
13297 mutex_unlock(&dev->struct_mutex);
13298
13299 return ret;
13300}
13301
Matt Roper38f3ce32014-12-02 07:45:25 -080013302/**
13303 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13304 * @plane: drm plane to clean up for
13305 * @fb: old framebuffer that was on plane
13306 *
13307 * Cleans up a framebuffer that has just been removed from a plane.
13308 */
13309void
13310intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013311 struct drm_framebuffer *fb,
13312 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013313{
13314 struct drm_device *dev = plane->dev;
13315 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13316
13317 if (WARN_ON(!obj))
13318 return;
13319
13320 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13321 !INTEL_INFO(dev)->cursor_needs_physical) {
13322 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013323 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013324 mutex_unlock(&dev->struct_mutex);
13325 }
Matt Roper465c1202014-05-29 08:06:54 -070013326}
13327
Chandra Konduru6156a452015-04-27 13:48:39 -070013328int
13329skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13330{
13331 int max_scale;
13332 struct drm_device *dev;
13333 struct drm_i915_private *dev_priv;
13334 int crtc_clock, cdclk;
13335
13336 if (!intel_crtc || !crtc_state)
13337 return DRM_PLANE_HELPER_NO_SCALING;
13338
13339 dev = intel_crtc->base.dev;
13340 dev_priv = dev->dev_private;
13341 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013342 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013343
13344 if (!crtc_clock || !cdclk)
13345 return DRM_PLANE_HELPER_NO_SCALING;
13346
13347 /*
13348 * skl max scale is lower of:
13349 * close to 3 but not 3, -1 is for that purpose
13350 * or
13351 * cdclk/crtc_clock
13352 */
13353 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13354
13355 return max_scale;
13356}
13357
Matt Roper465c1202014-05-29 08:06:54 -070013358static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013359intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013360 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013361 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013362{
Matt Roper2b875c22014-12-01 15:40:13 -080013363 struct drm_crtc *crtc = state->base.crtc;
13364 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013365 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013366 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13367 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013368
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013369 /* use scaler when colorkey is not required */
13370 if (INTEL_INFO(plane->dev)->gen >= 9 &&
Maarten Lankhorst818ed962015-06-15 12:33:54 +020013371 state->ckey.flags == I915_SET_COLORKEY_NONE) {
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013372 min_scale = 1;
13373 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013374 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013375 }
Sonika Jindald8106362015-04-10 14:37:28 +053013376
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013377 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13378 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013379 min_scale, max_scale,
13380 can_position, true,
13381 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013382}
13383
Gustavo Padovan14af2932014-10-24 14:51:31 +010013384static void
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013385intel_commit_primary_plane(struct drm_plane *plane,
13386 struct intel_plane_state *state)
13387{
Matt Roper2b875c22014-12-01 15:40:13 -080013388 struct drm_crtc *crtc = state->base.crtc;
13389 struct drm_framebuffer *fb = state->base.fb;
13390 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013391 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013392 struct intel_crtc *intel_crtc;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013393 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013394
Matt Roperea2c67b2014-12-23 10:41:52 -080013395 crtc = crtc ? crtc : plane->crtc;
13396 intel_crtc = to_intel_crtc(crtc);
13397
Matt Ropercf4c7c12014-12-04 10:27:42 -080013398 plane->fb = fb;
Matt Roper9dc806f2014-11-17 18:10:38 -080013399 crtc->x = src->x1 >> 16;
13400 crtc->y = src->y1 >> 16;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013401
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013402 if (!crtc->state->active)
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013403 return;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013404
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013405 if (state->visible)
13406 /* FIXME: kill this fastboot hack */
13407 intel_update_pipe_size(intel_crtc);
13408
13409 dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013410}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013411
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013412static void
13413intel_disable_primary_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013414 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013415{
13416 struct drm_device *dev = plane->dev;
13417 struct drm_i915_private *dev_priv = dev->dev_private;
13418
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013419 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13420}
13421
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013422static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13423 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013424{
13425 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013427
Ville Syrjäläf015c552015-06-24 22:00:02 +030013428 if (intel_crtc->atomic.update_wm_pre)
Matt Roper32b7eee2014-12-24 07:59:06 -080013429 intel_update_watermarks(crtc);
13430
Matt Roperc34c9ee2014-12-23 10:41:50 -080013431 /* Perform vblank evasion around commit operation */
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013432 if (crtc->state->active)
Maarten Lankhorst8f539a82015-07-13 16:30:32 +020013433 intel_pipe_update_start(intel_crtc, &intel_crtc->start_vbl_count);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013434
13435 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13436 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013437}
13438
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013439static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13440 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013441{
Matt Roper32b7eee2014-12-24 07:59:06 -080013442 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013443
Maarten Lankhorst8f539a82015-07-13 16:30:32 +020013444 if (crtc->state->active)
13445 intel_pipe_update_end(intel_crtc, intel_crtc->start_vbl_count);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013446}
13447
Matt Ropercf4c7c12014-12-04 10:27:42 -080013448/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013449 * intel_plane_destroy - destroy a plane
13450 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013451 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013452 * Common destruction function for all types of planes (primary, cursor,
13453 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013454 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013455void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013456{
13457 struct intel_plane *intel_plane = to_intel_plane(plane);
13458 drm_plane_cleanup(plane);
13459 kfree(intel_plane);
13460}
13461
Matt Roper65a3fea2015-01-21 16:35:42 -080013462const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013463 .update_plane = drm_atomic_helper_update_plane,
13464 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013465 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013466 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013467 .atomic_get_property = intel_plane_atomic_get_property,
13468 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013469 .atomic_duplicate_state = intel_plane_duplicate_state,
13470 .atomic_destroy_state = intel_plane_destroy_state,
13471
Matt Roper465c1202014-05-29 08:06:54 -070013472};
13473
13474static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13475 int pipe)
13476{
13477 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013478 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013479 const uint32_t *intel_primary_formats;
13480 int num_formats;
13481
13482 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13483 if (primary == NULL)
13484 return NULL;
13485
Matt Roper8e7d6882015-01-21 16:35:41 -080013486 state = intel_create_plane_state(&primary->base);
13487 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013488 kfree(primary);
13489 return NULL;
13490 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013491 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013492
Matt Roper465c1202014-05-29 08:06:54 -070013493 primary->can_scale = false;
13494 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013495 if (INTEL_INFO(dev)->gen >= 9) {
13496 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013497 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013498 }
Matt Roper465c1202014-05-29 08:06:54 -070013499 primary->pipe = pipe;
13500 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013501 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013502 primary->check_plane = intel_check_primary_plane;
13503 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013504 primary->disable_plane = intel_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013505 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13506 primary->plane = !pipe;
13507
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013508 if (INTEL_INFO(dev)->gen >= 9) {
13509 intel_primary_formats = skl_primary_formats;
13510 num_formats = ARRAY_SIZE(skl_primary_formats);
13511 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013512 intel_primary_formats = i965_primary_formats;
13513 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013514 } else {
13515 intel_primary_formats = i8xx_primary_formats;
13516 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013517 }
13518
13519 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013520 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013521 intel_primary_formats, num_formats,
13522 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013523
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013524 if (INTEL_INFO(dev)->gen >= 4)
13525 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013526
Matt Roperea2c67b2014-12-23 10:41:52 -080013527 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13528
Matt Roper465c1202014-05-29 08:06:54 -070013529 return &primary->base;
13530}
13531
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013532void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13533{
13534 if (!dev->mode_config.rotation_property) {
13535 unsigned long flags = BIT(DRM_ROTATE_0) |
13536 BIT(DRM_ROTATE_180);
13537
13538 if (INTEL_INFO(dev)->gen >= 9)
13539 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13540
13541 dev->mode_config.rotation_property =
13542 drm_mode_create_rotation_property(dev, flags);
13543 }
13544 if (dev->mode_config.rotation_property)
13545 drm_object_attach_property(&plane->base.base,
13546 dev->mode_config.rotation_property,
13547 plane->base.state->rotation);
13548}
13549
Matt Roper3d7d6512014-06-10 08:28:13 -070013550static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013551intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013552 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013553 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013554{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013555 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013556 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013557 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013558 unsigned stride;
13559 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013560
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013561 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13562 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013563 DRM_PLANE_HELPER_NO_SCALING,
13564 DRM_PLANE_HELPER_NO_SCALING,
13565 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013566 if (ret)
13567 return ret;
13568
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013569 /* if we want to turn off the cursor ignore width and height */
13570 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013571 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013572
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013573 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013574 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013575 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13576 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013577 return -EINVAL;
13578 }
13579
Matt Roperea2c67b2014-12-23 10:41:52 -080013580 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13581 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013582 DRM_DEBUG_KMS("buffer is too small\n");
13583 return -ENOMEM;
13584 }
13585
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013586 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013587 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013588 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013589 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013590
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013591 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013592}
13593
Matt Roperf4a2cf22014-12-01 15:40:12 -080013594static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013595intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013596 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013597{
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013598 intel_crtc_update_cursor(crtc, false);
13599}
13600
13601static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013602intel_commit_cursor_plane(struct drm_plane *plane,
13603 struct intel_plane_state *state)
13604{
Matt Roper2b875c22014-12-01 15:40:13 -080013605 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013606 struct drm_device *dev = plane->dev;
13607 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013608 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013609 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013610
Matt Roperea2c67b2014-12-23 10:41:52 -080013611 crtc = crtc ? crtc : plane->crtc;
13612 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013613
Matt Roperea2c67b2014-12-23 10:41:52 -080013614 plane->fb = state->base.fb;
13615 crtc->cursor_x = state->base.crtc_x;
13616 crtc->cursor_y = state->base.crtc_y;
13617
Gustavo Padovana912f122014-12-01 15:40:10 -080013618 if (intel_crtc->cursor_bo == obj)
13619 goto update;
13620
Matt Roperf4a2cf22014-12-01 15:40:12 -080013621 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013622 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013623 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013624 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013625 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013626 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013627
Gustavo Padovana912f122014-12-01 15:40:10 -080013628 intel_crtc->cursor_addr = addr;
13629 intel_crtc->cursor_bo = obj;
Gustavo Padovana912f122014-12-01 15:40:10 -080013630
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013631update:
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013632 if (crtc->state->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013633 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013634}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013635
Matt Roper3d7d6512014-06-10 08:28:13 -070013636static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13637 int pipe)
13638{
13639 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013640 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013641
13642 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13643 if (cursor == NULL)
13644 return NULL;
13645
Matt Roper8e7d6882015-01-21 16:35:41 -080013646 state = intel_create_plane_state(&cursor->base);
13647 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013648 kfree(cursor);
13649 return NULL;
13650 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013651 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013652
Matt Roper3d7d6512014-06-10 08:28:13 -070013653 cursor->can_scale = false;
13654 cursor->max_downscale = 1;
13655 cursor->pipe = pipe;
13656 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013657 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013658 cursor->check_plane = intel_check_cursor_plane;
13659 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013660 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013661
13662 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013663 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013664 intel_cursor_formats,
13665 ARRAY_SIZE(intel_cursor_formats),
13666 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013667
13668 if (INTEL_INFO(dev)->gen >= 4) {
13669 if (!dev->mode_config.rotation_property)
13670 dev->mode_config.rotation_property =
13671 drm_mode_create_rotation_property(dev,
13672 BIT(DRM_ROTATE_0) |
13673 BIT(DRM_ROTATE_180));
13674 if (dev->mode_config.rotation_property)
13675 drm_object_attach_property(&cursor->base.base,
13676 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013677 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013678 }
13679
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013680 if (INTEL_INFO(dev)->gen >=9)
13681 state->scaler_id = -1;
13682
Matt Roperea2c67b2014-12-23 10:41:52 -080013683 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13684
Matt Roper3d7d6512014-06-10 08:28:13 -070013685 return &cursor->base;
13686}
13687
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013688static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13689 struct intel_crtc_state *crtc_state)
13690{
13691 int i;
13692 struct intel_scaler *intel_scaler;
13693 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13694
13695 for (i = 0; i < intel_crtc->num_scalers; i++) {
13696 intel_scaler = &scaler_state->scalers[i];
13697 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013698 intel_scaler->mode = PS_SCALER_MODE_DYN;
13699 }
13700
13701 scaler_state->scaler_id = -1;
13702}
13703
Hannes Ederb358d0a2008-12-18 21:18:47 +010013704static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013705{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013706 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013707 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013708 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013709 struct drm_plane *primary = NULL;
13710 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013711 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013712
Daniel Vetter955382f2013-09-19 14:05:45 +020013713 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013714 if (intel_crtc == NULL)
13715 return;
13716
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013717 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13718 if (!crtc_state)
13719 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013720 intel_crtc->config = crtc_state;
13721 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013722 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013723
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013724 /* initialize shared scalers */
13725 if (INTEL_INFO(dev)->gen >= 9) {
13726 if (pipe == PIPE_C)
13727 intel_crtc->num_scalers = 1;
13728 else
13729 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13730
13731 skl_init_scalers(dev, intel_crtc, crtc_state);
13732 }
13733
Matt Roper465c1202014-05-29 08:06:54 -070013734 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013735 if (!primary)
13736 goto fail;
13737
13738 cursor = intel_cursor_plane_create(dev, pipe);
13739 if (!cursor)
13740 goto fail;
13741
Matt Roper465c1202014-05-29 08:06:54 -070013742 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013743 cursor, &intel_crtc_funcs);
13744 if (ret)
13745 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013746
13747 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013748 for (i = 0; i < 256; i++) {
13749 intel_crtc->lut_r[i] = i;
13750 intel_crtc->lut_g[i] = i;
13751 intel_crtc->lut_b[i] = i;
13752 }
13753
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013754 /*
13755 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013756 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013757 */
Jesse Barnes80824002009-09-10 15:28:06 -070013758 intel_crtc->pipe = pipe;
13759 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013760 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013761 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013762 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013763 }
13764
Chris Wilson4b0e3332014-05-30 16:35:26 +030013765 intel_crtc->cursor_base = ~0;
13766 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013767 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013768
Ville Syrjälä852eb002015-06-24 22:00:07 +030013769 intel_crtc->wm.cxsr_allowed = true;
13770
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013771 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13772 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13773 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13774 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13775
Jesse Barnes79e53942008-11-07 14:24:08 -080013776 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013777
13778 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013779 return;
13780
13781fail:
13782 if (primary)
13783 drm_plane_cleanup(primary);
13784 if (cursor)
13785 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013786 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013787 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013788}
13789
Jesse Barnes752aa882013-10-31 18:55:49 +020013790enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13791{
13792 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013793 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013794
Rob Clark51fd3712013-11-19 12:10:12 -050013795 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013796
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013797 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013798 return INVALID_PIPE;
13799
13800 return to_intel_crtc(encoder->crtc)->pipe;
13801}
13802
Carl Worth08d7b3d2009-04-29 14:43:54 -070013803int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013804 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013805{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013806 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013807 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013808 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013809
Rob Clark7707e652014-07-17 23:30:04 -040013810 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013811
Rob Clark7707e652014-07-17 23:30:04 -040013812 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013813 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013814 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013815 }
13816
Rob Clark7707e652014-07-17 23:30:04 -040013817 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013818 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013819
Daniel Vetterc05422d2009-08-11 16:05:30 +020013820 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013821}
13822
Daniel Vetter66a92782012-07-12 20:08:18 +020013823static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013824{
Daniel Vetter66a92782012-07-12 20:08:18 +020013825 struct drm_device *dev = encoder->base.dev;
13826 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013827 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013828 int entry = 0;
13829
Damien Lespiaub2784e12014-08-05 11:29:37 +010013830 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013831 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013832 index_mask |= (1 << entry);
13833
Jesse Barnes79e53942008-11-07 14:24:08 -080013834 entry++;
13835 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013836
Jesse Barnes79e53942008-11-07 14:24:08 -080013837 return index_mask;
13838}
13839
Chris Wilson4d302442010-12-14 19:21:29 +000013840static bool has_edp_a(struct drm_device *dev)
13841{
13842 struct drm_i915_private *dev_priv = dev->dev_private;
13843
13844 if (!IS_MOBILE(dev))
13845 return false;
13846
13847 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13848 return false;
13849
Damien Lespiaue3589902014-02-07 19:12:50 +000013850 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013851 return false;
13852
13853 return true;
13854}
13855
Jesse Barnes84b4e042014-06-25 08:24:29 -070013856static bool intel_crt_present(struct drm_device *dev)
13857{
13858 struct drm_i915_private *dev_priv = dev->dev_private;
13859
Damien Lespiau884497e2013-12-03 13:56:23 +000013860 if (INTEL_INFO(dev)->gen >= 9)
13861 return false;
13862
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013863 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013864 return false;
13865
13866 if (IS_CHERRYVIEW(dev))
13867 return false;
13868
13869 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13870 return false;
13871
13872 return true;
13873}
13874
Jesse Barnes79e53942008-11-07 14:24:08 -080013875static void intel_setup_outputs(struct drm_device *dev)
13876{
Eric Anholt725e30a2009-01-22 13:01:02 -080013877 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013878 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013879 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013880
Daniel Vetterc9093352013-06-06 22:22:47 +020013881 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013882
Jesse Barnes84b4e042014-06-25 08:24:29 -070013883 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013884 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013885
Vandana Kannanc776eb22014-08-19 12:05:01 +053013886 if (IS_BROXTON(dev)) {
13887 /*
13888 * FIXME: Broxton doesn't support port detection via the
13889 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13890 * detect the ports.
13891 */
13892 intel_ddi_init(dev, PORT_A);
13893 intel_ddi_init(dev, PORT_B);
13894 intel_ddi_init(dev, PORT_C);
13895 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013896 int found;
13897
Jesse Barnesde31fac2015-03-06 15:53:32 -080013898 /*
13899 * Haswell uses DDI functions to detect digital outputs.
13900 * On SKL pre-D0 the strap isn't connected, so we assume
13901 * it's there.
13902 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013903 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013904 /* WaIgnoreDDIAStrap: skl */
Jani Nikula5a2376d2015-08-14 10:53:17 +030013905 if (found || IS_SKYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013906 intel_ddi_init(dev, PORT_A);
13907
13908 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13909 * register */
13910 found = I915_READ(SFUSE_STRAP);
13911
13912 if (found & SFUSE_STRAP_DDIB_DETECTED)
13913 intel_ddi_init(dev, PORT_B);
13914 if (found & SFUSE_STRAP_DDIC_DETECTED)
13915 intel_ddi_init(dev, PORT_C);
13916 if (found & SFUSE_STRAP_DDID_DETECTED)
13917 intel_ddi_init(dev, PORT_D);
13918 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013919 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013920 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013921
13922 if (has_edp_a(dev))
13923 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013924
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013925 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013926 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013927 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013928 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013929 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013930 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013931 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013932 }
13933
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013934 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013935 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013936
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013937 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013938 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013939
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013940 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013941 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013942
Daniel Vetter270b3042012-10-27 15:52:05 +020013943 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013944 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013945 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013946 /*
13947 * The DP_DETECTED bit is the latched state of the DDC
13948 * SDA pin at boot. However since eDP doesn't require DDC
13949 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13950 * eDP ports may have been muxed to an alternate function.
13951 * Thus we can't rely on the DP_DETECTED bit alone to detect
13952 * eDP ports. Consult the VBT as well as DP_DETECTED to
13953 * detect eDP ports.
13954 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013955 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13956 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013957 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13958 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013959 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13960 intel_dp_is_edp(dev, PORT_B))
13961 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013962
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013963 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13964 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013965 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13966 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013967 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13968 intel_dp_is_edp(dev, PORT_C))
13969 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053013970
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013971 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013972 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013973 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13974 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013975 /* eDP not supported on port D, so don't check VBT */
13976 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13977 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013978 }
13979
Jani Nikula3cfca972013-08-27 15:12:26 +030013980 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020013981 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013982 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080013983
Paulo Zanonie2debe92013-02-18 19:00:27 -030013984 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013985 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013986 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020013987 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013988 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013989 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013990 }
Ma Ling27185ae2009-08-24 13:50:23 +080013991
Daniel Vetter3fec3d22015-07-07 09:10:07 +020013992 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013993 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080013994 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013995
13996 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013997
Paulo Zanonie2debe92013-02-18 19:00:27 -030013998 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013999 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014000 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014001 }
Ma Ling27185ae2009-08-24 13:50:23 +080014002
Paulo Zanonie2debe92013-02-18 19:00:27 -030014003 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014004
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014005 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014006 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014007 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014008 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014009 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014010 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014011 }
Ma Ling27185ae2009-08-24 13:50:23 +080014012
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014013 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014014 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014015 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014016 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014017 intel_dvo_init(dev);
14018
Zhenyu Wang103a1962009-11-27 11:44:36 +080014019 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014020 intel_tv_init(dev);
14021
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014022 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014023
Damien Lespiaub2784e12014-08-05 11:29:37 +010014024 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014025 encoder->base.possible_crtcs = encoder->crtc_mask;
14026 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014027 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014028 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014029
Paulo Zanonidde86e22012-12-01 12:04:25 -020014030 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014031
14032 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014033}
14034
14035static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14036{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014037 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014038 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014039
Daniel Vetteref2d6332014-02-10 18:00:38 +010014040 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014041 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014042 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014043 drm_gem_object_unreference(&intel_fb->obj->base);
14044 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014045 kfree(intel_fb);
14046}
14047
14048static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014049 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014050 unsigned int *handle)
14051{
14052 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014053 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014054
Chris Wilson05394f32010-11-08 19:18:58 +000014055 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014056}
14057
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014058static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14059 struct drm_file *file,
14060 unsigned flags, unsigned color,
14061 struct drm_clip_rect *clips,
14062 unsigned num_clips)
14063{
14064 struct drm_device *dev = fb->dev;
14065 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14066 struct drm_i915_gem_object *obj = intel_fb->obj;
14067
14068 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014069 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014070 mutex_unlock(&dev->struct_mutex);
14071
14072 return 0;
14073}
14074
Jesse Barnes79e53942008-11-07 14:24:08 -080014075static const struct drm_framebuffer_funcs intel_fb_funcs = {
14076 .destroy = intel_user_framebuffer_destroy,
14077 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014078 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014079};
14080
Damien Lespiaub3218032015-02-27 11:15:18 +000014081static
14082u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14083 uint32_t pixel_format)
14084{
14085 u32 gen = INTEL_INFO(dev)->gen;
14086
14087 if (gen >= 9) {
14088 /* "The stride in bytes must not exceed the of the size of 8K
14089 * pixels and 32K bytes."
14090 */
14091 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14092 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14093 return 32*1024;
14094 } else if (gen >= 4) {
14095 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14096 return 16*1024;
14097 else
14098 return 32*1024;
14099 } else if (gen >= 3) {
14100 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14101 return 8*1024;
14102 else
14103 return 16*1024;
14104 } else {
14105 /* XXX DSPC is limited to 4k tiled */
14106 return 8*1024;
14107 }
14108}
14109
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014110static int intel_framebuffer_init(struct drm_device *dev,
14111 struct intel_framebuffer *intel_fb,
14112 struct drm_mode_fb_cmd2 *mode_cmd,
14113 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014114{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014115 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014116 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014117 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014118
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014119 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14120
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014121 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14122 /* Enforce that fb modifier and tiling mode match, but only for
14123 * X-tiled. This is needed for FBC. */
14124 if (!!(obj->tiling_mode == I915_TILING_X) !=
14125 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14126 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14127 return -EINVAL;
14128 }
14129 } else {
14130 if (obj->tiling_mode == I915_TILING_X)
14131 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14132 else if (obj->tiling_mode == I915_TILING_Y) {
14133 DRM_DEBUG("No Y tiling for legacy addfb\n");
14134 return -EINVAL;
14135 }
14136 }
14137
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014138 /* Passed in modifier sanity checking. */
14139 switch (mode_cmd->modifier[0]) {
14140 case I915_FORMAT_MOD_Y_TILED:
14141 case I915_FORMAT_MOD_Yf_TILED:
14142 if (INTEL_INFO(dev)->gen < 9) {
14143 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14144 mode_cmd->modifier[0]);
14145 return -EINVAL;
14146 }
14147 case DRM_FORMAT_MOD_NONE:
14148 case I915_FORMAT_MOD_X_TILED:
14149 break;
14150 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014151 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14152 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014153 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014154 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014155
Damien Lespiaub3218032015-02-27 11:15:18 +000014156 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14157 mode_cmd->pixel_format);
14158 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14159 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14160 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014161 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014162 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014163
Damien Lespiaub3218032015-02-27 11:15:18 +000014164 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14165 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014166 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014167 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14168 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014169 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014170 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014171 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014172 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014173
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014174 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014175 mode_cmd->pitches[0] != obj->stride) {
14176 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14177 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014178 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014179 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014180
Ville Syrjälä57779d02012-10-31 17:50:14 +020014181 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014182 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014183 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014184 case DRM_FORMAT_RGB565:
14185 case DRM_FORMAT_XRGB8888:
14186 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014187 break;
14188 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014189 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014190 DRM_DEBUG("unsupported pixel format: %s\n",
14191 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014192 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014193 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014194 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014195 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014196 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14197 DRM_DEBUG("unsupported pixel format: %s\n",
14198 drm_get_format_name(mode_cmd->pixel_format));
14199 return -EINVAL;
14200 }
14201 break;
14202 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014203 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014204 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014205 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014206 DRM_DEBUG("unsupported pixel format: %s\n",
14207 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014208 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014209 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014210 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014211 case DRM_FORMAT_ABGR2101010:
14212 if (!IS_VALLEYVIEW(dev)) {
14213 DRM_DEBUG("unsupported pixel format: %s\n",
14214 drm_get_format_name(mode_cmd->pixel_format));
14215 return -EINVAL;
14216 }
14217 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014218 case DRM_FORMAT_YUYV:
14219 case DRM_FORMAT_UYVY:
14220 case DRM_FORMAT_YVYU:
14221 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014222 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014223 DRM_DEBUG("unsupported pixel format: %s\n",
14224 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014225 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014226 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014227 break;
14228 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014229 DRM_DEBUG("unsupported pixel format: %s\n",
14230 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014231 return -EINVAL;
14232 }
14233
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014234 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14235 if (mode_cmd->offsets[0] != 0)
14236 return -EINVAL;
14237
Damien Lespiauec2c9812015-01-20 12:51:45 +000014238 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014239 mode_cmd->pixel_format,
14240 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014241 /* FIXME drm helper for size checks (especially planar formats)? */
14242 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14243 return -EINVAL;
14244
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014245 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14246 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014247 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014248
Jesse Barnes79e53942008-11-07 14:24:08 -080014249 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14250 if (ret) {
14251 DRM_ERROR("framebuffer init failed %d\n", ret);
14252 return ret;
14253 }
14254
Jesse Barnes79e53942008-11-07 14:24:08 -080014255 return 0;
14256}
14257
Jesse Barnes79e53942008-11-07 14:24:08 -080014258static struct drm_framebuffer *
14259intel_user_framebuffer_create(struct drm_device *dev,
14260 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014261 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014262{
Chris Wilson05394f32010-11-08 19:18:58 +000014263 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014264
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014265 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14266 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014267 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014268 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014269
Chris Wilsond2dff872011-04-19 08:36:26 +010014270 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014271}
14272
Daniel Vetter4520f532013-10-09 09:18:51 +020014273#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014274static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014275{
14276}
14277#endif
14278
Jesse Barnes79e53942008-11-07 14:24:08 -080014279static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014280 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014281 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014282 .atomic_check = intel_atomic_check,
14283 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014284 .atomic_state_alloc = intel_atomic_state_alloc,
14285 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014286};
14287
Jesse Barnese70236a2009-09-21 10:42:27 -070014288/* Set up chip specific display functions */
14289static void intel_init_display(struct drm_device *dev)
14290{
14291 struct drm_i915_private *dev_priv = dev->dev_private;
14292
Daniel Vetteree9300b2013-06-03 22:40:22 +020014293 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14294 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014295 else if (IS_CHERRYVIEW(dev))
14296 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014297 else if (IS_VALLEYVIEW(dev))
14298 dev_priv->display.find_dpll = vlv_find_best_dpll;
14299 else if (IS_PINEVIEW(dev))
14300 dev_priv->display.find_dpll = pnv_find_best_dpll;
14301 else
14302 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14303
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014304 if (INTEL_INFO(dev)->gen >= 9) {
14305 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014306 dev_priv->display.get_initial_plane_config =
14307 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014308 dev_priv->display.crtc_compute_clock =
14309 haswell_crtc_compute_clock;
14310 dev_priv->display.crtc_enable = haswell_crtc_enable;
14311 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014312 dev_priv->display.update_primary_plane =
14313 skylake_update_primary_plane;
14314 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014315 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014316 dev_priv->display.get_initial_plane_config =
14317 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014318 dev_priv->display.crtc_compute_clock =
14319 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014320 dev_priv->display.crtc_enable = haswell_crtc_enable;
14321 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014322 dev_priv->display.update_primary_plane =
14323 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014324 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014325 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014326 dev_priv->display.get_initial_plane_config =
14327 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014328 dev_priv->display.crtc_compute_clock =
14329 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014330 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14331 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014332 dev_priv->display.update_primary_plane =
14333 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014334 } else if (IS_VALLEYVIEW(dev)) {
14335 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014336 dev_priv->display.get_initial_plane_config =
14337 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014338 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014339 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14340 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014341 dev_priv->display.update_primary_plane =
14342 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014343 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014344 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014345 dev_priv->display.get_initial_plane_config =
14346 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014347 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014348 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14349 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014350 dev_priv->display.update_primary_plane =
14351 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014352 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014353
Jesse Barnese70236a2009-09-21 10:42:27 -070014354 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014355 if (IS_SKYLAKE(dev))
14356 dev_priv->display.get_display_clock_speed =
14357 skylake_get_display_clock_speed;
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014358 else if (IS_BROXTON(dev))
14359 dev_priv->display.get_display_clock_speed =
14360 broxton_get_display_clock_speed;
Ville Syrjälä1652d192015-03-31 14:12:01 +030014361 else if (IS_BROADWELL(dev))
14362 dev_priv->display.get_display_clock_speed =
14363 broadwell_get_display_clock_speed;
14364 else if (IS_HASWELL(dev))
14365 dev_priv->display.get_display_clock_speed =
14366 haswell_get_display_clock_speed;
14367 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014368 dev_priv->display.get_display_clock_speed =
14369 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014370 else if (IS_GEN5(dev))
14371 dev_priv->display.get_display_clock_speed =
14372 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014373 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014374 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014375 dev_priv->display.get_display_clock_speed =
14376 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014377 else if (IS_GM45(dev))
14378 dev_priv->display.get_display_clock_speed =
14379 gm45_get_display_clock_speed;
14380 else if (IS_CRESTLINE(dev))
14381 dev_priv->display.get_display_clock_speed =
14382 i965gm_get_display_clock_speed;
14383 else if (IS_PINEVIEW(dev))
14384 dev_priv->display.get_display_clock_speed =
14385 pnv_get_display_clock_speed;
14386 else if (IS_G33(dev) || IS_G4X(dev))
14387 dev_priv->display.get_display_clock_speed =
14388 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014389 else if (IS_I915G(dev))
14390 dev_priv->display.get_display_clock_speed =
14391 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014392 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014393 dev_priv->display.get_display_clock_speed =
14394 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014395 else if (IS_PINEVIEW(dev))
14396 dev_priv->display.get_display_clock_speed =
14397 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014398 else if (IS_I915GM(dev))
14399 dev_priv->display.get_display_clock_speed =
14400 i915gm_get_display_clock_speed;
14401 else if (IS_I865G(dev))
14402 dev_priv->display.get_display_clock_speed =
14403 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014404 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014405 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014406 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014407 else { /* 830 */
14408 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014409 dev_priv->display.get_display_clock_speed =
14410 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014411 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014412
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014413 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014414 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014415 } else if (IS_GEN6(dev)) {
14416 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014417 } else if (IS_IVYBRIDGE(dev)) {
14418 /* FIXME: detect B0+ stepping and use auto training */
14419 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014420 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014421 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014422 if (IS_BROADWELL(dev)) {
14423 dev_priv->display.modeset_commit_cdclk =
14424 broadwell_modeset_commit_cdclk;
14425 dev_priv->display.modeset_calc_cdclk =
14426 broadwell_modeset_calc_cdclk;
14427 }
Jesse Barnes30a970c2013-11-04 13:48:12 -080014428 } else if (IS_VALLEYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014429 dev_priv->display.modeset_commit_cdclk =
14430 valleyview_modeset_commit_cdclk;
14431 dev_priv->display.modeset_calc_cdclk =
14432 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014433 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014434 dev_priv->display.modeset_commit_cdclk =
14435 broxton_modeset_commit_cdclk;
14436 dev_priv->display.modeset_calc_cdclk =
14437 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014438 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014439
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014440 switch (INTEL_INFO(dev)->gen) {
14441 case 2:
14442 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14443 break;
14444
14445 case 3:
14446 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14447 break;
14448
14449 case 4:
14450 case 5:
14451 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14452 break;
14453
14454 case 6:
14455 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14456 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014457 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014458 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014459 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14460 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014461 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014462 /* Drop through - unsupported since execlist only. */
14463 default:
14464 /* Default just returns -ENODEV to indicate unsupported */
14465 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014466 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014467
14468 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014469
14470 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014471}
14472
Jesse Barnesb690e962010-07-19 13:53:12 -070014473/*
14474 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14475 * resume, or other times. This quirk makes sure that's the case for
14476 * affected systems.
14477 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014478static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014479{
14480 struct drm_i915_private *dev_priv = dev->dev_private;
14481
14482 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014483 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014484}
14485
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014486static void quirk_pipeb_force(struct drm_device *dev)
14487{
14488 struct drm_i915_private *dev_priv = dev->dev_private;
14489
14490 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14491 DRM_INFO("applying pipe b force quirk\n");
14492}
14493
Keith Packard435793d2011-07-12 14:56:22 -070014494/*
14495 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14496 */
14497static void quirk_ssc_force_disable(struct drm_device *dev)
14498{
14499 struct drm_i915_private *dev_priv = dev->dev_private;
14500 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014501 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014502}
14503
Carsten Emde4dca20e2012-03-15 15:56:26 +010014504/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014505 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14506 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014507 */
14508static void quirk_invert_brightness(struct drm_device *dev)
14509{
14510 struct drm_i915_private *dev_priv = dev->dev_private;
14511 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014512 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014513}
14514
Scot Doyle9c72cc62014-07-03 23:27:50 +000014515/* Some VBT's incorrectly indicate no backlight is present */
14516static void quirk_backlight_present(struct drm_device *dev)
14517{
14518 struct drm_i915_private *dev_priv = dev->dev_private;
14519 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14520 DRM_INFO("applying backlight present quirk\n");
14521}
14522
Jesse Barnesb690e962010-07-19 13:53:12 -070014523struct intel_quirk {
14524 int device;
14525 int subsystem_vendor;
14526 int subsystem_device;
14527 void (*hook)(struct drm_device *dev);
14528};
14529
Egbert Eich5f85f172012-10-14 15:46:38 +020014530/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14531struct intel_dmi_quirk {
14532 void (*hook)(struct drm_device *dev);
14533 const struct dmi_system_id (*dmi_id_list)[];
14534};
14535
14536static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14537{
14538 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14539 return 1;
14540}
14541
14542static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14543 {
14544 .dmi_id_list = &(const struct dmi_system_id[]) {
14545 {
14546 .callback = intel_dmi_reverse_brightness,
14547 .ident = "NCR Corporation",
14548 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14549 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14550 },
14551 },
14552 { } /* terminating entry */
14553 },
14554 .hook = quirk_invert_brightness,
14555 },
14556};
14557
Ben Widawskyc43b5632012-04-16 14:07:40 -070014558static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014559 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14560 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14561
Jesse Barnesb690e962010-07-19 13:53:12 -070014562 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14563 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14564
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014565 /* 830 needs to leave pipe A & dpll A up */
14566 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14567
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014568 /* 830 needs to leave pipe B & dpll B up */
14569 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14570
Keith Packard435793d2011-07-12 14:56:22 -070014571 /* Lenovo U160 cannot use SSC on LVDS */
14572 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014573
14574 /* Sony Vaio Y cannot use SSC on LVDS */
14575 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014576
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014577 /* Acer Aspire 5734Z must invert backlight brightness */
14578 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14579
14580 /* Acer/eMachines G725 */
14581 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14582
14583 /* Acer/eMachines e725 */
14584 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14585
14586 /* Acer/Packard Bell NCL20 */
14587 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14588
14589 /* Acer Aspire 4736Z */
14590 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014591
14592 /* Acer Aspire 5336 */
14593 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014594
14595 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14596 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014597
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014598 /* Acer C720 Chromebook (Core i3 4005U) */
14599 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14600
jens steinb2a96012014-10-28 20:25:53 +010014601 /* Apple Macbook 2,1 (Core 2 T7400) */
14602 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14603
Scot Doyled4967d82014-07-03 23:27:52 +000014604 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14605 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014606
14607 /* HP Chromebook 14 (Celeron 2955U) */
14608 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014609
14610 /* Dell Chromebook 11 */
14611 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014612};
14613
14614static void intel_init_quirks(struct drm_device *dev)
14615{
14616 struct pci_dev *d = dev->pdev;
14617 int i;
14618
14619 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14620 struct intel_quirk *q = &intel_quirks[i];
14621
14622 if (d->device == q->device &&
14623 (d->subsystem_vendor == q->subsystem_vendor ||
14624 q->subsystem_vendor == PCI_ANY_ID) &&
14625 (d->subsystem_device == q->subsystem_device ||
14626 q->subsystem_device == PCI_ANY_ID))
14627 q->hook(dev);
14628 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014629 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14630 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14631 intel_dmi_quirks[i].hook(dev);
14632 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014633}
14634
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014635/* Disable the VGA plane that we never use */
14636static void i915_disable_vga(struct drm_device *dev)
14637{
14638 struct drm_i915_private *dev_priv = dev->dev_private;
14639 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014640 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014641
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014642 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014643 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014644 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014645 sr1 = inb(VGA_SR_DATA);
14646 outb(sr1 | 1<<5, VGA_SR_DATA);
14647 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14648 udelay(300);
14649
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014650 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014651 POSTING_READ(vga_reg);
14652}
14653
Daniel Vetterf8175862012-04-10 15:50:11 +020014654void intel_modeset_init_hw(struct drm_device *dev)
14655{
Ville Syrjäläb6283052015-06-03 15:45:07 +030014656 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014657 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014658 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014659 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014660}
14661
Jesse Barnes79e53942008-11-07 14:24:08 -080014662void intel_modeset_init(struct drm_device *dev)
14663{
Jesse Barnes652c3932009-08-17 13:31:43 -070014664 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014665 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014666 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014667 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014668
14669 drm_mode_config_init(dev);
14670
14671 dev->mode_config.min_width = 0;
14672 dev->mode_config.min_height = 0;
14673
Dave Airlie019d96c2011-09-29 16:20:42 +010014674 dev->mode_config.preferred_depth = 24;
14675 dev->mode_config.prefer_shadow = 1;
14676
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014677 dev->mode_config.allow_fb_modifiers = true;
14678
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014679 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014680
Jesse Barnesb690e962010-07-19 13:53:12 -070014681 intel_init_quirks(dev);
14682
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014683 intel_init_pm(dev);
14684
Ben Widawskye3c74752013-04-05 13:12:39 -070014685 if (INTEL_INFO(dev)->num_pipes == 0)
14686 return;
14687
Jesse Barnese70236a2009-09-21 10:42:27 -070014688 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014689 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014690
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014691 if (IS_GEN2(dev)) {
14692 dev->mode_config.max_width = 2048;
14693 dev->mode_config.max_height = 2048;
14694 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014695 dev->mode_config.max_width = 4096;
14696 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014697 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014698 dev->mode_config.max_width = 8192;
14699 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014700 }
Damien Lespiau068be562014-03-28 14:17:49 +000014701
Ville Syrjälädc41c152014-08-13 11:57:05 +030014702 if (IS_845G(dev) || IS_I865G(dev)) {
14703 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14704 dev->mode_config.cursor_height = 1023;
14705 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014706 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14707 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14708 } else {
14709 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14710 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14711 }
14712
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014713 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014714
Zhao Yakui28c97732009-10-09 11:39:41 +080014715 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014716 INTEL_INFO(dev)->num_pipes,
14717 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014718
Damien Lespiau055e3932014-08-18 13:49:10 +010014719 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014720 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014721 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014722 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014723 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014724 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014725 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014726 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014727 }
14728
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014729 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014730
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014731 /* Just disable it once at startup */
14732 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014733 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014734
14735 /* Just in case the BIOS is doing something questionable. */
Paulo Zanoni7733b492015-07-07 15:26:04 -030014736 intel_fbc_disable(dev_priv);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014737
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014738 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020014739 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014740 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014741
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014742 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014743 struct intel_initial_plane_config plane_config = {};
14744
Jesse Barnes46f297f2014-03-07 08:57:48 -080014745 if (!crtc->active)
14746 continue;
14747
Jesse Barnes46f297f2014-03-07 08:57:48 -080014748 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014749 * Note that reserving the BIOS fb up front prevents us
14750 * from stuffing other stolen allocations like the ring
14751 * on top. This prevents some ugliness at boot time, and
14752 * can even allow for smooth boot transitions if the BIOS
14753 * fb is large enough for the active pipe configuration.
14754 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014755 dev_priv->display.get_initial_plane_config(crtc,
14756 &plane_config);
14757
14758 /*
14759 * If the fb is shared between multiple heads, we'll
14760 * just get the first one.
14761 */
14762 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014763 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014764}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014765
Daniel Vetter7fad7982012-07-04 17:51:47 +020014766static void intel_enable_pipe_a(struct drm_device *dev)
14767{
14768 struct intel_connector *connector;
14769 struct drm_connector *crt = NULL;
14770 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014771 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014772
14773 /* We can't just switch on the pipe A, we need to set things up with a
14774 * proper mode and output configuration. As a gross hack, enable pipe A
14775 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014776 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014777 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14778 crt = &connector->base;
14779 break;
14780 }
14781 }
14782
14783 if (!crt)
14784 return;
14785
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014786 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014787 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014788}
14789
Daniel Vetterfa555832012-10-10 23:14:00 +020014790static bool
14791intel_check_plane_mapping(struct intel_crtc *crtc)
14792{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014793 struct drm_device *dev = crtc->base.dev;
14794 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014795 u32 reg, val;
14796
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014797 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014798 return true;
14799
14800 reg = DSPCNTR(!crtc->plane);
14801 val = I915_READ(reg);
14802
14803 if ((val & DISPLAY_PLANE_ENABLE) &&
14804 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14805 return false;
14806
14807 return true;
14808}
14809
Daniel Vetter24929352012-07-02 20:28:59 +020014810static void intel_sanitize_crtc(struct intel_crtc *crtc)
14811{
14812 struct drm_device *dev = crtc->base.dev;
14813 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020014814 struct intel_encoder *encoder;
Daniel Vetterfa555832012-10-10 23:14:00 +020014815 u32 reg;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020014816 bool enable;
Daniel Vetter24929352012-07-02 20:28:59 +020014817
Daniel Vetter24929352012-07-02 20:28:59 +020014818 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014819 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014820 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14821
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014822 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014823 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014824 if (crtc->active) {
Maarten Lankhorst3a03dfb2015-07-14 13:46:40 +020014825 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014826 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014827 drm_crtc_vblank_on(&crtc->base);
14828 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014829
Daniel Vetter24929352012-07-02 20:28:59 +020014830 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014831 * disable the crtc (and hence change the state) if it is wrong. Note
14832 * that gen4+ has a fixed plane -> pipe mapping. */
14833 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014834 bool plane;
14835
Daniel Vetter24929352012-07-02 20:28:59 +020014836 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14837 crtc->base.base.id);
14838
14839 /* Pipe has the wrong plane attached and the plane is active.
14840 * Temporarily change the plane mapping and disable everything
14841 * ... */
14842 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014843 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014844 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020014845 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014846 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020014847 }
Daniel Vetter24929352012-07-02 20:28:59 +020014848
Daniel Vetter7fad7982012-07-04 17:51:47 +020014849 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14850 crtc->pipe == PIPE_A && !crtc->active) {
14851 /* BIOS forgot to enable pipe A, this mostly happens after
14852 * resume. Force-enable the pipe to fix this, the update_dpms
14853 * call below we restore the pipe to the right state, but leave
14854 * the required bits on. */
14855 intel_enable_pipe_a(dev);
14856 }
14857
Daniel Vetter24929352012-07-02 20:28:59 +020014858 /* Adjust the state of the output pipe according to whether we
14859 * have active connectors/encoders. */
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020014860 enable = false;
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020014861 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14862 enable = true;
14863 break;
14864 }
Daniel Vetter24929352012-07-02 20:28:59 +020014865
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020014866 if (!enable)
14867 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014868
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020014869 if (crtc->active != crtc->base.state->active) {
Daniel Vetter24929352012-07-02 20:28:59 +020014870
14871 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020014872 * functions or because of calls to intel_crtc_disable_noatomic,
14873 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020014874 * pipe A quirk. */
14875 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14876 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014877 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014878 crtc->active ? "enabled" : "disabled");
14879
Maarten Lankhorst4be40c92015-07-14 13:45:32 +020014880 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014881 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014882 crtc->base.enabled = crtc->active;
14883
14884 /* Because we only establish the connector -> encoder ->
14885 * crtc links if something is active, this means the
14886 * crtc is now deactivated. Break the links. connector
14887 * -> encoder links are only establish when things are
14888 * actually up, hence no need to break them. */
14889 WARN_ON(crtc->active);
14890
Maarten Lankhorst2d406bb2015-08-05 12:37:09 +020014891 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetter24929352012-07-02 20:28:59 +020014892 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014893 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014894
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014895 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014896 /*
14897 * We start out with underrun reporting disabled to avoid races.
14898 * For correct bookkeeping mark this on active crtcs.
14899 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014900 * Also on gmch platforms we dont have any hardware bits to
14901 * disable the underrun reporting. Which means we need to start
14902 * out with underrun reporting disabled also on inactive pipes,
14903 * since otherwise we'll complain about the garbage we read when
14904 * e.g. coming up after runtime pm.
14905 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014906 * No protection against concurrent access is required - at
14907 * worst a fifo underrun happens which also sets this to false.
14908 */
14909 crtc->cpu_fifo_underrun_disabled = true;
14910 crtc->pch_fifo_underrun_disabled = true;
14911 }
Daniel Vetter24929352012-07-02 20:28:59 +020014912}
14913
14914static void intel_sanitize_encoder(struct intel_encoder *encoder)
14915{
14916 struct intel_connector *connector;
14917 struct drm_device *dev = encoder->base.dev;
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020014918 bool active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014919
14920 /* We need to check both for a crtc link (meaning that the
14921 * encoder is active and trying to read from a pipe) and the
14922 * pipe itself being active. */
14923 bool has_active_crtc = encoder->base.crtc &&
14924 to_intel_crtc(encoder->base.crtc)->active;
14925
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020014926 for_each_intel_connector(dev, connector) {
14927 if (connector->base.encoder != &encoder->base)
14928 continue;
14929
14930 active = true;
14931 break;
14932 }
14933
14934 if (active && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020014935 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14936 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014937 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014938
14939 /* Connector is active, but has no active pipe. This is
14940 * fallout from our resume register restoring. Disable
14941 * the encoder manually again. */
14942 if (encoder->base.crtc) {
14943 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14944 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014945 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014946 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014947 if (encoder->post_disable)
14948 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014949 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014950 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014951
14952 /* Inconsistent output/port/pipe state happens presumably due to
14953 * a bug in one of the get_hw_state functions. Or someplace else
14954 * in our code, like the register restore mess on resume. Clamp
14955 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014956 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014957 if (connector->encoder != encoder)
14958 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014959 connector->base.dpms = DRM_MODE_DPMS_OFF;
14960 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014961 }
14962 }
14963 /* Enabled encoders without active connectors will be fixed in
14964 * the crtc fixup. */
14965}
14966
Imre Deak04098752014-02-18 00:02:16 +020014967void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014968{
14969 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014970 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014971
Imre Deak04098752014-02-18 00:02:16 +020014972 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14973 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14974 i915_disable_vga(dev);
14975 }
14976}
14977
14978void i915_redisable_vga(struct drm_device *dev)
14979{
14980 struct drm_i915_private *dev_priv = dev->dev_private;
14981
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014982 /* This function can be called both from intel_modeset_setup_hw_state or
14983 * at a very early point in our resume sequence, where the power well
14984 * structures are not yet restored. Since this function is at a very
14985 * paranoid "someone might have enabled VGA while we were not looking"
14986 * level, just check if the power well is enabled instead of trying to
14987 * follow the "don't touch the power well if we don't need it" policy
14988 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014989 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014990 return;
14991
Imre Deak04098752014-02-18 00:02:16 +020014992 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014993}
14994
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014995static bool primary_get_hw_state(struct intel_crtc *crtc)
14996{
14997 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14998
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020014999 return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
15000}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015001
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015002static void readout_plane_state(struct intel_crtc *crtc,
15003 struct intel_crtc_state *crtc_state)
15004{
15005 struct intel_plane *p;
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015006 struct intel_plane_state *plane_state;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015007 bool active = crtc_state->base.active;
15008
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015009 for_each_intel_plane(crtc->base.dev, p) {
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015010 if (crtc->pipe != p->pipe)
15011 continue;
15012
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015013 plane_state = to_intel_plane_state(p->base.state);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020015014
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015015 if (p->base.type == DRM_PLANE_TYPE_PRIMARY)
15016 plane_state->visible = primary_get_hw_state(crtc);
15017 else {
15018 if (active)
15019 p->disable_plane(&p->base, &crtc->base);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020015020
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015021 plane_state->visible = false;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015022 }
15023 }
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015024}
15025
Daniel Vetter30e984d2013-06-05 13:34:17 +020015026static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015027{
15028 struct drm_i915_private *dev_priv = dev->dev_private;
15029 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015030 struct intel_crtc *crtc;
15031 struct intel_encoder *encoder;
15032 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015033 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015034
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015035 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb06f8b02015-07-14 13:42:49 +020015036 __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015037 memset(crtc->config, 0, sizeof(*crtc->config));
Maarten Lankhorstf7217902015-06-10 10:24:20 +020015038 crtc->config->base.crtc = &crtc->base;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015039
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015040 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015041 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015042
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015043 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015044 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015045
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020015046 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15047 if (crtc->base.state->active) {
15048 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15049 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15050 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15051
15052 /*
15053 * The initial mode needs to be set in order to keep
15054 * the atomic core happy. It wants a valid mode if the
15055 * crtc's enabled, so we do the above call.
15056 *
15057 * At this point some state updated by the connectors
15058 * in their ->detect() callback has not run yet, so
15059 * no recalculation can be done yet.
15060 *
15061 * Even if we could do a recalculation and modeset
15062 * right now it would cause a double modeset if
15063 * fbdev or userspace chooses a different initial mode.
15064 *
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020015065 * If that happens, someone indicated they wanted a
15066 * mode change, which means it's safe to do a full
15067 * recalculation.
15068 */
Daniel Vetter1ed51de2015-07-15 14:15:51 +020015069 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020015070 }
15071
15072 crtc->base.hwmode = crtc->config->base.adjusted_mode;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015073 readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
Daniel Vetter24929352012-07-02 20:28:59 +020015074
15075 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15076 crtc->base.base.id,
15077 crtc->active ? "enabled" : "disabled");
15078 }
15079
Daniel Vetter53589012013-06-05 13:34:16 +020015080 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15081 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15082
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015083 pll->on = pll->get_hw_state(dev_priv, pll,
15084 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015085 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015086 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015087 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015088 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015089 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015090 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015091 }
Daniel Vetter53589012013-06-05 13:34:16 +020015092 }
Daniel Vetter53589012013-06-05 13:34:16 +020015093
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015094 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015095 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015096
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015097 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015098 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015099 }
15100
Damien Lespiaub2784e12014-08-05 11:29:37 +010015101 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015102 pipe = 0;
15103
15104 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015105 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15106 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015107 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015108 } else {
15109 encoder->base.crtc = NULL;
15110 }
15111
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015112 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015113 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015114 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015115 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015116 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015117 }
15118
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015119 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015120 if (connector->get_hw_state(connector)) {
15121 connector->base.dpms = DRM_MODE_DPMS_ON;
Daniel Vetter24929352012-07-02 20:28:59 +020015122 connector->base.encoder = &connector->encoder->base;
15123 } else {
15124 connector->base.dpms = DRM_MODE_DPMS_OFF;
15125 connector->base.encoder = NULL;
15126 }
15127 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15128 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015129 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015130 connector->base.encoder ? "enabled" : "disabled");
15131 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015132}
15133
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015134/* Scan out the current hw modeset state,
15135 * and sanitizes it to the current state
15136 */
15137static void
15138intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015139{
15140 struct drm_i915_private *dev_priv = dev->dev_private;
15141 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015142 struct intel_crtc *crtc;
15143 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015144 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015145
15146 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015147
15148 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015149 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015150 intel_sanitize_encoder(encoder);
15151 }
15152
Damien Lespiau055e3932014-08-18 13:49:10 +010015153 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015154 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15155 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015156 intel_dump_pipe_config(crtc, crtc->config,
15157 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015158 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015159
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015160 intel_modeset_update_connector_atomic_state(dev);
15161
Daniel Vetter35c95372013-07-17 06:55:04 +020015162 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15163 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15164
15165 if (!pll->on || pll->active)
15166 continue;
15167
15168 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15169
15170 pll->disable(dev_priv, pll);
15171 pll->on = false;
15172 }
15173
Ville Syrjälä26e1fe42015-06-24 22:00:06 +030015174 if (IS_VALLEYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015175 vlv_wm_get_hw_state(dev);
15176 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015177 skl_wm_get_hw_state(dev);
15178 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015179 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015180
15181 for_each_intel_crtc(dev, crtc) {
15182 unsigned long put_domains;
15183
15184 put_domains = modeset_get_crtc_power_domains(&crtc->base);
15185 if (WARN_ON(put_domains))
15186 modeset_put_power_domains(dev_priv, put_domains);
15187 }
15188 intel_display_set_init_power(dev_priv, false);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015189}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015190
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015191void intel_display_resume(struct drm_device *dev)
15192{
15193 struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
15194 struct intel_connector *conn;
15195 struct intel_plane *plane;
15196 struct drm_crtc *crtc;
15197 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015198
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015199 if (!state)
15200 return;
15201
15202 state->acquire_ctx = dev->mode_config.acquire_ctx;
15203
15204 /* preserve complete old state, including dpll */
15205 intel_atomic_get_shared_dpll_state(state);
15206
15207 for_each_crtc(dev, crtc) {
15208 struct drm_crtc_state *crtc_state =
15209 drm_atomic_get_crtc_state(state, crtc);
15210
15211 ret = PTR_ERR_OR_ZERO(crtc_state);
15212 if (ret)
15213 goto err;
15214
15215 /* force a restore */
15216 crtc_state->mode_changed = true;
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015217 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015218
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015219 for_each_intel_plane(dev, plane) {
15220 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
15221 if (ret)
15222 goto err;
15223 }
15224
15225 for_each_intel_connector(dev, conn) {
15226 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
15227 if (ret)
15228 goto err;
15229 }
15230
15231 intel_modeset_setup_hw_state(dev);
15232
15233 i915_redisable_vga(dev);
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020015234 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015235 if (!ret)
15236 return;
15237
15238err:
15239 DRM_ERROR("Restoring old state failed with %i\n", ret);
15240 drm_atomic_state_free(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015241}
15242
15243void intel_modeset_gem_init(struct drm_device *dev)
15244{
Jesse Barnes92122782014-10-09 12:57:42 -070015245 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015246 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015247 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015248 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015249
Imre Deakae484342014-03-31 15:10:44 +030015250 mutex_lock(&dev->struct_mutex);
15251 intel_init_gt_powersave(dev);
15252 mutex_unlock(&dev->struct_mutex);
15253
Jesse Barnes92122782014-10-09 12:57:42 -070015254 /*
15255 * There may be no VBT; and if the BIOS enabled SSC we can
15256 * just keep using it to avoid unnecessary flicker. Whereas if the
15257 * BIOS isn't using it, don't assume it will work even if the VBT
15258 * indicates as much.
15259 */
15260 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15261 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15262 DREF_SSC1_ENABLE);
15263
Chris Wilson1833b132012-05-09 11:56:28 +010015264 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015265
15266 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015267
15268 /*
15269 * Make sure any fbs we allocated at startup are properly
15270 * pinned & fenced. When we do the allocation it's too early
15271 * for this.
15272 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015273 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015274 obj = intel_fb_obj(c->primary->fb);
15275 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015276 continue;
15277
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015278 mutex_lock(&dev->struct_mutex);
15279 ret = intel_pin_and_fence_fb_obj(c->primary,
15280 c->primary->fb,
15281 c->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010015282 NULL, NULL);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015283 mutex_unlock(&dev->struct_mutex);
15284 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015285 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15286 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015287 drm_framebuffer_unreference(c->primary->fb);
15288 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015289 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015290 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015291 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015292 }
15293 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015294
15295 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015296}
15297
Imre Deak4932e2c2014-02-11 17:12:48 +020015298void intel_connector_unregister(struct intel_connector *intel_connector)
15299{
15300 struct drm_connector *connector = &intel_connector->base;
15301
15302 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015303 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015304}
15305
Jesse Barnes79e53942008-11-07 14:24:08 -080015306void intel_modeset_cleanup(struct drm_device *dev)
15307{
Jesse Barnes652c3932009-08-17 13:31:43 -070015308 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015309 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015310
Imre Deak2eb52522014-11-19 15:30:05 +020015311 intel_disable_gt_powersave(dev);
15312
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015313 intel_backlight_unregister(dev);
15314
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015315 /*
15316 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015317 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015318 * experience fancy races otherwise.
15319 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015320 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015321
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015322 /*
15323 * Due to the hpd irq storm handling the hotplug work can re-arm the
15324 * poll handlers. Hence disable polling after hpd handling is shut down.
15325 */
Keith Packardf87ea762010-10-03 19:36:26 -070015326 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015327
Jesse Barnes723bfd72010-10-07 16:01:13 -070015328 intel_unregister_dsm_handler();
15329
Paulo Zanoni7733b492015-07-07 15:26:04 -030015330 intel_fbc_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015331
Chris Wilson1630fe72011-07-08 12:22:42 +010015332 /* flush any delayed tasks or pending work */
15333 flush_scheduled_work();
15334
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015335 /* destroy the backlight and sysfs files before encoders/connectors */
15336 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015337 struct intel_connector *intel_connector;
15338
15339 intel_connector = to_intel_connector(connector);
15340 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015341 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015342
Jesse Barnes79e53942008-11-07 14:24:08 -080015343 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015344
15345 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015346
15347 mutex_lock(&dev->struct_mutex);
15348 intel_cleanup_gt_powersave(dev);
15349 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015350}
15351
Dave Airlie28d52042009-09-21 14:33:58 +100015352/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015353 * Return which encoder is currently attached for connector.
15354 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015355struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015356{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015357 return &intel_attached_encoder(connector)->base;
15358}
Jesse Barnes79e53942008-11-07 14:24:08 -080015359
Chris Wilsondf0e9242010-09-09 16:20:55 +010015360void intel_connector_attach_encoder(struct intel_connector *connector,
15361 struct intel_encoder *encoder)
15362{
15363 connector->encoder = encoder;
15364 drm_mode_connector_attach_encoder(&connector->base,
15365 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015366}
Dave Airlie28d52042009-09-21 14:33:58 +100015367
15368/*
15369 * set vga decode state - true == enable VGA decode
15370 */
15371int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15372{
15373 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015374 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015375 u16 gmch_ctrl;
15376
Chris Wilson75fa0412014-02-07 18:37:02 -020015377 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15378 DRM_ERROR("failed to read control word\n");
15379 return -EIO;
15380 }
15381
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015382 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15383 return 0;
15384
Dave Airlie28d52042009-09-21 14:33:58 +100015385 if (state)
15386 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15387 else
15388 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015389
15390 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15391 DRM_ERROR("failed to write control word\n");
15392 return -EIO;
15393 }
15394
Dave Airlie28d52042009-09-21 14:33:58 +100015395 return 0;
15396}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015397
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015398struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015399
15400 u32 power_well_driver;
15401
Chris Wilson63b66e52013-08-08 15:12:06 +020015402 int num_transcoders;
15403
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015404 struct intel_cursor_error_state {
15405 u32 control;
15406 u32 position;
15407 u32 base;
15408 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015409 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015410
15411 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015412 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015413 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015414 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015415 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015416
15417 struct intel_plane_error_state {
15418 u32 control;
15419 u32 stride;
15420 u32 size;
15421 u32 pos;
15422 u32 addr;
15423 u32 surface;
15424 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015425 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015426
15427 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015428 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015429 enum transcoder cpu_transcoder;
15430
15431 u32 conf;
15432
15433 u32 htotal;
15434 u32 hblank;
15435 u32 hsync;
15436 u32 vtotal;
15437 u32 vblank;
15438 u32 vsync;
15439 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015440};
15441
15442struct intel_display_error_state *
15443intel_display_capture_error_state(struct drm_device *dev)
15444{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015445 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015446 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015447 int transcoders[] = {
15448 TRANSCODER_A,
15449 TRANSCODER_B,
15450 TRANSCODER_C,
15451 TRANSCODER_EDP,
15452 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015453 int i;
15454
Chris Wilson63b66e52013-08-08 15:12:06 +020015455 if (INTEL_INFO(dev)->num_pipes == 0)
15456 return NULL;
15457
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015458 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015459 if (error == NULL)
15460 return NULL;
15461
Imre Deak190be112013-11-25 17:15:31 +020015462 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015463 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15464
Damien Lespiau055e3932014-08-18 13:49:10 +010015465 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015466 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015467 __intel_display_power_is_enabled(dev_priv,
15468 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015469 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015470 continue;
15471
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015472 error->cursor[i].control = I915_READ(CURCNTR(i));
15473 error->cursor[i].position = I915_READ(CURPOS(i));
15474 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015475
15476 error->plane[i].control = I915_READ(DSPCNTR(i));
15477 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015478 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015479 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015480 error->plane[i].pos = I915_READ(DSPPOS(i));
15481 }
Paulo Zanonica291362013-03-06 20:03:14 -030015482 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15483 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015484 if (INTEL_INFO(dev)->gen >= 4) {
15485 error->plane[i].surface = I915_READ(DSPSURF(i));
15486 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15487 }
15488
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015489 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015490
Sonika Jindal3abfce72014-07-21 15:23:43 +053015491 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015492 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015493 }
15494
15495 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15496 if (HAS_DDI(dev_priv->dev))
15497 error->num_transcoders++; /* Account for eDP. */
15498
15499 for (i = 0; i < error->num_transcoders; i++) {
15500 enum transcoder cpu_transcoder = transcoders[i];
15501
Imre Deakddf9c532013-11-27 22:02:02 +020015502 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015503 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015504 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015505 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015506 continue;
15507
Chris Wilson63b66e52013-08-08 15:12:06 +020015508 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15509
15510 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15511 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15512 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15513 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15514 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15515 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15516 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015517 }
15518
15519 return error;
15520}
15521
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015522#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15523
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015524void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015525intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015526 struct drm_device *dev,
15527 struct intel_display_error_state *error)
15528{
Damien Lespiau055e3932014-08-18 13:49:10 +010015529 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015530 int i;
15531
Chris Wilson63b66e52013-08-08 15:12:06 +020015532 if (!error)
15533 return;
15534
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015535 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015536 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015537 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015538 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015539 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015540 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015541 err_printf(m, " Power: %s\n",
15542 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015543 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015544 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015545
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015546 err_printf(m, "Plane [%d]:\n", i);
15547 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15548 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015549 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015550 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15551 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015552 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015553 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015554 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015555 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015556 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15557 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015558 }
15559
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015560 err_printf(m, "Cursor [%d]:\n", i);
15561 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15562 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15563 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015564 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015565
15566 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015567 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015568 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015569 err_printf(m, " Power: %s\n",
15570 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015571 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15572 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15573 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15574 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15575 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15576 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15577 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15578 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015579}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015580
15581void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15582{
15583 struct intel_crtc *crtc;
15584
15585 for_each_intel_crtc(dev, crtc) {
15586 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015587
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015588 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015589
15590 work = crtc->unpin_work;
15591
15592 if (work && work->event &&
15593 work->event->base.file_priv == file) {
15594 kfree(work->event);
15595 work->event = NULL;
15596 }
15597
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015598 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015599 }
15600}