blob: 7298796847f60685d8c8a2b7f0c9f1dfcf88d1f9 [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"
Matt Roperc196e1d2015-01-21 16:35:48 -080040#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010041#include <drm/drm_dp_helper.h>
42#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070043#include <drm/drm_plane_helper.h>
44#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080045#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080046
Matt Roper465c1202014-05-29 08:06:54 -070047/* Primary plane formats supported by all gen */
48#define COMMON_PRIMARY_FORMATS \
49 DRM_FORMAT_C8, \
50 DRM_FORMAT_RGB565, \
51 DRM_FORMAT_XRGB8888, \
52 DRM_FORMAT_ARGB8888
53
54/* Primary plane formats for gen <= 3 */
55static const uint32_t intel_primary_formats_gen2[] = {
56 COMMON_PRIMARY_FORMATS,
57 DRM_FORMAT_XRGB1555,
58 DRM_FORMAT_ARGB1555,
59};
60
61/* Primary plane formats for gen >= 4 */
62static const uint32_t intel_primary_formats_gen4[] = {
63 COMMON_PRIMARY_FORMATS, \
64 DRM_FORMAT_XBGR8888,
65 DRM_FORMAT_ABGR8888,
66 DRM_FORMAT_XRGB2101010,
67 DRM_FORMAT_ARGB2101010,
68 DRM_FORMAT_XBGR2101010,
69 DRM_FORMAT_ABGR2101010,
70};
71
Matt Roper3d7d6512014-06-10 08:28:13 -070072/* Cursor formats */
73static const uint32_t intel_cursor_formats[] = {
74 DRM_FORMAT_ARGB8888,
75};
76
Chris Wilson6b383a72010-09-13 13:54:26 +010077static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080078
Jesse Barnesf1f644d2013-06-27 00:39:25 +030079static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020080 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030081static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020082 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030083
Damien Lespiaue7457a92013-08-08 22:28:59 +010084static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
85 int x, int y, struct drm_framebuffer *old_fb);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080086static int intel_framebuffer_init(struct drm_device *dev,
87 struct intel_framebuffer *ifb,
88 struct drm_mode_fb_cmd2 *mode_cmd,
89 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020090static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
91static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020092static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070093 struct intel_link_m_n *m_n,
94 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020095static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +020096static void haswell_set_pipeconf(struct drm_crtc *crtc);
97static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +020098static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020099 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200100static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200101 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800102static void intel_begin_crtc_commit(struct drm_crtc *crtc);
103static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100104
Dave Airlie0e32b392014-05-02 14:02:48 +1000105static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
106{
107 if (!connector->mst_port)
108 return connector->encoder;
109 else
110 return &connector->mst_port->mst_encoders[pipe]->base;
111}
112
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},
Vijay Purushothaman9505e012015-02-16 15:07:59 +0530393 .vco = { .min = 4860000, .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
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300401static void vlv_clock(int refclk, intel_clock_t *clock)
402{
403 clock->m = clock->m1 * clock->m2;
404 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200405 if (WARN_ON(clock->n == 0 || clock->p == 0))
406 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300407 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
408 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300409}
410
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300411/**
412 * Returns whether any output on the specified pipe is of the specified type
413 */
Damien Lespiau40935612014-10-29 11:16:59 +0000414bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300415{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300416 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300417 struct intel_encoder *encoder;
418
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300419 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300420 if (encoder->type == type)
421 return true;
422
423 return false;
424}
425
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200426/**
427 * Returns whether any output on the specified pipe will have the specified
428 * type after a staged modeset is complete, i.e., the same as
429 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
430 * encoder->crtc.
431 */
432static bool intel_pipe_will_have_type(struct intel_crtc *crtc, int type)
433{
434 struct drm_device *dev = crtc->base.dev;
435 struct intel_encoder *encoder;
436
437 for_each_intel_encoder(dev, encoder)
438 if (encoder->new_crtc == crtc && encoder->type == type)
439 return true;
440
441 return false;
442}
443
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300444static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
Chris Wilson1b894b52010-12-14 20:04:54 +0000445 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800446{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300447 struct drm_device *dev = crtc->base.dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800448 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800449
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200450 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100451 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000452 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800453 limit = &intel_limits_ironlake_dual_lvds_100m;
454 else
455 limit = &intel_limits_ironlake_dual_lvds;
456 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000457 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800458 limit = &intel_limits_ironlake_single_lvds_100m;
459 else
460 limit = &intel_limits_ironlake_single_lvds;
461 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200462 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800463 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800464
465 return limit;
466}
467
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300468static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
Ma Ling044c7c42009-03-18 20:13:23 +0800469{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300470 struct drm_device *dev = crtc->base.dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800471 const intel_limit_t *limit;
472
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200473 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100474 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700475 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800476 else
Keith Packarde4b36692009-06-05 19:22:17 -0700477 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200478 } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI) ||
479 intel_pipe_will_have_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700480 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200481 } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700482 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800483 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700484 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800485
486 return limit;
487}
488
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300489static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800490{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300491 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800492 const intel_limit_t *limit;
493
Eric Anholtbad720f2009-10-22 16:11:14 -0700494 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000495 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800496 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800497 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500498 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200499 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500500 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800501 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500502 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300503 } else if (IS_CHERRYVIEW(dev)) {
504 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700505 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300506 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100507 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200508 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100509 limit = &intel_limits_i9xx_lvds;
510 else
511 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800512 } else {
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200513 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700514 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200515 else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700516 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200517 else
518 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800519 }
520 return limit;
521}
522
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500523/* m1 is reserved as 0 in Pineview, n is a ring counter */
524static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800525{
Shaohua Li21778322009-02-23 15:19:16 +0800526 clock->m = clock->m2 + 2;
527 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200528 if (WARN_ON(clock->n == 0 || clock->p == 0))
529 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300530 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
531 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800532}
533
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200534static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
535{
536 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
537}
538
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200539static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800540{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200541 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800542 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200543 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
544 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300545 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
546 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800547}
548
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300549static void chv_clock(int refclk, intel_clock_t *clock)
550{
551 clock->m = clock->m1 * clock->m2;
552 clock->p = clock->p1 * clock->p2;
553 if (WARN_ON(clock->n == 0 || clock->p == 0))
554 return;
555 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
556 clock->n << 22);
557 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
558}
559
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800560#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800561/**
562 * Returns whether the given set of divisors are valid for a given refclk with
563 * the given connectors.
564 */
565
Chris Wilson1b894b52010-12-14 20:04:54 +0000566static bool intel_PLL_is_valid(struct drm_device *dev,
567 const intel_limit_t *limit,
568 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800569{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300570 if (clock->n < limit->n.min || limit->n.max < clock->n)
571 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800572 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400573 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800574 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400575 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800576 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400577 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300578
579 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
580 if (clock->m1 <= clock->m2)
581 INTELPllInvalid("m1 <= m2\n");
582
583 if (!IS_VALLEYVIEW(dev)) {
584 if (clock->p < limit->p.min || limit->p.max < clock->p)
585 INTELPllInvalid("p out of range\n");
586 if (clock->m < limit->m.min || limit->m.max < clock->m)
587 INTELPllInvalid("m out of range\n");
588 }
589
Jesse Barnes79e53942008-11-07 14:24:08 -0800590 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400591 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800592 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
593 * connector, etc., rather than just a single range.
594 */
595 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400596 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800597
598 return true;
599}
600
Ma Lingd4906092009-03-18 20:13:27 +0800601static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300602i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800603 int target, int refclk, intel_clock_t *match_clock,
604 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800605{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300606 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800607 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800608 int err = target;
609
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200610 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800611 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100612 * For LVDS just rely on its current settings for dual-channel.
613 * We haven't figured out how to reliably set up different
614 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800615 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100616 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800617 clock.p2 = limit->p2.p2_fast;
618 else
619 clock.p2 = limit->p2.p2_slow;
620 } else {
621 if (target < limit->p2.dot_limit)
622 clock.p2 = limit->p2.p2_slow;
623 else
624 clock.p2 = limit->p2.p2_fast;
625 }
626
Akshay Joshi0206e352011-08-16 15:34:10 -0400627 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800628
Zhao Yakui42158662009-11-20 11:24:18 +0800629 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
630 clock.m1++) {
631 for (clock.m2 = limit->m2.min;
632 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200633 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800634 break;
635 for (clock.n = limit->n.min;
636 clock.n <= limit->n.max; clock.n++) {
637 for (clock.p1 = limit->p1.min;
638 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800639 int this_err;
640
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200641 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000642 if (!intel_PLL_is_valid(dev, limit,
643 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800644 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800645 if (match_clock &&
646 clock.p != match_clock->p)
647 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800648
649 this_err = abs(clock.dot - target);
650 if (this_err < err) {
651 *best_clock = clock;
652 err = this_err;
653 }
654 }
655 }
656 }
657 }
658
659 return (err != target);
660}
661
Ma Lingd4906092009-03-18 20:13:27 +0800662static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300663pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200664 int target, int refclk, intel_clock_t *match_clock,
665 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200666{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300667 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200668 intel_clock_t clock;
669 int err = target;
670
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200671 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200672 /*
673 * For LVDS just rely on its current settings for dual-channel.
674 * We haven't figured out how to reliably set up different
675 * single/dual channel state, if we even can.
676 */
677 if (intel_is_dual_link_lvds(dev))
678 clock.p2 = limit->p2.p2_fast;
679 else
680 clock.p2 = limit->p2.p2_slow;
681 } else {
682 if (target < limit->p2.dot_limit)
683 clock.p2 = limit->p2.p2_slow;
684 else
685 clock.p2 = limit->p2.p2_fast;
686 }
687
688 memset(best_clock, 0, sizeof(*best_clock));
689
690 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
691 clock.m1++) {
692 for (clock.m2 = limit->m2.min;
693 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200694 for (clock.n = limit->n.min;
695 clock.n <= limit->n.max; clock.n++) {
696 for (clock.p1 = limit->p1.min;
697 clock.p1 <= limit->p1.max; clock.p1++) {
698 int this_err;
699
700 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800701 if (!intel_PLL_is_valid(dev, limit,
702 &clock))
703 continue;
704 if (match_clock &&
705 clock.p != match_clock->p)
706 continue;
707
708 this_err = abs(clock.dot - target);
709 if (this_err < err) {
710 *best_clock = clock;
711 err = this_err;
712 }
713 }
714 }
715 }
716 }
717
718 return (err != target);
719}
720
Ma Lingd4906092009-03-18 20:13:27 +0800721static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300722g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200723 int target, int refclk, intel_clock_t *match_clock,
724 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800725{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300726 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800727 intel_clock_t clock;
728 int max_n;
729 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400730 /* approximately equals target * 0.00585 */
731 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800732 found = false;
733
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200734 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100735 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800736 clock.p2 = limit->p2.p2_fast;
737 else
738 clock.p2 = limit->p2.p2_slow;
739 } else {
740 if (target < limit->p2.dot_limit)
741 clock.p2 = limit->p2.p2_slow;
742 else
743 clock.p2 = limit->p2.p2_fast;
744 }
745
746 memset(best_clock, 0, sizeof(*best_clock));
747 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200748 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800749 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200750 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800751 for (clock.m1 = limit->m1.max;
752 clock.m1 >= limit->m1.min; clock.m1--) {
753 for (clock.m2 = limit->m2.max;
754 clock.m2 >= limit->m2.min; clock.m2--) {
755 for (clock.p1 = limit->p1.max;
756 clock.p1 >= limit->p1.min; clock.p1--) {
757 int this_err;
758
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200759 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000760 if (!intel_PLL_is_valid(dev, limit,
761 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800762 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000763
764 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800765 if (this_err < err_most) {
766 *best_clock = clock;
767 err_most = this_err;
768 max_n = clock.n;
769 found = true;
770 }
771 }
772 }
773 }
774 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800775 return found;
776}
Ma Lingd4906092009-03-18 20:13:27 +0800777
Zhenyu Wang2c072452009-06-05 15:38:42 +0800778static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300779vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200780 int target, int refclk, intel_clock_t *match_clock,
781 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700782{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300783 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300784 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300785 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300786 /* min update 19.2 MHz */
787 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300788 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700789
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300790 target *= 5; /* fast clock */
791
792 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700793
794 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300795 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300796 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300797 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300798 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300799 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700800 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300801 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300802 unsigned int ppm, diff;
803
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300804 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
805 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300806
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300807 vlv_clock(refclk, &clock);
808
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300809 if (!intel_PLL_is_valid(dev, limit,
810 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300811 continue;
812
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300813 diff = abs(clock.dot - target);
814 ppm = div_u64(1000000ULL * diff, target);
815
816 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300817 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300818 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300819 found = true;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300820 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300821
Ville Syrjäläc6861222013-09-24 21:26:21 +0300822 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300823 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300824 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300825 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700826 }
827 }
828 }
829 }
830 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700831
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300832 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700833}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700834
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300835static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300836chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300837 int target, int refclk, intel_clock_t *match_clock,
838 intel_clock_t *best_clock)
839{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300840 struct drm_device *dev = crtc->base.dev;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300841 intel_clock_t clock;
842 uint64_t m2;
843 int found = false;
844
845 memset(best_clock, 0, sizeof(*best_clock));
846
847 /*
848 * Based on hardware doc, the n always set to 1, and m1 always
849 * set to 2. If requires to support 200Mhz refclk, we need to
850 * revisit this because n may not 1 anymore.
851 */
852 clock.n = 1, clock.m1 = 2;
853 target *= 5; /* fast clock */
854
855 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
856 for (clock.p2 = limit->p2.p2_fast;
857 clock.p2 >= limit->p2.p2_slow;
858 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
859
860 clock.p = clock.p1 * clock.p2;
861
862 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
863 clock.n) << 22, refclk * clock.m1);
864
865 if (m2 > INT_MAX/clock.m1)
866 continue;
867
868 clock.m2 = m2;
869
870 chv_clock(refclk, &clock);
871
872 if (!intel_PLL_is_valid(dev, limit, &clock))
873 continue;
874
875 /* based on hardware requirement, prefer bigger p
876 */
877 if (clock.p > best_clock->p) {
878 *best_clock = clock;
879 found = true;
880 }
881 }
882 }
883
884 return found;
885}
886
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300887bool intel_crtc_active(struct drm_crtc *crtc)
888{
889 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
890
891 /* Be paranoid as we can arrive here with only partial
892 * state retrieved from the hardware during setup.
893 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100894 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300895 * as Haswell has gained clock readout/fastboot support.
896 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000897 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300898 * properly reconstruct framebuffers.
899 */
Matt Roperf4510a22014-04-01 15:22:40 -0700900 return intel_crtc->active && crtc->primary->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200901 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300902}
903
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200904enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
905 enum pipe pipe)
906{
907 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
908 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
909
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200910 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200911}
912
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300913static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
914{
915 struct drm_i915_private *dev_priv = dev->dev_private;
916 u32 reg = PIPEDSL(pipe);
917 u32 line1, line2;
918 u32 line_mask;
919
920 if (IS_GEN2(dev))
921 line_mask = DSL_LINEMASK_GEN2;
922 else
923 line_mask = DSL_LINEMASK_GEN3;
924
925 line1 = I915_READ(reg) & line_mask;
926 mdelay(5);
927 line2 = I915_READ(reg) & line_mask;
928
929 return line1 == line2;
930}
931
Keith Packardab7ad7f2010-10-03 00:33:06 -0700932/*
933 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300934 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700935 *
936 * After disabling a pipe, we can't wait for vblank in the usual way,
937 * spinning on the vblank interrupt status bit, since we won't actually
938 * see an interrupt when the pipe is disabled.
939 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700940 * On Gen4 and above:
941 * wait for the pipe register state bit to turn off
942 *
943 * Otherwise:
944 * wait for the display line value to settle (it usually
945 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100946 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700947 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300948static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700949{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300950 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700951 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200952 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300953 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700954
Keith Packardab7ad7f2010-10-03 00:33:06 -0700955 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200956 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700957
Keith Packardab7ad7f2010-10-03 00:33:06 -0700958 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100959 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
960 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200961 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700962 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -0700963 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300964 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200965 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700966 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800967}
968
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000969/*
970 * ibx_digital_port_connected - is the specified port connected?
971 * @dev_priv: i915 private structure
972 * @port: the port to test
973 *
974 * Returns true if @port is connected, false otherwise.
975 */
976bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
977 struct intel_digital_port *port)
978{
979 u32 bit;
980
Damien Lespiauc36346e2012-12-13 16:09:03 +0000981 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +0200982 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +0000983 case PORT_B:
984 bit = SDE_PORTB_HOTPLUG;
985 break;
986 case PORT_C:
987 bit = SDE_PORTC_HOTPLUG;
988 break;
989 case PORT_D:
990 bit = SDE_PORTD_HOTPLUG;
991 break;
992 default:
993 return true;
994 }
995 } else {
Robin Schroereba905b2014-05-18 02:24:50 +0200996 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +0000997 case PORT_B:
998 bit = SDE_PORTB_HOTPLUG_CPT;
999 break;
1000 case PORT_C:
1001 bit = SDE_PORTC_HOTPLUG_CPT;
1002 break;
1003 case PORT_D:
1004 bit = SDE_PORTD_HOTPLUG_CPT;
1005 break;
1006 default:
1007 return true;
1008 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001009 }
1010
1011 return I915_READ(SDEISR) & bit;
1012}
1013
Jesse Barnesb24e7172011-01-04 15:09:30 -08001014static const char *state_string(bool enabled)
1015{
1016 return enabled ? "on" : "off";
1017}
1018
1019/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001020void assert_pll(struct drm_i915_private *dev_priv,
1021 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001022{
1023 int reg;
1024 u32 val;
1025 bool cur_state;
1026
1027 reg = DPLL(pipe);
1028 val = I915_READ(reg);
1029 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001030 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001031 "PLL state assertion failure (expected %s, current %s)\n",
1032 state_string(state), state_string(cur_state));
1033}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001034
Jani Nikula23538ef2013-08-27 15:12:22 +03001035/* XXX: the dsi pll is shared between MIPI DSI ports */
1036static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1037{
1038 u32 val;
1039 bool cur_state;
1040
1041 mutex_lock(&dev_priv->dpio_lock);
1042 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1043 mutex_unlock(&dev_priv->dpio_lock);
1044
1045 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001046 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001047 "DSI PLL state assertion failure (expected %s, current %s)\n",
1048 state_string(state), state_string(cur_state));
1049}
1050#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1051#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1052
Daniel Vetter55607e82013-06-16 21:42:39 +02001053struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001054intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001055{
Daniel Vettere2b78262013-06-07 23:10:03 +02001056 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1057
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001058 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001059 return NULL;
1060
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001061 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001062}
1063
Jesse Barnesb24e7172011-01-04 15:09:30 -08001064/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001065void assert_shared_dpll(struct drm_i915_private *dev_priv,
1066 struct intel_shared_dpll *pll,
1067 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001068{
Jesse Barnes040484a2011-01-03 12:14:26 -08001069 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001070 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001071
Chris Wilson92b27b02012-05-20 18:10:50 +01001072 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001073 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001074 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001075
Daniel Vetter53589012013-06-05 13:34:16 +02001076 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001077 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001078 "%s assertion failure (expected %s, current %s)\n",
1079 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001080}
Jesse Barnes040484a2011-01-03 12:14:26 -08001081
1082static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1083 enum pipe pipe, bool state)
1084{
1085 int reg;
1086 u32 val;
1087 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001088 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1089 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001090
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001091 if (HAS_DDI(dev_priv->dev)) {
1092 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001093 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001094 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001095 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001096 } else {
1097 reg = FDI_TX_CTL(pipe);
1098 val = I915_READ(reg);
1099 cur_state = !!(val & FDI_TX_ENABLE);
1100 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001101 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001102 "FDI TX state assertion failure (expected %s, current %s)\n",
1103 state_string(state), state_string(cur_state));
1104}
1105#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1106#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1107
1108static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1109 enum pipe pipe, bool state)
1110{
1111 int reg;
1112 u32 val;
1113 bool cur_state;
1114
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001115 reg = FDI_RX_CTL(pipe);
1116 val = I915_READ(reg);
1117 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001118 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001119 "FDI RX state assertion failure (expected %s, current %s)\n",
1120 state_string(state), state_string(cur_state));
1121}
1122#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1123#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1124
1125static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1126 enum pipe pipe)
1127{
1128 int reg;
1129 u32 val;
1130
1131 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001132 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001133 return;
1134
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001135 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001136 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001137 return;
1138
Jesse Barnes040484a2011-01-03 12:14:26 -08001139 reg = FDI_TX_CTL(pipe);
1140 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001141 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 -08001142}
1143
Daniel Vetter55607e82013-06-16 21:42:39 +02001144void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1145 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001146{
1147 int reg;
1148 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001149 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001150
1151 reg = FDI_RX_CTL(pipe);
1152 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001153 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001154 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001155 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1156 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001157}
1158
Daniel Vetterb680c372014-09-19 18:27:27 +02001159void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1160 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001161{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001162 struct drm_device *dev = dev_priv->dev;
1163 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001164 u32 val;
1165 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001166 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001167
Jani Nikulabedd4db2014-08-22 15:04:13 +03001168 if (WARN_ON(HAS_DDI(dev)))
1169 return;
1170
1171 if (HAS_PCH_SPLIT(dev)) {
1172 u32 port_sel;
1173
Jesse Barnesea0760c2011-01-04 15:09:32 -08001174 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001175 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1176
1177 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1178 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1179 panel_pipe = PIPE_B;
1180 /* XXX: else fix for eDP */
1181 } else if (IS_VALLEYVIEW(dev)) {
1182 /* presumably write lock depends on pipe, not port select */
1183 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1184 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001185 } else {
1186 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001187 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1188 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001189 }
1190
1191 val = I915_READ(pp_reg);
1192 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001193 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001194 locked = false;
1195
Rob Clarke2c719b2014-12-15 13:56:32 -05001196 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001197 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001198 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001199}
1200
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001201static void assert_cursor(struct drm_i915_private *dev_priv,
1202 enum pipe pipe, bool state)
1203{
1204 struct drm_device *dev = dev_priv->dev;
1205 bool cur_state;
1206
Paulo Zanonid9d82082014-02-27 16:30:56 -03001207 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001208 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001209 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001210 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001211
Rob Clarke2c719b2014-12-15 13:56:32 -05001212 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001213 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1214 pipe_name(pipe), state_string(state), state_string(cur_state));
1215}
1216#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1217#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1218
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001219void assert_pipe(struct drm_i915_private *dev_priv,
1220 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001221{
1222 int reg;
1223 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001224 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001225 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1226 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001227
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001228 /* if we need the pipe quirk it must be always on */
1229 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1230 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001231 state = true;
1232
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001233 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001234 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001235 cur_state = false;
1236 } else {
1237 reg = PIPECONF(cpu_transcoder);
1238 val = I915_READ(reg);
1239 cur_state = !!(val & PIPECONF_ENABLE);
1240 }
1241
Rob Clarke2c719b2014-12-15 13:56:32 -05001242 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001243 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001244 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001245}
1246
Chris Wilson931872f2012-01-16 23:01:13 +00001247static void assert_plane(struct drm_i915_private *dev_priv,
1248 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001249{
1250 int reg;
1251 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001252 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001253
1254 reg = DSPCNTR(plane);
1255 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001256 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001257 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001258 "plane %c assertion failure (expected %s, current %s)\n",
1259 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001260}
1261
Chris Wilson931872f2012-01-16 23:01:13 +00001262#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1263#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1264
Jesse Barnesb24e7172011-01-04 15:09:30 -08001265static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1266 enum pipe pipe)
1267{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001268 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001269 int reg, i;
1270 u32 val;
1271 int cur_pipe;
1272
Ville Syrjälä653e1022013-06-04 13:49:05 +03001273 /* Primary planes are fixed to pipes on gen4+ */
1274 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001275 reg = DSPCNTR(pipe);
1276 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001277 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001278 "plane %c assertion failure, should be disabled but not\n",
1279 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001280 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001281 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001282
Jesse Barnesb24e7172011-01-04 15:09:30 -08001283 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001284 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001285 reg = DSPCNTR(i);
1286 val = I915_READ(reg);
1287 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1288 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001289 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001290 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1291 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001292 }
1293}
1294
Jesse Barnes19332d72013-03-28 09:55:38 -07001295static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1296 enum pipe pipe)
1297{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001298 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001299 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001300 u32 val;
1301
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001302 if (INTEL_INFO(dev)->gen >= 9) {
1303 for_each_sprite(pipe, sprite) {
1304 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001305 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001306 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1307 sprite, pipe_name(pipe));
1308 }
1309 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001310 for_each_sprite(pipe, sprite) {
1311 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001312 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001313 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001314 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001315 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001316 }
1317 } else if (INTEL_INFO(dev)->gen >= 7) {
1318 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001319 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001320 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001321 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001322 plane_name(pipe), pipe_name(pipe));
1323 } else if (INTEL_INFO(dev)->gen >= 5) {
1324 reg = DVSCNTR(pipe);
1325 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001326 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001327 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1328 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001329 }
1330}
1331
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001332static void assert_vblank_disabled(struct drm_crtc *crtc)
1333{
Rob Clarke2c719b2014-12-15 13:56:32 -05001334 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001335 drm_crtc_vblank_put(crtc);
1336}
1337
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001338static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001339{
1340 u32 val;
1341 bool enabled;
1342
Rob Clarke2c719b2014-12-15 13:56:32 -05001343 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001344
Jesse Barnes92f25842011-01-04 15:09:34 -08001345 val = I915_READ(PCH_DREF_CONTROL);
1346 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1347 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001348 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001349}
1350
Daniel Vetterab9412b2013-05-03 11:49:46 +02001351static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1352 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001353{
1354 int reg;
1355 u32 val;
1356 bool enabled;
1357
Daniel Vetterab9412b2013-05-03 11:49:46 +02001358 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001359 val = I915_READ(reg);
1360 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001361 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001362 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1363 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001364}
1365
Keith Packard4e634382011-08-06 10:39:45 -07001366static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1367 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001368{
1369 if ((val & DP_PORT_EN) == 0)
1370 return false;
1371
1372 if (HAS_PCH_CPT(dev_priv->dev)) {
1373 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1374 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1375 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1376 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001377 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1378 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1379 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001380 } else {
1381 if ((val & DP_PIPE_MASK) != (pipe << 30))
1382 return false;
1383 }
1384 return true;
1385}
1386
Keith Packard1519b992011-08-06 10:35:34 -07001387static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1388 enum pipe pipe, u32 val)
1389{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001390 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001391 return false;
1392
1393 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001394 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001395 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001396 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1397 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1398 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001399 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001400 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001401 return false;
1402 }
1403 return true;
1404}
1405
1406static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1407 enum pipe pipe, u32 val)
1408{
1409 if ((val & LVDS_PORT_EN) == 0)
1410 return false;
1411
1412 if (HAS_PCH_CPT(dev_priv->dev)) {
1413 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1414 return false;
1415 } else {
1416 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1417 return false;
1418 }
1419 return true;
1420}
1421
1422static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1423 enum pipe pipe, u32 val)
1424{
1425 if ((val & ADPA_DAC_ENABLE) == 0)
1426 return false;
1427 if (HAS_PCH_CPT(dev_priv->dev)) {
1428 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1429 return false;
1430 } else {
1431 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1432 return false;
1433 }
1434 return true;
1435}
1436
Jesse Barnes291906f2011-02-02 12:28:03 -08001437static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001438 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001439{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001440 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001441 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001442 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001443 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001444
Rob Clarke2c719b2014-12-15 13:56:32 -05001445 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001446 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001447 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001448}
1449
1450static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1451 enum pipe pipe, int reg)
1452{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001453 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001454 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001455 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001456 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001457
Rob Clarke2c719b2014-12-15 13:56:32 -05001458 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001459 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001460 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001461}
1462
1463static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1464 enum pipe pipe)
1465{
1466 int reg;
1467 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001468
Keith Packardf0575e92011-07-25 22:12:43 -07001469 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1470 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1471 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001472
1473 reg = PCH_ADPA;
1474 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001475 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001476 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001477 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001478
1479 reg = PCH_LVDS;
1480 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001481 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001482 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001483 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001484
Paulo Zanonie2debe92013-02-18 19:00:27 -03001485 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1486 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1487 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001488}
1489
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001490static void intel_init_dpio(struct drm_device *dev)
1491{
1492 struct drm_i915_private *dev_priv = dev->dev_private;
1493
1494 if (!IS_VALLEYVIEW(dev))
1495 return;
1496
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001497 /*
1498 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1499 * CHV x1 PHY (DP/HDMI D)
1500 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1501 */
1502 if (IS_CHERRYVIEW(dev)) {
1503 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1504 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1505 } else {
1506 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1507 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001508}
1509
Ville Syrjäläd288f652014-10-28 13:20:22 +02001510static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001511 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001512{
Daniel Vetter426115c2013-07-11 22:13:42 +02001513 struct drm_device *dev = crtc->base.dev;
1514 struct drm_i915_private *dev_priv = dev->dev_private;
1515 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001516 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001517
Daniel Vetter426115c2013-07-11 22:13:42 +02001518 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001519
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001520 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001521 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1522
1523 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001524 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001525 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001526
Daniel Vetter426115c2013-07-11 22:13:42 +02001527 I915_WRITE(reg, dpll);
1528 POSTING_READ(reg);
1529 udelay(150);
1530
1531 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1532 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1533
Ville Syrjäläd288f652014-10-28 13:20:22 +02001534 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001535 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001536
1537 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001538 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001539 POSTING_READ(reg);
1540 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001541 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001542 POSTING_READ(reg);
1543 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001544 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001545 POSTING_READ(reg);
1546 udelay(150); /* wait for warmup */
1547}
1548
Ville Syrjäläd288f652014-10-28 13:20:22 +02001549static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001550 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001551{
1552 struct drm_device *dev = crtc->base.dev;
1553 struct drm_i915_private *dev_priv = dev->dev_private;
1554 int pipe = crtc->pipe;
1555 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001556 u32 tmp;
1557
1558 assert_pipe_disabled(dev_priv, crtc->pipe);
1559
1560 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1561
1562 mutex_lock(&dev_priv->dpio_lock);
1563
1564 /* Enable back the 10bit clock to display controller */
1565 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1566 tmp |= DPIO_DCLKP_EN;
1567 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1568
1569 /*
1570 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1571 */
1572 udelay(1);
1573
1574 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001575 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001576
1577 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001578 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001579 DRM_ERROR("PLL %d failed to lock\n", pipe);
1580
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001581 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001582 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001583 POSTING_READ(DPLL_MD(pipe));
1584
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001585 mutex_unlock(&dev_priv->dpio_lock);
1586}
1587
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001588static int intel_num_dvo_pipes(struct drm_device *dev)
1589{
1590 struct intel_crtc *crtc;
1591 int count = 0;
1592
1593 for_each_intel_crtc(dev, crtc)
1594 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001595 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001596
1597 return count;
1598}
1599
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001600static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001601{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001602 struct drm_device *dev = crtc->base.dev;
1603 struct drm_i915_private *dev_priv = dev->dev_private;
1604 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001605 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001606
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001607 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001608
1609 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001610 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001611
1612 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001613 if (IS_MOBILE(dev) && !IS_I830(dev))
1614 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001615
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001616 /* Enable DVO 2x clock on both PLLs if necessary */
1617 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1618 /*
1619 * It appears to be important that we don't enable this
1620 * for the current pipe before otherwise configuring the
1621 * PLL. No idea how this should be handled if multiple
1622 * DVO outputs are enabled simultaneosly.
1623 */
1624 dpll |= DPLL_DVO_2X_MODE;
1625 I915_WRITE(DPLL(!crtc->pipe),
1626 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1627 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001628
1629 /* Wait for the clocks to stabilize. */
1630 POSTING_READ(reg);
1631 udelay(150);
1632
1633 if (INTEL_INFO(dev)->gen >= 4) {
1634 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001635 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001636 } else {
1637 /* The pixel multiplier can only be updated once the
1638 * DPLL is enabled and the clocks are stable.
1639 *
1640 * So write it again.
1641 */
1642 I915_WRITE(reg, dpll);
1643 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001644
1645 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001646 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001647 POSTING_READ(reg);
1648 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001649 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001650 POSTING_READ(reg);
1651 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001652 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001653 POSTING_READ(reg);
1654 udelay(150); /* wait for warmup */
1655}
1656
1657/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001658 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001659 * @dev_priv: i915 private structure
1660 * @pipe: pipe PLL to disable
1661 *
1662 * Disable the PLL for @pipe, making sure the pipe is off first.
1663 *
1664 * Note! This is for pre-ILK only.
1665 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001666static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001667{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001668 struct drm_device *dev = crtc->base.dev;
1669 struct drm_i915_private *dev_priv = dev->dev_private;
1670 enum pipe pipe = crtc->pipe;
1671
1672 /* Disable DVO 2x clock on both PLLs if necessary */
1673 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001674 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001675 intel_num_dvo_pipes(dev) == 1) {
1676 I915_WRITE(DPLL(PIPE_B),
1677 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1678 I915_WRITE(DPLL(PIPE_A),
1679 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1680 }
1681
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001682 /* Don't disable pipe or pipe PLLs if needed */
1683 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1684 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001685 return;
1686
1687 /* Make sure the pipe isn't still relying on us */
1688 assert_pipe_disabled(dev_priv, pipe);
1689
Daniel Vetter50b44a42013-06-05 13:34:33 +02001690 I915_WRITE(DPLL(pipe), 0);
1691 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001692}
1693
Jesse Barnesf6071162013-10-01 10:41:38 -07001694static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1695{
1696 u32 val = 0;
1697
1698 /* Make sure the pipe isn't still relying on us */
1699 assert_pipe_disabled(dev_priv, pipe);
1700
Imre Deake5cbfbf2014-01-09 17:08:16 +02001701 /*
1702 * Leave integrated clock source and reference clock enabled for pipe B.
1703 * The latter is needed for VGA hotplug / manual detection.
1704 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001705 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001706 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001707 I915_WRITE(DPLL(pipe), val);
1708 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001709
1710}
1711
1712static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1713{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001714 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001715 u32 val;
1716
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001717 /* Make sure the pipe isn't still relying on us */
1718 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001719
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001720 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001721 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001722 if (pipe != PIPE_A)
1723 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1724 I915_WRITE(DPLL(pipe), val);
1725 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001726
1727 mutex_lock(&dev_priv->dpio_lock);
1728
1729 /* Disable 10bit clock to display controller */
1730 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1731 val &= ~DPIO_DCLKP_EN;
1732 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1733
Ville Syrjälä61407f62014-05-27 16:32:55 +03001734 /* disable left/right clock distribution */
1735 if (pipe != PIPE_B) {
1736 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1737 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1738 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1739 } else {
1740 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1741 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1742 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1743 }
1744
Ville Syrjäläd7520482014-04-09 13:28:59 +03001745 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001746}
1747
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001748void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1749 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001750{
1751 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001752 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001753
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001754 switch (dport->port) {
1755 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001756 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001757 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001758 break;
1759 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001760 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001761 dpll_reg = DPLL(0);
1762 break;
1763 case PORT_D:
1764 port_mask = DPLL_PORTD_READY_MASK;
1765 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001766 break;
1767 default:
1768 BUG();
1769 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001770
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001771 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001772 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001773 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001774}
1775
Daniel Vetterb14b1052014-04-24 23:55:13 +02001776static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1777{
1778 struct drm_device *dev = crtc->base.dev;
1779 struct drm_i915_private *dev_priv = dev->dev_private;
1780 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1781
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001782 if (WARN_ON(pll == NULL))
1783 return;
1784
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001785 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001786 if (pll->active == 0) {
1787 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1788 WARN_ON(pll->on);
1789 assert_shared_dpll_disabled(dev_priv, pll);
1790
1791 pll->mode_set(dev_priv, pll);
1792 }
1793}
1794
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001795/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001796 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001797 * @dev_priv: i915 private structure
1798 * @pipe: pipe PLL to enable
1799 *
1800 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1801 * drives the transcoder clock.
1802 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001803static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001804{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001805 struct drm_device *dev = crtc->base.dev;
1806 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001807 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001808
Daniel Vetter87a875b2013-06-05 13:34:19 +02001809 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001810 return;
1811
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001812 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001813 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001814
Damien Lespiau74dd6922014-07-29 18:06:17 +01001815 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001816 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001817 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001818
Daniel Vettercdbd2312013-06-05 13:34:03 +02001819 if (pll->active++) {
1820 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001821 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001822 return;
1823 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001824 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001825
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001826 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1827
Daniel Vetter46edb022013-06-05 13:34:12 +02001828 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001829 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001830 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001831}
1832
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001833static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001834{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001835 struct drm_device *dev = crtc->base.dev;
1836 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001837 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001838
Jesse Barnes92f25842011-01-04 15:09:34 -08001839 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001840 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001841 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001842 return;
1843
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001844 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001845 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001846
Daniel Vetter46edb022013-06-05 13:34:12 +02001847 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1848 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001849 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001850
Chris Wilson48da64a2012-05-13 20:16:12 +01001851 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001852 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001853 return;
1854 }
1855
Daniel Vettere9d69442013-06-05 13:34:15 +02001856 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001857 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001858 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001859 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001860
Daniel Vetter46edb022013-06-05 13:34:12 +02001861 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001862 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001863 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001864
1865 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001866}
1867
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001868static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1869 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001870{
Daniel Vetter23670b322012-11-01 09:15:30 +01001871 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001872 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001874 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001875
1876 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001877 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001878
1879 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001880 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001881 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001882
1883 /* FDI must be feeding us bits for PCH ports */
1884 assert_fdi_tx_enabled(dev_priv, pipe);
1885 assert_fdi_rx_enabled(dev_priv, pipe);
1886
Daniel Vetter23670b322012-11-01 09:15:30 +01001887 if (HAS_PCH_CPT(dev)) {
1888 /* Workaround: Set the timing override bit before enabling the
1889 * pch transcoder. */
1890 reg = TRANS_CHICKEN2(pipe);
1891 val = I915_READ(reg);
1892 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1893 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001894 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001895
Daniel Vetterab9412b2013-05-03 11:49:46 +02001896 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001897 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001898 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001899
1900 if (HAS_PCH_IBX(dev_priv->dev)) {
1901 /*
1902 * make the BPC in transcoder be consistent with
1903 * that in pipeconf reg.
1904 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001905 val &= ~PIPECONF_BPC_MASK;
1906 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001907 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001908
1909 val &= ~TRANS_INTERLACE_MASK;
1910 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001911 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001912 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001913 val |= TRANS_LEGACY_INTERLACED_ILK;
1914 else
1915 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001916 else
1917 val |= TRANS_PROGRESSIVE;
1918
Jesse Barnes040484a2011-01-03 12:14:26 -08001919 I915_WRITE(reg, val | TRANS_ENABLE);
1920 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001921 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001922}
1923
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001924static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001925 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001926{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001927 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001928
1929 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001930 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001931
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001932 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001933 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001934 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001935
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001936 /* Workaround: set timing override bit. */
1937 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001938 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001939 I915_WRITE(_TRANSA_CHICKEN2, val);
1940
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001941 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001942 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001943
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001944 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1945 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001946 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001947 else
1948 val |= TRANS_PROGRESSIVE;
1949
Daniel Vetterab9412b2013-05-03 11:49:46 +02001950 I915_WRITE(LPT_TRANSCONF, val);
1951 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001952 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001953}
1954
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001955static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1956 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001957{
Daniel Vetter23670b322012-11-01 09:15:30 +01001958 struct drm_device *dev = dev_priv->dev;
1959 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001960
1961 /* FDI relies on the transcoder */
1962 assert_fdi_tx_disabled(dev_priv, pipe);
1963 assert_fdi_rx_disabled(dev_priv, pipe);
1964
Jesse Barnes291906f2011-02-02 12:28:03 -08001965 /* Ports must be off as well */
1966 assert_pch_ports_disabled(dev_priv, pipe);
1967
Daniel Vetterab9412b2013-05-03 11:49:46 +02001968 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001969 val = I915_READ(reg);
1970 val &= ~TRANS_ENABLE;
1971 I915_WRITE(reg, val);
1972 /* wait for PCH transcoder off, transcoder state */
1973 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001974 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001975
1976 if (!HAS_PCH_IBX(dev)) {
1977 /* Workaround: Clear the timing override chicken bit again. */
1978 reg = TRANS_CHICKEN2(pipe);
1979 val = I915_READ(reg);
1980 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1981 I915_WRITE(reg, val);
1982 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001983}
1984
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001985static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001986{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001987 u32 val;
1988
Daniel Vetterab9412b2013-05-03 11:49:46 +02001989 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001990 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001991 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001992 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001993 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001994 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001995
1996 /* Workaround: clear timing override bit. */
1997 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001998 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001999 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002000}
2001
2002/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002003 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002004 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002005 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002006 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002007 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002008 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002009static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002010{
Paulo Zanoni03722642014-01-17 13:51:09 -02002011 struct drm_device *dev = crtc->base.dev;
2012 struct drm_i915_private *dev_priv = dev->dev_private;
2013 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002014 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2015 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002016 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002017 int reg;
2018 u32 val;
2019
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002020 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002021 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002022 assert_sprites_disabled(dev_priv, pipe);
2023
Paulo Zanoni681e5812012-12-06 11:12:38 -02002024 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002025 pch_transcoder = TRANSCODER_A;
2026 else
2027 pch_transcoder = pipe;
2028
Jesse Barnesb24e7172011-01-04 15:09:30 -08002029 /*
2030 * A pipe without a PLL won't actually be able to drive bits from
2031 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2032 * need the check.
2033 */
2034 if (!HAS_PCH_SPLIT(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002035 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002036 assert_dsi_pll_enabled(dev_priv);
2037 else
2038 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002039 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002040 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002041 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002042 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002043 assert_fdi_tx_pll_enabled(dev_priv,
2044 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002045 }
2046 /* FIXME: assert CPU port conditions for SNB+ */
2047 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002048
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002049 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002050 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002051 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002052 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2053 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002054 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002055 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002056
2057 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002058 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002059}
2060
2061/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002062 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002063 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002064 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002065 * Disable the pipe of @crtc, making sure that various hardware
2066 * specific requirements are met, if applicable, e.g. plane
2067 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002068 *
2069 * Will wait until the pipe has shut down before returning.
2070 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002071static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002072{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002073 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002074 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002075 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002076 int reg;
2077 u32 val;
2078
2079 /*
2080 * Make sure planes won't keep trying to pump pixels to us,
2081 * or we might hang the display.
2082 */
2083 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002084 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002085 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002086
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002087 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002088 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002089 if ((val & PIPECONF_ENABLE) == 0)
2090 return;
2091
Ville Syrjälä67adc642014-08-15 01:21:57 +03002092 /*
2093 * Double wide has implications for planes
2094 * so best keep it disabled when not needed.
2095 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002096 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002097 val &= ~PIPECONF_DOUBLE_WIDE;
2098
2099 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002100 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2101 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002102 val &= ~PIPECONF_ENABLE;
2103
2104 I915_WRITE(reg, val);
2105 if ((val & PIPECONF_ENABLE) == 0)
2106 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002107}
2108
Keith Packardd74362c2011-07-28 14:47:14 -07002109/*
2110 * Plane regs are double buffered, going from enabled->disabled needs a
2111 * trigger in order to latch. The display address reg provides this.
2112 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002113void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2114 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002115{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002116 struct drm_device *dev = dev_priv->dev;
2117 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002118
2119 I915_WRITE(reg, I915_READ(reg));
2120 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002121}
2122
Jesse Barnesb24e7172011-01-04 15:09:30 -08002123/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002124 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002125 * @plane: plane to be enabled
2126 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002127 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002128 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002129 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002130static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2131 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002132{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002133 struct drm_device *dev = plane->dev;
2134 struct drm_i915_private *dev_priv = dev->dev_private;
2135 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002136
2137 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002138 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002139
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002140 if (intel_crtc->primary_enabled)
2141 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002142
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002143 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002144
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002145 dev_priv->display.update_primary_plane(crtc, plane->fb,
2146 crtc->x, crtc->y);
Ville Syrjälä33c3b0d2014-06-24 13:59:28 +03002147
2148 /*
2149 * BDW signals flip done immediately if the plane
2150 * is disabled, even if the plane enable is already
2151 * armed to occur at the next vblank :(
2152 */
2153 if (IS_BROADWELL(dev))
2154 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002155}
2156
Jesse Barnesb24e7172011-01-04 15:09:30 -08002157/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002158 * intel_disable_primary_hw_plane - disable the primary hardware plane
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002159 * @plane: plane to be disabled
2160 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002161 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002162 * Disable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002163 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002164static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2165 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002166{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002167 struct drm_device *dev = plane->dev;
2168 struct drm_i915_private *dev_priv = dev->dev_private;
2169 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2170
Matt Roper32b7eee2014-12-24 07:59:06 -08002171 if (WARN_ON(!intel_crtc->active))
2172 return;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002173
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002174 if (!intel_crtc->primary_enabled)
2175 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002176
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002177 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002178
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002179 dev_priv->display.update_primary_plane(crtc, plane->fb,
2180 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002181}
2182
Chris Wilson693db182013-03-05 14:52:39 +00002183static bool need_vtd_wa(struct drm_device *dev)
2184{
2185#ifdef CONFIG_INTEL_IOMMU
2186 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2187 return true;
2188#endif
2189 return false;
2190}
2191
Damien Lespiauec2c9812015-01-20 12:51:45 +00002192int
Daniel Vetter091df6c2015-02-10 17:16:10 +00002193intel_fb_align_height(struct drm_device *dev, int height,
2194 uint32_t pixel_format,
2195 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002196{
2197 int tile_height;
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002198 uint32_t bits_per_pixel;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002199
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002200 switch (fb_format_modifier) {
2201 case DRM_FORMAT_MOD_NONE:
2202 tile_height = 1;
2203 break;
2204 case I915_FORMAT_MOD_X_TILED:
2205 tile_height = IS_GEN2(dev) ? 16 : 8;
2206 break;
2207 case I915_FORMAT_MOD_Y_TILED:
2208 tile_height = 32;
2209 break;
2210 case I915_FORMAT_MOD_Yf_TILED:
2211 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2212 switch (bits_per_pixel) {
2213 default:
2214 case 8:
2215 tile_height = 64;
2216 break;
2217 case 16:
2218 case 32:
2219 tile_height = 32;
2220 break;
2221 case 64:
2222 tile_height = 16;
2223 break;
2224 case 128:
2225 WARN_ONCE(1,
2226 "128-bit pixels are not supported for display!");
2227 tile_height = 16;
2228 break;
2229 }
2230 break;
2231 default:
2232 MISSING_CASE(fb_format_modifier);
2233 tile_height = 1;
2234 break;
2235 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002236
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002237 return ALIGN(height, tile_height);
2238}
2239
Chris Wilson127bd2a2010-07-23 23:32:05 +01002240int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002241intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2242 struct drm_framebuffer *fb,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002243 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002244{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002245 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002246 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002247 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002248 u32 alignment;
2249 int ret;
2250
Matt Roperebcdd392014-07-09 16:22:11 -07002251 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2252
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002253 switch (fb->modifier[0]) {
2254 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002255 if (INTEL_INFO(dev)->gen >= 9)
2256 alignment = 256 * 1024;
2257 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002258 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002259 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002260 alignment = 4 * 1024;
2261 else
2262 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002263 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002264 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002265 if (INTEL_INFO(dev)->gen >= 9)
2266 alignment = 256 * 1024;
2267 else {
2268 /* pin() will align the object as required by fence */
2269 alignment = 0;
2270 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002271 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002272 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002273 case I915_FORMAT_MOD_Yf_TILED:
2274 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2275 "Y tiling bo slipped through, driver bug!\n"))
2276 return -EINVAL;
2277 alignment = 1 * 1024 * 1024;
2278 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002279 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002280 MISSING_CASE(fb->modifier[0]);
2281 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002282 }
2283
Chris Wilson693db182013-03-05 14:52:39 +00002284 /* Note that the w/a also requires 64 PTE of padding following the
2285 * bo. We currently fill all unused PTE with the shadow page and so
2286 * we should always have valid PTE following the scanout preventing
2287 * the VT-d warning.
2288 */
2289 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2290 alignment = 256 * 1024;
2291
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002292 /*
2293 * Global gtt pte registers are special registers which actually forward
2294 * writes to a chunk of system memory. Which means that there is no risk
2295 * that the register values disappear as soon as we call
2296 * intel_runtime_pm_put(), so it is correct to wrap only the
2297 * pin/unpin/fence and not more.
2298 */
2299 intel_runtime_pm_get(dev_priv);
2300
Chris Wilsonce453d82011-02-21 14:43:56 +00002301 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002302 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002303 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002304 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002305
2306 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2307 * fence, whereas 965+ only requires a fence if using
2308 * framebuffer compression. For simplicity, we always install
2309 * a fence as the cost is not that onerous.
2310 */
Chris Wilson06d98132012-04-17 15:31:24 +01002311 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002312 if (ret)
2313 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002314
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002315 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002316
Chris Wilsonce453d82011-02-21 14:43:56 +00002317 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002318 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002319 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002320
2321err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01002322 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002323err_interruptible:
2324 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002325 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002326 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002327}
2328
Damien Lespiauf63bdb52015-02-10 19:32:24 +00002329static void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002330{
Matt Roperebcdd392014-07-09 16:22:11 -07002331 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2332
Chris Wilson1690e1e2011-12-14 13:57:08 +01002333 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01002334 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002335}
2336
Daniel Vetterc2c75132012-07-05 12:17:30 +02002337/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2338 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002339unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2340 unsigned int tiling_mode,
2341 unsigned int cpp,
2342 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002343{
Chris Wilsonbc752862013-02-21 20:04:31 +00002344 if (tiling_mode != I915_TILING_NONE) {
2345 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002346
Chris Wilsonbc752862013-02-21 20:04:31 +00002347 tile_rows = *y / 8;
2348 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002349
Chris Wilsonbc752862013-02-21 20:04:31 +00002350 tiles = *x / (512/cpp);
2351 *x %= 512/cpp;
2352
2353 return tile_rows * pitch * 8 + tiles * 4096;
2354 } else {
2355 unsigned int offset;
2356
2357 offset = *y * pitch + *x * cpp;
2358 *y = 0;
2359 *x = (offset & 4095) / cpp;
2360 return offset & -4096;
2361 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002362}
2363
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002364static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002365{
2366 switch (format) {
2367 case DISPPLANE_8BPP:
2368 return DRM_FORMAT_C8;
2369 case DISPPLANE_BGRX555:
2370 return DRM_FORMAT_XRGB1555;
2371 case DISPPLANE_BGRX565:
2372 return DRM_FORMAT_RGB565;
2373 default:
2374 case DISPPLANE_BGRX888:
2375 return DRM_FORMAT_XRGB8888;
2376 case DISPPLANE_RGBX888:
2377 return DRM_FORMAT_XBGR8888;
2378 case DISPPLANE_BGRX101010:
2379 return DRM_FORMAT_XRGB2101010;
2380 case DISPPLANE_RGBX101010:
2381 return DRM_FORMAT_XBGR2101010;
2382 }
2383}
2384
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002385static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2386{
2387 switch (format) {
2388 case PLANE_CTL_FORMAT_RGB_565:
2389 return DRM_FORMAT_RGB565;
2390 default:
2391 case PLANE_CTL_FORMAT_XRGB_8888:
2392 if (rgb_order) {
2393 if (alpha)
2394 return DRM_FORMAT_ABGR8888;
2395 else
2396 return DRM_FORMAT_XBGR8888;
2397 } else {
2398 if (alpha)
2399 return DRM_FORMAT_ARGB8888;
2400 else
2401 return DRM_FORMAT_XRGB8888;
2402 }
2403 case PLANE_CTL_FORMAT_XRGB_2101010:
2404 if (rgb_order)
2405 return DRM_FORMAT_XBGR2101010;
2406 else
2407 return DRM_FORMAT_XRGB2101010;
2408 }
2409}
2410
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002411static bool
2412intel_alloc_plane_obj(struct intel_crtc *crtc,
2413 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002414{
2415 struct drm_device *dev = crtc->base.dev;
2416 struct drm_i915_gem_object *obj = NULL;
2417 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002418 struct drm_framebuffer *fb = &plane_config->fb->base;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002419 u32 base = plane_config->base;
2420
Chris Wilsonff2652e2014-03-10 08:07:02 +00002421 if (plane_config->size == 0)
2422 return false;
2423
Jesse Barnes46f297f2014-03-07 08:57:48 -08002424 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2425 plane_config->size);
2426 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002427 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002428
Damien Lespiau49af4492015-01-20 12:51:44 +00002429 obj->tiling_mode = plane_config->tiling;
2430 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002431 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002432
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002433 mode_cmd.pixel_format = fb->pixel_format;
2434 mode_cmd.width = fb->width;
2435 mode_cmd.height = fb->height;
2436 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002437 mode_cmd.modifier[0] = fb->modifier[0];
2438 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002439
2440 mutex_lock(&dev->struct_mutex);
2441
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002442 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002443 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002444 DRM_DEBUG_KMS("intel fb init failed\n");
2445 goto out_unref_obj;
2446 }
2447
Daniel Vettera071fa02014-06-18 23:28:09 +02002448 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002449 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002450
2451 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2452 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002453
2454out_unref_obj:
2455 drm_gem_object_unreference(&obj->base);
2456 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002457 return false;
2458}
2459
Matt Roperafd65eb2015-02-03 13:10:04 -08002460/* Update plane->state->fb to match plane->fb after driver-internal updates */
2461static void
2462update_state_fb(struct drm_plane *plane)
2463{
2464 if (plane->fb == plane->state->fb)
2465 return;
2466
2467 if (plane->state->fb)
2468 drm_framebuffer_unreference(plane->state->fb);
2469 plane->state->fb = plane->fb;
2470 if (plane->state->fb)
2471 drm_framebuffer_reference(plane->state->fb);
2472}
2473
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002474static void
2475intel_find_plane_obj(struct intel_crtc *intel_crtc,
2476 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002477{
2478 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002479 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002480 struct drm_crtc *c;
2481 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002482 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002483
Damien Lespiau2d140302015-02-05 17:22:18 +00002484 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002485 return;
2486
Damien Lespiauf55548b2015-02-05 18:30:20 +00002487 if (intel_alloc_plane_obj(intel_crtc, plane_config)) {
Damien Lespiaufb9981a2015-02-05 19:24:25 +00002488 struct drm_plane *primary = intel_crtc->base.primary;
2489
2490 primary->fb = &plane_config->fb->base;
2491 primary->state->crtc = &intel_crtc->base;
2492 update_state_fb(primary);
2493
Jesse Barnes484b41d2014-03-07 08:57:55 -08002494 return;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002495 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002496
Damien Lespiau2d140302015-02-05 17:22:18 +00002497 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002498
2499 /*
2500 * Failed to alloc the obj, check to see if we should share
2501 * an fb with another CRTC instead
2502 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002503 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002504 i = to_intel_crtc(c);
2505
2506 if (c == &intel_crtc->base)
2507 continue;
2508
Matt Roper2ff8fde2014-07-08 07:50:07 -07002509 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002510 continue;
2511
Matt Roper2ff8fde2014-07-08 07:50:07 -07002512 obj = intel_fb_obj(c->primary->fb);
2513 if (obj == NULL)
2514 continue;
2515
2516 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Damien Lespiaufb9981a2015-02-05 19:24:25 +00002517 struct drm_plane *primary = intel_crtc->base.primary;
2518
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002519 if (obj->tiling_mode != I915_TILING_NONE)
2520 dev_priv->preserve_bios_swizzle = true;
2521
Dave Airlie66e514c2014-04-03 07:51:54 +10002522 drm_framebuffer_reference(c->primary->fb);
Damien Lespiaufb9981a2015-02-05 19:24:25 +00002523 primary->fb = c->primary->fb;
2524 primary->state->crtc = &intel_crtc->base;
Damien Lespiau5ba76c42015-02-05 17:22:15 +00002525 update_state_fb(intel_crtc->base.primary);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002526 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002527 break;
2528 }
2529 }
Matt Roperafd65eb2015-02-03 13:10:04 -08002530
Jesse Barnes46f297f2014-03-07 08:57:48 -08002531}
2532
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002533static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2534 struct drm_framebuffer *fb,
2535 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002536{
2537 struct drm_device *dev = crtc->dev;
2538 struct drm_i915_private *dev_priv = dev->dev_private;
2539 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002540 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002541 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002542 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002543 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002544 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302545 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002546
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002547 if (!intel_crtc->primary_enabled) {
2548 I915_WRITE(reg, 0);
2549 if (INTEL_INFO(dev)->gen >= 4)
2550 I915_WRITE(DSPSURF(plane), 0);
2551 else
2552 I915_WRITE(DSPADDR(plane), 0);
2553 POSTING_READ(reg);
2554 return;
2555 }
2556
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002557 obj = intel_fb_obj(fb);
2558 if (WARN_ON(obj == NULL))
2559 return;
2560
2561 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2562
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002563 dspcntr = DISPPLANE_GAMMA_ENABLE;
2564
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002565 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002566
2567 if (INTEL_INFO(dev)->gen < 4) {
2568 if (intel_crtc->pipe == PIPE_B)
2569 dspcntr |= DISPPLANE_SEL_PIPE_B;
2570
2571 /* pipesrc and dspsize control the size that is scaled from,
2572 * which should always be the user's requested size.
2573 */
2574 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002575 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2576 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002577 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002578 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2579 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002580 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2581 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002582 I915_WRITE(PRIMPOS(plane), 0);
2583 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002584 }
2585
Ville Syrjälä57779d02012-10-31 17:50:14 +02002586 switch (fb->pixel_format) {
2587 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002588 dspcntr |= DISPPLANE_8BPP;
2589 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002590 case DRM_FORMAT_XRGB1555:
2591 case DRM_FORMAT_ARGB1555:
2592 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002593 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002594 case DRM_FORMAT_RGB565:
2595 dspcntr |= DISPPLANE_BGRX565;
2596 break;
2597 case DRM_FORMAT_XRGB8888:
2598 case DRM_FORMAT_ARGB8888:
2599 dspcntr |= DISPPLANE_BGRX888;
2600 break;
2601 case DRM_FORMAT_XBGR8888:
2602 case DRM_FORMAT_ABGR8888:
2603 dspcntr |= DISPPLANE_RGBX888;
2604 break;
2605 case DRM_FORMAT_XRGB2101010:
2606 case DRM_FORMAT_ARGB2101010:
2607 dspcntr |= DISPPLANE_BGRX101010;
2608 break;
2609 case DRM_FORMAT_XBGR2101010:
2610 case DRM_FORMAT_ABGR2101010:
2611 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002612 break;
2613 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002614 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002615 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002616
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002617 if (INTEL_INFO(dev)->gen >= 4 &&
2618 obj->tiling_mode != I915_TILING_NONE)
2619 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002620
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002621 if (IS_G4X(dev))
2622 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2623
Ville Syrjäläb98971272014-08-27 16:51:22 +03002624 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002625
Daniel Vetterc2c75132012-07-05 12:17:30 +02002626 if (INTEL_INFO(dev)->gen >= 4) {
2627 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002628 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002629 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002630 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002631 linear_offset -= intel_crtc->dspaddr_offset;
2632 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002633 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002634 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002635
Matt Roper8e7d6882015-01-21 16:35:41 -08002636 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302637 dspcntr |= DISPPLANE_ROTATE_180;
2638
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002639 x += (intel_crtc->config->pipe_src_w - 1);
2640 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302641
2642 /* Finding the last pixel of the last line of the display
2643 data and adding to linear_offset*/
2644 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002645 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2646 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302647 }
2648
2649 I915_WRITE(reg, dspcntr);
2650
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002651 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2652 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2653 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002654 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002655 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002656 I915_WRITE(DSPSURF(plane),
2657 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002658 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002659 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002660 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002661 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002662 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002663}
2664
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002665static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2666 struct drm_framebuffer *fb,
2667 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002668{
2669 struct drm_device *dev = crtc->dev;
2670 struct drm_i915_private *dev_priv = dev->dev_private;
2671 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002672 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002673 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002674 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002675 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002676 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302677 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002678
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002679 if (!intel_crtc->primary_enabled) {
2680 I915_WRITE(reg, 0);
2681 I915_WRITE(DSPSURF(plane), 0);
2682 POSTING_READ(reg);
2683 return;
2684 }
2685
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002686 obj = intel_fb_obj(fb);
2687 if (WARN_ON(obj == NULL))
2688 return;
2689
2690 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2691
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002692 dspcntr = DISPPLANE_GAMMA_ENABLE;
2693
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002694 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002695
2696 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2697 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2698
Ville Syrjälä57779d02012-10-31 17:50:14 +02002699 switch (fb->pixel_format) {
2700 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002701 dspcntr |= DISPPLANE_8BPP;
2702 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002703 case DRM_FORMAT_RGB565:
2704 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002705 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002706 case DRM_FORMAT_XRGB8888:
2707 case DRM_FORMAT_ARGB8888:
2708 dspcntr |= DISPPLANE_BGRX888;
2709 break;
2710 case DRM_FORMAT_XBGR8888:
2711 case DRM_FORMAT_ABGR8888:
2712 dspcntr |= DISPPLANE_RGBX888;
2713 break;
2714 case DRM_FORMAT_XRGB2101010:
2715 case DRM_FORMAT_ARGB2101010:
2716 dspcntr |= DISPPLANE_BGRX101010;
2717 break;
2718 case DRM_FORMAT_XBGR2101010:
2719 case DRM_FORMAT_ABGR2101010:
2720 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002721 break;
2722 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002723 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002724 }
2725
2726 if (obj->tiling_mode != I915_TILING_NONE)
2727 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002728
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002729 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002730 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002731
Ville Syrjäläb98971272014-08-27 16:51:22 +03002732 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002733 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002734 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002735 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002736 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002737 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002738 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302739 dspcntr |= DISPPLANE_ROTATE_180;
2740
2741 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002742 x += (intel_crtc->config->pipe_src_w - 1);
2743 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302744
2745 /* Finding the last pixel of the last line of the display
2746 data and adding to linear_offset*/
2747 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002748 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2749 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302750 }
2751 }
2752
2753 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002754
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002755 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2756 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2757 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002758 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002759 I915_WRITE(DSPSURF(plane),
2760 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002761 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002762 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2763 } else {
2764 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2765 I915_WRITE(DSPLINOFF(plane), linear_offset);
2766 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002767 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002768}
2769
Damien Lespiaub3218032015-02-27 11:15:18 +00002770u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2771 uint32_t pixel_format)
2772{
2773 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2774
2775 /*
2776 * The stride is either expressed as a multiple of 64 bytes
2777 * chunks for linear buffers or in number of tiles for tiled
2778 * buffers.
2779 */
2780 switch (fb_modifier) {
2781 case DRM_FORMAT_MOD_NONE:
2782 return 64;
2783 case I915_FORMAT_MOD_X_TILED:
2784 if (INTEL_INFO(dev)->gen == 2)
2785 return 128;
2786 return 512;
2787 case I915_FORMAT_MOD_Y_TILED:
2788 /* No need to check for old gens and Y tiling since this is
2789 * about the display engine and those will be blocked before
2790 * we get here.
2791 */
2792 return 128;
2793 case I915_FORMAT_MOD_Yf_TILED:
2794 if (bits_per_pixel == 8)
2795 return 64;
2796 else
2797 return 128;
2798 default:
2799 MISSING_CASE(fb_modifier);
2800 return 64;
2801 }
2802}
2803
Damien Lespiau70d21f02013-07-03 21:06:04 +01002804static void skylake_update_primary_plane(struct drm_crtc *crtc,
2805 struct drm_framebuffer *fb,
2806 int x, int y)
2807{
2808 struct drm_device *dev = crtc->dev;
2809 struct drm_i915_private *dev_priv = dev->dev_private;
2810 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002811 struct drm_i915_gem_object *obj;
2812 int pipe = intel_crtc->pipe;
Damien Lespiaub3218032015-02-27 11:15:18 +00002813 u32 plane_ctl, stride_div;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002814
2815 if (!intel_crtc->primary_enabled) {
2816 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2817 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2818 POSTING_READ(PLANE_CTL(pipe, 0));
2819 return;
2820 }
2821
2822 plane_ctl = PLANE_CTL_ENABLE |
2823 PLANE_CTL_PIPE_GAMMA_ENABLE |
2824 PLANE_CTL_PIPE_CSC_ENABLE;
2825
2826 switch (fb->pixel_format) {
2827 case DRM_FORMAT_RGB565:
2828 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2829 break;
2830 case DRM_FORMAT_XRGB8888:
2831 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2832 break;
2833 case DRM_FORMAT_XBGR8888:
2834 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2835 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2836 break;
2837 case DRM_FORMAT_XRGB2101010:
2838 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2839 break;
2840 case DRM_FORMAT_XBGR2101010:
2841 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2842 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2843 break;
2844 default:
2845 BUG();
2846 }
2847
Daniel Vetter30af77c2015-02-10 17:16:11 +00002848 switch (fb->modifier[0]) {
2849 case DRM_FORMAT_MOD_NONE:
Damien Lespiau70d21f02013-07-03 21:06:04 +01002850 break;
Daniel Vetter30af77c2015-02-10 17:16:11 +00002851 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau70d21f02013-07-03 21:06:04 +01002852 plane_ctl |= PLANE_CTL_TILED_X;
Damien Lespiaub3218032015-02-27 11:15:18 +00002853 break;
2854 case I915_FORMAT_MOD_Y_TILED:
2855 plane_ctl |= PLANE_CTL_TILED_Y;
2856 break;
2857 case I915_FORMAT_MOD_Yf_TILED:
2858 plane_ctl |= PLANE_CTL_TILED_YF;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002859 break;
2860 default:
Damien Lespiaub3218032015-02-27 11:15:18 +00002861 MISSING_CASE(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002862 }
2863
2864 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Matt Roper8e7d6882015-01-21 16:35:41 -08002865 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
Sonika Jindal1447dde2014-10-04 10:53:31 +01002866 plane_ctl |= PLANE_CTL_ROTATE_180;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002867
Damien Lespiaub3218032015-02-27 11:15:18 +00002868 obj = intel_fb_obj(fb);
2869 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
2870 fb->pixel_format);
2871
Damien Lespiau70d21f02013-07-03 21:06:04 +01002872 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
2873
2874 DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
2875 i915_gem_obj_ggtt_offset(obj),
2876 x, y, fb->width, fb->height,
2877 fb->pitches[0]);
2878
2879 I915_WRITE(PLANE_POS(pipe, 0), 0);
2880 I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
2881 I915_WRITE(PLANE_SIZE(pipe, 0),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002882 (intel_crtc->config->pipe_src_h - 1) << 16 |
2883 (intel_crtc->config->pipe_src_w - 1));
Damien Lespiaub3218032015-02-27 11:15:18 +00002884 I915_WRITE(PLANE_STRIDE(pipe, 0), fb->pitches[0] / stride_div);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002885 I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
2886
2887 POSTING_READ(PLANE_SURF(pipe, 0));
2888}
2889
Jesse Barnes17638cd2011-06-24 12:19:23 -07002890/* Assume fb object is pinned & idle & fenced and just update base pointers */
2891static int
2892intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2893 int x, int y, enum mode_set_atomic state)
2894{
2895 struct drm_device *dev = crtc->dev;
2896 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002897
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002898 if (dev_priv->display.disable_fbc)
2899 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07002900
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002901 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2902
2903 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07002904}
2905
Ville Syrjälä75147472014-11-24 18:28:11 +02002906static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02002907{
Ville Syrjälä96a02912013-02-18 19:08:49 +02002908 struct drm_crtc *crtc;
2909
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002910 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002911 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2912 enum plane plane = intel_crtc->plane;
2913
2914 intel_prepare_page_flip(dev, plane);
2915 intel_finish_page_flip_plane(dev, plane);
2916 }
Ville Syrjälä75147472014-11-24 18:28:11 +02002917}
2918
2919static void intel_update_primary_planes(struct drm_device *dev)
2920{
2921 struct drm_i915_private *dev_priv = dev->dev_private;
2922 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02002923
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002924 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002925 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2926
Rob Clark51fd3712013-11-19 12:10:12 -05002927 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002928 /*
2929 * FIXME: Once we have proper support for primary planes (and
2930 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10002931 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002932 */
Matt Roperf4510a22014-04-01 15:22:40 -07002933 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07002934 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10002935 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07002936 crtc->x,
2937 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05002938 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02002939 }
2940}
2941
Ville Syrjälä75147472014-11-24 18:28:11 +02002942void intel_prepare_reset(struct drm_device *dev)
2943{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02002944 struct drm_i915_private *dev_priv = to_i915(dev);
2945 struct intel_crtc *crtc;
2946
Ville Syrjälä75147472014-11-24 18:28:11 +02002947 /* no reset support for gen2 */
2948 if (IS_GEN2(dev))
2949 return;
2950
2951 /* reset doesn't touch the display */
2952 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
2953 return;
2954
2955 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02002956
2957 /*
2958 * Disabling the crtcs gracefully seems nicer. Also the
2959 * g33 docs say we should at least disable all the planes.
2960 */
2961 for_each_intel_crtc(dev, crtc) {
2962 if (crtc->active)
2963 dev_priv->display.crtc_disable(&crtc->base);
2964 }
Ville Syrjälä75147472014-11-24 18:28:11 +02002965}
2966
2967void intel_finish_reset(struct drm_device *dev)
2968{
2969 struct drm_i915_private *dev_priv = to_i915(dev);
2970
2971 /*
2972 * Flips in the rings will be nuked by the reset,
2973 * so complete all pending flips so that user space
2974 * will get its events and not get stuck.
2975 */
2976 intel_complete_page_flips(dev);
2977
2978 /* no reset support for gen2 */
2979 if (IS_GEN2(dev))
2980 return;
2981
2982 /* reset doesn't touch the display */
2983 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
2984 /*
2985 * Flips in the rings have been nuked by the reset,
2986 * so update the base address of all primary
2987 * planes to the the last fb to make sure we're
2988 * showing the correct fb after a reset.
2989 */
2990 intel_update_primary_planes(dev);
2991 return;
2992 }
2993
2994 /*
2995 * The display has been reset as well,
2996 * so need a full re-initialization.
2997 */
2998 intel_runtime_pm_disable_interrupts(dev_priv);
2999 intel_runtime_pm_enable_interrupts(dev_priv);
3000
3001 intel_modeset_init_hw(dev);
3002
3003 spin_lock_irq(&dev_priv->irq_lock);
3004 if (dev_priv->display.hpd_irq_setup)
3005 dev_priv->display.hpd_irq_setup(dev);
3006 spin_unlock_irq(&dev_priv->irq_lock);
3007
3008 intel_modeset_setup_hw_state(dev, true);
3009
3010 intel_hpd_init(dev_priv);
3011
3012 drm_modeset_unlock_all(dev);
3013}
3014
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003015static int
Chris Wilson14667a42012-04-03 17:58:35 +01003016intel_finish_fb(struct drm_framebuffer *old_fb)
3017{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003018 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01003019 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3020 bool was_interruptible = dev_priv->mm.interruptible;
3021 int ret;
3022
Chris Wilson14667a42012-04-03 17:58:35 +01003023 /* Big Hammer, we also need to ensure that any pending
3024 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3025 * current scanout is retired before unpinning the old
3026 * framebuffer.
3027 *
3028 * This should only fail upon a hung GPU, in which case we
3029 * can safely continue.
3030 */
3031 dev_priv->mm.interruptible = false;
3032 ret = i915_gem_object_finish_gpu(obj);
3033 dev_priv->mm.interruptible = was_interruptible;
3034
3035 return ret;
3036}
3037
Chris Wilson7d5e3792014-03-04 13:15:08 +00003038static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3039{
3040 struct drm_device *dev = crtc->dev;
3041 struct drm_i915_private *dev_priv = dev->dev_private;
3042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003043 bool pending;
3044
3045 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3046 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3047 return false;
3048
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003049 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003050 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003051 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003052
3053 return pending;
3054}
3055
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003056static void intel_update_pipe_size(struct intel_crtc *crtc)
3057{
3058 struct drm_device *dev = crtc->base.dev;
3059 struct drm_i915_private *dev_priv = dev->dev_private;
3060 const struct drm_display_mode *adjusted_mode;
3061
3062 if (!i915.fastboot)
3063 return;
3064
3065 /*
3066 * Update pipe size and adjust fitter if needed: the reason for this is
3067 * that in compute_mode_changes we check the native mode (not the pfit
3068 * mode) to see if we can flip rather than do a full mode set. In the
3069 * fastboot case, we'll flip, but if we don't update the pipesrc and
3070 * pfit state, we'll end up with a big fb scanned out into the wrong
3071 * sized surface.
3072 *
3073 * To fix this properly, we need to hoist the checks up into
3074 * compute_mode_changes (or above), check the actual pfit state and
3075 * whether the platform allows pfit disable with pipe active, and only
3076 * then update the pipesrc and pfit state, even on the flip path.
3077 */
3078
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003079 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003080
3081 I915_WRITE(PIPESRC(crtc->pipe),
3082 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3083 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003084 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003085 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3086 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003087 I915_WRITE(PF_CTL(crtc->pipe), 0);
3088 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3089 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3090 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003091 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3092 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003093}
3094
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003095static void intel_fdi_normal_train(struct drm_crtc *crtc)
3096{
3097 struct drm_device *dev = crtc->dev;
3098 struct drm_i915_private *dev_priv = dev->dev_private;
3099 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3100 int pipe = intel_crtc->pipe;
3101 u32 reg, temp;
3102
3103 /* enable normal train */
3104 reg = FDI_TX_CTL(pipe);
3105 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003106 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003107 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3108 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003109 } else {
3110 temp &= ~FDI_LINK_TRAIN_NONE;
3111 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003112 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003113 I915_WRITE(reg, temp);
3114
3115 reg = FDI_RX_CTL(pipe);
3116 temp = I915_READ(reg);
3117 if (HAS_PCH_CPT(dev)) {
3118 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3119 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3120 } else {
3121 temp &= ~FDI_LINK_TRAIN_NONE;
3122 temp |= FDI_LINK_TRAIN_NONE;
3123 }
3124 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3125
3126 /* wait one idle pattern time */
3127 POSTING_READ(reg);
3128 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003129
3130 /* IVB wants error correction enabled */
3131 if (IS_IVYBRIDGE(dev))
3132 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3133 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003134}
3135
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003136static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01003137{
Matt Roper83d65732015-02-25 13:12:16 -08003138 return crtc->base.state->enable && crtc->active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003139 crtc->config->has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01003140}
3141
Daniel Vetter01a415f2012-10-27 15:58:40 +02003142static void ivb_modeset_global_resources(struct drm_device *dev)
3143{
3144 struct drm_i915_private *dev_priv = dev->dev_private;
3145 struct intel_crtc *pipe_B_crtc =
3146 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
3147 struct intel_crtc *pipe_C_crtc =
3148 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
3149 uint32_t temp;
3150
Daniel Vetter1e833f42013-02-19 22:31:57 +01003151 /*
3152 * When everything is off disable fdi C so that we could enable fdi B
3153 * with all lanes. Note that we don't care about enabled pipes without
3154 * an enabled pch encoder.
3155 */
3156 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
3157 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02003158 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3159 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3160
3161 temp = I915_READ(SOUTH_CHICKEN1);
3162 temp &= ~FDI_BC_BIFURCATION_SELECT;
3163 DRM_DEBUG_KMS("disabling fdi C rx\n");
3164 I915_WRITE(SOUTH_CHICKEN1, temp);
3165 }
3166}
3167
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003168/* The FDI link training functions for ILK/Ibexpeak. */
3169static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3170{
3171 struct drm_device *dev = crtc->dev;
3172 struct drm_i915_private *dev_priv = dev->dev_private;
3173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3174 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003175 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003176
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003177 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003178 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003179
Adam Jacksone1a44742010-06-25 15:32:14 -04003180 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3181 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003182 reg = FDI_RX_IMR(pipe);
3183 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003184 temp &= ~FDI_RX_SYMBOL_LOCK;
3185 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003186 I915_WRITE(reg, temp);
3187 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003188 udelay(150);
3189
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003190 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003191 reg = FDI_TX_CTL(pipe);
3192 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003193 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003194 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003195 temp &= ~FDI_LINK_TRAIN_NONE;
3196 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003197 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003198
Chris Wilson5eddb702010-09-11 13:48:45 +01003199 reg = FDI_RX_CTL(pipe);
3200 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003201 temp &= ~FDI_LINK_TRAIN_NONE;
3202 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003203 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3204
3205 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003206 udelay(150);
3207
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003208 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003209 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3210 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3211 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003212
Chris Wilson5eddb702010-09-11 13:48:45 +01003213 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003214 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003215 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003216 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3217
3218 if ((temp & FDI_RX_BIT_LOCK)) {
3219 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003220 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003221 break;
3222 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003223 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003224 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003225 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003226
3227 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003228 reg = FDI_TX_CTL(pipe);
3229 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003230 temp &= ~FDI_LINK_TRAIN_NONE;
3231 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003232 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003233
Chris Wilson5eddb702010-09-11 13:48:45 +01003234 reg = FDI_RX_CTL(pipe);
3235 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003236 temp &= ~FDI_LINK_TRAIN_NONE;
3237 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003238 I915_WRITE(reg, temp);
3239
3240 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003241 udelay(150);
3242
Chris Wilson5eddb702010-09-11 13:48:45 +01003243 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003244 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003245 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003246 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3247
3248 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003249 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003250 DRM_DEBUG_KMS("FDI train 2 done.\n");
3251 break;
3252 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003253 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003254 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003255 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003256
3257 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003258
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003259}
3260
Akshay Joshi0206e352011-08-16 15:34:10 -04003261static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003262 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3263 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3264 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3265 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3266};
3267
3268/* The FDI link training functions for SNB/Cougarpoint. */
3269static void gen6_fdi_link_train(struct drm_crtc *crtc)
3270{
3271 struct drm_device *dev = crtc->dev;
3272 struct drm_i915_private *dev_priv = dev->dev_private;
3273 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3274 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003275 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003276
Adam Jacksone1a44742010-06-25 15:32:14 -04003277 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3278 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003279 reg = FDI_RX_IMR(pipe);
3280 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003281 temp &= ~FDI_RX_SYMBOL_LOCK;
3282 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003283 I915_WRITE(reg, temp);
3284
3285 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003286 udelay(150);
3287
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003288 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003289 reg = FDI_TX_CTL(pipe);
3290 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003291 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003292 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003293 temp &= ~FDI_LINK_TRAIN_NONE;
3294 temp |= FDI_LINK_TRAIN_PATTERN_1;
3295 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3296 /* SNB-B */
3297 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003298 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003299
Daniel Vetterd74cf322012-10-26 10:58:13 +02003300 I915_WRITE(FDI_RX_MISC(pipe),
3301 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3302
Chris Wilson5eddb702010-09-11 13:48:45 +01003303 reg = FDI_RX_CTL(pipe);
3304 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003305 if (HAS_PCH_CPT(dev)) {
3306 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3307 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3308 } else {
3309 temp &= ~FDI_LINK_TRAIN_NONE;
3310 temp |= FDI_LINK_TRAIN_PATTERN_1;
3311 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003312 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3313
3314 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003315 udelay(150);
3316
Akshay Joshi0206e352011-08-16 15:34:10 -04003317 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003318 reg = FDI_TX_CTL(pipe);
3319 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003320 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3321 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003322 I915_WRITE(reg, temp);
3323
3324 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003325 udelay(500);
3326
Sean Paulfa37d392012-03-02 12:53:39 -05003327 for (retry = 0; retry < 5; retry++) {
3328 reg = FDI_RX_IIR(pipe);
3329 temp = I915_READ(reg);
3330 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3331 if (temp & FDI_RX_BIT_LOCK) {
3332 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3333 DRM_DEBUG_KMS("FDI train 1 done.\n");
3334 break;
3335 }
3336 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003337 }
Sean Paulfa37d392012-03-02 12:53:39 -05003338 if (retry < 5)
3339 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003340 }
3341 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003342 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003343
3344 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003345 reg = FDI_TX_CTL(pipe);
3346 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003347 temp &= ~FDI_LINK_TRAIN_NONE;
3348 temp |= FDI_LINK_TRAIN_PATTERN_2;
3349 if (IS_GEN6(dev)) {
3350 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3351 /* SNB-B */
3352 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3353 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003354 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003355
Chris Wilson5eddb702010-09-11 13:48:45 +01003356 reg = FDI_RX_CTL(pipe);
3357 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003358 if (HAS_PCH_CPT(dev)) {
3359 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3360 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3361 } else {
3362 temp &= ~FDI_LINK_TRAIN_NONE;
3363 temp |= FDI_LINK_TRAIN_PATTERN_2;
3364 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003365 I915_WRITE(reg, temp);
3366
3367 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003368 udelay(150);
3369
Akshay Joshi0206e352011-08-16 15:34:10 -04003370 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003371 reg = FDI_TX_CTL(pipe);
3372 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003373 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3374 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003375 I915_WRITE(reg, temp);
3376
3377 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003378 udelay(500);
3379
Sean Paulfa37d392012-03-02 12:53:39 -05003380 for (retry = 0; retry < 5; retry++) {
3381 reg = FDI_RX_IIR(pipe);
3382 temp = I915_READ(reg);
3383 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3384 if (temp & FDI_RX_SYMBOL_LOCK) {
3385 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3386 DRM_DEBUG_KMS("FDI train 2 done.\n");
3387 break;
3388 }
3389 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003390 }
Sean Paulfa37d392012-03-02 12:53:39 -05003391 if (retry < 5)
3392 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003393 }
3394 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003395 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003396
3397 DRM_DEBUG_KMS("FDI train done.\n");
3398}
3399
Jesse Barnes357555c2011-04-28 15:09:55 -07003400/* Manual link training for Ivy Bridge A0 parts */
3401static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3402{
3403 struct drm_device *dev = crtc->dev;
3404 struct drm_i915_private *dev_priv = dev->dev_private;
3405 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3406 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003407 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003408
3409 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3410 for train result */
3411 reg = FDI_RX_IMR(pipe);
3412 temp = I915_READ(reg);
3413 temp &= ~FDI_RX_SYMBOL_LOCK;
3414 temp &= ~FDI_RX_BIT_LOCK;
3415 I915_WRITE(reg, temp);
3416
3417 POSTING_READ(reg);
3418 udelay(150);
3419
Daniel Vetter01a415f2012-10-27 15:58:40 +02003420 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3421 I915_READ(FDI_RX_IIR(pipe)));
3422
Jesse Barnes139ccd32013-08-19 11:04:55 -07003423 /* Try each vswing and preemphasis setting twice before moving on */
3424 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3425 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003426 reg = FDI_TX_CTL(pipe);
3427 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003428 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3429 temp &= ~FDI_TX_ENABLE;
3430 I915_WRITE(reg, temp);
3431
3432 reg = FDI_RX_CTL(pipe);
3433 temp = I915_READ(reg);
3434 temp &= ~FDI_LINK_TRAIN_AUTO;
3435 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3436 temp &= ~FDI_RX_ENABLE;
3437 I915_WRITE(reg, temp);
3438
3439 /* enable CPU FDI TX and PCH FDI RX */
3440 reg = FDI_TX_CTL(pipe);
3441 temp = I915_READ(reg);
3442 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003443 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003444 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003445 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003446 temp |= snb_b_fdi_train_param[j/2];
3447 temp |= FDI_COMPOSITE_SYNC;
3448 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3449
3450 I915_WRITE(FDI_RX_MISC(pipe),
3451 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3452
3453 reg = FDI_RX_CTL(pipe);
3454 temp = I915_READ(reg);
3455 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3456 temp |= FDI_COMPOSITE_SYNC;
3457 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3458
3459 POSTING_READ(reg);
3460 udelay(1); /* should be 0.5us */
3461
3462 for (i = 0; i < 4; i++) {
3463 reg = FDI_RX_IIR(pipe);
3464 temp = I915_READ(reg);
3465 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3466
3467 if (temp & FDI_RX_BIT_LOCK ||
3468 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3469 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3470 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3471 i);
3472 break;
3473 }
3474 udelay(1); /* should be 0.5us */
3475 }
3476 if (i == 4) {
3477 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3478 continue;
3479 }
3480
3481 /* Train 2 */
3482 reg = FDI_TX_CTL(pipe);
3483 temp = I915_READ(reg);
3484 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3485 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3486 I915_WRITE(reg, temp);
3487
3488 reg = FDI_RX_CTL(pipe);
3489 temp = I915_READ(reg);
3490 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3491 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003492 I915_WRITE(reg, temp);
3493
3494 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003495 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003496
Jesse Barnes139ccd32013-08-19 11:04:55 -07003497 for (i = 0; i < 4; i++) {
3498 reg = FDI_RX_IIR(pipe);
3499 temp = I915_READ(reg);
3500 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003501
Jesse Barnes139ccd32013-08-19 11:04:55 -07003502 if (temp & FDI_RX_SYMBOL_LOCK ||
3503 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3504 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3505 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3506 i);
3507 goto train_done;
3508 }
3509 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003510 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003511 if (i == 4)
3512 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003513 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003514
Jesse Barnes139ccd32013-08-19 11:04:55 -07003515train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003516 DRM_DEBUG_KMS("FDI train done.\n");
3517}
3518
Daniel Vetter88cefb62012-08-12 19:27:14 +02003519static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003520{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003521 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003522 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003523 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003524 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003525
Jesse Barnesc64e3112010-09-10 11:27:03 -07003526
Jesse Barnes0e23b992010-09-10 11:10:00 -07003527 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003528 reg = FDI_RX_CTL(pipe);
3529 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003530 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003531 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003532 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003533 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3534
3535 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003536 udelay(200);
3537
3538 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003539 temp = I915_READ(reg);
3540 I915_WRITE(reg, temp | FDI_PCDCLK);
3541
3542 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003543 udelay(200);
3544
Paulo Zanoni20749732012-11-23 15:30:38 -02003545 /* Enable CPU FDI TX PLL, always on for Ironlake */
3546 reg = FDI_TX_CTL(pipe);
3547 temp = I915_READ(reg);
3548 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3549 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003550
Paulo Zanoni20749732012-11-23 15:30:38 -02003551 POSTING_READ(reg);
3552 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003553 }
3554}
3555
Daniel Vetter88cefb62012-08-12 19:27:14 +02003556static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3557{
3558 struct drm_device *dev = intel_crtc->base.dev;
3559 struct drm_i915_private *dev_priv = dev->dev_private;
3560 int pipe = intel_crtc->pipe;
3561 u32 reg, temp;
3562
3563 /* Switch from PCDclk to Rawclk */
3564 reg = FDI_RX_CTL(pipe);
3565 temp = I915_READ(reg);
3566 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3567
3568 /* Disable CPU FDI TX PLL */
3569 reg = FDI_TX_CTL(pipe);
3570 temp = I915_READ(reg);
3571 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3572
3573 POSTING_READ(reg);
3574 udelay(100);
3575
3576 reg = FDI_RX_CTL(pipe);
3577 temp = I915_READ(reg);
3578 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3579
3580 /* Wait for the clocks to turn off. */
3581 POSTING_READ(reg);
3582 udelay(100);
3583}
3584
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003585static void ironlake_fdi_disable(struct drm_crtc *crtc)
3586{
3587 struct drm_device *dev = crtc->dev;
3588 struct drm_i915_private *dev_priv = dev->dev_private;
3589 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3590 int pipe = intel_crtc->pipe;
3591 u32 reg, temp;
3592
3593 /* disable CPU FDI tx and PCH FDI rx */
3594 reg = FDI_TX_CTL(pipe);
3595 temp = I915_READ(reg);
3596 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3597 POSTING_READ(reg);
3598
3599 reg = FDI_RX_CTL(pipe);
3600 temp = I915_READ(reg);
3601 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003602 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003603 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3604
3605 POSTING_READ(reg);
3606 udelay(100);
3607
3608 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003609 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003610 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003611
3612 /* still set train pattern 1 */
3613 reg = FDI_TX_CTL(pipe);
3614 temp = I915_READ(reg);
3615 temp &= ~FDI_LINK_TRAIN_NONE;
3616 temp |= FDI_LINK_TRAIN_PATTERN_1;
3617 I915_WRITE(reg, temp);
3618
3619 reg = FDI_RX_CTL(pipe);
3620 temp = I915_READ(reg);
3621 if (HAS_PCH_CPT(dev)) {
3622 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3623 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3624 } else {
3625 temp &= ~FDI_LINK_TRAIN_NONE;
3626 temp |= FDI_LINK_TRAIN_PATTERN_1;
3627 }
3628 /* BPC in FDI rx is consistent with that in PIPECONF */
3629 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003630 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003631 I915_WRITE(reg, temp);
3632
3633 POSTING_READ(reg);
3634 udelay(100);
3635}
3636
Chris Wilson5dce5b932014-01-20 10:17:36 +00003637bool intel_has_pending_fb_unpin(struct drm_device *dev)
3638{
3639 struct intel_crtc *crtc;
3640
3641 /* Note that we don't need to be called with mode_config.lock here
3642 * as our list of CRTC objects is static for the lifetime of the
3643 * device and so cannot disappear as we iterate. Similarly, we can
3644 * happily treat the predicates as racy, atomic checks as userspace
3645 * cannot claim and pin a new fb without at least acquring the
3646 * struct_mutex and so serialising with us.
3647 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003648 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003649 if (atomic_read(&crtc->unpin_work_count) == 0)
3650 continue;
3651
3652 if (crtc->unpin_work)
3653 intel_wait_for_vblank(dev, crtc->pipe);
3654
3655 return true;
3656 }
3657
3658 return false;
3659}
3660
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003661static void page_flip_completed(struct intel_crtc *intel_crtc)
3662{
3663 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3664 struct intel_unpin_work *work = intel_crtc->unpin_work;
3665
3666 /* ensure that the unpin work is consistent wrt ->pending. */
3667 smp_rmb();
3668 intel_crtc->unpin_work = NULL;
3669
3670 if (work->event)
3671 drm_send_vblank_event(intel_crtc->base.dev,
3672 intel_crtc->pipe,
3673 work->event);
3674
3675 drm_crtc_vblank_put(&intel_crtc->base);
3676
3677 wake_up_all(&dev_priv->pending_flip_queue);
3678 queue_work(dev_priv->wq, &work->work);
3679
3680 trace_i915_flip_complete(intel_crtc->plane,
3681 work->pending_flip_obj);
3682}
3683
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003684void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003685{
Chris Wilson0f911282012-04-17 10:05:38 +01003686 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003687 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003688
Daniel Vetter2c10d572012-12-20 21:24:07 +01003689 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003690 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3691 !intel_crtc_has_pending_flip(crtc),
3692 60*HZ) == 0)) {
3693 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003694
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003695 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003696 if (intel_crtc->unpin_work) {
3697 WARN_ONCE(1, "Removing stuck page flip\n");
3698 page_flip_completed(intel_crtc);
3699 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003700 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003701 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003702
Chris Wilson975d5682014-08-20 13:13:34 +01003703 if (crtc->primary->fb) {
3704 mutex_lock(&dev->struct_mutex);
3705 intel_finish_fb(crtc->primary->fb);
3706 mutex_unlock(&dev->struct_mutex);
3707 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003708}
3709
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003710/* Program iCLKIP clock to the desired frequency */
3711static void lpt_program_iclkip(struct drm_crtc *crtc)
3712{
3713 struct drm_device *dev = crtc->dev;
3714 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003715 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003716 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3717 u32 temp;
3718
Daniel Vetter09153002012-12-12 14:06:44 +01003719 mutex_lock(&dev_priv->dpio_lock);
3720
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003721 /* It is necessary to ungate the pixclk gate prior to programming
3722 * the divisors, and gate it back when it is done.
3723 */
3724 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3725
3726 /* Disable SSCCTL */
3727 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003728 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3729 SBI_SSCCTL_DISABLE,
3730 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003731
3732 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003733 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003734 auxdiv = 1;
3735 divsel = 0x41;
3736 phaseinc = 0x20;
3737 } else {
3738 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003739 * but the adjusted_mode->crtc_clock in in KHz. To get the
3740 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003741 * convert the virtual clock precision to KHz here for higher
3742 * precision.
3743 */
3744 u32 iclk_virtual_root_freq = 172800 * 1000;
3745 u32 iclk_pi_range = 64;
3746 u32 desired_divisor, msb_divisor_value, pi_value;
3747
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003748 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003749 msb_divisor_value = desired_divisor / iclk_pi_range;
3750 pi_value = desired_divisor % iclk_pi_range;
3751
3752 auxdiv = 0;
3753 divsel = msb_divisor_value - 2;
3754 phaseinc = pi_value;
3755 }
3756
3757 /* This should not happen with any sane values */
3758 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3759 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3760 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3761 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3762
3763 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 +03003764 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003765 auxdiv,
3766 divsel,
3767 phasedir,
3768 phaseinc);
3769
3770 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003771 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003772 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3773 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3774 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3775 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3776 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3777 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003778 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003779
3780 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003781 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003782 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3783 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003784 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003785
3786 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003787 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003788 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003789 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003790
3791 /* Wait for initialization time */
3792 udelay(24);
3793
3794 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003795
3796 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003797}
3798
Daniel Vetter275f01b22013-05-03 11:49:47 +02003799static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3800 enum pipe pch_transcoder)
3801{
3802 struct drm_device *dev = crtc->base.dev;
3803 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003804 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003805
3806 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3807 I915_READ(HTOTAL(cpu_transcoder)));
3808 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3809 I915_READ(HBLANK(cpu_transcoder)));
3810 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3811 I915_READ(HSYNC(cpu_transcoder)));
3812
3813 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3814 I915_READ(VTOTAL(cpu_transcoder)));
3815 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3816 I915_READ(VBLANK(cpu_transcoder)));
3817 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3818 I915_READ(VSYNC(cpu_transcoder)));
3819 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3820 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3821}
3822
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003823static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3824{
3825 struct drm_i915_private *dev_priv = dev->dev_private;
3826 uint32_t temp;
3827
3828 temp = I915_READ(SOUTH_CHICKEN1);
3829 if (temp & FDI_BC_BIFURCATION_SELECT)
3830 return;
3831
3832 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3833 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3834
3835 temp |= FDI_BC_BIFURCATION_SELECT;
3836 DRM_DEBUG_KMS("enabling fdi C rx\n");
3837 I915_WRITE(SOUTH_CHICKEN1, temp);
3838 POSTING_READ(SOUTH_CHICKEN1);
3839}
3840
3841static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3842{
3843 struct drm_device *dev = intel_crtc->base.dev;
3844 struct drm_i915_private *dev_priv = dev->dev_private;
3845
3846 switch (intel_crtc->pipe) {
3847 case PIPE_A:
3848 break;
3849 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003850 if (intel_crtc->config->fdi_lanes > 2)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003851 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3852 else
3853 cpt_enable_fdi_bc_bifurcation(dev);
3854
3855 break;
3856 case PIPE_C:
3857 cpt_enable_fdi_bc_bifurcation(dev);
3858
3859 break;
3860 default:
3861 BUG();
3862 }
3863}
3864
Jesse Barnesf67a5592011-01-05 10:31:48 -08003865/*
3866 * Enable PCH resources required for PCH ports:
3867 * - PCH PLLs
3868 * - FDI training & RX/TX
3869 * - update transcoder timings
3870 * - DP transcoding bits
3871 * - transcoder
3872 */
3873static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003874{
3875 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003876 struct drm_i915_private *dev_priv = dev->dev_private;
3877 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3878 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003879 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003880
Daniel Vetterab9412b2013-05-03 11:49:46 +02003881 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003882
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003883 if (IS_IVYBRIDGE(dev))
3884 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3885
Daniel Vettercd986ab2012-10-26 10:58:12 +02003886 /* Write the TU size bits before fdi link training, so that error
3887 * detection works. */
3888 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3889 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3890
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003891 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003892 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003893
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003894 /* We need to program the right clock selection before writing the pixel
3895 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003896 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003897 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003898
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003899 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003900 temp |= TRANS_DPLL_ENABLE(pipe);
3901 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003902 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003903 temp |= sel;
3904 else
3905 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003906 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003907 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003908
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003909 /* XXX: pch pll's can be enabled any time before we enable the PCH
3910 * transcoder, and we actually should do this to not upset any PCH
3911 * transcoder that already use the clock when we share it.
3912 *
3913 * Note that enable_shared_dpll tries to do the right thing, but
3914 * get_shared_dpll unconditionally resets the pll - we need that to have
3915 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02003916 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003917
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003918 /* set transcoder timing, panel must allow it */
3919 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003920 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003921
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003922 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003923
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003924 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003925 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003926 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003927 reg = TRANS_DP_CTL(pipe);
3928 temp = I915_READ(reg);
3929 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003930 TRANS_DP_SYNC_MASK |
3931 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003932 temp |= (TRANS_DP_OUTPUT_ENABLE |
3933 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003934 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003935
3936 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003937 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003938 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003939 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003940
3941 switch (intel_trans_dp_port_sel(crtc)) {
3942 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003943 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003944 break;
3945 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003946 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003947 break;
3948 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003949 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003950 break;
3951 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003952 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003953 }
3954
Chris Wilson5eddb702010-09-11 13:48:45 +01003955 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003956 }
3957
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003958 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003959}
3960
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003961static void lpt_pch_enable(struct drm_crtc *crtc)
3962{
3963 struct drm_device *dev = crtc->dev;
3964 struct drm_i915_private *dev_priv = dev->dev_private;
3965 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003966 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003967
Daniel Vetterab9412b2013-05-03 11:49:46 +02003968 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003969
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003970 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003971
Paulo Zanoni0540e482012-10-31 18:12:40 -02003972 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003973 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003974
Paulo Zanoni937bb612012-10-31 18:12:47 -02003975 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003976}
3977
Daniel Vetter716c2e52014-06-25 22:02:02 +03003978void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003979{
Daniel Vettere2b78262013-06-07 23:10:03 +02003980 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003981
3982 if (pll == NULL)
3983 return;
3984
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02003985 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02003986 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003987 return;
3988 }
3989
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02003990 pll->config.crtc_mask &= ~(1 << crtc->pipe);
3991 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003992 WARN_ON(pll->on);
3993 WARN_ON(pll->active);
3994 }
3995
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003996 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003997}
3998
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02003999struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4000 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004001{
Daniel Vettere2b78262013-06-07 23:10:03 +02004002 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004003 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004004 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004005
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004006 if (HAS_PCH_IBX(dev_priv->dev)) {
4007 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004008 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004009 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004010
Daniel Vetter46edb022013-06-05 13:34:12 +02004011 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4012 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004013
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004014 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004015
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004016 goto found;
4017 }
4018
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004019 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4020 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004021
4022 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004023 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004024 continue;
4025
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004026 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004027 &pll->new_config->hw_state,
4028 sizeof(pll->new_config->hw_state)) == 0) {
4029 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004030 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004031 pll->new_config->crtc_mask,
4032 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004033 goto found;
4034 }
4035 }
4036
4037 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004038 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4039 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004040 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004041 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4042 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004043 goto found;
4044 }
4045 }
4046
4047 return NULL;
4048
4049found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004050 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004051 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004052
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004053 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004054 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4055 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004056
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004057 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004058
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004059 return pll;
4060}
4061
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004062/**
4063 * intel_shared_dpll_start_config - start a new PLL staged config
4064 * @dev_priv: DRM device
4065 * @clear_pipes: mask of pipes that will have their PLLs freed
4066 *
4067 * Starts a new PLL staged config, copying the current config but
4068 * releasing the references of pipes specified in clear_pipes.
4069 */
4070static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4071 unsigned clear_pipes)
4072{
4073 struct intel_shared_dpll *pll;
4074 enum intel_dpll_id i;
4075
4076 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4077 pll = &dev_priv->shared_dplls[i];
4078
4079 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4080 GFP_KERNEL);
4081 if (!pll->new_config)
4082 goto cleanup;
4083
4084 pll->new_config->crtc_mask &= ~clear_pipes;
4085 }
4086
4087 return 0;
4088
4089cleanup:
4090 while (--i >= 0) {
4091 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004092 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004093 pll->new_config = NULL;
4094 }
4095
4096 return -ENOMEM;
4097}
4098
4099static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4100{
4101 struct intel_shared_dpll *pll;
4102 enum intel_dpll_id i;
4103
4104 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4105 pll = &dev_priv->shared_dplls[i];
4106
4107 WARN_ON(pll->new_config == &pll->config);
4108
4109 pll->config = *pll->new_config;
4110 kfree(pll->new_config);
4111 pll->new_config = NULL;
4112 }
4113}
4114
4115static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4116{
4117 struct intel_shared_dpll *pll;
4118 enum intel_dpll_id i;
4119
4120 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4121 pll = &dev_priv->shared_dplls[i];
4122
4123 WARN_ON(pll->new_config == &pll->config);
4124
4125 kfree(pll->new_config);
4126 pll->new_config = NULL;
4127 }
4128}
4129
Daniel Vettera1520312013-05-03 11:49:50 +02004130static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004131{
4132 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004133 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004134 u32 temp;
4135
4136 temp = I915_READ(dslreg);
4137 udelay(500);
4138 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004139 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004140 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004141 }
4142}
4143
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004144static void skylake_pfit_enable(struct intel_crtc *crtc)
4145{
4146 struct drm_device *dev = crtc->base.dev;
4147 struct drm_i915_private *dev_priv = dev->dev_private;
4148 int pipe = crtc->pipe;
4149
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004150 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004151 I915_WRITE(PS_CTL(pipe), PS_ENABLE);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004152 I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4153 I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004154 }
4155}
4156
Jesse Barnesb074cec2013-04-25 12:55:02 -07004157static void ironlake_pfit_enable(struct intel_crtc *crtc)
4158{
4159 struct drm_device *dev = crtc->base.dev;
4160 struct drm_i915_private *dev_priv = dev->dev_private;
4161 int pipe = crtc->pipe;
4162
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004163 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004164 /* Force use of hard-coded filter coefficients
4165 * as some pre-programmed values are broken,
4166 * e.g. x201.
4167 */
4168 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4169 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4170 PF_PIPE_SEL_IVB(pipe));
4171 else
4172 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004173 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4174 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004175 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004176}
4177
Matt Roper4a3b8762014-12-23 10:41:51 -08004178static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004179{
4180 struct drm_device *dev = crtc->dev;
4181 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004182 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004183 struct intel_plane *intel_plane;
4184
Matt Roperaf2b6532014-04-01 15:22:32 -07004185 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4186 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004187 if (intel_plane->pipe == pipe)
4188 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004189 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004190}
4191
Matt Roper4a3b8762014-12-23 10:41:51 -08004192static void intel_disable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004193{
4194 struct drm_device *dev = crtc->dev;
4195 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004196 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004197 struct intel_plane *intel_plane;
4198
Matt Roperaf2b6532014-04-01 15:22:32 -07004199 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4200 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004201 if (intel_plane->pipe == pipe)
Matt Ropercf4c7c12014-12-04 10:27:42 -08004202 plane->funcs->disable_plane(plane);
Matt Roperaf2b6532014-04-01 15:22:32 -07004203 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004204}
4205
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004206void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004207{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004208 struct drm_device *dev = crtc->base.dev;
4209 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004210
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004211 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004212 return;
4213
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004214 /* We can only enable IPS after we enable a plane and wait for a vblank */
4215 intel_wait_for_vblank(dev, crtc->pipe);
4216
Paulo Zanonid77e4532013-09-24 13:52:55 -03004217 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004218 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004219 mutex_lock(&dev_priv->rps.hw_lock);
4220 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4221 mutex_unlock(&dev_priv->rps.hw_lock);
4222 /* Quoting Art Runyan: "its not safe to expect any particular
4223 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004224 * mailbox." Moreover, the mailbox may return a bogus state,
4225 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004226 */
4227 } else {
4228 I915_WRITE(IPS_CTL, IPS_ENABLE);
4229 /* The bit only becomes 1 in the next vblank, so this wait here
4230 * is essentially intel_wait_for_vblank. If we don't have this
4231 * and don't wait for vblanks until the end of crtc_enable, then
4232 * the HW state readout code will complain that the expected
4233 * IPS_CTL value is not the one we read. */
4234 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4235 DRM_ERROR("Timed out waiting for IPS enable\n");
4236 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004237}
4238
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004239void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004240{
4241 struct drm_device *dev = crtc->base.dev;
4242 struct drm_i915_private *dev_priv = dev->dev_private;
4243
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004244 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004245 return;
4246
4247 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004248 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004249 mutex_lock(&dev_priv->rps.hw_lock);
4250 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4251 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004252 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4253 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4254 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004255 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004256 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004257 POSTING_READ(IPS_CTL);
4258 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004259
4260 /* We need to wait for a vblank before we can disable the plane. */
4261 intel_wait_for_vblank(dev, crtc->pipe);
4262}
4263
4264/** Loads the palette/gamma unit for the CRTC with the prepared values */
4265static void intel_crtc_load_lut(struct drm_crtc *crtc)
4266{
4267 struct drm_device *dev = crtc->dev;
4268 struct drm_i915_private *dev_priv = dev->dev_private;
4269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4270 enum pipe pipe = intel_crtc->pipe;
4271 int palreg = PALETTE(pipe);
4272 int i;
4273 bool reenable_ips = false;
4274
4275 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004276 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004277 return;
4278
4279 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004280 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004281 assert_dsi_pll_enabled(dev_priv);
4282 else
4283 assert_pll_enabled(dev_priv, pipe);
4284 }
4285
4286 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304287 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004288 palreg = LGC_PALETTE(pipe);
4289
4290 /* Workaround : Do not read or write the pipe palette/gamma data while
4291 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4292 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004293 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004294 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4295 GAMMA_MODE_MODE_SPLIT)) {
4296 hsw_disable_ips(intel_crtc);
4297 reenable_ips = true;
4298 }
4299
4300 for (i = 0; i < 256; i++) {
4301 I915_WRITE(palreg + 4 * i,
4302 (intel_crtc->lut_r[i] << 16) |
4303 (intel_crtc->lut_g[i] << 8) |
4304 intel_crtc->lut_b[i]);
4305 }
4306
4307 if (reenable_ips)
4308 hsw_enable_ips(intel_crtc);
4309}
4310
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004311static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4312{
4313 if (!enable && intel_crtc->overlay) {
4314 struct drm_device *dev = intel_crtc->base.dev;
4315 struct drm_i915_private *dev_priv = dev->dev_private;
4316
4317 mutex_lock(&dev->struct_mutex);
4318 dev_priv->mm.interruptible = false;
4319 (void) intel_overlay_switch_off(intel_crtc->overlay);
4320 dev_priv->mm.interruptible = true;
4321 mutex_unlock(&dev->struct_mutex);
4322 }
4323
4324 /* Let userspace switch the overlay on again. In most cases userspace
4325 * has to recompute where to put it anyway.
4326 */
4327}
4328
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004329static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004330{
4331 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004332 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4333 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004334
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03004335 intel_enable_primary_hw_plane(crtc->primary, crtc);
Matt Roper4a3b8762014-12-23 10:41:51 -08004336 intel_enable_sprite_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004337 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004338 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004339
4340 hsw_enable_ips(intel_crtc);
4341
4342 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004343 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004344 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004345
4346 /*
4347 * FIXME: Once we grow proper nuclear flip support out of this we need
4348 * to compute the mask of flip planes precisely. For the time being
4349 * consider this a flip from a NULL plane.
4350 */
4351 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004352}
4353
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004354static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004355{
4356 struct drm_device *dev = crtc->dev;
4357 struct drm_i915_private *dev_priv = dev->dev_private;
4358 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4359 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004360
4361 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004362
Paulo Zanonie35fef22015-02-09 14:46:29 -02004363 if (dev_priv->fbc.crtc == intel_crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004364 intel_fbc_disable(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004365
4366 hsw_disable_ips(intel_crtc);
4367
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004368 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004369 intel_crtc_update_cursor(crtc, false);
Matt Roper4a3b8762014-12-23 10:41:51 -08004370 intel_disable_sprite_planes(crtc);
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03004371 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004372
Daniel Vetterf99d7062014-06-19 16:01:59 +02004373 /*
4374 * FIXME: Once we grow proper nuclear flip support out of this we need
4375 * to compute the mask of flip planes precisely. For the time being
4376 * consider this a flip to a NULL plane.
4377 */
4378 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004379}
4380
Jesse Barnesf67a5592011-01-05 10:31:48 -08004381static void ironlake_crtc_enable(struct drm_crtc *crtc)
4382{
4383 struct drm_device *dev = crtc->dev;
4384 struct drm_i915_private *dev_priv = dev->dev_private;
4385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004386 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004387 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004388
Matt Roper83d65732015-02-25 13:12:16 -08004389 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004390
Jesse Barnesf67a5592011-01-05 10:31:48 -08004391 if (intel_crtc->active)
4392 return;
4393
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004394 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004395 intel_prepare_shared_dpll(intel_crtc);
4396
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004397 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304398 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004399
4400 intel_set_pipe_timings(intel_crtc);
4401
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004402 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004403 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004404 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004405 }
4406
4407 ironlake_set_pipeconf(crtc);
4408
Jesse Barnesf67a5592011-01-05 10:31:48 -08004409 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004410
Daniel Vettera72e4c92014-09-30 10:56:47 +02004411 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4412 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004413
Daniel Vetterf6736a12013-06-05 13:34:30 +02004414 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004415 if (encoder->pre_enable)
4416 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004417
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004418 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004419 /* Note: FDI PLL enabling _must_ be done before we enable the
4420 * cpu pipes, hence this is separate from all the other fdi/pch
4421 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004422 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004423 } else {
4424 assert_fdi_tx_disabled(dev_priv, pipe);
4425 assert_fdi_rx_disabled(dev_priv, pipe);
4426 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004427
Jesse Barnesb074cec2013-04-25 12:55:02 -07004428 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004429
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004430 /*
4431 * On ILK+ LUT must be loaded before the pipe is running but with
4432 * clocks enabled
4433 */
4434 intel_crtc_load_lut(crtc);
4435
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004436 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004437 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004438
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004439 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004440 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004441
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004442 assert_vblank_disabled(crtc);
4443 drm_crtc_vblank_on(crtc);
4444
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004445 for_each_encoder_on_crtc(dev, crtc, encoder)
4446 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004447
4448 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004449 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004450
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004451 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004452}
4453
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004454/* IPS only exists on ULT machines and is tied to pipe A. */
4455static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4456{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004457 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004458}
4459
Paulo Zanonie4916942013-09-20 16:21:19 -03004460/*
4461 * This implements the workaround described in the "notes" section of the mode
4462 * set sequence documentation. When going from no pipes or single pipe to
4463 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4464 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4465 */
4466static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4467{
4468 struct drm_device *dev = crtc->base.dev;
4469 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4470
4471 /* We want to get the other_active_crtc only if there's only 1 other
4472 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004473 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004474 if (!crtc_it->active || crtc_it == crtc)
4475 continue;
4476
4477 if (other_active_crtc)
4478 return;
4479
4480 other_active_crtc = crtc_it;
4481 }
4482 if (!other_active_crtc)
4483 return;
4484
4485 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4486 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4487}
4488
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004489static void haswell_crtc_enable(struct drm_crtc *crtc)
4490{
4491 struct drm_device *dev = crtc->dev;
4492 struct drm_i915_private *dev_priv = dev->dev_private;
4493 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4494 struct intel_encoder *encoder;
4495 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004496
Matt Roper83d65732015-02-25 13:12:16 -08004497 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004498
4499 if (intel_crtc->active)
4500 return;
4501
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004502 if (intel_crtc_to_shared_dpll(intel_crtc))
4503 intel_enable_shared_dpll(intel_crtc);
4504
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004505 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304506 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004507
4508 intel_set_pipe_timings(intel_crtc);
4509
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004510 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4511 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4512 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004513 }
4514
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004515 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004516 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004517 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004518 }
4519
4520 haswell_set_pipeconf(crtc);
4521
4522 intel_set_pipe_csc(crtc);
4523
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004524 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004525
Daniel Vettera72e4c92014-09-30 10:56:47 +02004526 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004527 for_each_encoder_on_crtc(dev, crtc, encoder)
4528 if (encoder->pre_enable)
4529 encoder->pre_enable(encoder);
4530
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004531 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004532 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4533 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004534 dev_priv->display.fdi_link_train(crtc);
4535 }
4536
Paulo Zanoni1f544382012-10-24 11:32:00 -02004537 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004538
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004539 if (IS_SKYLAKE(dev))
4540 skylake_pfit_enable(intel_crtc);
4541 else
4542 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004543
4544 /*
4545 * On ILK+ LUT must be loaded before the pipe is running but with
4546 * clocks enabled
4547 */
4548 intel_crtc_load_lut(crtc);
4549
Paulo Zanoni1f544382012-10-24 11:32:00 -02004550 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004551 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004552
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004553 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004554 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004555
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004556 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004557 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004558
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004559 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004560 intel_ddi_set_vc_payload_alloc(crtc, true);
4561
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004562 assert_vblank_disabled(crtc);
4563 drm_crtc_vblank_on(crtc);
4564
Jani Nikula8807e552013-08-30 19:40:32 +03004565 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004566 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004567 intel_opregion_notify_encoder(encoder, true);
4568 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004569
Paulo Zanonie4916942013-09-20 16:21:19 -03004570 /* If we change the relative order between pipe/planes enabling, we need
4571 * to change the workaround. */
4572 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004573 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004574}
4575
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004576static void skylake_pfit_disable(struct intel_crtc *crtc)
4577{
4578 struct drm_device *dev = crtc->base.dev;
4579 struct drm_i915_private *dev_priv = dev->dev_private;
4580 int pipe = crtc->pipe;
4581
4582 /* To avoid upsetting the power well on haswell only disable the pfit if
4583 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004584 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004585 I915_WRITE(PS_CTL(pipe), 0);
4586 I915_WRITE(PS_WIN_POS(pipe), 0);
4587 I915_WRITE(PS_WIN_SZ(pipe), 0);
4588 }
4589}
4590
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004591static void ironlake_pfit_disable(struct intel_crtc *crtc)
4592{
4593 struct drm_device *dev = crtc->base.dev;
4594 struct drm_i915_private *dev_priv = dev->dev_private;
4595 int pipe = crtc->pipe;
4596
4597 /* To avoid upsetting the power well on haswell only disable the pfit if
4598 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004599 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004600 I915_WRITE(PF_CTL(pipe), 0);
4601 I915_WRITE(PF_WIN_POS(pipe), 0);
4602 I915_WRITE(PF_WIN_SZ(pipe), 0);
4603 }
4604}
4605
Jesse Barnes6be4a602010-09-10 10:26:01 -07004606static void ironlake_crtc_disable(struct drm_crtc *crtc)
4607{
4608 struct drm_device *dev = crtc->dev;
4609 struct drm_i915_private *dev_priv = dev->dev_private;
4610 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004611 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004612 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004613 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004614
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004615 if (!intel_crtc->active)
4616 return;
4617
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004618 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004619
Daniel Vetterea9d7582012-07-10 10:42:52 +02004620 for_each_encoder_on_crtc(dev, crtc, encoder)
4621 encoder->disable(encoder);
4622
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004623 drm_crtc_vblank_off(crtc);
4624 assert_vblank_disabled(crtc);
4625
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004626 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004627 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004628
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004629 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004630
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004631 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004632
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004633 for_each_encoder_on_crtc(dev, crtc, encoder)
4634 if (encoder->post_disable)
4635 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004636
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004637 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004638 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004639
Daniel Vetterd925c592013-06-05 13:34:04 +02004640 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004641
Daniel Vetterd925c592013-06-05 13:34:04 +02004642 if (HAS_PCH_CPT(dev)) {
4643 /* disable TRANS_DP_CTL */
4644 reg = TRANS_DP_CTL(pipe);
4645 temp = I915_READ(reg);
4646 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4647 TRANS_DP_PORT_SEL_MASK);
4648 temp |= TRANS_DP_PORT_SEL_NONE;
4649 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004650
Daniel Vetterd925c592013-06-05 13:34:04 +02004651 /* disable DPLL_SEL */
4652 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004653 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004654 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004655 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004656
4657 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004658 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004659
4660 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004661 }
4662
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004663 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004664 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004665
4666 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004667 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004668 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004669}
4670
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004671static void haswell_crtc_disable(struct drm_crtc *crtc)
4672{
4673 struct drm_device *dev = crtc->dev;
4674 struct drm_i915_private *dev_priv = dev->dev_private;
4675 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4676 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004677 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004678
4679 if (!intel_crtc->active)
4680 return;
4681
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004682 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004683
Jani Nikula8807e552013-08-30 19:40:32 +03004684 for_each_encoder_on_crtc(dev, crtc, encoder) {
4685 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004686 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004687 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004688
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004689 drm_crtc_vblank_off(crtc);
4690 assert_vblank_disabled(crtc);
4691
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004692 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004693 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4694 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004695 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004696
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004697 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03004698 intel_ddi_set_vc_payload_alloc(crtc, false);
4699
Paulo Zanoniad80a812012-10-24 16:06:19 -02004700 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004701
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004702 if (IS_SKYLAKE(dev))
4703 skylake_pfit_disable(intel_crtc);
4704 else
4705 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004706
Paulo Zanoni1f544382012-10-24 11:32:00 -02004707 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004708
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004709 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004710 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004711 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004712 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004713
Imre Deak97b040a2014-06-25 22:01:50 +03004714 for_each_encoder_on_crtc(dev, crtc, encoder)
4715 if (encoder->post_disable)
4716 encoder->post_disable(encoder);
4717
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004718 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004719 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004720
4721 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004722 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004723 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004724
4725 if (intel_crtc_to_shared_dpll(intel_crtc))
4726 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004727}
4728
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004729static void ironlake_crtc_off(struct drm_crtc *crtc)
4730{
4731 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004732 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004733}
4734
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004735
Jesse Barnes2dd24552013-04-25 12:55:01 -07004736static void i9xx_pfit_enable(struct intel_crtc *crtc)
4737{
4738 struct drm_device *dev = crtc->base.dev;
4739 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004740 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07004741
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02004742 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004743 return;
4744
Daniel Vetterc0b03412013-05-28 12:05:54 +02004745 /*
4746 * The panel fitter should only be adjusted whilst the pipe is disabled,
4747 * according to register description and PRM.
4748 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004749 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4750 assert_pipe_disabled(dev_priv, crtc->pipe);
4751
Jesse Barnesb074cec2013-04-25 12:55:02 -07004752 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4753 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004754
4755 /* Border color in case we don't scale up to the full screen. Black by
4756 * default, change to something else for debugging. */
4757 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004758}
4759
Dave Airlied05410f2014-06-05 13:22:59 +10004760static enum intel_display_power_domain port_to_power_domain(enum port port)
4761{
4762 switch (port) {
4763 case PORT_A:
4764 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4765 case PORT_B:
4766 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4767 case PORT_C:
4768 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4769 case PORT_D:
4770 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4771 default:
4772 WARN_ON_ONCE(1);
4773 return POWER_DOMAIN_PORT_OTHER;
4774 }
4775}
4776
Imre Deak77d22dc2014-03-05 16:20:52 +02004777#define for_each_power_domain(domain, mask) \
4778 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4779 if ((1 << (domain)) & (mask))
4780
Imre Deak319be8a2014-03-04 19:22:57 +02004781enum intel_display_power_domain
4782intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004783{
Imre Deak319be8a2014-03-04 19:22:57 +02004784 struct drm_device *dev = intel_encoder->base.dev;
4785 struct intel_digital_port *intel_dig_port;
4786
4787 switch (intel_encoder->type) {
4788 case INTEL_OUTPUT_UNKNOWN:
4789 /* Only DDI platforms should ever use this output type */
4790 WARN_ON_ONCE(!HAS_DDI(dev));
4791 case INTEL_OUTPUT_DISPLAYPORT:
4792 case INTEL_OUTPUT_HDMI:
4793 case INTEL_OUTPUT_EDP:
4794 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10004795 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10004796 case INTEL_OUTPUT_DP_MST:
4797 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4798 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02004799 case INTEL_OUTPUT_ANALOG:
4800 return POWER_DOMAIN_PORT_CRT;
4801 case INTEL_OUTPUT_DSI:
4802 return POWER_DOMAIN_PORT_DSI;
4803 default:
4804 return POWER_DOMAIN_PORT_OTHER;
4805 }
4806}
4807
4808static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4809{
4810 struct drm_device *dev = crtc->dev;
4811 struct intel_encoder *intel_encoder;
4812 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4813 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02004814 unsigned long mask;
4815 enum transcoder transcoder;
4816
4817 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4818
4819 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4820 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004821 if (intel_crtc->config->pch_pfit.enabled ||
4822 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02004823 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4824
Imre Deak319be8a2014-03-04 19:22:57 +02004825 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4826 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4827
Imre Deak77d22dc2014-03-05 16:20:52 +02004828 return mask;
4829}
4830
Imre Deak77d22dc2014-03-05 16:20:52 +02004831static void modeset_update_crtc_power_domains(struct drm_device *dev)
4832{
4833 struct drm_i915_private *dev_priv = dev->dev_private;
4834 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4835 struct intel_crtc *crtc;
4836
4837 /*
4838 * First get all needed power domains, then put all unneeded, to avoid
4839 * any unnecessary toggling of the power wells.
4840 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004841 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004842 enum intel_display_power_domain domain;
4843
Matt Roper83d65732015-02-25 13:12:16 -08004844 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02004845 continue;
4846
Imre Deak319be8a2014-03-04 19:22:57 +02004847 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02004848
4849 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4850 intel_display_power_get(dev_priv, domain);
4851 }
4852
Ville Syrjälä50f6e502014-11-06 14:49:12 +02004853 if (dev_priv->display.modeset_global_resources)
4854 dev_priv->display.modeset_global_resources(dev);
4855
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004856 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004857 enum intel_display_power_domain domain;
4858
4859 for_each_power_domain(domain, crtc->enabled_power_domains)
4860 intel_display_power_put(dev_priv, domain);
4861
4862 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4863 }
4864
4865 intel_display_set_init_power(dev_priv, false);
4866}
4867
Ville Syrjälädfcab172014-06-13 13:37:47 +03004868/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004869static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004870{
Jesse Barnes586f49d2013-11-04 16:06:59 -08004871 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08004872
Jesse Barnes586f49d2013-11-04 16:06:59 -08004873 /* Obtain SKU information */
4874 mutex_lock(&dev_priv->dpio_lock);
4875 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4876 CCK_FUSE_HPLL_FREQ_MASK;
4877 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004878
Ville Syrjälädfcab172014-06-13 13:37:47 +03004879 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004880}
4881
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004882static void vlv_update_cdclk(struct drm_device *dev)
4883{
4884 struct drm_i915_private *dev_priv = dev->dev_private;
4885
4886 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03004887 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004888 dev_priv->vlv_cdclk_freq);
4889
4890 /*
4891 * Program the gmbus_freq based on the cdclk frequency.
4892 * BSpec erroneously claims we should aim for 4MHz, but
4893 * in fact 1MHz is the correct frequency.
4894 */
Ville Syrjälä6be1e3d2014-10-16 20:52:31 +03004895 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004896}
4897
Jesse Barnes30a970c2013-11-04 13:48:12 -08004898/* Adjust CDclk dividers to allow high res or save power if possible */
4899static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4900{
4901 struct drm_i915_private *dev_priv = dev->dev_private;
4902 u32 val, cmd;
4903
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03004904 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02004905
Ville Syrjälädfcab172014-06-13 13:37:47 +03004906 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08004907 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03004908 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004909 cmd = 1;
4910 else
4911 cmd = 0;
4912
4913 mutex_lock(&dev_priv->rps.hw_lock);
4914 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4915 val &= ~DSPFREQGUAR_MASK;
4916 val |= (cmd << DSPFREQGUAR_SHIFT);
4917 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4918 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4919 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4920 50)) {
4921 DRM_ERROR("timed out waiting for CDclk change\n");
4922 }
4923 mutex_unlock(&dev_priv->rps.hw_lock);
4924
Ville Syrjälädfcab172014-06-13 13:37:47 +03004925 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03004926 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004927
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03004928 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004929
4930 mutex_lock(&dev_priv->dpio_lock);
4931 /* adjust cdclk divider */
4932 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03004933 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004934 val |= divider;
4935 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03004936
4937 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4938 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4939 50))
4940 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08004941 mutex_unlock(&dev_priv->dpio_lock);
4942 }
4943
4944 mutex_lock(&dev_priv->dpio_lock);
4945 /* adjust self-refresh exit latency value */
4946 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4947 val &= ~0x7f;
4948
4949 /*
4950 * For high bandwidth configs, we set a higher latency in the bunit
4951 * so that the core display fetch happens in time to avoid underruns.
4952 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03004953 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004954 val |= 4500 / 250; /* 4.5 usec */
4955 else
4956 val |= 3000 / 250; /* 3.0 usec */
4957 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4958 mutex_unlock(&dev_priv->dpio_lock);
4959
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004960 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004961}
4962
Ville Syrjälä383c5a62014-06-28 02:03:57 +03004963static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4964{
4965 struct drm_i915_private *dev_priv = dev->dev_private;
4966 u32 val, cmd;
4967
4968 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4969
4970 switch (cdclk) {
4971 case 400000:
4972 cmd = 3;
4973 break;
4974 case 333333:
4975 case 320000:
4976 cmd = 2;
4977 break;
4978 case 266667:
4979 cmd = 1;
4980 break;
4981 case 200000:
4982 cmd = 0;
4983 break;
4984 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01004985 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03004986 return;
4987 }
4988
4989 mutex_lock(&dev_priv->rps.hw_lock);
4990 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4991 val &= ~DSPFREQGUAR_MASK_CHV;
4992 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4993 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4994 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4995 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4996 50)) {
4997 DRM_ERROR("timed out waiting for CDclk change\n");
4998 }
4999 mutex_unlock(&dev_priv->rps.hw_lock);
5000
5001 vlv_update_cdclk(dev);
5002}
5003
Jesse Barnes30a970c2013-11-04 13:48:12 -08005004static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5005 int max_pixclk)
5006{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005007 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005008
Ville Syrjäläd49a3402014-06-28 02:03:58 +03005009 /* FIXME: Punit isn't quite ready yet */
5010 if (IS_CHERRYVIEW(dev_priv->dev))
5011 return 400000;
5012
Jesse Barnes30a970c2013-11-04 13:48:12 -08005013 /*
5014 * Really only a few cases to deal with, as only 4 CDclks are supported:
5015 * 200MHz
5016 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005017 * 320/333MHz (depends on HPLL freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005018 * 400MHz
5019 * So we check to see whether we're above 90% of the lower bin and
5020 * adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005021 *
5022 * We seem to get an unstable or solid color picture at 200MHz.
5023 * Not sure what's wrong. For now use 200MHz only when all pipes
5024 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005025 */
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005026 if (max_pixclk > freq_320*9/10)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005027 return 400000;
5028 else if (max_pixclk > 266667*9/10)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005029 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005030 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005031 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005032 else
5033 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005034}
5035
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005036/* compute the max pixel clock for new configuration */
5037static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005038{
5039 struct drm_device *dev = dev_priv->dev;
5040 struct intel_crtc *intel_crtc;
5041 int max_pixclk = 0;
5042
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005043 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005044 if (intel_crtc->new_enabled)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005045 max_pixclk = max(max_pixclk,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005046 intel_crtc->new_config->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005047 }
5048
5049 return max_pixclk;
5050}
5051
5052static void valleyview_modeset_global_pipes(struct drm_device *dev,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005053 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005054{
5055 struct drm_i915_private *dev_priv = dev->dev_private;
5056 struct intel_crtc *intel_crtc;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005057 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005058
Imre Deakd60c4472014-03-27 17:45:10 +02005059 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
5060 dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005061 return;
5062
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005063 /* disable/enable all currently active pipes while we change cdclk */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005064 for_each_intel_crtc(dev, intel_crtc)
Matt Roper83d65732015-02-25 13:12:16 -08005065 if (intel_crtc->base.state->enable)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005066 *prepare_pipes |= (1 << intel_crtc->pipe);
5067}
5068
5069static void valleyview_modeset_global_resources(struct drm_device *dev)
5070{
5071 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005072 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005073 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5074
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005075 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005076 /*
5077 * FIXME: We can end up here with all power domains off, yet
5078 * with a CDCLK frequency other than the minimum. To account
5079 * for this take the PIPE-A power domain, which covers the HW
5080 * blocks needed for the following programming. This can be
5081 * removed once it's guaranteed that we get here either with
5082 * the minimum CDCLK set, or the required power domains
5083 * enabled.
5084 */
5085 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5086
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005087 if (IS_CHERRYVIEW(dev))
5088 cherryview_set_cdclk(dev, req_cdclk);
5089 else
5090 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005091
5092 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005093 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005094}
5095
Jesse Barnes89b667f2013-04-18 14:51:36 -07005096static void valleyview_crtc_enable(struct drm_crtc *crtc)
5097{
5098 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005099 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005100 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5101 struct intel_encoder *encoder;
5102 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005103 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005104
Matt Roper83d65732015-02-25 13:12:16 -08005105 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005106
5107 if (intel_crtc->active)
5108 return;
5109
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005110 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305111
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005112 if (!is_dsi) {
5113 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005114 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005115 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005116 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005117 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005118
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005119 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305120 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005121
5122 intel_set_pipe_timings(intel_crtc);
5123
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005124 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5125 struct drm_i915_private *dev_priv = dev->dev_private;
5126
5127 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5128 I915_WRITE(CHV_CANVAS(pipe), 0);
5129 }
5130
Daniel Vetter5b18e572014-04-24 23:55:06 +02005131 i9xx_set_pipeconf(intel_crtc);
5132
Jesse Barnes89b667f2013-04-18 14:51:36 -07005133 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005134
Daniel Vettera72e4c92014-09-30 10:56:47 +02005135 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005136
Jesse Barnes89b667f2013-04-18 14:51:36 -07005137 for_each_encoder_on_crtc(dev, crtc, encoder)
5138 if (encoder->pre_pll_enable)
5139 encoder->pre_pll_enable(encoder);
5140
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005141 if (!is_dsi) {
5142 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005143 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005144 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005145 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005146 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005147
5148 for_each_encoder_on_crtc(dev, crtc, encoder)
5149 if (encoder->pre_enable)
5150 encoder->pre_enable(encoder);
5151
Jesse Barnes2dd24552013-04-25 12:55:01 -07005152 i9xx_pfit_enable(intel_crtc);
5153
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005154 intel_crtc_load_lut(crtc);
5155
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005156 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005157 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005158
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005159 assert_vblank_disabled(crtc);
5160 drm_crtc_vblank_on(crtc);
5161
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005162 for_each_encoder_on_crtc(dev, crtc, encoder)
5163 encoder->enable(encoder);
5164
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005165 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005166
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005167 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005168 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005169}
5170
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005171static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5172{
5173 struct drm_device *dev = crtc->base.dev;
5174 struct drm_i915_private *dev_priv = dev->dev_private;
5175
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005176 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5177 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005178}
5179
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005180static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005181{
5182 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005183 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005184 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005185 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005186 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005187
Matt Roper83d65732015-02-25 13:12:16 -08005188 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005189
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005190 if (intel_crtc->active)
5191 return;
5192
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005193 i9xx_set_pll_dividers(intel_crtc);
5194
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005195 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305196 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005197
5198 intel_set_pipe_timings(intel_crtc);
5199
Daniel Vetter5b18e572014-04-24 23:55:06 +02005200 i9xx_set_pipeconf(intel_crtc);
5201
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005202 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005203
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005204 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005205 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005206
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005207 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005208 if (encoder->pre_enable)
5209 encoder->pre_enable(encoder);
5210
Daniel Vetterf6736a12013-06-05 13:34:30 +02005211 i9xx_enable_pll(intel_crtc);
5212
Jesse Barnes2dd24552013-04-25 12:55:01 -07005213 i9xx_pfit_enable(intel_crtc);
5214
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005215 intel_crtc_load_lut(crtc);
5216
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005217 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005218 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005219
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005220 assert_vblank_disabled(crtc);
5221 drm_crtc_vblank_on(crtc);
5222
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005223 for_each_encoder_on_crtc(dev, crtc, encoder)
5224 encoder->enable(encoder);
5225
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005226 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005227
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005228 /*
5229 * Gen2 reports pipe underruns whenever all planes are disabled.
5230 * So don't enable underrun reporting before at least some planes
5231 * are enabled.
5232 * FIXME: Need to fix the logic to work when we turn off all planes
5233 * but leave the pipe running.
5234 */
5235 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005236 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005237
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005238 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005239 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005240}
5241
Daniel Vetter87476d62013-04-11 16:29:06 +02005242static void i9xx_pfit_disable(struct intel_crtc *crtc)
5243{
5244 struct drm_device *dev = crtc->base.dev;
5245 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005246
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005247 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005248 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005249
5250 assert_pipe_disabled(dev_priv, crtc->pipe);
5251
Daniel Vetter328d8e82013-05-08 10:36:31 +02005252 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5253 I915_READ(PFIT_CONTROL));
5254 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005255}
5256
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005257static void i9xx_crtc_disable(struct drm_crtc *crtc)
5258{
5259 struct drm_device *dev = crtc->dev;
5260 struct drm_i915_private *dev_priv = dev->dev_private;
5261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005262 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005263 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005264
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005265 if (!intel_crtc->active)
5266 return;
5267
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005268 /*
5269 * Gen2 reports pipe underruns whenever all planes are disabled.
5270 * So diasble underrun reporting before all the planes get disabled.
5271 * FIXME: Need to fix the logic to work when we turn off all planes
5272 * but leave the pipe running.
5273 */
5274 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005275 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005276
Imre Deak564ed192014-06-13 14:54:21 +03005277 /*
5278 * Vblank time updates from the shadow to live plane control register
5279 * are blocked if the memory self-refresh mode is active at that
5280 * moment. So to make sure the plane gets truly disabled, disable
5281 * first the self-refresh mode. The self-refresh enable bit in turn
5282 * will be checked/applied by the HW only at the next frame start
5283 * event which is after the vblank start event, so we need to have a
5284 * wait-for-vblank between disabling the plane and the pipe.
5285 */
5286 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005287 intel_crtc_disable_planes(crtc);
5288
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005289 /*
5290 * On gen2 planes are double buffered but the pipe isn't, so we must
5291 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03005292 * We also need to wait on all gmch platforms because of the
5293 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005294 */
Imre Deak564ed192014-06-13 14:54:21 +03005295 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005296
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005297 for_each_encoder_on_crtc(dev, crtc, encoder)
5298 encoder->disable(encoder);
5299
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005300 drm_crtc_vblank_off(crtc);
5301 assert_vblank_disabled(crtc);
5302
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005303 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005304
Daniel Vetter87476d62013-04-11 16:29:06 +02005305 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005306
Jesse Barnes89b667f2013-04-18 14:51:36 -07005307 for_each_encoder_on_crtc(dev, crtc, encoder)
5308 if (encoder->post_disable)
5309 encoder->post_disable(encoder);
5310
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005311 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005312 if (IS_CHERRYVIEW(dev))
5313 chv_disable_pll(dev_priv, pipe);
5314 else if (IS_VALLEYVIEW(dev))
5315 vlv_disable_pll(dev_priv, pipe);
5316 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005317 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005318 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005319
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005320 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005321 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005322
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005323 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005324 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005325
Daniel Vetterefa96242014-04-24 23:55:02 +02005326 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005327 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02005328 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005329}
5330
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005331static void i9xx_crtc_off(struct drm_crtc *crtc)
5332{
5333}
5334
Borun Fub04c5bd2014-07-12 10:02:27 +05305335/* Master function to enable/disable CRTC and corresponding power wells */
5336void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01005337{
Chris Wilsoncdd59982010-09-08 16:30:16 +01005338 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005339 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005341 enum intel_display_power_domain domain;
5342 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005343
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005344 if (enable) {
5345 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005346 domains = get_crtc_power_domains(crtc);
5347 for_each_power_domain(domain, domains)
5348 intel_display_power_get(dev_priv, domain);
5349 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005350
5351 dev_priv->display.crtc_enable(crtc);
5352 }
5353 } else {
5354 if (intel_crtc->active) {
5355 dev_priv->display.crtc_disable(crtc);
5356
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005357 domains = intel_crtc->enabled_power_domains;
5358 for_each_power_domain(domain, domains)
5359 intel_display_power_put(dev_priv, domain);
5360 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005361 }
5362 }
Borun Fub04c5bd2014-07-12 10:02:27 +05305363}
5364
5365/**
5366 * Sets the power management mode of the pipe and plane.
5367 */
5368void intel_crtc_update_dpms(struct drm_crtc *crtc)
5369{
5370 struct drm_device *dev = crtc->dev;
5371 struct intel_encoder *intel_encoder;
5372 bool enable = false;
5373
5374 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5375 enable |= intel_encoder->connectors_active;
5376
5377 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005378}
5379
Daniel Vetter976f8a22012-07-08 22:34:21 +02005380static void intel_crtc_disable(struct drm_crtc *crtc)
5381{
5382 struct drm_device *dev = crtc->dev;
5383 struct drm_connector *connector;
5384 struct drm_i915_private *dev_priv = dev->dev_private;
5385
5386 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08005387 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005388
5389 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005390 dev_priv->display.off(crtc);
5391
Gustavo Padovan455a6802014-12-01 15:40:11 -08005392 crtc->primary->funcs->disable_plane(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005393
5394 /* Update computed state. */
5395 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5396 if (!connector->encoder || !connector->encoder->crtc)
5397 continue;
5398
5399 if (connector->encoder->crtc != crtc)
5400 continue;
5401
5402 connector->dpms = DRM_MODE_DPMS_OFF;
5403 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01005404 }
5405}
5406
Chris Wilsonea5b2132010-08-04 13:50:23 +01005407void intel_encoder_destroy(struct drm_encoder *encoder)
5408{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005409 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01005410
Chris Wilsonea5b2132010-08-04 13:50:23 +01005411 drm_encoder_cleanup(encoder);
5412 kfree(intel_encoder);
5413}
5414
Damien Lespiau92373292013-08-08 22:28:57 +01005415/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005416 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5417 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01005418static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005419{
5420 if (mode == DRM_MODE_DPMS_ON) {
5421 encoder->connectors_active = true;
5422
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005423 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005424 } else {
5425 encoder->connectors_active = false;
5426
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005427 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005428 }
5429}
5430
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005431/* Cross check the actual hw state with our own modeset state tracking (and it's
5432 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02005433static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005434{
5435 if (connector->get_hw_state(connector)) {
5436 struct intel_encoder *encoder = connector->encoder;
5437 struct drm_crtc *crtc;
5438 bool encoder_enabled;
5439 enum pipe pipe;
5440
5441 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5442 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03005443 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005444
Dave Airlie0e32b392014-05-02 14:02:48 +10005445 /* there is no real hw state for MST connectors */
5446 if (connector->mst_port)
5447 return;
5448
Rob Clarke2c719b2014-12-15 13:56:32 -05005449 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005450 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05005451 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005452 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005453
Dave Airlie36cd7442014-05-02 13:44:18 +10005454 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05005455 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10005456 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005457
Dave Airlie36cd7442014-05-02 13:44:18 +10005458 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05005459 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
5460 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10005461 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005462
Dave Airlie36cd7442014-05-02 13:44:18 +10005463 crtc = encoder->base.crtc;
5464
Matt Roper83d65732015-02-25 13:12:16 -08005465 I915_STATE_WARN(!crtc->state->enable,
5466 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05005467 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5468 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10005469 "encoder active on the wrong pipe\n");
5470 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005471 }
5472}
5473
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005474/* Even simpler default implementation, if there's really no special case to
5475 * consider. */
5476void intel_connector_dpms(struct drm_connector *connector, int mode)
5477{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005478 /* All the simple cases only support two dpms states. */
5479 if (mode != DRM_MODE_DPMS_ON)
5480 mode = DRM_MODE_DPMS_OFF;
5481
5482 if (mode == connector->dpms)
5483 return;
5484
5485 connector->dpms = mode;
5486
5487 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01005488 if (connector->encoder)
5489 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005490
Daniel Vetterb9805142012-08-31 17:37:33 +02005491 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005492}
5493
Daniel Vetterf0947c32012-07-02 13:10:34 +02005494/* Simple connector->get_hw_state implementation for encoders that support only
5495 * one connector and no cloning and hence the encoder state determines the state
5496 * of the connector. */
5497bool intel_connector_get_hw_state(struct intel_connector *connector)
5498{
Daniel Vetter24929352012-07-02 20:28:59 +02005499 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005500 struct intel_encoder *encoder = connector->encoder;
5501
5502 return encoder->get_hw_state(encoder, &pipe);
5503}
5504
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005505static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005506 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005507{
5508 struct drm_i915_private *dev_priv = dev->dev_private;
5509 struct intel_crtc *pipe_B_crtc =
5510 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5511
5512 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5513 pipe_name(pipe), pipe_config->fdi_lanes);
5514 if (pipe_config->fdi_lanes > 4) {
5515 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5516 pipe_name(pipe), pipe_config->fdi_lanes);
5517 return false;
5518 }
5519
Paulo Zanonibafb6552013-11-02 21:07:44 -07005520 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005521 if (pipe_config->fdi_lanes > 2) {
5522 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5523 pipe_config->fdi_lanes);
5524 return false;
5525 } else {
5526 return true;
5527 }
5528 }
5529
5530 if (INTEL_INFO(dev)->num_pipes == 2)
5531 return true;
5532
5533 /* Ivybridge 3 pipe is really complicated */
5534 switch (pipe) {
5535 case PIPE_A:
5536 return true;
5537 case PIPE_B:
5538 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5539 pipe_config->fdi_lanes > 2) {
5540 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5541 pipe_name(pipe), pipe_config->fdi_lanes);
5542 return false;
5543 }
5544 return true;
5545 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01005546 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005547 pipe_B_crtc->config->fdi_lanes <= 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005548 if (pipe_config->fdi_lanes > 2) {
5549 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5550 pipe_name(pipe), pipe_config->fdi_lanes);
5551 return false;
5552 }
5553 } else {
5554 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5555 return false;
5556 }
5557 return true;
5558 default:
5559 BUG();
5560 }
5561}
5562
Daniel Vettere29c22c2013-02-21 00:00:16 +01005563#define RETRY 1
5564static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005565 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02005566{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005567 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005568 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02005569 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01005570 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005571
Daniel Vettere29c22c2013-02-21 00:00:16 +01005572retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02005573 /* FDI is a binary signal running at ~2.7GHz, encoding
5574 * each output octet as 10 bits. The actual frequency
5575 * is stored as a divider into a 100MHz clock, and the
5576 * mode pixel clock is stored in units of 1KHz.
5577 * Hence the bw of each lane in terms of the mode signal
5578 * is:
5579 */
5580 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5581
Damien Lespiau241bfc32013-09-25 16:45:37 +01005582 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005583
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005584 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005585 pipe_config->pipe_bpp);
5586
5587 pipe_config->fdi_lanes = lane;
5588
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005589 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005590 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005591
Daniel Vettere29c22c2013-02-21 00:00:16 +01005592 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5593 intel_crtc->pipe, pipe_config);
5594 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5595 pipe_config->pipe_bpp -= 2*3;
5596 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5597 pipe_config->pipe_bpp);
5598 needs_recompute = true;
5599 pipe_config->bw_constrained = true;
5600
5601 goto retry;
5602 }
5603
5604 if (needs_recompute)
5605 return RETRY;
5606
5607 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005608}
5609
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005610static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005611 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005612{
Jani Nikulad330a952014-01-21 11:24:25 +02005613 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03005614 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07005615 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005616}
5617
Daniel Vettera43f6e02013-06-07 23:10:32 +02005618static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005619 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08005620{
Daniel Vettera43f6e02013-06-07 23:10:32 +02005621 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02005622 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02005623 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01005624
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005625 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005626 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005627 int clock_limit =
5628 dev_priv->display.get_display_clock_speed(dev);
5629
5630 /*
5631 * Enable pixel doubling when the dot clock
5632 * is > 90% of the (display) core speed.
5633 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03005634 * GDG double wide on either pipe,
5635 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005636 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03005637 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01005638 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005639 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005640 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005641 }
5642
Damien Lespiau241bfc32013-09-25 16:45:37 +01005643 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005644 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005645 }
Chris Wilson89749352010-09-12 18:25:19 +01005646
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005647 /*
5648 * Pipe horizontal size must be even in:
5649 * - DVO ganged mode
5650 * - LVDS dual channel mode
5651 * - Double wide pipe
5652 */
Ander Conselvan de Oliveirab4f2bf42015-02-26 09:44:45 +02005653 if ((intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005654 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5655 pipe_config->pipe_src_w &= ~1;
5656
Damien Lespiau8693a822013-05-03 18:48:11 +01005657 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5658 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03005659 */
5660 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5661 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005662 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03005663
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005664 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005665 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005666 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005667 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5668 * for lvds. */
5669 pipe_config->pipe_bpp = 8*3;
5670 }
5671
Damien Lespiauf5adf942013-06-24 18:29:34 +01005672 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005673 hsw_compute_ips_config(crtc, pipe_config);
5674
Daniel Vetter877d48d2013-04-19 11:24:43 +02005675 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02005676 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02005677
Daniel Vettere29c22c2013-02-21 00:00:16 +01005678 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005679}
5680
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005681static int valleyview_get_display_clock_speed(struct drm_device *dev)
5682{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005683 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005684 u32 val;
5685 int divider;
5686
Ville Syrjäläd49a3402014-06-28 02:03:58 +03005687 /* FIXME: Punit isn't quite ready yet */
5688 if (IS_CHERRYVIEW(dev))
5689 return 400000;
5690
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005691 if (dev_priv->hpll_freq == 0)
5692 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
5693
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005694 mutex_lock(&dev_priv->dpio_lock);
5695 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5696 mutex_unlock(&dev_priv->dpio_lock);
5697
5698 divider = val & DISPLAY_FREQUENCY_VALUES;
5699
Ville Syrjälä7d007f42014-06-13 13:37:53 +03005700 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5701 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5702 "cdclk change in progress\n");
5703
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005704 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005705}
5706
Jesse Barnese70236a2009-09-21 10:42:27 -07005707static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08005708{
Jesse Barnese70236a2009-09-21 10:42:27 -07005709 return 400000;
5710}
Jesse Barnes79e53942008-11-07 14:24:08 -08005711
Jesse Barnese70236a2009-09-21 10:42:27 -07005712static int i915_get_display_clock_speed(struct drm_device *dev)
5713{
5714 return 333000;
5715}
Jesse Barnes79e53942008-11-07 14:24:08 -08005716
Jesse Barnese70236a2009-09-21 10:42:27 -07005717static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5718{
5719 return 200000;
5720}
Jesse Barnes79e53942008-11-07 14:24:08 -08005721
Daniel Vetter257a7ff2013-07-26 08:35:42 +02005722static int pnv_get_display_clock_speed(struct drm_device *dev)
5723{
5724 u16 gcfgc = 0;
5725
5726 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5727
5728 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5729 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5730 return 267000;
5731 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5732 return 333000;
5733 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5734 return 444000;
5735 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5736 return 200000;
5737 default:
5738 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5739 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5740 return 133000;
5741 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5742 return 167000;
5743 }
5744}
5745
Jesse Barnese70236a2009-09-21 10:42:27 -07005746static int i915gm_get_display_clock_speed(struct drm_device *dev)
5747{
5748 u16 gcfgc = 0;
5749
5750 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5751
5752 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08005753 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07005754 else {
5755 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5756 case GC_DISPLAY_CLOCK_333_MHZ:
5757 return 333000;
5758 default:
5759 case GC_DISPLAY_CLOCK_190_200_MHZ:
5760 return 190000;
5761 }
5762 }
5763}
Jesse Barnes79e53942008-11-07 14:24:08 -08005764
Jesse Barnese70236a2009-09-21 10:42:27 -07005765static int i865_get_display_clock_speed(struct drm_device *dev)
5766{
5767 return 266000;
5768}
5769
5770static int i855_get_display_clock_speed(struct drm_device *dev)
5771{
5772 u16 hpllcc = 0;
5773 /* Assume that the hardware is in the high speed state. This
5774 * should be the default.
5775 */
5776 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5777 case GC_CLOCK_133_200:
5778 case GC_CLOCK_100_200:
5779 return 200000;
5780 case GC_CLOCK_166_250:
5781 return 250000;
5782 case GC_CLOCK_100_133:
5783 return 133000;
5784 }
5785
5786 /* Shouldn't happen */
5787 return 0;
5788}
5789
5790static int i830_get_display_clock_speed(struct drm_device *dev)
5791{
5792 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08005793}
5794
Zhenyu Wang2c072452009-06-05 15:38:42 +08005795static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005796intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005797{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005798 while (*num > DATA_LINK_M_N_MASK ||
5799 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08005800 *num >>= 1;
5801 *den >>= 1;
5802 }
5803}
5804
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005805static void compute_m_n(unsigned int m, unsigned int n,
5806 uint32_t *ret_m, uint32_t *ret_n)
5807{
5808 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5809 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5810 intel_reduce_m_n_ratio(ret_m, ret_n);
5811}
5812
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005813void
5814intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5815 int pixel_clock, int link_clock,
5816 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005817{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005818 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005819
5820 compute_m_n(bits_per_pixel * pixel_clock,
5821 link_clock * nlanes * 8,
5822 &m_n->gmch_m, &m_n->gmch_n);
5823
5824 compute_m_n(pixel_clock, link_clock,
5825 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005826}
5827
Chris Wilsona7615032011-01-12 17:04:08 +00005828static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5829{
Jani Nikulad330a952014-01-21 11:24:25 +02005830 if (i915.panel_use_ssc >= 0)
5831 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005832 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07005833 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00005834}
5835
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005836static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005837{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005838 struct drm_device *dev = crtc->base.dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005839 struct drm_i915_private *dev_priv = dev->dev_private;
5840 int refclk;
5841
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005842 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02005843 refclk = 100000;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02005844 } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005845 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005846 refclk = dev_priv->vbt.lvds_ssc_freq;
5847 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005848 } else if (!IS_GEN2(dev)) {
5849 refclk = 96000;
5850 } else {
5851 refclk = 48000;
5852 }
5853
5854 return refclk;
5855}
5856
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005857static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005858{
Daniel Vetter7df00d72013-05-21 21:54:55 +02005859 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005860}
Daniel Vetterf47709a2013-03-28 10:42:02 +01005861
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005862static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5863{
5864 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005865}
5866
Daniel Vetterf47709a2013-03-28 10:42:02 +01005867static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005868 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08005869 intel_clock_t *reduced_clock)
5870{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005871 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005872 u32 fp, fp2 = 0;
5873
5874 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005875 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005876 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005877 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005878 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005879 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005880 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005881 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005882 }
5883
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005884 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005885
Daniel Vetterf47709a2013-03-28 10:42:02 +01005886 crtc->lowfreq_avail = false;
Bob Paauwee1f234b2014-11-11 09:29:18 -08005887 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
Jani Nikulad330a952014-01-21 11:24:25 +02005888 reduced_clock && i915.powersave) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005889 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005890 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005891 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02005892 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005893 }
5894}
5895
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005896static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5897 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005898{
5899 u32 reg_val;
5900
5901 /*
5902 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5903 * and set it to a reasonable value instead.
5904 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005905 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005906 reg_val &= 0xffffff00;
5907 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005908 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005909
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005910 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005911 reg_val &= 0x8cffffff;
5912 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005913 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005914
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005915 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005916 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005917 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005918
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005919 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005920 reg_val &= 0x00ffffff;
5921 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005922 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005923}
5924
Daniel Vetterb5518422013-05-03 11:49:48 +02005925static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5926 struct intel_link_m_n *m_n)
5927{
5928 struct drm_device *dev = crtc->base.dev;
5929 struct drm_i915_private *dev_priv = dev->dev_private;
5930 int pipe = crtc->pipe;
5931
Daniel Vettere3b95f12013-05-03 11:49:49 +02005932 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5933 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5934 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5935 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005936}
5937
5938static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07005939 struct intel_link_m_n *m_n,
5940 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02005941{
5942 struct drm_device *dev = crtc->base.dev;
5943 struct drm_i915_private *dev_priv = dev->dev_private;
5944 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005945 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02005946
5947 if (INTEL_INFO(dev)->gen >= 5) {
5948 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5949 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5950 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5951 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07005952 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5953 * for gen < 8) and if DRRS is supported (to make sure the
5954 * registers are not unnecessarily accessed).
5955 */
Durgadoss R44395bf2015-02-13 15:33:02 +05305956 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005957 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07005958 I915_WRITE(PIPE_DATA_M2(transcoder),
5959 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5960 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5961 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5962 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5963 }
Daniel Vetterb5518422013-05-03 11:49:48 +02005964 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02005965 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5966 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5967 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5968 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005969 }
5970}
5971
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305972void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005973{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305974 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
5975
5976 if (m_n == M1_N1) {
5977 dp_m_n = &crtc->config->dp_m_n;
5978 dp_m2_n2 = &crtc->config->dp_m2_n2;
5979 } else if (m_n == M2_N2) {
5980
5981 /*
5982 * M2_N2 registers are not supported. Hence m2_n2 divider value
5983 * needs to be programmed into M1_N1.
5984 */
5985 dp_m_n = &crtc->config->dp_m2_n2;
5986 } else {
5987 DRM_ERROR("Unsupported divider value\n");
5988 return;
5989 }
5990
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005991 if (crtc->config->has_pch_encoder)
5992 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005993 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305994 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005995}
5996
Ville Syrjäläd288f652014-10-28 13:20:22 +02005997static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02005998 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005999{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006000 u32 dpll, dpll_md;
6001
6002 /*
6003 * Enable DPIO clock input. We should never disable the reference
6004 * clock for pipe B, since VGA hotplug / manual detection depends
6005 * on it.
6006 */
6007 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6008 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6009 /* We should never disable this, set it here for state tracking */
6010 if (crtc->pipe == PIPE_B)
6011 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6012 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006013 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006014
Ville Syrjäläd288f652014-10-28 13:20:22 +02006015 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006016 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006017 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006018}
6019
Ville Syrjäläd288f652014-10-28 13:20:22 +02006020static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006021 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006022{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006023 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006024 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006025 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006026 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006027 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006028 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006029
Daniel Vetter09153002012-12-12 14:06:44 +01006030 mutex_lock(&dev_priv->dpio_lock);
6031
Ville Syrjäläd288f652014-10-28 13:20:22 +02006032 bestn = pipe_config->dpll.n;
6033 bestm1 = pipe_config->dpll.m1;
6034 bestm2 = pipe_config->dpll.m2;
6035 bestp1 = pipe_config->dpll.p1;
6036 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006037
Jesse Barnes89b667f2013-04-18 14:51:36 -07006038 /* See eDP HDMI DPIO driver vbios notes doc */
6039
6040 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006041 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006042 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006043
6044 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006045 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006046
6047 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006048 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006049 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006050 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006051
6052 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006053 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006054
6055 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006056 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6057 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6058 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006059 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006060
6061 /*
6062 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6063 * but we don't support that).
6064 * Note: don't use the DAC post divider as it seems unstable.
6065 */
6066 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006067 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006068
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006069 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006070 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006071
Jesse Barnes89b667f2013-04-18 14:51:36 -07006072 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006073 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006074 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6075 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006076 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03006077 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006078 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006079 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006080 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006081
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006082 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006083 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006084 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006085 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006086 0x0df40000);
6087 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006088 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006089 0x0df70000);
6090 } else { /* HDMI or VGA */
6091 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006092 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006093 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006094 0x0df70000);
6095 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006096 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006097 0x0df40000);
6098 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006099
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006100 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006101 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006102 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6103 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006104 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006105 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006106
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006107 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006108 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006109}
6110
Ville Syrjäläd288f652014-10-28 13:20:22 +02006111static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006112 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006113{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006114 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006115 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6116 DPLL_VCO_ENABLE;
6117 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006118 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006119
Ville Syrjäläd288f652014-10-28 13:20:22 +02006120 pipe_config->dpll_hw_state.dpll_md =
6121 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006122}
6123
Ville Syrjäläd288f652014-10-28 13:20:22 +02006124static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006125 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006126{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006127 struct drm_device *dev = crtc->base.dev;
6128 struct drm_i915_private *dev_priv = dev->dev_private;
6129 int pipe = crtc->pipe;
6130 int dpll_reg = DPLL(crtc->pipe);
6131 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ville Syrjälä580d3812014-04-09 13:29:00 +03006132 u32 loopfilter, intcoeff;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006133 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
6134 int refclk;
6135
Ville Syrjäläd288f652014-10-28 13:20:22 +02006136 bestn = pipe_config->dpll.n;
6137 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6138 bestm1 = pipe_config->dpll.m1;
6139 bestm2 = pipe_config->dpll.m2 >> 22;
6140 bestp1 = pipe_config->dpll.p1;
6141 bestp2 = pipe_config->dpll.p2;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006142
6143 /*
6144 * Enable Refclk and SSC
6145 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006146 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02006147 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006148
6149 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006150
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006151 /* p1 and p2 divider */
6152 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6153 5 << DPIO_CHV_S1_DIV_SHIFT |
6154 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6155 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6156 1 << DPIO_CHV_K_DIV_SHIFT);
6157
6158 /* Feedback post-divider - m2 */
6159 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6160
6161 /* Feedback refclk divider - n and m1 */
6162 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6163 DPIO_CHV_M1_DIV_BY_2 |
6164 1 << DPIO_CHV_N_DIV_SHIFT);
6165
6166 /* M2 fraction division */
6167 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
6168
6169 /* M2 fraction division enable */
6170 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
6171 DPIO_CHV_FRAC_DIV_EN |
6172 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
6173
6174 /* Loop filter */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006175 refclk = i9xx_get_refclk(crtc, 0);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006176 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
6177 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
6178 if (refclk == 100000)
6179 intcoeff = 11;
6180 else if (refclk == 38400)
6181 intcoeff = 10;
6182 else
6183 intcoeff = 9;
6184 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
6185 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6186
6187 /* AFC Recal */
6188 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6189 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6190 DPIO_AFC_RECAL);
6191
6192 mutex_unlock(&dev_priv->dpio_lock);
6193}
6194
Ville Syrjäläd288f652014-10-28 13:20:22 +02006195/**
6196 * vlv_force_pll_on - forcibly enable just the PLL
6197 * @dev_priv: i915 private structure
6198 * @pipe: pipe PLL to enable
6199 * @dpll: PLL configuration
6200 *
6201 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6202 * in cases where we need the PLL enabled even when @pipe is not going to
6203 * be enabled.
6204 */
6205void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
6206 const struct dpll *dpll)
6207{
6208 struct intel_crtc *crtc =
6209 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006210 struct intel_crtc_state pipe_config = {
Ville Syrjäläd288f652014-10-28 13:20:22 +02006211 .pixel_multiplier = 1,
6212 .dpll = *dpll,
6213 };
6214
6215 if (IS_CHERRYVIEW(dev)) {
6216 chv_update_pll(crtc, &pipe_config);
6217 chv_prepare_pll(crtc, &pipe_config);
6218 chv_enable_pll(crtc, &pipe_config);
6219 } else {
6220 vlv_update_pll(crtc, &pipe_config);
6221 vlv_prepare_pll(crtc, &pipe_config);
6222 vlv_enable_pll(crtc, &pipe_config);
6223 }
6224}
6225
6226/**
6227 * vlv_force_pll_off - forcibly disable just the PLL
6228 * @dev_priv: i915 private structure
6229 * @pipe: pipe PLL to disable
6230 *
6231 * Disable the PLL for @pipe. To be used in cases where we need
6232 * the PLL enabled even when @pipe is not going to be enabled.
6233 */
6234void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
6235{
6236 if (IS_CHERRYVIEW(dev))
6237 chv_disable_pll(to_i915(dev), pipe);
6238 else
6239 vlv_disable_pll(to_i915(dev), pipe);
6240}
6241
Daniel Vetterf47709a2013-03-28 10:42:02 +01006242static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006243 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006244 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006245 int num_connectors)
6246{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006247 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006248 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006249 u32 dpll;
6250 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006251 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006252
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006253 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306254
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006255 is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
6256 intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006257
6258 dpll = DPLL_VGA_MODE_DIS;
6259
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006260 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006261 dpll |= DPLLB_MODE_LVDS;
6262 else
6263 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006264
Daniel Vetteref1b4602013-06-01 17:17:04 +02006265 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006266 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02006267 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006268 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02006269
6270 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006271 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006272
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006273 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006274 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006275
6276 /* compute bitmask from p1 value */
6277 if (IS_PINEVIEW(dev))
6278 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6279 else {
6280 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6281 if (IS_G4X(dev) && reduced_clock)
6282 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6283 }
6284 switch (clock->p2) {
6285 case 5:
6286 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6287 break;
6288 case 7:
6289 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6290 break;
6291 case 10:
6292 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6293 break;
6294 case 14:
6295 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6296 break;
6297 }
6298 if (INTEL_INFO(dev)->gen >= 4)
6299 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6300
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006301 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006302 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006303 else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006304 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6305 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6306 else
6307 dpll |= PLL_REF_INPUT_DREFCLK;
6308
6309 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006310 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006311
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006312 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006313 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02006314 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006315 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006316 }
6317}
6318
Daniel Vetterf47709a2013-03-28 10:42:02 +01006319static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006320 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006321 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006322 int num_connectors)
6323{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006324 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006325 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006326 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006327 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006328
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006329 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306330
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006331 dpll = DPLL_VGA_MODE_DIS;
6332
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006333 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006334 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6335 } else {
6336 if (clock->p1 == 2)
6337 dpll |= PLL_P1_DIVIDE_BY_TWO;
6338 else
6339 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6340 if (clock->p2 == 4)
6341 dpll |= PLL_P2_DIVIDE_BY_4;
6342 }
6343
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006344 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006345 dpll |= DPLL_DVO_2X_MODE;
6346
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006347 if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006348 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6349 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6350 else
6351 dpll |= PLL_REF_INPUT_DREFCLK;
6352
6353 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006354 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006355}
6356
Daniel Vetter8a654f32013-06-01 17:16:22 +02006357static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006358{
6359 struct drm_device *dev = intel_crtc->base.dev;
6360 struct drm_i915_private *dev_priv = dev->dev_private;
6361 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006362 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02006363 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006364 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006365 uint32_t crtc_vtotal, crtc_vblank_end;
6366 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006367
6368 /* We need to be careful not to changed the adjusted mode, for otherwise
6369 * the hw state checker will get angry at the mismatch. */
6370 crtc_vtotal = adjusted_mode->crtc_vtotal;
6371 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006372
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006373 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006374 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006375 crtc_vtotal -= 1;
6376 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006377
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006378 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006379 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6380 else
6381 vsyncshift = adjusted_mode->crtc_hsync_start -
6382 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006383 if (vsyncshift < 0)
6384 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006385 }
6386
6387 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006388 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006389
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006390 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006391 (adjusted_mode->crtc_hdisplay - 1) |
6392 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006393 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006394 (adjusted_mode->crtc_hblank_start - 1) |
6395 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006396 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006397 (adjusted_mode->crtc_hsync_start - 1) |
6398 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6399
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006400 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006401 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006402 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006403 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006404 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006405 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006406 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006407 (adjusted_mode->crtc_vsync_start - 1) |
6408 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6409
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006410 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6411 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6412 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6413 * bits. */
6414 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6415 (pipe == PIPE_B || pipe == PIPE_C))
6416 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6417
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006418 /* pipesrc controls the size that is scaled from, which should
6419 * always be the user's requested size.
6420 */
6421 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006422 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6423 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006424}
6425
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006426static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006427 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006428{
6429 struct drm_device *dev = crtc->base.dev;
6430 struct drm_i915_private *dev_priv = dev->dev_private;
6431 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6432 uint32_t tmp;
6433
6434 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006435 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6436 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006437 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006438 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6439 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006440 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006441 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6442 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006443
6444 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006445 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6446 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006447 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006448 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6449 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006450 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006451 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6452 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006453
6454 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006455 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6456 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
6457 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006458 }
6459
6460 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006461 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6462 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6463
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006464 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
6465 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006466}
6467
Daniel Vetterf6a83282014-02-11 15:28:57 -08006468void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006469 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03006470{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006471 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
6472 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
6473 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
6474 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006475
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006476 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
6477 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
6478 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
6479 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006480
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006481 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006482
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006483 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
6484 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006485}
6486
Daniel Vetter84b046f2013-02-19 18:48:54 +01006487static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6488{
6489 struct drm_device *dev = intel_crtc->base.dev;
6490 struct drm_i915_private *dev_priv = dev->dev_private;
6491 uint32_t pipeconf;
6492
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006493 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006494
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03006495 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6496 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6497 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02006498
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006499 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006500 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006501
Daniel Vetterff9ce462013-04-24 14:57:17 +02006502 /* only g4x and later have fancy bpc/dither controls */
6503 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006504 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006505 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02006506 pipeconf |= PIPECONF_DITHER_EN |
6507 PIPECONF_DITHER_TYPE_SP;
6508
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006509 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006510 case 18:
6511 pipeconf |= PIPECONF_6BPC;
6512 break;
6513 case 24:
6514 pipeconf |= PIPECONF_8BPC;
6515 break;
6516 case 30:
6517 pipeconf |= PIPECONF_10BPC;
6518 break;
6519 default:
6520 /* Case prevented by intel_choose_pipe_bpp_dither. */
6521 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01006522 }
6523 }
6524
6525 if (HAS_PIPE_CXSR(dev)) {
6526 if (intel_crtc->lowfreq_avail) {
6527 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6528 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6529 } else {
6530 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01006531 }
6532 }
6533
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006534 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006535 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006536 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006537 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6538 else
6539 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6540 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01006541 pipeconf |= PIPECONF_PROGRESSIVE;
6542
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006543 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006544 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03006545
Daniel Vetter84b046f2013-02-19 18:48:54 +01006546 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6547 POSTING_READ(PIPECONF(intel_crtc->pipe));
6548}
6549
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006550static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
6551 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08006552{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006553 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006554 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07006555 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07006556 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02006557 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006558 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01006559 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08006560 const intel_limit_t *limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08006561
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02006562 for_each_intel_encoder(dev, encoder) {
6563 if (encoder->new_crtc != crtc)
6564 continue;
6565
Chris Wilson5eddb702010-09-11 13:48:45 +01006566 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006567 case INTEL_OUTPUT_LVDS:
6568 is_lvds = true;
6569 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006570 case INTEL_OUTPUT_DSI:
6571 is_dsi = true;
6572 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02006573 default:
6574 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006575 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006576
Eric Anholtc751ce42010-03-25 11:48:48 -07006577 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08006578 }
6579
Jani Nikulaf2335332013-09-13 11:03:09 +03006580 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02006581 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006582
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006583 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006584 refclk = i9xx_get_refclk(crtc, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03006585
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006586 /*
6587 * Returns a set of divisors for the desired target clock with
6588 * the given refclk, or FALSE. The returned values represent
6589 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6590 * 2) / p1 / p2.
6591 */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006592 limit = intel_limit(crtc, refclk);
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006593 ok = dev_priv->display.find_dpll(limit, crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006594 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006595 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03006596 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006597 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6598 return -EINVAL;
6599 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006600
Jani Nikulaf2335332013-09-13 11:03:09 +03006601 if (is_lvds && dev_priv->lvds_downclock_avail) {
6602 /*
6603 * Ensure we match the reduced clock's P to the target
6604 * clock. If the clocks don't match, we can't switch
6605 * the display clock by using the FP0/FP1. In such case
6606 * we will disable the LVDS downclock feature.
6607 */
6608 has_reduced_clock =
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006609 dev_priv->display.find_dpll(limit, crtc,
Jani Nikulaf2335332013-09-13 11:03:09 +03006610 dev_priv->lvds_downclock,
6611 refclk, &clock,
6612 &reduced_clock);
6613 }
6614 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006615 crtc_state->dpll.n = clock.n;
6616 crtc_state->dpll.m1 = clock.m1;
6617 crtc_state->dpll.m2 = clock.m2;
6618 crtc_state->dpll.p1 = clock.p1;
6619 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006620 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006621
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006622 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006623 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306624 has_reduced_clock ? &reduced_clock : NULL,
6625 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006626 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006627 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006628 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006629 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006630 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006631 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006632 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02006633 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006634 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006635
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02006636 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07006637}
6638
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006639static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006640 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006641{
6642 struct drm_device *dev = crtc->base.dev;
6643 struct drm_i915_private *dev_priv = dev->dev_private;
6644 uint32_t tmp;
6645
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02006646 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6647 return;
6648
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006649 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02006650 if (!(tmp & PFIT_ENABLE))
6651 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006652
Daniel Vetter06922822013-07-11 13:35:40 +02006653 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006654 if (INTEL_INFO(dev)->gen < 4) {
6655 if (crtc->pipe != PIPE_B)
6656 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006657 } else {
6658 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6659 return;
6660 }
6661
Daniel Vetter06922822013-07-11 13:35:40 +02006662 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006663 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6664 if (INTEL_INFO(dev)->gen < 5)
6665 pipe_config->gmch_pfit.lvds_border_bits =
6666 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6667}
6668
Jesse Barnesacbec812013-09-20 11:29:32 -07006669static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006670 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07006671{
6672 struct drm_device *dev = crtc->base.dev;
6673 struct drm_i915_private *dev_priv = dev->dev_private;
6674 int pipe = pipe_config->cpu_transcoder;
6675 intel_clock_t clock;
6676 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07006677 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07006678
Shobhit Kumarf573de52014-07-30 20:32:37 +05306679 /* In case of MIPI DPLL will not even be used */
6680 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6681 return;
6682
Jesse Barnesacbec812013-09-20 11:29:32 -07006683 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006684 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07006685 mutex_unlock(&dev_priv->dpio_lock);
6686
6687 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6688 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6689 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6690 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6691 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6692
Ville Syrjäläf6466282013-10-14 14:50:31 +03006693 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07006694
Ville Syrjäläf6466282013-10-14 14:50:31 +03006695 /* clock.dot is the fast clock */
6696 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07006697}
6698
Damien Lespiau5724dbd2015-01-20 12:51:52 +00006699static void
6700i9xx_get_initial_plane_config(struct intel_crtc *crtc,
6701 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006702{
6703 struct drm_device *dev = crtc->base.dev;
6704 struct drm_i915_private *dev_priv = dev->dev_private;
6705 u32 val, base, offset;
6706 int pipe = crtc->pipe, plane = crtc->plane;
6707 int fourcc, pixel_format;
6708 int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006709 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00006710 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006711
Damien Lespiau42a7b082015-02-05 19:35:13 +00006712 val = I915_READ(DSPCNTR(plane));
6713 if (!(val & DISPLAY_PLANE_ENABLE))
6714 return;
6715
Damien Lespiaud9806c92015-01-21 14:07:19 +00006716 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00006717 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006718 DRM_DEBUG_KMS("failed to alloc fb\n");
6719 return;
6720 }
6721
Damien Lespiau1b842c82015-01-21 13:50:54 +00006722 fb = &intel_fb->base;
6723
Daniel Vetter18c52472015-02-10 17:16:09 +00006724 if (INTEL_INFO(dev)->gen >= 4) {
6725 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00006726 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00006727 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
6728 }
6729 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006730
6731 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00006732 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006733 fb->pixel_format = fourcc;
6734 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006735
6736 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00006737 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006738 offset = I915_READ(DSPTILEOFF(plane));
6739 else
6740 offset = I915_READ(DSPLINOFF(plane));
6741 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6742 } else {
6743 base = I915_READ(DSPADDR(plane));
6744 }
6745 plane_config->base = base;
6746
6747 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006748 fb->width = ((val >> 16) & 0xfff) + 1;
6749 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006750
6751 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006752 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006753
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006754 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00006755 fb->pixel_format,
6756 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006757
Damien Lespiaub113d5e2015-01-20 12:51:46 +00006758 plane_config->size = PAGE_ALIGN(fb->pitches[0] * aligned_height);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006759
Damien Lespiau2844a922015-01-20 12:51:48 +00006760 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
6761 pipe_name(pipe), plane, fb->width, fb->height,
6762 fb->bits_per_pixel, base, fb->pitches[0],
6763 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006764
Damien Lespiau2d140302015-02-05 17:22:18 +00006765 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006766}
6767
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006768static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006769 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006770{
6771 struct drm_device *dev = crtc->base.dev;
6772 struct drm_i915_private *dev_priv = dev->dev_private;
6773 int pipe = pipe_config->cpu_transcoder;
6774 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6775 intel_clock_t clock;
6776 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6777 int refclk = 100000;
6778
6779 mutex_lock(&dev_priv->dpio_lock);
6780 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6781 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6782 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6783 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6784 mutex_unlock(&dev_priv->dpio_lock);
6785
6786 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6787 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6788 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6789 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6790 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6791
6792 chv_clock(refclk, &clock);
6793
6794 /* clock.dot is the fast clock */
6795 pipe_config->port_clock = clock.dot / 5;
6796}
6797
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006798static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006799 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006800{
6801 struct drm_device *dev = crtc->base.dev;
6802 struct drm_i915_private *dev_priv = dev->dev_private;
6803 uint32_t tmp;
6804
Daniel Vetterf458ebb2014-09-30 10:56:39 +02006805 if (!intel_display_power_is_enabled(dev_priv,
6806 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02006807 return false;
6808
Daniel Vettere143a212013-07-04 12:01:15 +02006809 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006810 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006811
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006812 tmp = I915_READ(PIPECONF(crtc->pipe));
6813 if (!(tmp & PIPECONF_ENABLE))
6814 return false;
6815
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006816 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6817 switch (tmp & PIPECONF_BPC_MASK) {
6818 case PIPECONF_6BPC:
6819 pipe_config->pipe_bpp = 18;
6820 break;
6821 case PIPECONF_8BPC:
6822 pipe_config->pipe_bpp = 24;
6823 break;
6824 case PIPECONF_10BPC:
6825 pipe_config->pipe_bpp = 30;
6826 break;
6827 default:
6828 break;
6829 }
6830 }
6831
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02006832 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6833 pipe_config->limited_color_range = true;
6834
Ville Syrjälä282740f2013-09-04 18:30:03 +03006835 if (INTEL_INFO(dev)->gen < 4)
6836 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6837
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006838 intel_get_pipe_timings(crtc, pipe_config);
6839
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006840 i9xx_get_pfit_config(crtc, pipe_config);
6841
Daniel Vetter6c49f242013-06-06 12:45:25 +02006842 if (INTEL_INFO(dev)->gen >= 4) {
6843 tmp = I915_READ(DPLL_MD(crtc->pipe));
6844 pipe_config->pixel_multiplier =
6845 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6846 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006847 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02006848 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6849 tmp = I915_READ(DPLL(crtc->pipe));
6850 pipe_config->pixel_multiplier =
6851 ((tmp & SDVO_MULTIPLIER_MASK)
6852 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6853 } else {
6854 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6855 * port and will be fixed up in the encoder->get_config
6856 * function. */
6857 pipe_config->pixel_multiplier = 1;
6858 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006859 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6860 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006861 /*
6862 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
6863 * on 830. Filter it out here so that we don't
6864 * report errors due to that.
6865 */
6866 if (IS_I830(dev))
6867 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
6868
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006869 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6870 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03006871 } else {
6872 /* Mask out read-only status bits. */
6873 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6874 DPLL_PORTC_READY_MASK |
6875 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006876 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02006877
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006878 if (IS_CHERRYVIEW(dev))
6879 chv_crtc_clock_get(crtc, pipe_config);
6880 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07006881 vlv_crtc_clock_get(crtc, pipe_config);
6882 else
6883 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03006884
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006885 return true;
6886}
6887
Paulo Zanonidde86e22012-12-01 12:04:25 -02006888static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07006889{
6890 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006891 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006892 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006893 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006894 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006895 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07006896 bool has_ck505 = false;
6897 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006898
6899 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01006900 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07006901 switch (encoder->type) {
6902 case INTEL_OUTPUT_LVDS:
6903 has_panel = true;
6904 has_lvds = true;
6905 break;
6906 case INTEL_OUTPUT_EDP:
6907 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03006908 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07006909 has_cpu_edp = true;
6910 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02006911 default:
6912 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006913 }
6914 }
6915
Keith Packard99eb6a02011-09-26 14:29:12 -07006916 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006917 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07006918 can_ssc = has_ck505;
6919 } else {
6920 has_ck505 = false;
6921 can_ssc = true;
6922 }
6923
Imre Deak2de69052013-05-08 13:14:04 +03006924 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6925 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006926
6927 /* Ironlake: try to setup display ref clock before DPLL
6928 * enabling. This is only under driver's control after
6929 * PCH B stepping, previous chipset stepping should be
6930 * ignoring this setting.
6931 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006932 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006933
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006934 /* As we must carefully and slowly disable/enable each source in turn,
6935 * compute the final state we want first and check if we need to
6936 * make any changes at all.
6937 */
6938 final = val;
6939 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07006940 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006941 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07006942 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006943 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6944
6945 final &= ~DREF_SSC_SOURCE_MASK;
6946 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6947 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006948
Keith Packard199e5d72011-09-22 12:01:57 -07006949 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006950 final |= DREF_SSC_SOURCE_ENABLE;
6951
6952 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6953 final |= DREF_SSC1_ENABLE;
6954
6955 if (has_cpu_edp) {
6956 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6957 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6958 else
6959 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6960 } else
6961 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6962 } else {
6963 final |= DREF_SSC_SOURCE_DISABLE;
6964 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6965 }
6966
6967 if (final == val)
6968 return;
6969
6970 /* Always enable nonspread source */
6971 val &= ~DREF_NONSPREAD_SOURCE_MASK;
6972
6973 if (has_ck505)
6974 val |= DREF_NONSPREAD_CK505_ENABLE;
6975 else
6976 val |= DREF_NONSPREAD_SOURCE_ENABLE;
6977
6978 if (has_panel) {
6979 val &= ~DREF_SSC_SOURCE_MASK;
6980 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006981
Keith Packard199e5d72011-09-22 12:01:57 -07006982 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07006983 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006984 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006985 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02006986 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006987 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006988
6989 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006990 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006991 POSTING_READ(PCH_DREF_CONTROL);
6992 udelay(200);
6993
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006994 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006995
6996 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07006997 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07006998 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006999 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007000 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007001 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007002 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007003 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007004 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007005
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007006 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007007 POSTING_READ(PCH_DREF_CONTROL);
7008 udelay(200);
7009 } else {
7010 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7011
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007012 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007013
7014 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007015 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007016
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007017 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007018 POSTING_READ(PCH_DREF_CONTROL);
7019 udelay(200);
7020
7021 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007022 val &= ~DREF_SSC_SOURCE_MASK;
7023 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007024
7025 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007026 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007027
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007028 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007029 POSTING_READ(PCH_DREF_CONTROL);
7030 udelay(200);
7031 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007032
7033 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007034}
7035
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007036static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007037{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007038 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007039
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007040 tmp = I915_READ(SOUTH_CHICKEN2);
7041 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7042 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007043
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007044 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7045 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7046 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007047
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007048 tmp = I915_READ(SOUTH_CHICKEN2);
7049 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7050 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007051
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007052 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7053 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7054 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007055}
7056
7057/* WaMPhyProgramming:hsw */
7058static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7059{
7060 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007061
7062 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7063 tmp &= ~(0xFF << 24);
7064 tmp |= (0x12 << 24);
7065 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7066
Paulo Zanonidde86e22012-12-01 12:04:25 -02007067 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7068 tmp |= (1 << 11);
7069 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7070
7071 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7072 tmp |= (1 << 11);
7073 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7074
Paulo Zanonidde86e22012-12-01 12:04:25 -02007075 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7076 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7077 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7078
7079 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7080 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7081 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7082
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007083 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7084 tmp &= ~(7 << 13);
7085 tmp |= (5 << 13);
7086 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007087
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007088 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7089 tmp &= ~(7 << 13);
7090 tmp |= (5 << 13);
7091 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007092
7093 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7094 tmp &= ~0xFF;
7095 tmp |= 0x1C;
7096 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7097
7098 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7099 tmp &= ~0xFF;
7100 tmp |= 0x1C;
7101 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7102
7103 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7104 tmp &= ~(0xFF << 16);
7105 tmp |= (0x1C << 16);
7106 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7107
7108 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7109 tmp &= ~(0xFF << 16);
7110 tmp |= (0x1C << 16);
7111 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7112
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007113 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7114 tmp |= (1 << 27);
7115 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007116
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007117 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7118 tmp |= (1 << 27);
7119 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007120
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007121 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7122 tmp &= ~(0xF << 28);
7123 tmp |= (4 << 28);
7124 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007125
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007126 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7127 tmp &= ~(0xF << 28);
7128 tmp |= (4 << 28);
7129 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007130}
7131
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007132/* Implements 3 different sequences from BSpec chapter "Display iCLK
7133 * Programming" based on the parameters passed:
7134 * - Sequence to enable CLKOUT_DP
7135 * - Sequence to enable CLKOUT_DP without spread
7136 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7137 */
7138static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
7139 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007140{
7141 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007142 uint32_t reg, tmp;
7143
7144 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7145 with_spread = true;
7146 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
7147 with_fdi, "LP PCH doesn't have FDI\n"))
7148 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007149
7150 mutex_lock(&dev_priv->dpio_lock);
7151
7152 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7153 tmp &= ~SBI_SSCCTL_DISABLE;
7154 tmp |= SBI_SSCCTL_PATHALT;
7155 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7156
7157 udelay(24);
7158
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007159 if (with_spread) {
7160 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7161 tmp &= ~SBI_SSCCTL_PATHALT;
7162 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007163
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007164 if (with_fdi) {
7165 lpt_reset_fdi_mphy(dev_priv);
7166 lpt_program_fdi_mphy(dev_priv);
7167 }
7168 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02007169
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007170 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7171 SBI_GEN0 : SBI_DBUFF0;
7172 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7173 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7174 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01007175
7176 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007177}
7178
Paulo Zanoni47701c32013-07-23 11:19:25 -03007179/* Sequence to disable CLKOUT_DP */
7180static void lpt_disable_clkout_dp(struct drm_device *dev)
7181{
7182 struct drm_i915_private *dev_priv = dev->dev_private;
7183 uint32_t reg, tmp;
7184
7185 mutex_lock(&dev_priv->dpio_lock);
7186
7187 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7188 SBI_GEN0 : SBI_DBUFF0;
7189 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7190 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7191 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7192
7193 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7194 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7195 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7196 tmp |= SBI_SSCCTL_PATHALT;
7197 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7198 udelay(32);
7199 }
7200 tmp |= SBI_SSCCTL_DISABLE;
7201 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7202 }
7203
7204 mutex_unlock(&dev_priv->dpio_lock);
7205}
7206
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007207static void lpt_init_pch_refclk(struct drm_device *dev)
7208{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007209 struct intel_encoder *encoder;
7210 bool has_vga = false;
7211
Damien Lespiaub2784e12014-08-05 11:29:37 +01007212 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007213 switch (encoder->type) {
7214 case INTEL_OUTPUT_ANALOG:
7215 has_vga = true;
7216 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007217 default:
7218 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007219 }
7220 }
7221
Paulo Zanoni47701c32013-07-23 11:19:25 -03007222 if (has_vga)
7223 lpt_enable_clkout_dp(dev, true, true);
7224 else
7225 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007226}
7227
Paulo Zanonidde86e22012-12-01 12:04:25 -02007228/*
7229 * Initialize reference clocks when the driver loads
7230 */
7231void intel_init_pch_refclk(struct drm_device *dev)
7232{
7233 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7234 ironlake_init_pch_refclk(dev);
7235 else if (HAS_PCH_LPT(dev))
7236 lpt_init_pch_refclk(dev);
7237}
7238
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007239static int ironlake_get_refclk(struct drm_crtc *crtc)
7240{
7241 struct drm_device *dev = crtc->dev;
7242 struct drm_i915_private *dev_priv = dev->dev_private;
7243 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007244 int num_connectors = 0;
7245 bool is_lvds = false;
7246
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007247 for_each_intel_encoder(dev, encoder) {
7248 if (encoder->new_crtc != to_intel_crtc(crtc))
7249 continue;
7250
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007251 switch (encoder->type) {
7252 case INTEL_OUTPUT_LVDS:
7253 is_lvds = true;
7254 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007255 default:
7256 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007257 }
7258 num_connectors++;
7259 }
7260
7261 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007262 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007263 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007264 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007265 }
7266
7267 return 120000;
7268}
7269
Daniel Vetter6ff93602013-04-19 11:24:36 +02007270static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03007271{
7272 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
7273 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7274 int pipe = intel_crtc->pipe;
7275 uint32_t val;
7276
Daniel Vetter78114072013-06-13 00:54:57 +02007277 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03007278
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007279 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03007280 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007281 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007282 break;
7283 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007284 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007285 break;
7286 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007287 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007288 break;
7289 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007290 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007291 break;
7292 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03007293 /* Case prevented by intel_choose_pipe_bpp_dither. */
7294 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03007295 }
7296
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007297 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03007298 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7299
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007300 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03007301 val |= PIPECONF_INTERLACED_ILK;
7302 else
7303 val |= PIPECONF_PROGRESSIVE;
7304
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007305 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007306 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007307
Paulo Zanonic8203562012-09-12 10:06:29 -03007308 I915_WRITE(PIPECONF(pipe), val);
7309 POSTING_READ(PIPECONF(pipe));
7310}
7311
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007312/*
7313 * Set up the pipe CSC unit.
7314 *
7315 * Currently only full range RGB to limited range RGB conversion
7316 * is supported, but eventually this should handle various
7317 * RGB<->YCbCr scenarios as well.
7318 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01007319static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007320{
7321 struct drm_device *dev = crtc->dev;
7322 struct drm_i915_private *dev_priv = dev->dev_private;
7323 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7324 int pipe = intel_crtc->pipe;
7325 uint16_t coeff = 0x7800; /* 1.0 */
7326
7327 /*
7328 * TODO: Check what kind of values actually come out of the pipe
7329 * with these coeff/postoff values and adjust to get the best
7330 * accuracy. Perhaps we even need to take the bpc value into
7331 * consideration.
7332 */
7333
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007334 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007335 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7336
7337 /*
7338 * GY/GU and RY/RU should be the other way around according
7339 * to BSpec, but reality doesn't agree. Just set them up in
7340 * a way that results in the correct picture.
7341 */
7342 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7343 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7344
7345 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7346 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7347
7348 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7349 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7350
7351 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7352 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7353 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7354
7355 if (INTEL_INFO(dev)->gen > 6) {
7356 uint16_t postoff = 0;
7357
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007358 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02007359 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007360
7361 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7362 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7363 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7364
7365 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7366 } else {
7367 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7368
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007369 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007370 mode |= CSC_BLACK_SCREEN_OFFSET;
7371
7372 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7373 }
7374}
7375
Daniel Vetter6ff93602013-04-19 11:24:36 +02007376static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007377{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007378 struct drm_device *dev = crtc->dev;
7379 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007380 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007381 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007382 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007383 uint32_t val;
7384
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007385 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007386
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007387 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007388 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7389
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007390 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007391 val |= PIPECONF_INTERLACED_ILK;
7392 else
7393 val |= PIPECONF_PROGRESSIVE;
7394
Paulo Zanoni702e7a52012-10-23 18:29:59 -02007395 I915_WRITE(PIPECONF(cpu_transcoder), val);
7396 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007397
7398 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7399 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007400
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05307401 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007402 val = 0;
7403
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007404 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007405 case 18:
7406 val |= PIPEMISC_DITHER_6_BPC;
7407 break;
7408 case 24:
7409 val |= PIPEMISC_DITHER_8_BPC;
7410 break;
7411 case 30:
7412 val |= PIPEMISC_DITHER_10_BPC;
7413 break;
7414 case 36:
7415 val |= PIPEMISC_DITHER_12_BPC;
7416 break;
7417 default:
7418 /* Case prevented by pipe_config_set_bpp. */
7419 BUG();
7420 }
7421
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007422 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007423 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7424
7425 I915_WRITE(PIPEMISC(pipe), val);
7426 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007427}
7428
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007429static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007430 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007431 intel_clock_t *clock,
7432 bool *has_reduced_clock,
7433 intel_clock_t *reduced_clock)
7434{
7435 struct drm_device *dev = crtc->dev;
7436 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007437 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007438 int refclk;
7439 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02007440 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007441
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007442 is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007443
7444 refclk = ironlake_get_refclk(crtc);
7445
7446 /*
7447 * Returns a set of divisors for the desired target clock with the given
7448 * refclk, or FALSE. The returned values represent the clock equation:
7449 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7450 */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007451 limit = intel_limit(intel_crtc, refclk);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007452 ret = dev_priv->display.find_dpll(limit, intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007453 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007454 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007455 if (!ret)
7456 return false;
7457
7458 if (is_lvds && dev_priv->lvds_downclock_avail) {
7459 /*
7460 * Ensure we match the reduced clock's P to the target clock.
7461 * If the clocks don't match, we can't switch the display clock
7462 * by using the FP0/FP1. In such case we will disable the LVDS
7463 * downclock feature.
7464 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02007465 *has_reduced_clock =
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007466 dev_priv->display.find_dpll(limit, intel_crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007467 dev_priv->lvds_downclock,
7468 refclk, clock,
7469 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007470 }
7471
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007472 return true;
7473}
7474
Paulo Zanonid4b19312012-11-29 11:29:32 -02007475int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7476{
7477 /*
7478 * Account for spread spectrum to avoid
7479 * oversubscribing the link. Max center spread
7480 * is 2.5%; use 5% for safety's sake.
7481 */
7482 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02007483 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02007484}
7485
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007486static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02007487{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007488 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007489}
7490
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007491static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007492 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007493 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007494 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007495{
7496 struct drm_crtc *crtc = &intel_crtc->base;
7497 struct drm_device *dev = crtc->dev;
7498 struct drm_i915_private *dev_priv = dev->dev_private;
7499 struct intel_encoder *intel_encoder;
7500 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007501 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02007502 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007503
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007504 for_each_intel_encoder(dev, intel_encoder) {
7505 if (intel_encoder->new_crtc != to_intel_crtc(crtc))
7506 continue;
7507
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007508 switch (intel_encoder->type) {
7509 case INTEL_OUTPUT_LVDS:
7510 is_lvds = true;
7511 break;
7512 case INTEL_OUTPUT_SDVO:
7513 case INTEL_OUTPUT_HDMI:
7514 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007515 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007516 default:
7517 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007518 }
7519
7520 num_connectors++;
7521 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007522
Chris Wilsonc1858122010-12-03 21:35:48 +00007523 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07007524 factor = 21;
7525 if (is_lvds) {
7526 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007527 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02007528 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07007529 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007530 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07007531 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00007532
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007533 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02007534 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00007535
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007536 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7537 *fp2 |= FP_CB_TUNE;
7538
Chris Wilson5eddb702010-09-11 13:48:45 +01007539 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08007540
Eric Anholta07d6782011-03-30 13:01:08 -07007541 if (is_lvds)
7542 dpll |= DPLLB_MODE_LVDS;
7543 else
7544 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007545
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007546 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007547 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007548
7549 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007550 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007551 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007552 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08007553
Eric Anholta07d6782011-03-30 13:01:08 -07007554 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007555 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007556 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007557 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007558
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007559 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07007560 case 5:
7561 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7562 break;
7563 case 7:
7564 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7565 break;
7566 case 10:
7567 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7568 break;
7569 case 14:
7570 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7571 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007572 }
7573
Daniel Vetterb4c09f32013-04-30 14:01:42 +02007574 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007575 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08007576 else
7577 dpll |= PLL_REF_INPUT_DREFCLK;
7578
Daniel Vetter959e16d2013-06-05 13:34:21 +02007579 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007580}
7581
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007582static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
7583 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007584{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007585 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007586 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007587 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03007588 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01007589 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007590 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007591
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007592 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08007593
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007594 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7595 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
7596
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007597 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007598 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007599 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007600 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7601 return -EINVAL;
7602 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01007603 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007604 if (!crtc_state->clock_set) {
7605 crtc_state->dpll.n = clock.n;
7606 crtc_state->dpll.m1 = clock.m1;
7607 crtc_state->dpll.m2 = clock.m2;
7608 crtc_state->dpll.p1 = clock.p1;
7609 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007610 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007611
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007612 /* 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 +02007613 if (crtc_state->has_pch_encoder) {
7614 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007615 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007616 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007617
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007618 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007619 &fp, &reduced_clock,
7620 has_reduced_clock ? &fp2 : NULL);
7621
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007622 crtc_state->dpll_hw_state.dpll = dpll;
7623 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007624 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007625 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007626 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007627 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007628
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007629 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007630 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03007631 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007632 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07007633 return -EINVAL;
7634 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02007635 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007636
Jani Nikulad330a952014-01-21 11:24:25 +02007637 if (is_lvds && has_reduced_clock && i915.powersave)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007638 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02007639 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007640 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007641
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007642 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007643}
7644
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007645static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7646 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02007647{
7648 struct drm_device *dev = crtc->base.dev;
7649 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007650 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02007651
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007652 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7653 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7654 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7655 & ~TU_SIZE_MASK;
7656 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7657 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7658 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7659}
7660
7661static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7662 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007663 struct intel_link_m_n *m_n,
7664 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007665{
7666 struct drm_device *dev = crtc->base.dev;
7667 struct drm_i915_private *dev_priv = dev->dev_private;
7668 enum pipe pipe = crtc->pipe;
7669
7670 if (INTEL_INFO(dev)->gen >= 5) {
7671 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7672 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7673 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7674 & ~TU_SIZE_MASK;
7675 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7676 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7677 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007678 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7679 * gen < 8) and if DRRS is supported (to make sure the
7680 * registers are not unnecessarily read).
7681 */
7682 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007683 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007684 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7685 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7686 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7687 & ~TU_SIZE_MASK;
7688 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7689 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7690 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7691 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007692 } else {
7693 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7694 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7695 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7696 & ~TU_SIZE_MASK;
7697 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7698 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7699 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7700 }
7701}
7702
7703void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007704 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007705{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007706 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007707 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7708 else
7709 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007710 &pipe_config->dp_m_n,
7711 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007712}
7713
Daniel Vetter72419202013-04-04 13:28:53 +02007714static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007715 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02007716{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007717 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007718 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02007719}
7720
Jesse Barnesbd2e2442014-11-13 17:51:47 +00007721static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007722 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00007723{
7724 struct drm_device *dev = crtc->base.dev;
7725 struct drm_i915_private *dev_priv = dev->dev_private;
7726 uint32_t tmp;
7727
7728 tmp = I915_READ(PS_CTL(crtc->pipe));
7729
7730 if (tmp & PS_ENABLE) {
7731 pipe_config->pch_pfit.enabled = true;
7732 pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
7733 pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
7734 }
7735}
7736
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007737static void
7738skylake_get_initial_plane_config(struct intel_crtc *crtc,
7739 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007740{
7741 struct drm_device *dev = crtc->base.dev;
7742 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00007743 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007744 int pipe = crtc->pipe;
7745 int fourcc, pixel_format;
7746 int aligned_height;
7747 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007748 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007749
Damien Lespiaud9806c92015-01-21 14:07:19 +00007750 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007751 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007752 DRM_DEBUG_KMS("failed to alloc fb\n");
7753 return;
7754 }
7755
Damien Lespiau1b842c82015-01-21 13:50:54 +00007756 fb = &intel_fb->base;
7757
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007758 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00007759 if (!(val & PLANE_CTL_ENABLE))
7760 goto error;
7761
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007762 pixel_format = val & PLANE_CTL_FORMAT_MASK;
7763 fourcc = skl_format_to_fourcc(pixel_format,
7764 val & PLANE_CTL_ORDER_RGBX,
7765 val & PLANE_CTL_ALPHA_MASK);
7766 fb->pixel_format = fourcc;
7767 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
7768
Damien Lespiau40f46282015-02-27 11:15:21 +00007769 tiling = val & PLANE_CTL_TILED_MASK;
7770 switch (tiling) {
7771 case PLANE_CTL_TILED_LINEAR:
7772 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
7773 break;
7774 case PLANE_CTL_TILED_X:
7775 plane_config->tiling = I915_TILING_X;
7776 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7777 break;
7778 case PLANE_CTL_TILED_Y:
7779 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
7780 break;
7781 case PLANE_CTL_TILED_YF:
7782 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
7783 break;
7784 default:
7785 MISSING_CASE(tiling);
7786 goto error;
7787 }
7788
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007789 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
7790 plane_config->base = base;
7791
7792 offset = I915_READ(PLANE_OFFSET(pipe, 0));
7793
7794 val = I915_READ(PLANE_SIZE(pipe, 0));
7795 fb->height = ((val >> 16) & 0xfff) + 1;
7796 fb->width = ((val >> 0) & 0x1fff) + 1;
7797
7798 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00007799 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
7800 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007801 fb->pitches[0] = (val & 0x3ff) * stride_mult;
7802
7803 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007804 fb->pixel_format,
7805 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007806
7807 plane_config->size = ALIGN(fb->pitches[0] * aligned_height, PAGE_SIZE);
7808
7809 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7810 pipe_name(pipe), fb->width, fb->height,
7811 fb->bits_per_pixel, base, fb->pitches[0],
7812 plane_config->size);
7813
Damien Lespiau2d140302015-02-05 17:22:18 +00007814 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00007815 return;
7816
7817error:
7818 kfree(fb);
7819}
7820
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007821static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007822 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007823{
7824 struct drm_device *dev = crtc->base.dev;
7825 struct drm_i915_private *dev_priv = dev->dev_private;
7826 uint32_t tmp;
7827
7828 tmp = I915_READ(PF_CTL(crtc->pipe));
7829
7830 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01007831 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007832 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7833 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02007834
7835 /* We currently do not free assignements of panel fitters on
7836 * ivb/hsw (since we don't use the higher upscaling modes which
7837 * differentiates them) so just WARN about this case for now. */
7838 if (IS_GEN7(dev)) {
7839 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7840 PF_PIPE_SEL_IVB(crtc->pipe));
7841 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007842 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007843}
7844
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007845static void
7846ironlake_get_initial_plane_config(struct intel_crtc *crtc,
7847 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007848{
7849 struct drm_device *dev = crtc->base.dev;
7850 struct drm_i915_private *dev_priv = dev->dev_private;
7851 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00007852 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007853 int fourcc, pixel_format;
7854 int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007855 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007856 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007857
Damien Lespiau42a7b082015-02-05 19:35:13 +00007858 val = I915_READ(DSPCNTR(pipe));
7859 if (!(val & DISPLAY_PLANE_ENABLE))
7860 return;
7861
Damien Lespiaud9806c92015-01-21 14:07:19 +00007862 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007863 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007864 DRM_DEBUG_KMS("failed to alloc fb\n");
7865 return;
7866 }
7867
Damien Lespiau1b842c82015-01-21 13:50:54 +00007868 fb = &intel_fb->base;
7869
Daniel Vetter18c52472015-02-10 17:16:09 +00007870 if (INTEL_INFO(dev)->gen >= 4) {
7871 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007872 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007873 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7874 }
7875 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007876
7877 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007878 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007879 fb->pixel_format = fourcc;
7880 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007881
Damien Lespiauaeee5a42015-01-20 12:51:47 +00007882 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007883 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00007884 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007885 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00007886 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00007887 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007888 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00007889 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007890 }
7891 plane_config->base = base;
7892
7893 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007894 fb->width = ((val >> 16) & 0xfff) + 1;
7895 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007896
7897 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007898 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007899
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007900 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007901 fb->pixel_format,
7902 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007903
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007904 plane_config->size = PAGE_ALIGN(fb->pitches[0] * aligned_height);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007905
Damien Lespiau2844a922015-01-20 12:51:48 +00007906 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7907 pipe_name(pipe), fb->width, fb->height,
7908 fb->bits_per_pixel, base, fb->pitches[0],
7909 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007910
Damien Lespiau2d140302015-02-05 17:22:18 +00007911 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007912}
7913
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007914static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007915 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007916{
7917 struct drm_device *dev = crtc->base.dev;
7918 struct drm_i915_private *dev_priv = dev->dev_private;
7919 uint32_t tmp;
7920
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007921 if (!intel_display_power_is_enabled(dev_priv,
7922 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03007923 return false;
7924
Daniel Vettere143a212013-07-04 12:01:15 +02007925 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007926 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007927
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007928 tmp = I915_READ(PIPECONF(crtc->pipe));
7929 if (!(tmp & PIPECONF_ENABLE))
7930 return false;
7931
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007932 switch (tmp & PIPECONF_BPC_MASK) {
7933 case PIPECONF_6BPC:
7934 pipe_config->pipe_bpp = 18;
7935 break;
7936 case PIPECONF_8BPC:
7937 pipe_config->pipe_bpp = 24;
7938 break;
7939 case PIPECONF_10BPC:
7940 pipe_config->pipe_bpp = 30;
7941 break;
7942 case PIPECONF_12BPC:
7943 pipe_config->pipe_bpp = 36;
7944 break;
7945 default:
7946 break;
7947 }
7948
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007949 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7950 pipe_config->limited_color_range = true;
7951
Daniel Vetterab9412b2013-05-03 11:49:46 +02007952 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02007953 struct intel_shared_dpll *pll;
7954
Daniel Vetter88adfff2013-03-28 10:42:01 +01007955 pipe_config->has_pch_encoder = true;
7956
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007957 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7958 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7959 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007960
7961 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007962
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007963 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02007964 pipe_config->shared_dpll =
7965 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007966 } else {
7967 tmp = I915_READ(PCH_DPLL_SEL);
7968 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7969 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7970 else
7971 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7972 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02007973
7974 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7975
7976 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7977 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02007978
7979 tmp = pipe_config->dpll_hw_state.dpll;
7980 pipe_config->pixel_multiplier =
7981 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7982 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007983
7984 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007985 } else {
7986 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007987 }
7988
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007989 intel_get_pipe_timings(crtc, pipe_config);
7990
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007991 ironlake_get_pfit_config(crtc, pipe_config);
7992
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007993 return true;
7994}
7995
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007996static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7997{
7998 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007999 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008000
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008001 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008002 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008003 pipe_name(crtc->pipe));
8004
Rob Clarke2c719b2014-12-15 13:56:32 -05008005 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8006 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8007 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8008 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8009 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8010 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008011 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008012 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008013 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008014 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008015 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008016 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008017 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008018 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008019 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008020
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008021 /*
8022 * In theory we can still leave IRQs enabled, as long as only the HPD
8023 * interrupts remain enabled. We used to check for that, but since it's
8024 * gen-specific and since we only disable LCPLL after we fully disable
8025 * the interrupts, the check below should be enough.
8026 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008027 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008028}
8029
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008030static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8031{
8032 struct drm_device *dev = dev_priv->dev;
8033
8034 if (IS_HASWELL(dev))
8035 return I915_READ(D_COMP_HSW);
8036 else
8037 return I915_READ(D_COMP_BDW);
8038}
8039
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008040static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8041{
8042 struct drm_device *dev = dev_priv->dev;
8043
8044 if (IS_HASWELL(dev)) {
8045 mutex_lock(&dev_priv->rps.hw_lock);
8046 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8047 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008048 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008049 mutex_unlock(&dev_priv->rps.hw_lock);
8050 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008051 I915_WRITE(D_COMP_BDW, val);
8052 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008053 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008054}
8055
8056/*
8057 * This function implements pieces of two sequences from BSpec:
8058 * - Sequence for display software to disable LCPLL
8059 * - Sequence for display software to allow package C8+
8060 * The steps implemented here are just the steps that actually touch the LCPLL
8061 * register. Callers should take care of disabling all the display engine
8062 * functions, doing the mode unset, fixing interrupts, etc.
8063 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008064static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8065 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008066{
8067 uint32_t val;
8068
8069 assert_can_disable_lcpll(dev_priv);
8070
8071 val = I915_READ(LCPLL_CTL);
8072
8073 if (switch_to_fclk) {
8074 val |= LCPLL_CD_SOURCE_FCLK;
8075 I915_WRITE(LCPLL_CTL, val);
8076
8077 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
8078 LCPLL_CD_SOURCE_FCLK_DONE, 1))
8079 DRM_ERROR("Switching to FCLK failed\n");
8080
8081 val = I915_READ(LCPLL_CTL);
8082 }
8083
8084 val |= LCPLL_PLL_DISABLE;
8085 I915_WRITE(LCPLL_CTL, val);
8086 POSTING_READ(LCPLL_CTL);
8087
8088 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
8089 DRM_ERROR("LCPLL still locked\n");
8090
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008091 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008092 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008093 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008094 ndelay(100);
8095
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008096 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8097 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008098 DRM_ERROR("D_COMP RCOMP still in progress\n");
8099
8100 if (allow_power_down) {
8101 val = I915_READ(LCPLL_CTL);
8102 val |= LCPLL_POWER_DOWN_ALLOW;
8103 I915_WRITE(LCPLL_CTL, val);
8104 POSTING_READ(LCPLL_CTL);
8105 }
8106}
8107
8108/*
8109 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8110 * source.
8111 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008112static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008113{
8114 uint32_t val;
8115
8116 val = I915_READ(LCPLL_CTL);
8117
8118 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8119 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8120 return;
8121
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008122 /*
8123 * Make sure we're not on PC8 state before disabling PC8, otherwise
8124 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008125 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02008126 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03008127
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008128 if (val & LCPLL_POWER_DOWN_ALLOW) {
8129 val &= ~LCPLL_POWER_DOWN_ALLOW;
8130 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02008131 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008132 }
8133
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008134 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008135 val |= D_COMP_COMP_FORCE;
8136 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008137 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008138
8139 val = I915_READ(LCPLL_CTL);
8140 val &= ~LCPLL_PLL_DISABLE;
8141 I915_WRITE(LCPLL_CTL, val);
8142
8143 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
8144 DRM_ERROR("LCPLL not locked yet\n");
8145
8146 if (val & LCPLL_CD_SOURCE_FCLK) {
8147 val = I915_READ(LCPLL_CTL);
8148 val &= ~LCPLL_CD_SOURCE_FCLK;
8149 I915_WRITE(LCPLL_CTL, val);
8150
8151 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
8152 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
8153 DRM_ERROR("Switching back to LCPLL failed\n");
8154 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03008155
Mika Kuoppala59bad942015-01-16 11:34:40 +02008156 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008157}
8158
Paulo Zanoni765dab672014-03-07 20:08:18 -03008159/*
8160 * Package states C8 and deeper are really deep PC states that can only be
8161 * reached when all the devices on the system allow it, so even if the graphics
8162 * device allows PC8+, it doesn't mean the system will actually get to these
8163 * states. Our driver only allows PC8+ when going into runtime PM.
8164 *
8165 * The requirements for PC8+ are that all the outputs are disabled, the power
8166 * well is disabled and most interrupts are disabled, and these are also
8167 * requirements for runtime PM. When these conditions are met, we manually do
8168 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8169 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8170 * hang the machine.
8171 *
8172 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8173 * the state of some registers, so when we come back from PC8+ we need to
8174 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8175 * need to take care of the registers kept by RC6. Notice that this happens even
8176 * if we don't put the device in PCI D3 state (which is what currently happens
8177 * because of the runtime PM support).
8178 *
8179 * For more, read "Display Sequences for Package C8" on the hardware
8180 * documentation.
8181 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008182void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008183{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008184 struct drm_device *dev = dev_priv->dev;
8185 uint32_t val;
8186
Paulo Zanonic67a4702013-08-19 13:18:09 -03008187 DRM_DEBUG_KMS("Enabling package C8+\n");
8188
Paulo Zanonic67a4702013-08-19 13:18:09 -03008189 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8190 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8191 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8192 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8193 }
8194
8195 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008196 hsw_disable_lcpll(dev_priv, true, true);
8197}
8198
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008199void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008200{
8201 struct drm_device *dev = dev_priv->dev;
8202 uint32_t val;
8203
Paulo Zanonic67a4702013-08-19 13:18:09 -03008204 DRM_DEBUG_KMS("Disabling package C8+\n");
8205
8206 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008207 lpt_init_pch_refclk(dev);
8208
8209 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8210 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8211 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8212 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8213 }
8214
8215 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008216}
8217
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008218static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8219 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008220{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008221 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008222 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03008223
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008224 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02008225
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008226 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008227}
8228
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008229static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8230 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008231 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008232{
Damien Lespiau3148ade2014-11-21 16:14:56 +00008233 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008234
8235 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
8236 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
8237
8238 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00008239 case SKL_DPLL0:
8240 /*
8241 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
8242 * of the shared DPLL framework and thus needs to be read out
8243 * separately
8244 */
8245 dpll_ctl1 = I915_READ(DPLL_CTRL1);
8246 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
8247 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008248 case SKL_DPLL1:
8249 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
8250 break;
8251 case SKL_DPLL2:
8252 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
8253 break;
8254 case SKL_DPLL3:
8255 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
8256 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008257 }
8258}
8259
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008260static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8261 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008262 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008263{
8264 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8265
8266 switch (pipe_config->ddi_pll_sel) {
8267 case PORT_CLK_SEL_WRPLL1:
8268 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
8269 break;
8270 case PORT_CLK_SEL_WRPLL2:
8271 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
8272 break;
8273 }
8274}
8275
Daniel Vetter26804af2014-06-25 22:01:55 +03008276static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008277 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03008278{
8279 struct drm_device *dev = crtc->base.dev;
8280 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03008281 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03008282 enum port port;
8283 uint32_t tmp;
8284
8285 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
8286
8287 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
8288
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008289 if (IS_SKYLAKE(dev))
8290 skylake_get_ddi_pll(dev_priv, port, pipe_config);
8291 else
8292 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03008293
Daniel Vetterd452c5b2014-07-04 11:27:39 -03008294 if (pipe_config->shared_dpll >= 0) {
8295 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8296
8297 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8298 &pipe_config->dpll_hw_state));
8299 }
8300
Daniel Vetter26804af2014-06-25 22:01:55 +03008301 /*
8302 * Haswell has only FDI/PCH transcoder A. It is which is connected to
8303 * DDI E. So just check whether this pipe is wired to DDI E and whether
8304 * the PCH transcoder is on.
8305 */
Damien Lespiauca370452013-12-03 13:56:24 +00008306 if (INTEL_INFO(dev)->gen < 9 &&
8307 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03008308 pipe_config->has_pch_encoder = true;
8309
8310 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
8311 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8312 FDI_DP_PORT_WIDTH_SHIFT) + 1;
8313
8314 ironlake_get_fdi_m_n_config(crtc, pipe_config);
8315 }
8316}
8317
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008318static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008319 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008320{
8321 struct drm_device *dev = crtc->base.dev;
8322 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008323 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008324 uint32_t tmp;
8325
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008326 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02008327 POWER_DOMAIN_PIPE(crtc->pipe)))
8328 return false;
8329
Daniel Vettere143a212013-07-04 12:01:15 +02008330 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008331 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8332
Daniel Vettereccb1402013-05-22 00:50:22 +02008333 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8334 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8335 enum pipe trans_edp_pipe;
8336 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8337 default:
8338 WARN(1, "unknown pipe linked to edp transcoder\n");
8339 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8340 case TRANS_DDI_EDP_INPUT_A_ON:
8341 trans_edp_pipe = PIPE_A;
8342 break;
8343 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8344 trans_edp_pipe = PIPE_B;
8345 break;
8346 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8347 trans_edp_pipe = PIPE_C;
8348 break;
8349 }
8350
8351 if (trans_edp_pipe == crtc->pipe)
8352 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8353 }
8354
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008355 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02008356 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03008357 return false;
8358
Daniel Vettereccb1402013-05-22 00:50:22 +02008359 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008360 if (!(tmp & PIPECONF_ENABLE))
8361 return false;
8362
Daniel Vetter26804af2014-06-25 22:01:55 +03008363 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008364
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008365 intel_get_pipe_timings(crtc, pipe_config);
8366
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008367 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008368 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
8369 if (IS_SKYLAKE(dev))
8370 skylake_get_pfit_config(crtc, pipe_config);
8371 else
8372 ironlake_get_pfit_config(crtc, pipe_config);
8373 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01008374
Jesse Barnese59150d2014-01-07 13:30:45 -08008375 if (IS_HASWELL(dev))
8376 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
8377 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008378
Clint Taylorebb69c92014-09-30 10:30:22 -07008379 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
8380 pipe_config->pixel_multiplier =
8381 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
8382 } else {
8383 pipe_config->pixel_multiplier = 1;
8384 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008385
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008386 return true;
8387}
8388
Chris Wilson560b85b2010-08-07 11:01:38 +01008389static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8390{
8391 struct drm_device *dev = crtc->dev;
8392 struct drm_i915_private *dev_priv = dev->dev_private;
8393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03008394 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01008395
Ville Syrjälädc41c152014-08-13 11:57:05 +03008396 if (base) {
8397 unsigned int width = intel_crtc->cursor_width;
8398 unsigned int height = intel_crtc->cursor_height;
8399 unsigned int stride = roundup_pow_of_two(width) * 4;
8400
8401 switch (stride) {
8402 default:
8403 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8404 width, stride);
8405 stride = 256;
8406 /* fallthrough */
8407 case 256:
8408 case 512:
8409 case 1024:
8410 case 2048:
8411 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008412 }
8413
Ville Syrjälädc41c152014-08-13 11:57:05 +03008414 cntl |= CURSOR_ENABLE |
8415 CURSOR_GAMMA_ENABLE |
8416 CURSOR_FORMAT_ARGB |
8417 CURSOR_STRIDE(stride);
8418
8419 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008420 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008421
Ville Syrjälädc41c152014-08-13 11:57:05 +03008422 if (intel_crtc->cursor_cntl != 0 &&
8423 (intel_crtc->cursor_base != base ||
8424 intel_crtc->cursor_size != size ||
8425 intel_crtc->cursor_cntl != cntl)) {
8426 /* On these chipsets we can only modify the base/size/stride
8427 * whilst the cursor is disabled.
8428 */
8429 I915_WRITE(_CURACNTR, 0);
8430 POSTING_READ(_CURACNTR);
8431 intel_crtc->cursor_cntl = 0;
8432 }
8433
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008434 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03008435 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008436 intel_crtc->cursor_base = base;
8437 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03008438
8439 if (intel_crtc->cursor_size != size) {
8440 I915_WRITE(CURSIZE, size);
8441 intel_crtc->cursor_size = size;
8442 }
8443
Chris Wilson4b0e3332014-05-30 16:35:26 +03008444 if (intel_crtc->cursor_cntl != cntl) {
8445 I915_WRITE(_CURACNTR, cntl);
8446 POSTING_READ(_CURACNTR);
8447 intel_crtc->cursor_cntl = cntl;
8448 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008449}
8450
8451static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
8452{
8453 struct drm_device *dev = crtc->dev;
8454 struct drm_i915_private *dev_priv = dev->dev_private;
8455 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8456 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008457 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01008458
Chris Wilson4b0e3332014-05-30 16:35:26 +03008459 cntl = 0;
8460 if (base) {
8461 cntl = MCURSOR_GAMMA_ENABLE;
8462 switch (intel_crtc->cursor_width) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308463 case 64:
8464 cntl |= CURSOR_MODE_64_ARGB_AX;
8465 break;
8466 case 128:
8467 cntl |= CURSOR_MODE_128_ARGB_AX;
8468 break;
8469 case 256:
8470 cntl |= CURSOR_MODE_256_ARGB_AX;
8471 break;
8472 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01008473 MISSING_CASE(intel_crtc->cursor_width);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308474 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01008475 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008476 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03008477
8478 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8479 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01008480 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008481
Matt Roper8e7d6882015-01-21 16:35:41 -08008482 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008483 cntl |= CURSOR_ROTATE_180;
8484
Chris Wilson4b0e3332014-05-30 16:35:26 +03008485 if (intel_crtc->cursor_cntl != cntl) {
8486 I915_WRITE(CURCNTR(pipe), cntl);
8487 POSTING_READ(CURCNTR(pipe));
8488 intel_crtc->cursor_cntl = cntl;
8489 }
8490
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008491 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008492 I915_WRITE(CURBASE(pipe), base);
8493 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008494
8495 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008496}
8497
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008498/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01008499static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8500 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008501{
8502 struct drm_device *dev = crtc->dev;
8503 struct drm_i915_private *dev_priv = dev->dev_private;
8504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8505 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07008506 int x = crtc->cursor_x;
8507 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008508 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008509
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008510 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008511 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008512
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008513 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008514 base = 0;
8515
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008516 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008517 base = 0;
8518
8519 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008520 if (x + intel_crtc->cursor_width <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008521 base = 0;
8522
8523 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8524 x = -x;
8525 }
8526 pos |= x << CURSOR_X_SHIFT;
8527
8528 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008529 if (y + intel_crtc->cursor_height <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008530 base = 0;
8531
8532 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8533 y = -y;
8534 }
8535 pos |= y << CURSOR_Y_SHIFT;
8536
Chris Wilson4b0e3332014-05-30 16:35:26 +03008537 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008538 return;
8539
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008540 I915_WRITE(CURPOS(pipe), pos);
8541
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008542 /* ILK+ do this automagically */
8543 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08008544 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008545 base += (intel_crtc->cursor_height *
8546 intel_crtc->cursor_width - 1) * 4;
8547 }
8548
Ville Syrjälä8ac54662014-08-12 19:39:54 +03008549 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008550 i845_update_cursor(crtc, base);
8551 else
8552 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008553}
8554
Ville Syrjälädc41c152014-08-13 11:57:05 +03008555static bool cursor_size_ok(struct drm_device *dev,
8556 uint32_t width, uint32_t height)
8557{
8558 if (width == 0 || height == 0)
8559 return false;
8560
8561 /*
8562 * 845g/865g are special in that they are only limited by
8563 * the width of their cursors, the height is arbitrary up to
8564 * the precision of the register. Everything else requires
8565 * square cursors, limited to a few power-of-two sizes.
8566 */
8567 if (IS_845G(dev) || IS_I865G(dev)) {
8568 if ((width & 63) != 0)
8569 return false;
8570
8571 if (width > (IS_845G(dev) ? 64 : 512))
8572 return false;
8573
8574 if (height > 1023)
8575 return false;
8576 } else {
8577 switch (width | height) {
8578 case 256:
8579 case 128:
8580 if (IS_GEN2(dev))
8581 return false;
8582 case 64:
8583 break;
8584 default:
8585 return false;
8586 }
8587 }
8588
8589 return true;
8590}
8591
Jesse Barnes79e53942008-11-07 14:24:08 -08008592static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01008593 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08008594{
James Simmons72034252010-08-03 01:33:19 +01008595 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008597
James Simmons72034252010-08-03 01:33:19 +01008598 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008599 intel_crtc->lut_r[i] = red[i] >> 8;
8600 intel_crtc->lut_g[i] = green[i] >> 8;
8601 intel_crtc->lut_b[i] = blue[i] >> 8;
8602 }
8603
8604 intel_crtc_load_lut(crtc);
8605}
8606
Jesse Barnes79e53942008-11-07 14:24:08 -08008607/* VESA 640x480x72Hz mode to set on the pipe */
8608static struct drm_display_mode load_detect_mode = {
8609 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8610 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8611};
8612
Daniel Vettera8bb6812014-02-10 18:00:39 +01008613struct drm_framebuffer *
8614__intel_framebuffer_create(struct drm_device *dev,
8615 struct drm_mode_fb_cmd2 *mode_cmd,
8616 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01008617{
8618 struct intel_framebuffer *intel_fb;
8619 int ret;
8620
8621 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8622 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03008623 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01008624 return ERR_PTR(-ENOMEM);
8625 }
8626
8627 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008628 if (ret)
8629 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01008630
8631 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008632err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03008633 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008634 kfree(intel_fb);
8635
8636 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01008637}
8638
Daniel Vetterb5ea6422014-03-02 21:18:00 +01008639static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01008640intel_framebuffer_create(struct drm_device *dev,
8641 struct drm_mode_fb_cmd2 *mode_cmd,
8642 struct drm_i915_gem_object *obj)
8643{
8644 struct drm_framebuffer *fb;
8645 int ret;
8646
8647 ret = i915_mutex_lock_interruptible(dev);
8648 if (ret)
8649 return ERR_PTR(ret);
8650 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8651 mutex_unlock(&dev->struct_mutex);
8652
8653 return fb;
8654}
8655
Chris Wilsond2dff872011-04-19 08:36:26 +01008656static u32
8657intel_framebuffer_pitch_for_width(int width, int bpp)
8658{
8659 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8660 return ALIGN(pitch, 64);
8661}
8662
8663static u32
8664intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8665{
8666 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02008667 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01008668}
8669
8670static struct drm_framebuffer *
8671intel_framebuffer_create_for_mode(struct drm_device *dev,
8672 struct drm_display_mode *mode,
8673 int depth, int bpp)
8674{
8675 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00008676 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01008677
8678 obj = i915_gem_alloc_object(dev,
8679 intel_framebuffer_size_for_mode(mode, bpp));
8680 if (obj == NULL)
8681 return ERR_PTR(-ENOMEM);
8682
8683 mode_cmd.width = mode->hdisplay;
8684 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008685 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8686 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00008687 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01008688
8689 return intel_framebuffer_create(dev, &mode_cmd, obj);
8690}
8691
8692static struct drm_framebuffer *
8693mode_fits_in_fbdev(struct drm_device *dev,
8694 struct drm_display_mode *mode)
8695{
Daniel Vetter4520f532013-10-09 09:18:51 +02008696#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01008697 struct drm_i915_private *dev_priv = dev->dev_private;
8698 struct drm_i915_gem_object *obj;
8699 struct drm_framebuffer *fb;
8700
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008701 if (!dev_priv->fbdev)
8702 return NULL;
8703
8704 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01008705 return NULL;
8706
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008707 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008708 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01008709
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008710 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008711 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8712 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01008713 return NULL;
8714
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008715 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01008716 return NULL;
8717
8718 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02008719#else
8720 return NULL;
8721#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01008722}
8723
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008724bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01008725 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05008726 struct intel_load_detect_pipe *old,
8727 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008728{
8729 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008730 struct intel_encoder *intel_encoder =
8731 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08008732 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008733 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008734 struct drm_crtc *crtc = NULL;
8735 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02008736 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05008737 struct drm_mode_config *config = &dev->mode_config;
8738 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008739
Chris Wilsond2dff872011-04-19 08:36:26 +01008740 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008741 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008742 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008743
Rob Clark51fd3712013-11-19 12:10:12 -05008744retry:
8745 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8746 if (ret)
8747 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008748
Jesse Barnes79e53942008-11-07 14:24:08 -08008749 /*
8750 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01008751 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008752 * - if the connector already has an assigned crtc, use it (but make
8753 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01008754 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008755 * - try to find the first unused crtc that can drive this connector,
8756 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08008757 */
8758
8759 /* See if we already have a CRTC for this connector */
8760 if (encoder->crtc) {
8761 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01008762
Rob Clark51fd3712013-11-19 12:10:12 -05008763 ret = drm_modeset_lock(&crtc->mutex, ctx);
8764 if (ret)
8765 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01008766 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
8767 if (ret)
8768 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01008769
Daniel Vetter24218aa2012-08-12 19:27:11 +02008770 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008771 old->load_detect_temp = false;
8772
8773 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008774 if (connector->dpms != DRM_MODE_DPMS_ON)
8775 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01008776
Chris Wilson71731882011-04-19 23:10:58 +01008777 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08008778 }
8779
8780 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008781 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008782 i++;
8783 if (!(encoder->possible_crtcs & (1 << i)))
8784 continue;
Matt Roper83d65732015-02-25 13:12:16 -08008785 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03008786 continue;
8787 /* This can occur when applying the pipe A quirk on resume. */
8788 if (to_intel_crtc(possible_crtc)->new_enabled)
8789 continue;
8790
8791 crtc = possible_crtc;
8792 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008793 }
8794
8795 /*
8796 * If we didn't find an unused CRTC, don't use any.
8797 */
8798 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01008799 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05008800 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08008801 }
8802
Rob Clark51fd3712013-11-19 12:10:12 -05008803 ret = drm_modeset_lock(&crtc->mutex, ctx);
8804 if (ret)
8805 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01008806 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
8807 if (ret)
8808 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02008809 intel_encoder->new_crtc = to_intel_crtc(crtc);
8810 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008811
8812 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008813 intel_crtc->new_enabled = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008814 intel_crtc->new_config = intel_crtc->config;
Daniel Vetter24218aa2012-08-12 19:27:11 +02008815 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008816 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01008817 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08008818
Chris Wilson64927112011-04-20 07:25:26 +01008819 if (!mode)
8820 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08008821
Chris Wilsond2dff872011-04-19 08:36:26 +01008822 /* We need a framebuffer large enough to accommodate all accesses
8823 * that the plane may generate whilst we perform load detection.
8824 * We can not rely on the fbcon either being present (we get called
8825 * during its initialisation to detect all boot displays, or it may
8826 * not even exist) or that it is large enough to satisfy the
8827 * requested mode.
8828 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02008829 fb = mode_fits_in_fbdev(dev, mode);
8830 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008831 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008832 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8833 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01008834 } else
8835 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008836 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008837 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008838 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008839 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008840
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008841 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01008842 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01008843 if (old->release_fb)
8844 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008845 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008846 }
Chris Wilson71731882011-04-19 23:10:58 +01008847
Jesse Barnes79e53942008-11-07 14:24:08 -08008848 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008849 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01008850 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008851
8852 fail:
Matt Roper83d65732015-02-25 13:12:16 -08008853 intel_crtc->new_enabled = crtc->state->enable;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008854 if (intel_crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008855 intel_crtc->new_config = intel_crtc->config;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008856 else
8857 intel_crtc->new_config = NULL;
Rob Clark51fd3712013-11-19 12:10:12 -05008858fail_unlock:
8859 if (ret == -EDEADLK) {
8860 drm_modeset_backoff(ctx);
8861 goto retry;
8862 }
8863
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008864 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08008865}
8866
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008867void intel_release_load_detect_pipe(struct drm_connector *connector,
Ville Syrjälä208bf9f2014-08-11 13:15:35 +03008868 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08008869{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008870 struct intel_encoder *intel_encoder =
8871 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01008872 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01008873 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008874 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008875
Chris Wilsond2dff872011-04-19 08:36:26 +01008876 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008877 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008878 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008879
Chris Wilson8261b192011-04-19 23:18:09 +01008880 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02008881 to_intel_connector(connector)->new_encoder = NULL;
8882 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008883 intel_crtc->new_enabled = false;
8884 intel_crtc->new_config = NULL;
Daniel Vetterfc303102012-07-09 10:40:58 +02008885 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01008886
Daniel Vetter36206362012-12-10 20:42:17 +01008887 if (old->release_fb) {
8888 drm_framebuffer_unregister_private(old->release_fb);
8889 drm_framebuffer_unreference(old->release_fb);
8890 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008891
Chris Wilson0622a532011-04-21 09:32:11 +01008892 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008893 }
8894
Eric Anholtc751ce42010-03-25 11:48:48 -07008895 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008896 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8897 connector->funcs->dpms(connector, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008898}
8899
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008900static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008901 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008902{
8903 struct drm_i915_private *dev_priv = dev->dev_private;
8904 u32 dpll = pipe_config->dpll_hw_state.dpll;
8905
8906 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008907 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008908 else if (HAS_PCH_SPLIT(dev))
8909 return 120000;
8910 else if (!IS_GEN2(dev))
8911 return 96000;
8912 else
8913 return 48000;
8914}
8915
Jesse Barnes79e53942008-11-07 14:24:08 -08008916/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008917static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008918 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08008919{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008920 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008921 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008922 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008923 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008924 u32 fp;
8925 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008926 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08008927
8928 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03008929 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008930 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03008931 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008932
8933 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008934 if (IS_PINEVIEW(dev)) {
8935 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8936 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08008937 } else {
8938 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8939 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8940 }
8941
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008942 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008943 if (IS_PINEVIEW(dev))
8944 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8945 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08008946 else
8947 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08008948 DPLL_FPA01_P1_POST_DIV_SHIFT);
8949
8950 switch (dpll & DPLL_MODE_MASK) {
8951 case DPLLB_MODE_DAC_SERIAL:
8952 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8953 5 : 10;
8954 break;
8955 case DPLLB_MODE_LVDS:
8956 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8957 7 : 14;
8958 break;
8959 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08008960 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08008961 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008962 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008963 }
8964
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008965 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008966 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008967 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008968 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008969 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02008970 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008971 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08008972
8973 if (is_lvds) {
8974 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8975 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008976
8977 if (lvds & LVDS_CLKB_POWER_UP)
8978 clock.p2 = 7;
8979 else
8980 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08008981 } else {
8982 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8983 clock.p1 = 2;
8984 else {
8985 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8986 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8987 }
8988 if (dpll & PLL_P2_DIVIDE_BY_4)
8989 clock.p2 = 4;
8990 else
8991 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08008992 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008993
8994 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008995 }
8996
Ville Syrjälä18442d02013-09-13 16:00:08 +03008997 /*
8998 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01008999 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03009000 * encoder's get_config() function.
9001 */
9002 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009003}
9004
Ville Syrjälä6878da02013-09-13 15:59:11 +03009005int intel_dotclock_calculate(int link_freq,
9006 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009007{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009008 /*
9009 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009010 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009011 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009012 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009013 *
9014 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009015 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08009016 */
9017
Ville Syrjälä6878da02013-09-13 15:59:11 +03009018 if (!m_n->link_n)
9019 return 0;
9020
9021 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9022}
9023
Ville Syrjälä18442d02013-09-13 16:00:08 +03009024static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009025 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03009026{
9027 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009028
9029 /* read out port_clock from the DPLL */
9030 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03009031
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009032 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03009033 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01009034 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03009035 * agree once we know their relationship in the encoder's
9036 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009037 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02009038 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03009039 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
9040 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08009041}
9042
9043/** Returns the currently programmed mode of the given pipe. */
9044struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9045 struct drm_crtc *crtc)
9046{
Jesse Barnes548f2452011-02-17 10:40:53 -08009047 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009048 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009049 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009050 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009051 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009052 int htot = I915_READ(HTOTAL(cpu_transcoder));
9053 int hsync = I915_READ(HSYNC(cpu_transcoder));
9054 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9055 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03009056 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08009057
9058 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9059 if (!mode)
9060 return NULL;
9061
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009062 /*
9063 * Construct a pipe_config sufficient for getting the clock info
9064 * back out of crtc_clock_get.
9065 *
9066 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9067 * to use a real value here instead.
9068 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03009069 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009070 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009071 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9072 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9073 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009074 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
9075
Ville Syrjälä773ae032013-09-23 17:48:20 +03009076 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08009077 mode->hdisplay = (htot & 0xffff) + 1;
9078 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9079 mode->hsync_start = (hsync & 0xffff) + 1;
9080 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9081 mode->vdisplay = (vtot & 0xffff) + 1;
9082 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9083 mode->vsync_start = (vsync & 0xffff) + 1;
9084 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9085
9086 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08009087
9088 return mode;
9089}
9090
Jesse Barnes652c3932009-08-17 13:31:43 -07009091static void intel_decrease_pllclock(struct drm_crtc *crtc)
9092{
9093 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03009094 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07009095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009096
Sonika Jindalbaff2962014-07-22 11:16:35 +05309097 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07009098 return;
9099
9100 if (!dev_priv->lvds_downclock_avail)
9101 return;
9102
9103 /*
9104 * Since this is called by a timer, we should never get here in
9105 * the manual case.
9106 */
9107 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01009108 int pipe = intel_crtc->pipe;
9109 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02009110 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01009111
Zhao Yakui44d98a62009-10-09 11:39:40 +08009112 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009113
Sean Paul8ac5a6d2012-02-13 13:14:51 -05009114 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009115
Chris Wilson074b5e12012-05-02 12:07:06 +01009116 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07009117 dpll |= DISPLAY_RATE_SELECT_FPA1;
9118 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009119 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009120 dpll = I915_READ(dpll_reg);
9121 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08009122 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009123 }
9124
9125}
9126
Chris Wilsonf047e392012-07-21 12:31:41 +01009127void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07009128{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009129 struct drm_i915_private *dev_priv = dev->dev_private;
9130
Chris Wilsonf62a0072014-02-21 17:55:39 +00009131 if (dev_priv->mm.busy)
9132 return;
9133
Paulo Zanoni43694d62014-03-07 20:08:08 -03009134 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009135 i915_update_gfx_val(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00009136 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01009137}
9138
9139void intel_mark_idle(struct drm_device *dev)
9140{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009141 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00009142 struct drm_crtc *crtc;
9143
Chris Wilsonf62a0072014-02-21 17:55:39 +00009144 if (!dev_priv->mm.busy)
9145 return;
9146
9147 dev_priv->mm.busy = false;
9148
Jani Nikulad330a952014-01-21 11:24:25 +02009149 if (!i915.powersave)
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03009150 goto out;
Chris Wilson725a5b52013-01-08 11:02:57 +00009151
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009152 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07009153 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00009154 continue;
9155
9156 intel_decrease_pllclock(crtc);
9157 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009158
Damien Lespiau3d13ef22014-02-07 19:12:47 +00009159 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009160 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03009161
9162out:
Paulo Zanoni43694d62014-03-07 20:08:08 -03009163 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01009164}
9165
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009166static void intel_crtc_set_state(struct intel_crtc *crtc,
9167 struct intel_crtc_state *crtc_state)
9168{
9169 kfree(crtc->config);
9170 crtc->config = crtc_state;
Ander Conselvan de Oliveira16f3f652015-01-15 14:55:27 +02009171 crtc->base.state = &crtc_state->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009172}
9173
Jesse Barnes79e53942008-11-07 14:24:08 -08009174static void intel_crtc_destroy(struct drm_crtc *crtc)
9175{
9176 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009177 struct drm_device *dev = crtc->dev;
9178 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +02009179
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009180 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009181 work = intel_crtc->unpin_work;
9182 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009183 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009184
9185 if (work) {
9186 cancel_work_sync(&work->work);
9187 kfree(work);
9188 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009189
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +02009190 intel_crtc_set_state(intel_crtc, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08009191 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009192
Jesse Barnes79e53942008-11-07 14:24:08 -08009193 kfree(intel_crtc);
9194}
9195
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009196static void intel_unpin_work_fn(struct work_struct *__work)
9197{
9198 struct intel_unpin_work *work =
9199 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009200 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +02009201 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009202
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009203 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +00009204 intel_unpin_fb_obj(intel_fb_obj(work->old_fb));
Chris Wilson05394f32010-11-08 19:18:58 +00009205 drm_gem_object_unreference(&work->pending_flip_obj->base);
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +00009206 drm_framebuffer_unreference(work->old_fb);
Chris Wilsond9e86c02010-11-10 16:40:20 +00009207
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02009208 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +00009209
9210 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +00009211 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009212 mutex_unlock(&dev->struct_mutex);
9213
Daniel Vetterf99d7062014-06-19 16:01:59 +02009214 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
9215
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009216 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9217 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9218
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009219 kfree(work);
9220}
9221
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009222static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01009223 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009224{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009225 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9226 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009227 unsigned long flags;
9228
9229 /* Ignore early vblank irqs */
9230 if (intel_crtc == NULL)
9231 return;
9232
Daniel Vetterf3260382014-09-15 14:55:23 +02009233 /*
9234 * This is called both by irq handlers and the reset code (to complete
9235 * lost pageflips) so needs the full irqsave spinlocks.
9236 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009237 spin_lock_irqsave(&dev->event_lock, flags);
9238 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00009239
9240 /* Ensure we don't miss a work->pending update ... */
9241 smp_rmb();
9242
9243 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009244 spin_unlock_irqrestore(&dev->event_lock, flags);
9245 return;
9246 }
9247
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009248 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01009249
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009250 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009251}
9252
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009253void intel_finish_page_flip(struct drm_device *dev, int pipe)
9254{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009255 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009256 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9257
Mario Kleiner49b14a52010-12-09 07:00:07 +01009258 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009259}
9260
9261void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9262{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009263 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009264 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9265
Mario Kleiner49b14a52010-12-09 07:00:07 +01009266 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009267}
9268
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009269/* Is 'a' after or equal to 'b'? */
9270static bool g4x_flip_count_after_eq(u32 a, u32 b)
9271{
9272 return !((a - b) & 0x80000000);
9273}
9274
9275static bool page_flip_finished(struct intel_crtc *crtc)
9276{
9277 struct drm_device *dev = crtc->base.dev;
9278 struct drm_i915_private *dev_priv = dev->dev_private;
9279
Ville Syrjäläbdfa7542014-05-27 21:33:09 +03009280 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
9281 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
9282 return true;
9283
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009284 /*
9285 * The relevant registers doen't exist on pre-ctg.
9286 * As the flip done interrupt doesn't trigger for mmio
9287 * flips on gmch platforms, a flip count check isn't
9288 * really needed there. But since ctg has the registers,
9289 * include it in the check anyway.
9290 */
9291 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9292 return true;
9293
9294 /*
9295 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9296 * used the same base address. In that case the mmio flip might
9297 * have completed, but the CS hasn't even executed the flip yet.
9298 *
9299 * A flip count check isn't enough as the CS might have updated
9300 * the base address just after start of vblank, but before we
9301 * managed to process the interrupt. This means we'd complete the
9302 * CS flip too soon.
9303 *
9304 * Combining both checks should get us a good enough result. It may
9305 * still happen that the CS flip has been executed, but has not
9306 * yet actually completed. But in case the base address is the same
9307 * anyway, we don't really care.
9308 */
9309 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9310 crtc->unpin_work->gtt_offset &&
9311 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9312 crtc->unpin_work->flip_count);
9313}
9314
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009315void intel_prepare_page_flip(struct drm_device *dev, int plane)
9316{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009317 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009318 struct intel_crtc *intel_crtc =
9319 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9320 unsigned long flags;
9321
Daniel Vetterf3260382014-09-15 14:55:23 +02009322
9323 /*
9324 * This is called both by irq handlers and the reset code (to complete
9325 * lost pageflips) so needs the full irqsave spinlocks.
9326 *
9327 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +00009328 * generate a page-flip completion irq, i.e. every modeset
9329 * is also accompanied by a spurious intel_prepare_page_flip().
9330 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009331 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009332 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00009333 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009334 spin_unlock_irqrestore(&dev->event_lock, flags);
9335}
9336
Robin Schroereba905b2014-05-18 02:24:50 +02009337static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00009338{
9339 /* Ensure that the work item is consistent when activating it ... */
9340 smp_wmb();
9341 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9342 /* and that it is marked active as soon as the irq could fire. */
9343 smp_wmb();
9344}
9345
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009346static int intel_gen2_queue_flip(struct drm_device *dev,
9347 struct drm_crtc *crtc,
9348 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009349 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009350 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009351 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009352{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009353 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009354 u32 flip_mask;
9355 int ret;
9356
Daniel Vetter6d90c952012-04-26 23:28:05 +02009357 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009358 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009359 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009360
9361 /* Can't queue multiple flips, so wait for the previous
9362 * one to finish before executing the next.
9363 */
9364 if (intel_crtc->plane)
9365 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9366 else
9367 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009368 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9369 intel_ring_emit(ring, MI_NOOP);
9370 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9371 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9372 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009373 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009374 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00009375
9376 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009377 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009378 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009379}
9380
9381static int intel_gen3_queue_flip(struct drm_device *dev,
9382 struct drm_crtc *crtc,
9383 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009384 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009385 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009386 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009387{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009388 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009389 u32 flip_mask;
9390 int ret;
9391
Daniel Vetter6d90c952012-04-26 23:28:05 +02009392 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009393 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009394 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009395
9396 if (intel_crtc->plane)
9397 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9398 else
9399 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009400 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9401 intel_ring_emit(ring, MI_NOOP);
9402 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9403 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9404 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009405 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009406 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009407
Chris Wilsone7d841c2012-12-03 11:36:30 +00009408 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009409 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009410 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009411}
9412
9413static int intel_gen4_queue_flip(struct drm_device *dev,
9414 struct drm_crtc *crtc,
9415 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009416 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009417 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009418 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009419{
9420 struct drm_i915_private *dev_priv = dev->dev_private;
9421 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9422 uint32_t pf, pipesrc;
9423 int ret;
9424
Daniel Vetter6d90c952012-04-26 23:28:05 +02009425 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009426 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009427 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009428
9429 /* i965+ uses the linear or tiled offsets from the
9430 * Display Registers (which do not change across a page-flip)
9431 * so we need only reprogram the base address.
9432 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02009433 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9434 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9435 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009436 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +02009437 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009438
9439 /* XXX Enabling the panel-fitter across page-flip is so far
9440 * untested on non-native modes, so ignore it for now.
9441 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9442 */
9443 pf = 0;
9444 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009445 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009446
9447 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009448 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009449 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009450}
9451
9452static int intel_gen6_queue_flip(struct drm_device *dev,
9453 struct drm_crtc *crtc,
9454 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009455 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009456 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009457 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009458{
9459 struct drm_i915_private *dev_priv = dev->dev_private;
9460 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9461 uint32_t pf, pipesrc;
9462 int ret;
9463
Daniel Vetter6d90c952012-04-26 23:28:05 +02009464 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009465 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009466 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009467
Daniel Vetter6d90c952012-04-26 23:28:05 +02009468 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9469 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9470 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009471 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009472
Chris Wilson99d9acd2012-04-17 20:37:00 +01009473 /* Contrary to the suggestions in the documentation,
9474 * "Enable Panel Fitter" does not seem to be required when page
9475 * flipping with a non-native mode, and worse causes a normal
9476 * modeset to fail.
9477 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9478 */
9479 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009480 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009481 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009482
9483 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009484 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009485 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009486}
9487
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009488static int intel_gen7_queue_flip(struct drm_device *dev,
9489 struct drm_crtc *crtc,
9490 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009491 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009492 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009493 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009494{
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009496 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01009497 int len, ret;
9498
Robin Schroereba905b2014-05-18 02:24:50 +02009499 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009500 case PLANE_A:
9501 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9502 break;
9503 case PLANE_B:
9504 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9505 break;
9506 case PLANE_C:
9507 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9508 break;
9509 default:
9510 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009511 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009512 }
9513
Chris Wilsonffe74d72013-08-26 20:58:12 +01009514 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01009515 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01009516 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01009517 /*
9518 * On Gen 8, SRM is now taking an extra dword to accommodate
9519 * 48bits addresses, and we need a NOOP for the batch size to
9520 * stay even.
9521 */
9522 if (IS_GEN8(dev))
9523 len += 2;
9524 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009525
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009526 /*
9527 * BSpec MI_DISPLAY_FLIP for IVB:
9528 * "The full packet must be contained within the same cache line."
9529 *
9530 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9531 * cacheline, if we ever start emitting more commands before
9532 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9533 * then do the cacheline alignment, and finally emit the
9534 * MI_DISPLAY_FLIP.
9535 */
9536 ret = intel_ring_cacheline_align(ring);
9537 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009538 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009539
Chris Wilsonffe74d72013-08-26 20:58:12 +01009540 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009541 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009542 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009543
Chris Wilsonffe74d72013-08-26 20:58:12 +01009544 /* Unmask the flip-done completion message. Note that the bspec says that
9545 * we should do this for both the BCS and RCS, and that we must not unmask
9546 * more than one flip event at any time (or ensure that one flip message
9547 * can be sent by waiting for flip-done prior to queueing new flips).
9548 * Experimentation says that BCS works despite DERRMR masking all
9549 * flip-done completion events and that unmasking all planes at once
9550 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9551 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9552 */
9553 if (ring->id == RCS) {
9554 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9555 intel_ring_emit(ring, DERRMR);
9556 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9557 DERRMR_PIPEB_PRI_FLIP_DONE |
9558 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +01009559 if (IS_GEN8(dev))
9560 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9561 MI_SRM_LRM_GLOBAL_GTT);
9562 else
9563 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9564 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01009565 intel_ring_emit(ring, DERRMR);
9566 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +01009567 if (IS_GEN8(dev)) {
9568 intel_ring_emit(ring, 0);
9569 intel_ring_emit(ring, MI_NOOP);
9570 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009571 }
9572
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009573 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009574 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009575 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009576 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00009577
9578 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009579 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009580 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009581}
9582
Sourab Gupta84c33a62014-06-02 16:47:17 +05309583static bool use_mmio_flip(struct intel_engine_cs *ring,
9584 struct drm_i915_gem_object *obj)
9585{
9586 /*
9587 * This is not being used for older platforms, because
9588 * non-availability of flip done interrupt forces us to use
9589 * CS flips. Older platforms derive flip done using some clever
9590 * tricks involving the flip_pending status bits and vblank irqs.
9591 * So using MMIO flips there would disrupt this mechanism.
9592 */
9593
Chris Wilson8e09bf82014-07-08 10:40:30 +01009594 if (ring == NULL)
9595 return true;
9596
Sourab Gupta84c33a62014-06-02 16:47:17 +05309597 if (INTEL_INFO(ring->dev)->gen < 5)
9598 return false;
9599
9600 if (i915.use_mmio_flip < 0)
9601 return false;
9602 else if (i915.use_mmio_flip > 0)
9603 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +01009604 else if (i915.enable_execlists)
9605 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +05309606 else
John Harrison41c52412014-11-24 18:49:43 +00009607 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309608}
9609
Damien Lespiauff944562014-11-20 14:58:16 +00009610static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
9611{
9612 struct drm_device *dev = intel_crtc->base.dev;
9613 struct drm_i915_private *dev_priv = dev->dev_private;
9614 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
9615 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
9616 struct drm_i915_gem_object *obj = intel_fb->obj;
9617 const enum pipe pipe = intel_crtc->pipe;
9618 u32 ctl, stride;
9619
9620 ctl = I915_READ(PLANE_CTL(pipe, 0));
9621 ctl &= ~PLANE_CTL_TILED_MASK;
9622 if (obj->tiling_mode == I915_TILING_X)
9623 ctl |= PLANE_CTL_TILED_X;
9624
9625 /*
9626 * The stride is either expressed as a multiple of 64 bytes chunks for
9627 * linear buffers or in number of tiles for tiled buffers.
9628 */
9629 stride = fb->pitches[0] >> 6;
9630 if (obj->tiling_mode == I915_TILING_X)
9631 stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
9632
9633 /*
9634 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
9635 * PLANE_SURF updates, the update is then guaranteed to be atomic.
9636 */
9637 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
9638 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
9639
9640 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
9641 POSTING_READ(PLANE_SURF(pipe, 0));
9642}
9643
9644static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +05309645{
9646 struct drm_device *dev = intel_crtc->base.dev;
9647 struct drm_i915_private *dev_priv = dev->dev_private;
9648 struct intel_framebuffer *intel_fb =
9649 to_intel_framebuffer(intel_crtc->base.primary->fb);
9650 struct drm_i915_gem_object *obj = intel_fb->obj;
9651 u32 dspcntr;
9652 u32 reg;
9653
Sourab Gupta84c33a62014-06-02 16:47:17 +05309654 reg = DSPCNTR(intel_crtc->plane);
9655 dspcntr = I915_READ(reg);
9656
Damien Lespiauc5d97472014-10-25 00:11:11 +01009657 if (obj->tiling_mode != I915_TILING_NONE)
9658 dspcntr |= DISPPLANE_TILED;
9659 else
9660 dspcntr &= ~DISPPLANE_TILED;
9661
Sourab Gupta84c33a62014-06-02 16:47:17 +05309662 I915_WRITE(reg, dspcntr);
9663
9664 I915_WRITE(DSPSURF(intel_crtc->plane),
9665 intel_crtc->unpin_work->gtt_offset);
9666 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009667
Damien Lespiauff944562014-11-20 14:58:16 +00009668}
9669
9670/*
9671 * XXX: This is the temporary way to update the plane registers until we get
9672 * around to using the usual plane update functions for MMIO flips
9673 */
9674static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9675{
9676 struct drm_device *dev = intel_crtc->base.dev;
9677 bool atomic_update;
9678 u32 start_vbl_count;
9679
9680 intel_mark_page_flip_active(intel_crtc);
9681
9682 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
9683
9684 if (INTEL_INFO(dev)->gen >= 9)
9685 skl_do_mmio_flip(intel_crtc);
9686 else
9687 /* use_mmio_flip() retricts MMIO flips to ilk+ */
9688 ilk_do_mmio_flip(intel_crtc);
9689
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009690 if (atomic_update)
9691 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309692}
9693
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009694static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +05309695{
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009696 struct intel_crtc *crtc =
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +02009697 container_of(work, struct intel_crtc, mmio_flip.work);
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009698 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +05309699
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009700 mmio_flip = &crtc->mmio_flip;
9701 if (mmio_flip->req)
John Harrison9c654812014-11-24 18:49:35 +00009702 WARN_ON(__i915_wait_request(mmio_flip->req,
9703 crtc->reset_counter,
9704 false, NULL, NULL) != 0);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309705
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009706 intel_do_mmio_flip(crtc);
9707 if (mmio_flip->req) {
9708 mutex_lock(&crtc->base.dev->struct_mutex);
John Harrison146d84f2014-12-05 13:49:33 +00009709 i915_gem_request_assign(&mmio_flip->req, NULL);
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009710 mutex_unlock(&crtc->base.dev->struct_mutex);
9711 }
Sourab Gupta84c33a62014-06-02 16:47:17 +05309712}
9713
9714static int intel_queue_mmio_flip(struct drm_device *dev,
9715 struct drm_crtc *crtc,
9716 struct drm_framebuffer *fb,
9717 struct drm_i915_gem_object *obj,
9718 struct intel_engine_cs *ring,
9719 uint32_t flags)
9720{
Sourab Gupta84c33a62014-06-02 16:47:17 +05309721 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309722
John Harrisoncc8c4cc2014-11-24 18:49:34 +00009723 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
9724 obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309725
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +02009726 schedule_work(&intel_crtc->mmio_flip.work);
9727
Sourab Gupta84c33a62014-06-02 16:47:17 +05309728 return 0;
9729}
9730
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009731static int intel_default_queue_flip(struct drm_device *dev,
9732 struct drm_crtc *crtc,
9733 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009734 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009735 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009736 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009737{
9738 return -ENODEV;
9739}
9740
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009741static bool __intel_pageflip_stall_check(struct drm_device *dev,
9742 struct drm_crtc *crtc)
9743{
9744 struct drm_i915_private *dev_priv = dev->dev_private;
9745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9746 struct intel_unpin_work *work = intel_crtc->unpin_work;
9747 u32 addr;
9748
9749 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9750 return true;
9751
9752 if (!work->enable_stall_check)
9753 return false;
9754
9755 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +01009756 if (work->flip_queued_req &&
9757 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009758 return false;
9759
Daniel Vetter1e3feef2015-02-13 21:03:45 +01009760 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009761 }
9762
Daniel Vetter1e3feef2015-02-13 21:03:45 +01009763 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009764 return false;
9765
9766 /* Potential stall - if we see that the flip has happened,
9767 * assume a missed interrupt. */
9768 if (INTEL_INFO(dev)->gen >= 4)
9769 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9770 else
9771 addr = I915_READ(DSPADDR(intel_crtc->plane));
9772
9773 /* There is a potential issue here with a false positive after a flip
9774 * to the same address. We could address this by checking for a
9775 * non-incrementing frame counter.
9776 */
9777 return addr == work->gtt_offset;
9778}
9779
9780void intel_check_page_flip(struct drm_device *dev, int pipe)
9781{
9782 struct drm_i915_private *dev_priv = dev->dev_private;
9783 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9784 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterf3260382014-09-15 14:55:23 +02009785
9786 WARN_ON(!in_irq());
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009787
9788 if (crtc == NULL)
9789 return;
9790
Daniel Vetterf3260382014-09-15 14:55:23 +02009791 spin_lock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009792 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9793 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Daniel Vetter1e3feef2015-02-13 21:03:45 +01009794 intel_crtc->unpin_work->flip_queued_vblank,
9795 drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009796 page_flip_completed(intel_crtc);
9797 }
Daniel Vetterf3260382014-09-15 14:55:23 +02009798 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009799}
9800
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009801static int intel_crtc_page_flip(struct drm_crtc *crtc,
9802 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009803 struct drm_pending_vblank_event *event,
9804 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009805{
9806 struct drm_device *dev = crtc->dev;
9807 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -07009808 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -07009809 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009810 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -08009811 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +02009812 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009813 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009814 struct intel_engine_cs *ring;
Chris Wilson52e68632010-08-08 10:15:59 +01009815 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009816
Matt Roper2ff8fde2014-07-08 07:50:07 -07009817 /*
9818 * drm_mode_page_flip_ioctl() should already catch this, but double
9819 * check to be safe. In the future we may enable pageflipping from
9820 * a disabled primary plane.
9821 */
9822 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9823 return -EBUSY;
9824
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009825 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -07009826 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009827 return -EINVAL;
9828
9829 /*
9830 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9831 * Note that pitch changes could also affect these register.
9832 */
9833 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -07009834 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9835 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009836 return -EINVAL;
9837
Chris Wilsonf900db42014-02-20 09:26:13 +00009838 if (i915_terminally_wedged(&dev_priv->gpu_error))
9839 goto out_hang;
9840
Daniel Vetterb14c5672013-09-19 12:18:32 +02009841 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009842 if (work == NULL)
9843 return -ENOMEM;
9844
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009845 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009846 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +00009847 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009848 INIT_WORK(&work->work, intel_unpin_work_fn);
9849
Daniel Vetter87b6b102014-05-15 15:33:46 +02009850 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009851 if (ret)
9852 goto free_work;
9853
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009854 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009855 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009856 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009857 /* Before declaring the flip queue wedged, check if
9858 * the hardware completed the operation behind our backs.
9859 */
9860 if (__intel_pageflip_stall_check(dev, crtc)) {
9861 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
9862 page_flip_completed(intel_crtc);
9863 } else {
9864 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009865 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +01009866
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009867 drm_crtc_vblank_put(crtc);
9868 kfree(work);
9869 return -EBUSY;
9870 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009871 }
9872 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009873 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009874
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009875 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9876 flush_workqueue(dev_priv->wq);
9877
Chris Wilson79158102012-05-23 11:13:58 +01009878 ret = i915_mutex_lock_interruptible(dev);
9879 if (ret)
9880 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009881
Jesse Barnes75dfca82010-02-10 15:09:44 -08009882 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +00009883 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009884 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009885
Matt Roperf4510a22014-04-01 15:22:40 -07009886 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -08009887 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -08009888
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009889 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009890
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009891 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02009892 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009893
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009894 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +02009895 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009896
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009897 if (IS_VALLEYVIEW(dev)) {
9898 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +00009899 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +01009900 /* vlv: DISPLAY_FLIP fails to change tiling */
9901 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +00009902 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +01009903 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009904 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +00009905 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009906 if (ring == NULL || ring->id != RCS)
9907 ring = &dev_priv->ring[BCS];
9908 } else {
9909 ring = &dev_priv->ring[RCS];
9910 }
9911
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00009912 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009913 if (ret)
9914 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009915
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009916 work->gtt_offset =
9917 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9918
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009919 if (use_mmio_flip(ring, obj)) {
Sourab Gupta84c33a62014-06-02 16:47:17 +05309920 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9921 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009922 if (ret)
9923 goto cleanup_unpin;
9924
John Harrisonf06cc1b2014-11-24 18:49:37 +00009925 i915_gem_request_assign(&work->flip_queued_req,
9926 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009927 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +05309928 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009929 page_flip_flags);
9930 if (ret)
9931 goto cleanup_unpin;
9932
John Harrisonf06cc1b2014-11-24 18:49:37 +00009933 i915_gem_request_assign(&work->flip_queued_req,
9934 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009935 }
9936
Daniel Vetter1e3feef2015-02-13 21:03:45 +01009937 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009938 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009939
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +00009940 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +02009941 INTEL_FRONTBUFFER_PRIMARY(pipe));
9942
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02009943 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +02009944 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009945 mutex_unlock(&dev->struct_mutex);
9946
Jesse Barnese5510fa2010-07-01 16:48:37 -07009947 trace_i915_flip_request(intel_crtc->plane, obj);
9948
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009949 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01009950
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009951cleanup_unpin:
9952 intel_unpin_fb_obj(obj);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009953cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009954 atomic_dec(&intel_crtc->unpin_work_count);
Matt Roperf4510a22014-04-01 15:22:40 -07009955 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -08009956 update_state_fb(crtc->primary);
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +00009957 drm_framebuffer_unreference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +00009958 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01009959 mutex_unlock(&dev->struct_mutex);
9960
Chris Wilson79158102012-05-23 11:13:58 +01009961cleanup:
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009962 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +01009963 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009964 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +01009965
Daniel Vetter87b6b102014-05-15 15:33:46 +02009966 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009967free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01009968 kfree(work);
9969
Chris Wilsonf900db42014-02-20 09:26:13 +00009970 if (ret == -EIO) {
9971out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -08009972 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +01009973 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009974 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +02009975 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009976 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +01009977 }
Chris Wilsonf900db42014-02-20 09:26:13 +00009978 }
Chris Wilson96b099f2010-06-07 14:03:04 +01009979 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009980}
9981
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009982static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009983 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9984 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -08009985 .atomic_begin = intel_begin_crtc_commit,
9986 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009987};
9988
Daniel Vetter9a935852012-07-05 22:34:27 +02009989/**
9990 * intel_modeset_update_staged_output_state
9991 *
9992 * Updates the staged output configuration state, e.g. after we've read out the
9993 * current hw state.
9994 */
9995static void intel_modeset_update_staged_output_state(struct drm_device *dev)
9996{
Ville Syrjälä76688512014-01-10 11:28:06 +02009997 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009998 struct intel_encoder *encoder;
9999 struct intel_connector *connector;
10000
10001 list_for_each_entry(connector, &dev->mode_config.connector_list,
10002 base.head) {
10003 connector->new_encoder =
10004 to_intel_encoder(connector->base.encoder);
10005 }
10006
Damien Lespiaub2784e12014-08-05 11:29:37 +010010007 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010008 encoder->new_crtc =
10009 to_intel_crtc(encoder->base.crtc);
10010 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010011
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010012 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010013 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010014
10015 if (crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010016 crtc->new_config = crtc->config;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010017 else
10018 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010019 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010020}
10021
10022/**
10023 * intel_modeset_commit_output_state
10024 *
10025 * This function copies the stage display pipe configuration to the real one.
10026 */
10027static void intel_modeset_commit_output_state(struct drm_device *dev)
10028{
Ville Syrjälä76688512014-01-10 11:28:06 +020010029 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010030 struct intel_encoder *encoder;
10031 struct intel_connector *connector;
10032
10033 list_for_each_entry(connector, &dev->mode_config.connector_list,
10034 base.head) {
10035 connector->base.encoder = &connector->new_encoder->base;
10036 }
10037
Damien Lespiaub2784e12014-08-05 11:29:37 +010010038 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010039 encoder->base.crtc = &encoder->new_crtc->base;
10040 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010041
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010042 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010043 crtc->base.state->enable = crtc->new_enabled;
Ville Syrjälä76688512014-01-10 11:28:06 +020010044 crtc->base.enabled = crtc->new_enabled;
10045 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010046}
10047
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010048static void
Robin Schroereba905b2014-05-18 02:24:50 +020010049connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010050 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010051{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010052 int bpp = pipe_config->pipe_bpp;
10053
10054 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10055 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010056 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010057
10058 /* Don't use an invalid EDID bpc value */
10059 if (connector->base.display_info.bpc &&
10060 connector->base.display_info.bpc * 3 < bpp) {
10061 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10062 bpp, connector->base.display_info.bpc*3);
10063 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10064 }
10065
10066 /* Clamp bpp to 8 on screens without EDID 1.4 */
10067 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10068 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10069 bpp);
10070 pipe_config->pipe_bpp = 24;
10071 }
10072}
10073
10074static int
10075compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10076 struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010077 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010078{
10079 struct drm_device *dev = crtc->base.dev;
10080 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010081 int bpp;
10082
Daniel Vetterd42264b2013-03-28 16:38:08 +010010083 switch (fb->pixel_format) {
10084 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010085 bpp = 8*3; /* since we go through a colormap */
10086 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010087 case DRM_FORMAT_XRGB1555:
10088 case DRM_FORMAT_ARGB1555:
10089 /* checked in intel_framebuffer_init already */
10090 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10091 return -EINVAL;
10092 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010093 bpp = 6*3; /* min is 18bpp */
10094 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010095 case DRM_FORMAT_XBGR8888:
10096 case DRM_FORMAT_ABGR8888:
10097 /* checked in intel_framebuffer_init already */
10098 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10099 return -EINVAL;
10100 case DRM_FORMAT_XRGB8888:
10101 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010102 bpp = 8*3;
10103 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010104 case DRM_FORMAT_XRGB2101010:
10105 case DRM_FORMAT_ARGB2101010:
10106 case DRM_FORMAT_XBGR2101010:
10107 case DRM_FORMAT_ABGR2101010:
10108 /* checked in intel_framebuffer_init already */
10109 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +010010110 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010111 bpp = 10*3;
10112 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +010010113 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010114 default:
10115 DRM_DEBUG_KMS("unsupported depth\n");
10116 return -EINVAL;
10117 }
10118
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010119 pipe_config->pipe_bpp = bpp;
10120
10121 /* Clamp display bpp to EDID value */
10122 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010123 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +020010124 if (!connector->new_encoder ||
10125 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010126 continue;
10127
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010128 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010129 }
10130
10131 return bpp;
10132}
10133
Daniel Vetter644db712013-09-19 14:53:58 +020010134static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10135{
10136 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10137 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010010138 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020010139 mode->crtc_hdisplay, mode->crtc_hsync_start,
10140 mode->crtc_hsync_end, mode->crtc_htotal,
10141 mode->crtc_vdisplay, mode->crtc_vsync_start,
10142 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10143}
10144
Daniel Vetterc0b03412013-05-28 12:05:54 +020010145static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010146 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020010147 const char *context)
10148{
10149 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10150 context, pipe_name(crtc->pipe));
10151
10152 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10153 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10154 pipe_config->pipe_bpp, pipe_config->dither);
10155 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10156 pipe_config->has_pch_encoder,
10157 pipe_config->fdi_lanes,
10158 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10159 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10160 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010161 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10162 pipe_config->has_dp_encoder,
10163 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10164 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10165 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010166
10167 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10168 pipe_config->has_dp_encoder,
10169 pipe_config->dp_m2_n2.gmch_m,
10170 pipe_config->dp_m2_n2.gmch_n,
10171 pipe_config->dp_m2_n2.link_m,
10172 pipe_config->dp_m2_n2.link_n,
10173 pipe_config->dp_m2_n2.tu);
10174
Daniel Vetter55072d12014-11-20 16:10:28 +010010175 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
10176 pipe_config->has_audio,
10177 pipe_config->has_infoframe);
10178
Daniel Vetterc0b03412013-05-28 12:05:54 +020010179 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010180 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010181 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010182 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
10183 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030010184 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010185 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10186 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010187 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10188 pipe_config->gmch_pfit.control,
10189 pipe_config->gmch_pfit.pgm_ratios,
10190 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010191 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020010192 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010193 pipe_config->pch_pfit.size,
10194 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010195 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030010196 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010197}
10198
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010199static bool encoders_cloneable(const struct intel_encoder *a,
10200 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010201{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010202 /* masks could be asymmetric, so check both ways */
10203 return a == b || (a->cloneable & (1 << b->type) &&
10204 b->cloneable & (1 << a->type));
10205}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010206
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010207static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10208 struct intel_encoder *encoder)
10209{
10210 struct drm_device *dev = crtc->base.dev;
10211 struct intel_encoder *source_encoder;
10212
Damien Lespiaub2784e12014-08-05 11:29:37 +010010213 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010214 if (source_encoder->new_crtc != crtc)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010215 continue;
10216
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010217 if (!encoders_cloneable(encoder, source_encoder))
10218 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010219 }
10220
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010221 return true;
10222}
10223
10224static bool check_encoder_cloning(struct intel_crtc *crtc)
10225{
10226 struct drm_device *dev = crtc->base.dev;
10227 struct intel_encoder *encoder;
10228
Damien Lespiaub2784e12014-08-05 11:29:37 +010010229 for_each_intel_encoder(dev, encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010230 if (encoder->new_crtc != crtc)
10231 continue;
10232
10233 if (!check_single_encoder_cloning(crtc, encoder))
10234 return false;
10235 }
10236
10237 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010238}
10239
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010240static bool check_digital_port_conflicts(struct drm_device *dev)
10241{
10242 struct intel_connector *connector;
10243 unsigned int used_ports = 0;
10244
10245 /*
10246 * Walk the connector list instead of the encoder
10247 * list to detect the problem on ddi platforms
10248 * where there's just one encoder per digital port.
10249 */
10250 list_for_each_entry(connector,
10251 &dev->mode_config.connector_list, base.head) {
10252 struct intel_encoder *encoder = connector->new_encoder;
10253
10254 if (!encoder)
10255 continue;
10256
10257 WARN_ON(!encoder->new_crtc);
10258
10259 switch (encoder->type) {
10260 unsigned int port_mask;
10261 case INTEL_OUTPUT_UNKNOWN:
10262 if (WARN_ON(!HAS_DDI(dev)))
10263 break;
10264 case INTEL_OUTPUT_DISPLAYPORT:
10265 case INTEL_OUTPUT_HDMI:
10266 case INTEL_OUTPUT_EDP:
10267 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
10268
10269 /* the same port mustn't appear more than once */
10270 if (used_ports & port_mask)
10271 return false;
10272
10273 used_ports |= port_mask;
10274 default:
10275 break;
10276 }
10277 }
10278
10279 return true;
10280}
10281
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010282static struct intel_crtc_state *
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010283intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010284 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010285 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +020010286{
10287 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +020010288 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010289 struct intel_crtc_state *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +010010290 int plane_bpp, ret = -EINVAL;
10291 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020010292
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010293 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010294 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10295 return ERR_PTR(-EINVAL);
10296 }
10297
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010298 if (!check_digital_port_conflicts(dev)) {
10299 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
10300 return ERR_PTR(-EINVAL);
10301 }
10302
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010303 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10304 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020010305 return ERR_PTR(-ENOMEM);
10306
Matt Roper07878242015-02-25 11:43:26 -080010307 pipe_config->base.crtc = crtc;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010308 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
10309 drm_mode_copy(&pipe_config->base.mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010310
Daniel Vettere143a212013-07-04 12:01:15 +020010311 pipe_config->cpu_transcoder =
10312 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010313 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010314
Imre Deak2960bc92013-07-30 13:36:32 +030010315 /*
10316 * Sanitize sync polarity flags based on requested ones. If neither
10317 * positive or negative polarity is requested, treat this as meaning
10318 * negative polarity.
10319 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010320 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030010321 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010322 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030010323
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010324 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030010325 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010326 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030010327
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010328 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10329 * plane pixel format and any sink constraints into account. Returns the
10330 * source plane bpp so that dithering can be selected on mismatches
10331 * after encoders and crtc also have had their say. */
10332 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10333 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010334 if (plane_bpp < 0)
10335 goto fail;
10336
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010337 /*
10338 * Determine the real pipe dimensions. Note that stereo modes can
10339 * increase the actual pipe size due to the frame doubling and
10340 * insertion of additional space for blanks between the frame. This
10341 * is stored in the crtc timings. We use the requested mode to do this
10342 * computation to clearly distinguish it from the adjusted mode, which
10343 * can be changed by the connectors in the below retry loop.
10344 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010345 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080010346 &pipe_config->pipe_src_w,
10347 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010348
Daniel Vettere29c22c2013-02-21 00:00:16 +010010349encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020010350 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020010351 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020010352 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010353
Daniel Vetter135c81b2013-07-21 21:37:09 +020010354 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010355 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
10356 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020010357
Daniel Vetter7758a112012-07-08 19:40:39 +020010358 /* Pass our mode to the connectors and the CRTC to give them a chance to
10359 * adjust it according to limitations or connector properties, and also
10360 * a chance to reject the mode entirely.
10361 */
Damien Lespiaub2784e12014-08-05 11:29:37 +010010362 for_each_intel_encoder(dev, encoder) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010363
10364 if (&encoder->new_crtc->base != crtc)
10365 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010010366
Daniel Vetterefea6e82013-07-21 21:36:59 +020010367 if (!(encoder->compute_config(encoder, pipe_config))) {
10368 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020010369 goto fail;
10370 }
10371 }
10372
Daniel Vetterff9a6752013-06-01 17:16:21 +020010373 /* Set default port clock if not overwritten by the encoder. Needs to be
10374 * done afterwards in case the encoder adjusts the mode. */
10375 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010376 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010010377 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010378
Daniel Vettera43f6e02013-06-07 23:10:32 +020010379 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010380 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010381 DRM_DEBUG_KMS("CRTC fixup failed\n");
10382 goto fail;
10383 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010010384
10385 if (ret == RETRY) {
10386 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10387 ret = -EINVAL;
10388 goto fail;
10389 }
10390
10391 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10392 retry = false;
10393 goto encoder_retry;
10394 }
10395
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010396 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10397 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10398 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10399
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010400 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020010401fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010402 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010403 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020010404}
10405
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010406/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10407 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10408static void
10409intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10410 unsigned *prepare_pipes, unsigned *disable_pipes)
10411{
10412 struct intel_crtc *intel_crtc;
10413 struct drm_device *dev = crtc->dev;
10414 struct intel_encoder *encoder;
10415 struct intel_connector *connector;
10416 struct drm_crtc *tmp_crtc;
10417
10418 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
10419
10420 /* Check which crtcs have changed outputs connected to them, these need
10421 * to be part of the prepare_pipes mask. We don't (yet) support global
10422 * modeset across multiple crtcs, so modeset_pipes will only have one
10423 * bit set at most. */
10424 list_for_each_entry(connector, &dev->mode_config.connector_list,
10425 base.head) {
10426 if (connector->base.encoder == &connector->new_encoder->base)
10427 continue;
10428
10429 if (connector->base.encoder) {
10430 tmp_crtc = connector->base.encoder->crtc;
10431
10432 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10433 }
10434
10435 if (connector->new_encoder)
10436 *prepare_pipes |=
10437 1 << connector->new_encoder->new_crtc->pipe;
10438 }
10439
Damien Lespiaub2784e12014-08-05 11:29:37 +010010440 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010441 if (encoder->base.crtc == &encoder->new_crtc->base)
10442 continue;
10443
10444 if (encoder->base.crtc) {
10445 tmp_crtc = encoder->base.crtc;
10446
10447 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10448 }
10449
10450 if (encoder->new_crtc)
10451 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
10452 }
10453
Ville Syrjälä76688512014-01-10 11:28:06 +020010454 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010455 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010456 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010457 continue;
10458
Ville Syrjälä76688512014-01-10 11:28:06 +020010459 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010460 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020010461 else
10462 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010463 }
10464
10465
10466 /* set_mode is also used to update properties on life display pipes. */
10467 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020010468 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010469 *prepare_pipes |= 1 << intel_crtc->pipe;
10470
Daniel Vetterb6c51642013-04-12 18:48:43 +020010471 /*
10472 * For simplicity do a full modeset on any pipe where the output routing
10473 * changed. We could be more clever, but that would require us to be
10474 * more careful with calling the relevant encoder->mode_set functions.
10475 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010476 if (*prepare_pipes)
10477 *modeset_pipes = *prepare_pipes;
10478
10479 /* ... and mask these out. */
10480 *modeset_pipes &= ~(*disable_pipes);
10481 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020010482
10483 /*
10484 * HACK: We don't (yet) fully support global modesets. intel_set_config
10485 * obies this rule, but the modeset restore mode of
10486 * intel_modeset_setup_hw_state does not.
10487 */
10488 *modeset_pipes &= 1 << intel_crtc->pipe;
10489 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020010490
10491 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10492 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010493}
10494
Daniel Vetterea9d7582012-07-10 10:42:52 +020010495static bool intel_crtc_in_use(struct drm_crtc *crtc)
10496{
10497 struct drm_encoder *encoder;
10498 struct drm_device *dev = crtc->dev;
10499
10500 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10501 if (encoder->crtc == crtc)
10502 return true;
10503
10504 return false;
10505}
10506
10507static void
10508intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10509{
Daniel Vetterba41c0de2014-11-03 15:04:55 +010010510 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020010511 struct intel_encoder *intel_encoder;
10512 struct intel_crtc *intel_crtc;
10513 struct drm_connector *connector;
10514
Daniel Vetterba41c0de2014-11-03 15:04:55 +010010515 intel_shared_dpll_commit(dev_priv);
10516
Damien Lespiaub2784e12014-08-05 11:29:37 +010010517 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020010518 if (!intel_encoder->base.crtc)
10519 continue;
10520
10521 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10522
10523 if (prepare_pipes & (1 << intel_crtc->pipe))
10524 intel_encoder->connectors_active = false;
10525 }
10526
10527 intel_modeset_commit_output_state(dev);
10528
Ville Syrjälä76688512014-01-10 11:28:06 +020010529 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010530 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010531 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010532 WARN_ON(intel_crtc->new_config &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010533 intel_crtc->new_config != intel_crtc->config);
Matt Roper83d65732015-02-25 13:12:16 -080010534 WARN_ON(intel_crtc->base.state->enable != !!intel_crtc->new_config);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010535 }
10536
10537 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10538 if (!connector->encoder || !connector->encoder->crtc)
10539 continue;
10540
10541 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10542
10543 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020010544 struct drm_property *dpms_property =
10545 dev->mode_config.dpms_property;
10546
Daniel Vetterea9d7582012-07-10 10:42:52 +020010547 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050010548 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020010549 dpms_property,
10550 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010551
10552 intel_encoder = to_intel_encoder(connector->encoder);
10553 intel_encoder->connectors_active = true;
10554 }
10555 }
10556
10557}
10558
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010559static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010560{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010561 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010562
10563 if (clock1 == clock2)
10564 return true;
10565
10566 if (!clock1 || !clock2)
10567 return false;
10568
10569 diff = abs(clock1 - clock2);
10570
10571 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10572 return true;
10573
10574 return false;
10575}
10576
Daniel Vetter25c5b262012-07-08 22:08:04 +020010577#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10578 list_for_each_entry((intel_crtc), \
10579 &(dev)->mode_config.crtc_list, \
10580 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020010581 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020010582
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010583static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010584intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010585 struct intel_crtc_state *current_config,
10586 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010587{
Daniel Vetter66e985c2013-06-05 13:34:20 +020010588#define PIPE_CONF_CHECK_X(name) \
10589 if (current_config->name != pipe_config->name) { \
10590 DRM_ERROR("mismatch in " #name " " \
10591 "(expected 0x%08x, found 0x%08x)\n", \
10592 current_config->name, \
10593 pipe_config->name); \
10594 return false; \
10595 }
10596
Daniel Vetter08a24032013-04-19 11:25:34 +020010597#define PIPE_CONF_CHECK_I(name) \
10598 if (current_config->name != pipe_config->name) { \
10599 DRM_ERROR("mismatch in " #name " " \
10600 "(expected %i, found %i)\n", \
10601 current_config->name, \
10602 pipe_config->name); \
10603 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010010604 }
10605
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010606/* This is required for BDW+ where there is only one set of registers for
10607 * switching between high and low RR.
10608 * This macro can be used whenever a comparison has to be made between one
10609 * hw state and multiple sw state variables.
10610 */
10611#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10612 if ((current_config->name != pipe_config->name) && \
10613 (current_config->alt_name != pipe_config->name)) { \
10614 DRM_ERROR("mismatch in " #name " " \
10615 "(expected %i or %i, found %i)\n", \
10616 current_config->name, \
10617 current_config->alt_name, \
10618 pipe_config->name); \
10619 return false; \
10620 }
10621
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010622#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10623 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070010624 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010625 "(expected %i, found %i)\n", \
10626 current_config->name & (mask), \
10627 pipe_config->name & (mask)); \
10628 return false; \
10629 }
10630
Ville Syrjälä5e550652013-09-06 23:29:07 +030010631#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10632 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10633 DRM_ERROR("mismatch in " #name " " \
10634 "(expected %i, found %i)\n", \
10635 current_config->name, \
10636 pipe_config->name); \
10637 return false; \
10638 }
10639
Daniel Vetterbb760062013-06-06 14:55:52 +020010640#define PIPE_CONF_QUIRK(quirk) \
10641 ((current_config->quirks | pipe_config->quirks) & (quirk))
10642
Daniel Vettereccb1402013-05-22 00:50:22 +020010643 PIPE_CONF_CHECK_I(cpu_transcoder);
10644
Daniel Vetter08a24032013-04-19 11:25:34 +020010645 PIPE_CONF_CHECK_I(has_pch_encoder);
10646 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020010647 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10648 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10649 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10650 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10651 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020010652
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010653 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010654
10655 if (INTEL_INFO(dev)->gen < 8) {
10656 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10657 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10658 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10659 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10660 PIPE_CONF_CHECK_I(dp_m_n.tu);
10661
10662 if (current_config->has_drrs) {
10663 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10664 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10665 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10666 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10667 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10668 }
10669 } else {
10670 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10671 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10672 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10673 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10674 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10675 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010676
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010677 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
10678 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
10679 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
10680 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
10681 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
10682 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010683
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010684 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
10685 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
10686 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
10687 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
10688 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
10689 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010690
Daniel Vetterc93f54c2013-06-27 19:47:19 +020010691 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020010692 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020010693 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10694 IS_VALLEYVIEW(dev))
10695 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080010696 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020010697
Daniel Vetter9ed109a2014-04-24 23:54:52 +020010698 PIPE_CONF_CHECK_I(has_audio);
10699
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010700 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010701 DRM_MODE_FLAG_INTERLACE);
10702
Daniel Vetterbb760062013-06-06 14:55:52 +020010703 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010704 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020010705 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010706 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020010707 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010708 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020010709 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010710 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020010711 DRM_MODE_FLAG_NVSYNC);
10712 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010713
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010714 PIPE_CONF_CHECK_I(pipe_src_w);
10715 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010716
Daniel Vetter99535992014-04-13 12:00:33 +020010717 /*
10718 * FIXME: BIOS likes to set up a cloned config with lvds+external
10719 * screen. Since we don't yet re-compute the pipe config when moving
10720 * just the lvds port away to another pipe the sw tracking won't match.
10721 *
10722 * Proper atomic modesets with recomputed global state will fix this.
10723 * Until then just don't check gmch state for inherited modes.
10724 */
10725 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10726 PIPE_CONF_CHECK_I(gmch_pfit.control);
10727 /* pfit ratios are autocomputed by the hw on gen4+ */
10728 if (INTEL_INFO(dev)->gen < 4)
10729 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10730 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10731 }
10732
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010733 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10734 if (current_config->pch_pfit.enabled) {
10735 PIPE_CONF_CHECK_I(pch_pfit.pos);
10736 PIPE_CONF_CHECK_I(pch_pfit.size);
10737 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010738
Jesse Barnese59150d2014-01-07 13:30:45 -080010739 /* BDW+ don't expose a synchronous way to read the state */
10740 if (IS_HASWELL(dev))
10741 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010742
Ville Syrjälä282740f2013-09-04 18:30:03 +030010743 PIPE_CONF_CHECK_I(double_wide);
10744
Daniel Vetter26804af2014-06-25 22:01:55 +030010745 PIPE_CONF_CHECK_X(ddi_pll_sel);
10746
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010747 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010748 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020010749 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010750 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10751 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010752 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000010753 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
10754 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
10755 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010756
Ville Syrjälä42571ae2013-09-06 23:29:00 +030010757 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10758 PIPE_CONF_CHECK_I(pipe_bpp);
10759
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010760 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080010761 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030010762
Daniel Vetter66e985c2013-06-05 13:34:20 +020010763#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020010764#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010765#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010766#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030010767#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020010768#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010769
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010770 return true;
10771}
10772
Damien Lespiau08db6652014-11-04 17:06:52 +000010773static void check_wm_state(struct drm_device *dev)
10774{
10775 struct drm_i915_private *dev_priv = dev->dev_private;
10776 struct skl_ddb_allocation hw_ddb, *sw_ddb;
10777 struct intel_crtc *intel_crtc;
10778 int plane;
10779
10780 if (INTEL_INFO(dev)->gen < 9)
10781 return;
10782
10783 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
10784 sw_ddb = &dev_priv->wm.skl_hw.ddb;
10785
10786 for_each_intel_crtc(dev, intel_crtc) {
10787 struct skl_ddb_entry *hw_entry, *sw_entry;
10788 const enum pipe pipe = intel_crtc->pipe;
10789
10790 if (!intel_crtc->active)
10791 continue;
10792
10793 /* planes */
10794 for_each_plane(pipe, plane) {
10795 hw_entry = &hw_ddb.plane[pipe][plane];
10796 sw_entry = &sw_ddb->plane[pipe][plane];
10797
10798 if (skl_ddb_entry_equal(hw_entry, sw_entry))
10799 continue;
10800
10801 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
10802 "(expected (%u,%u), found (%u,%u))\n",
10803 pipe_name(pipe), plane + 1,
10804 sw_entry->start, sw_entry->end,
10805 hw_entry->start, hw_entry->end);
10806 }
10807
10808 /* cursor */
10809 hw_entry = &hw_ddb.cursor[pipe];
10810 sw_entry = &sw_ddb->cursor[pipe];
10811
10812 if (skl_ddb_entry_equal(hw_entry, sw_entry))
10813 continue;
10814
10815 DRM_ERROR("mismatch in DDB state pipe %c cursor "
10816 "(expected (%u,%u), found (%u,%u))\n",
10817 pipe_name(pipe),
10818 sw_entry->start, sw_entry->end,
10819 hw_entry->start, hw_entry->end);
10820 }
10821}
10822
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010823static void
10824check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010825{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010826 struct intel_connector *connector;
10827
10828 list_for_each_entry(connector, &dev->mode_config.connector_list,
10829 base.head) {
10830 /* This also checks the encoder/connector hw state with the
10831 * ->get_hw_state callbacks. */
10832 intel_connector_check_state(connector);
10833
Rob Clarke2c719b2014-12-15 13:56:32 -050010834 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010835 "connector's staged encoder doesn't match current encoder\n");
10836 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010837}
10838
10839static void
10840check_encoder_state(struct drm_device *dev)
10841{
10842 struct intel_encoder *encoder;
10843 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010844
Damien Lespiaub2784e12014-08-05 11:29:37 +010010845 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010846 bool enabled = false;
10847 bool active = false;
10848 enum pipe pipe, tracked_pipe;
10849
10850 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10851 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030010852 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010853
Rob Clarke2c719b2014-12-15 13:56:32 -050010854 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010855 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010856 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010857 "encoder's active_connectors set, but no crtc\n");
10858
10859 list_for_each_entry(connector, &dev->mode_config.connector_list,
10860 base.head) {
10861 if (connector->base.encoder != &encoder->base)
10862 continue;
10863 enabled = true;
10864 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10865 active = true;
10866 }
Dave Airlie0e32b392014-05-02 14:02:48 +100010867 /*
10868 * for MST connectors if we unplug the connector is gone
10869 * away but the encoder is still connected to a crtc
10870 * until a modeset happens in response to the hotplug.
10871 */
10872 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10873 continue;
10874
Rob Clarke2c719b2014-12-15 13:56:32 -050010875 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010876 "encoder's enabled state mismatch "
10877 "(expected %i, found %i)\n",
10878 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050010879 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010880 "active encoder with no crtc\n");
10881
Rob Clarke2c719b2014-12-15 13:56:32 -050010882 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010883 "encoder's computed active state doesn't match tracked active state "
10884 "(expected %i, found %i)\n", active, encoder->connectors_active);
10885
10886 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050010887 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010888 "encoder's hw state doesn't match sw tracking "
10889 "(expected %i, found %i)\n",
10890 encoder->connectors_active, active);
10891
10892 if (!encoder->base.crtc)
10893 continue;
10894
10895 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050010896 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010897 "active encoder's pipe doesn't match"
10898 "(expected %i, found %i)\n",
10899 tracked_pipe, pipe);
10900
10901 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010902}
10903
10904static void
10905check_crtc_state(struct drm_device *dev)
10906{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010907 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010908 struct intel_crtc *crtc;
10909 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010910 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010911
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010912 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010913 bool enabled = false;
10914 bool active = false;
10915
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010916 memset(&pipe_config, 0, sizeof(pipe_config));
10917
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010918 DRM_DEBUG_KMS("[CRTC:%d]\n",
10919 crtc->base.base.id);
10920
Matt Roper83d65732015-02-25 13:12:16 -080010921 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010922 "active crtc, but not enabled in sw tracking\n");
10923
Damien Lespiaub2784e12014-08-05 11:29:37 +010010924 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010925 if (encoder->base.crtc != &crtc->base)
10926 continue;
10927 enabled = true;
10928 if (encoder->connectors_active)
10929 active = true;
10930 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010931
Rob Clarke2c719b2014-12-15 13:56:32 -050010932 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010933 "crtc's computed active state doesn't match tracked active state "
10934 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080010935 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010936 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080010937 "(expected %i, found %i)\n", enabled,
10938 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010939
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010940 active = dev_priv->display.get_pipe_config(crtc,
10941 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020010942
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030010943 /* hw state is inconsistent with the pipe quirk */
10944 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
10945 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020010946 active = crtc->active;
10947
Damien Lespiaub2784e12014-08-05 11:29:37 +010010948 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030010949 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020010950 if (encoder->base.crtc != &crtc->base)
10951 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010010952 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020010953 encoder->get_config(encoder, &pipe_config);
10954 }
10955
Rob Clarke2c719b2014-12-15 13:56:32 -050010956 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010957 "crtc active state doesn't match with hw state "
10958 "(expected %i, found %i)\n", crtc->active, active);
10959
Daniel Vetterc0b03412013-05-28 12:05:54 +020010960 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010961 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050010962 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020010963 intel_dump_pipe_config(crtc, &pipe_config,
10964 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010965 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020010966 "[sw state]");
10967 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010968 }
10969}
10970
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010971static void
10972check_shared_dpll_state(struct drm_device *dev)
10973{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010974 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010975 struct intel_crtc *crtc;
10976 struct intel_dpll_hw_state dpll_hw_state;
10977 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020010978
10979 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10980 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10981 int enabled_crtcs = 0, active_crtcs = 0;
10982 bool active;
10983
10984 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10985
10986 DRM_DEBUG_KMS("%s\n", pll->name);
10987
10988 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10989
Rob Clarke2c719b2014-12-15 13:56:32 -050010990 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020010991 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020010992 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050010993 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020010994 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010995 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020010996 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010997 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020010998 "pll on state mismatch (expected %i, found %i)\n",
10999 pll->on, active);
11000
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011001 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011002 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020011003 enabled_crtcs++;
11004 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11005 active_crtcs++;
11006 }
Rob Clarke2c719b2014-12-15 13:56:32 -050011007 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020011008 "pll active crtcs mismatch (expected %i, found %i)\n",
11009 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050011010 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020011011 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011012 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011013
Rob Clarke2c719b2014-12-15 13:56:32 -050011014 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020011015 sizeof(dpll_hw_state)),
11016 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020011017 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011018}
11019
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011020void
11021intel_modeset_check_state(struct drm_device *dev)
11022{
Damien Lespiau08db6652014-11-04 17:06:52 +000011023 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011024 check_connector_state(dev);
11025 check_encoder_state(dev);
11026 check_crtc_state(dev);
11027 check_shared_dpll_state(dev);
11028}
11029
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011030void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030011031 int dotclock)
11032{
11033 /*
11034 * FDI already provided one idea for the dotclock.
11035 * Yell if the encoder disagrees.
11036 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011037 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030011038 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011039 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030011040}
11041
Ville Syrjälä80715b22014-05-15 20:23:23 +030011042static void update_scanline_offset(struct intel_crtc *crtc)
11043{
11044 struct drm_device *dev = crtc->base.dev;
11045
11046 /*
11047 * The scanline counter increments at the leading edge of hsync.
11048 *
11049 * On most platforms it starts counting from vtotal-1 on the
11050 * first active line. That means the scanline counter value is
11051 * always one less than what we would expect. Ie. just after
11052 * start of vblank, which also occurs at start of hsync (on the
11053 * last active line), the scanline counter will read vblank_start-1.
11054 *
11055 * On gen2 the scanline counter starts counting from 1 instead
11056 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11057 * to keep the value positive), instead of adding one.
11058 *
11059 * On HSW+ the behaviour of the scanline counter depends on the output
11060 * type. For DP ports it behaves like most other platforms, but on HDMI
11061 * there's an extra 1 line difference. So we need to add two instead of
11062 * one to the value.
11063 */
11064 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011065 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030011066 int vtotal;
11067
11068 vtotal = mode->crtc_vtotal;
11069 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11070 vtotal /= 2;
11071
11072 crtc->scanline_offset = vtotal - 1;
11073 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030011074 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030011075 crtc->scanline_offset = 2;
11076 } else
11077 crtc->scanline_offset = 1;
11078}
11079
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011080static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011081intel_modeset_compute_config(struct drm_crtc *crtc,
11082 struct drm_display_mode *mode,
11083 struct drm_framebuffer *fb,
11084 unsigned *modeset_pipes,
11085 unsigned *prepare_pipes,
11086 unsigned *disable_pipes)
11087{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011088 struct intel_crtc_state *pipe_config = NULL;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011089
11090 intel_modeset_affected_pipes(crtc, modeset_pipes,
11091 prepare_pipes, disable_pipes);
11092
11093 if ((*modeset_pipes) == 0)
11094 goto out;
11095
11096 /*
11097 * Note this needs changes when we start tracking multiple modes
11098 * and crtcs. At that point we'll need to compute the whole config
11099 * (i.e. one pipe_config for each crtc) rather than just the one
11100 * for this crtc.
11101 */
11102 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
11103 if (IS_ERR(pipe_config)) {
11104 goto out;
11105 }
11106 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11107 "[modeset]");
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011108
11109out:
11110 return pipe_config;
11111}
11112
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011113static int __intel_set_mode_setup_plls(struct drm_device *dev,
11114 unsigned modeset_pipes,
11115 unsigned disable_pipes)
11116{
11117 struct drm_i915_private *dev_priv = to_i915(dev);
11118 unsigned clear_pipes = modeset_pipes | disable_pipes;
11119 struct intel_crtc *intel_crtc;
11120 int ret = 0;
11121
11122 if (!dev_priv->display.crtc_compute_clock)
11123 return 0;
11124
11125 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
11126 if (ret)
11127 goto done;
11128
11129 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
11130 struct intel_crtc_state *state = intel_crtc->new_config;
11131 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11132 state);
11133 if (ret) {
11134 intel_shared_dpll_abort_config(dev_priv);
11135 goto done;
11136 }
11137 }
11138
11139done:
11140 return ret;
11141}
11142
Daniel Vetterf30da182013-04-11 20:22:50 +020011143static int __intel_set_mode(struct drm_crtc *crtc,
11144 struct drm_display_mode *mode,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011145 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011146 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011147 unsigned modeset_pipes,
11148 unsigned prepare_pipes,
11149 unsigned disable_pipes)
Daniel Vettera6778b32012-07-02 09:56:42 +020011150{
11151 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030011152 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011153 struct drm_display_mode *saved_mode;
Daniel Vetter25c5b262012-07-08 22:08:04 +020011154 struct intel_crtc *intel_crtc;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011155 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020011156
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011157 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011158 if (!saved_mode)
11159 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020011160
Tim Gardner3ac18232012-12-07 07:54:26 -070011161 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011162
Ville Syrjäläb9950a12014-11-21 21:00:36 +020011163 if (modeset_pipes)
11164 to_intel_crtc(crtc)->new_config = pipe_config;
11165
Jesse Barnes30a970c2013-11-04 13:48:12 -080011166 /*
11167 * See if the config requires any additional preparation, e.g.
11168 * to adjust global state with pipes off. We need to do this
11169 * here so we can get the modeset_pipe updated config for the new
11170 * mode set on this crtc. For other crtcs we need to use the
11171 * adjusted_mode bits in the crtc directly.
11172 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020011173 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +020011174 valleyview_modeset_global_pipes(dev, &prepare_pipes);
Jesse Barnes30a970c2013-11-04 13:48:12 -080011175
Ville Syrjäläc164f832013-11-05 22:34:12 +020011176 /* may have added more to prepare_pipes than we should */
11177 prepare_pipes &= ~disable_pipes;
11178 }
11179
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011180 ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
11181 if (ret)
11182 goto done;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +020011183
Daniel Vetter460da9162013-03-27 00:44:51 +010011184 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11185 intel_crtc_disable(&intel_crtc->base);
11186
Daniel Vetterea9d7582012-07-10 10:42:52 +020011187 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011188 if (intel_crtc->base.state->enable)
Daniel Vetterea9d7582012-07-10 10:42:52 +020011189 dev_priv->display.crtc_disable(&intel_crtc->base);
11190 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011191
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020011192 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11193 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011194 *
11195 * Note we'll need to fix this up when we start tracking multiple
11196 * pipes; here we assume a single modeset_pipe and only track the
11197 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020011198 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011199 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020011200 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011201 /* mode_set/enable/disable functions rely on a correct pipe
11202 * config. */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020011203 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020011204
11205 /*
11206 * Calculate and store various constants which
11207 * are later needed by vblank and swap-completion
11208 * timestamping. They are derived from true hwmode.
11209 */
11210 drm_calc_timestamping_constants(crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011211 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011212 }
Daniel Vetter7758a112012-07-08 19:40:39 +020011213
Daniel Vetterea9d7582012-07-10 10:42:52 +020011214 /* Only after disabling all output pipelines that will be changed can we
11215 * update the the output configuration. */
11216 intel_modeset_update_state(dev, prepare_pipes);
11217
Ville Syrjälä50f6e502014-11-06 14:49:12 +020011218 modeset_update_crtc_power_domains(dev);
Daniel Vetter47fab732012-10-26 10:58:18 +020011219
Daniel Vettera6778b32012-07-02 09:56:42 +020011220 /* Set up the DPLL and any encoders state that needs to adjust or depend
11221 * on the DPLL.
11222 */
Daniel Vetter25c5b262012-07-08 22:08:04 +020011223 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Gustavo Padovan455a6802014-12-01 15:40:11 -080011224 struct drm_plane *primary = intel_crtc->base.primary;
11225 int vdisplay, hdisplay;
Daniel Vetter4c107942014-04-24 23:55:05 +020011226
Gustavo Padovan455a6802014-12-01 15:40:11 -080011227 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11228 ret = primary->funcs->update_plane(primary, &intel_crtc->base,
11229 fb, 0, 0,
11230 hdisplay, vdisplay,
11231 x << 16, y << 16,
11232 hdisplay << 16, vdisplay << 16);
Daniel Vettera6778b32012-07-02 09:56:42 +020011233 }
11234
11235 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030011236 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11237 update_scanline_offset(intel_crtc);
11238
Daniel Vetter25c5b262012-07-08 22:08:04 +020011239 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030011240 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011241
Daniel Vettera6778b32012-07-02 09:56:42 +020011242 /* FIXME: add subpixel order */
11243done:
Matt Roper83d65732015-02-25 13:12:16 -080011244 if (ret && crtc->state->enable)
Tim Gardner3ac18232012-12-07 07:54:26 -070011245 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011246
Tim Gardner3ac18232012-12-07 07:54:26 -070011247 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020011248 return ret;
11249}
11250
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011251static int intel_set_mode_pipes(struct drm_crtc *crtc,
11252 struct drm_display_mode *mode,
11253 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011254 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011255 unsigned modeset_pipes,
11256 unsigned prepare_pipes,
11257 unsigned disable_pipes)
11258{
11259 int ret;
11260
11261 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
11262 prepare_pipes, disable_pipes);
11263
11264 if (ret == 0)
11265 intel_modeset_check_state(crtc->dev);
11266
11267 return ret;
11268}
11269
Damien Lespiaue7457a92013-08-08 22:28:59 +010011270static int intel_set_mode(struct drm_crtc *crtc,
11271 struct drm_display_mode *mode,
11272 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +020011273{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011274 struct intel_crtc_state *pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011275 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetterf30da182013-04-11 20:22:50 +020011276
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011277 pipe_config = intel_modeset_compute_config(crtc, mode, fb,
11278 &modeset_pipes,
11279 &prepare_pipes,
11280 &disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020011281
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011282 if (IS_ERR(pipe_config))
11283 return PTR_ERR(pipe_config);
Daniel Vetterf30da182013-04-11 20:22:50 +020011284
Jesse Barnes7f27126e2014-11-05 14:26:06 -080011285 return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
11286 modeset_pipes, prepare_pipes,
11287 disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020011288}
11289
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011290void intel_crtc_restore_mode(struct drm_crtc *crtc)
11291{
Matt Roperf4510a22014-04-01 15:22:40 -070011292 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011293}
11294
Daniel Vetter25c5b262012-07-08 22:08:04 +020011295#undef for_each_intel_crtc_masked
11296
Daniel Vetterd9e55602012-07-04 22:16:09 +020011297static void intel_set_config_free(struct intel_set_config *config)
11298{
11299 if (!config)
11300 return;
11301
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011302 kfree(config->save_connector_encoders);
11303 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020011304 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020011305 kfree(config);
11306}
11307
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011308static int intel_set_config_save_state(struct drm_device *dev,
11309 struct intel_set_config *config)
11310{
Ville Syrjälä76688512014-01-10 11:28:06 +020011311 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011312 struct drm_encoder *encoder;
11313 struct drm_connector *connector;
11314 int count;
11315
Ville Syrjälä76688512014-01-10 11:28:06 +020011316 config->save_crtc_enabled =
11317 kcalloc(dev->mode_config.num_crtc,
11318 sizeof(bool), GFP_KERNEL);
11319 if (!config->save_crtc_enabled)
11320 return -ENOMEM;
11321
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011322 config->save_encoder_crtcs =
11323 kcalloc(dev->mode_config.num_encoder,
11324 sizeof(struct drm_crtc *), GFP_KERNEL);
11325 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011326 return -ENOMEM;
11327
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011328 config->save_connector_encoders =
11329 kcalloc(dev->mode_config.num_connector,
11330 sizeof(struct drm_encoder *), GFP_KERNEL);
11331 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011332 return -ENOMEM;
11333
11334 /* Copy data. Note that driver private data is not affected.
11335 * Should anything bad happen only the expected state is
11336 * restored, not the drivers personal bookkeeping.
11337 */
11338 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010011339 for_each_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011340 config->save_crtc_enabled[count++] = crtc->state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011341 }
11342
11343 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011344 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011345 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011346 }
11347
11348 count = 0;
11349 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011350 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011351 }
11352
11353 return 0;
11354}
11355
11356static void intel_set_config_restore_state(struct drm_device *dev,
11357 struct intel_set_config *config)
11358{
Ville Syrjälä76688512014-01-10 11:28:06 +020011359 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011360 struct intel_encoder *encoder;
11361 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011362 int count;
11363
11364 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011365 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011366 crtc->new_enabled = config->save_crtc_enabled[count++];
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011367
11368 if (crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011369 crtc->new_config = crtc->config;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011370 else
11371 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020011372 }
11373
11374 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010011375 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011376 encoder->new_crtc =
11377 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011378 }
11379
11380 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020011381 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11382 connector->new_encoder =
11383 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011384 }
11385}
11386
Imre Deake3de42b2013-05-03 19:44:07 +020011387static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010011388is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020011389{
11390 int i;
11391
Chris Wilson2e57f472013-07-17 12:14:40 +010011392 if (set->num_connectors == 0)
11393 return false;
11394
11395 if (WARN_ON(set->connectors == NULL))
11396 return false;
11397
11398 for (i = 0; i < set->num_connectors; i++)
11399 if (set->connectors[i]->encoder &&
11400 set->connectors[i]->encoder->crtc == set->crtc &&
11401 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020011402 return true;
11403
11404 return false;
11405}
11406
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011407static void
11408intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11409 struct intel_set_config *config)
11410{
11411
11412 /* We should be able to check here if the fb has the same properties
11413 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010011414 if (is_crtc_connector_off(set)) {
11415 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070011416 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070011417 /*
11418 * If we have no fb, we can only flip as long as the crtc is
11419 * active, otherwise we need a full mode set. The crtc may
11420 * be active if we've only disabled the primary plane, or
11421 * in fastboot situations.
11422 */
Matt Roperf4510a22014-04-01 15:22:40 -070011423 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030011424 struct intel_crtc *intel_crtc =
11425 to_intel_crtc(set->crtc);
11426
Matt Roper3b150f02014-05-29 08:06:53 -070011427 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030011428 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11429 config->fb_changed = true;
11430 } else {
11431 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11432 config->mode_changed = true;
11433 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011434 } else if (set->fb == NULL) {
11435 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010011436 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070011437 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011438 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020011439 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011440 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020011441 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011442 }
11443
Daniel Vetter835c5872012-07-10 18:11:08 +020011444 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011445 config->fb_changed = true;
11446
11447 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11448 DRM_DEBUG_KMS("modes are different, full mode set\n");
11449 drm_mode_debug_printmodeline(&set->crtc->mode);
11450 drm_mode_debug_printmodeline(set->mode);
11451 config->mode_changed = true;
11452 }
Chris Wilsona1d95702013-08-13 18:48:47 +010011453
11454 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11455 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011456}
11457
Daniel Vetter2e431052012-07-04 22:42:15 +020011458static int
Daniel Vetter9a935852012-07-05 22:34:27 +020011459intel_modeset_stage_output_state(struct drm_device *dev,
11460 struct drm_mode_set *set,
11461 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +020011462{
Daniel Vetter9a935852012-07-05 22:34:27 +020011463 struct intel_connector *connector;
11464 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020011465 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030011466 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020011467
Damien Lespiau9abdda72013-02-13 13:29:23 +000011468 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020011469 * of connectors. For paranoia, double-check this. */
11470 WARN_ON(!set->fb && (set->num_connectors != 0));
11471 WARN_ON(set->fb && (set->num_connectors == 0));
11472
Daniel Vetter9a935852012-07-05 22:34:27 +020011473 list_for_each_entry(connector, &dev->mode_config.connector_list,
11474 base.head) {
11475 /* Otherwise traverse passed in connector list and get encoders
11476 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020011477 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011478 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100011479 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020011480 break;
11481 }
11482 }
11483
Daniel Vetter9a935852012-07-05 22:34:27 +020011484 /* If we disable the crtc, disable all its connectors. Also, if
11485 * the connector is on the changing crtc but not on the new
11486 * connector list, disable it. */
11487 if ((!set->fb || ro == set->num_connectors) &&
11488 connector->base.encoder &&
11489 connector->base.encoder->crtc == set->crtc) {
11490 connector->new_encoder = NULL;
11491
11492 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11493 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011494 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020011495 }
11496
11497
11498 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +020011499 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011500 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011501 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011502 }
11503 /* connector->new_encoder is now updated for all connectors. */
11504
11505 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +020011506 list_for_each_entry(connector, &dev->mode_config.connector_list,
11507 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011508 struct drm_crtc *new_crtc;
11509
Daniel Vetter9a935852012-07-05 22:34:27 +020011510 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020011511 continue;
11512
Daniel Vetter9a935852012-07-05 22:34:27 +020011513 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020011514
11515 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011516 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020011517 new_crtc = set->crtc;
11518 }
11519
11520 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010011521 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11522 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011523 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020011524 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011525 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020011526
11527 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11528 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011529 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020011530 new_crtc->base.id);
11531 }
11532
11533 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010011534 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011535 int num_connectors = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020011536 list_for_each_entry(connector,
11537 &dev->mode_config.connector_list,
11538 base.head) {
11539 if (connector->new_encoder == encoder) {
11540 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011541 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020011542 }
11543 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011544
11545 if (num_connectors == 0)
11546 encoder->new_crtc = NULL;
11547 else if (num_connectors > 1)
11548 return -EINVAL;
11549
Daniel Vetter9a935852012-07-05 22:34:27 +020011550 /* Only now check for crtc changes so we don't miss encoders
11551 * that will be disabled. */
11552 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +020011553 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011554 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011555 }
11556 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011557 /* Now we've also updated encoder->new_crtc for all encoders. */
Dave Airlie0e32b392014-05-02 14:02:48 +100011558 list_for_each_entry(connector, &dev->mode_config.connector_list,
11559 base.head) {
11560 if (connector->new_encoder)
11561 if (connector->new_encoder != connector->encoder)
11562 connector->encoder = connector->new_encoder;
11563 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011564 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011565 crtc->new_enabled = false;
11566
Damien Lespiaub2784e12014-08-05 11:29:37 +010011567 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011568 if (encoder->new_crtc == crtc) {
11569 crtc->new_enabled = true;
11570 break;
11571 }
11572 }
11573
Matt Roper83d65732015-02-25 13:12:16 -080011574 if (crtc->new_enabled != crtc->base.state->enable) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011575 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11576 crtc->new_enabled ? "en" : "dis");
11577 config->mode_changed = true;
11578 }
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011579
11580 if (crtc->new_enabled)
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011581 crtc->new_config = crtc->config;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011582 else
11583 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020011584 }
11585
Daniel Vetter2e431052012-07-04 22:42:15 +020011586 return 0;
11587}
11588
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011589static void disable_crtc_nofb(struct intel_crtc *crtc)
11590{
11591 struct drm_device *dev = crtc->base.dev;
11592 struct intel_encoder *encoder;
11593 struct intel_connector *connector;
11594
11595 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11596 pipe_name(crtc->pipe));
11597
11598 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11599 if (connector->new_encoder &&
11600 connector->new_encoder->new_crtc == crtc)
11601 connector->new_encoder = NULL;
11602 }
11603
Damien Lespiaub2784e12014-08-05 11:29:37 +010011604 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011605 if (encoder->new_crtc == crtc)
11606 encoder->new_crtc = NULL;
11607 }
11608
11609 crtc->new_enabled = false;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011610 crtc->new_config = NULL;
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011611}
11612
Daniel Vetter2e431052012-07-04 22:42:15 +020011613static int intel_crtc_set_config(struct drm_mode_set *set)
11614{
11615 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020011616 struct drm_mode_set save_set;
11617 struct intel_set_config *config;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011618 struct intel_crtc_state *pipe_config;
Jesse Barnes50f52752014-11-07 13:11:00 -080011619 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetter2e431052012-07-04 22:42:15 +020011620 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020011621
Daniel Vetter8d3e3752012-07-05 16:09:09 +020011622 BUG_ON(!set);
11623 BUG_ON(!set->crtc);
11624 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020011625
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010011626 /* Enforce sane interface api - has been abused by the fb helper. */
11627 BUG_ON(!set->mode && set->fb);
11628 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020011629
Daniel Vetter2e431052012-07-04 22:42:15 +020011630 if (set->fb) {
11631 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11632 set->crtc->base.id, set->fb->base.id,
11633 (int)set->num_connectors, set->x, set->y);
11634 } else {
11635 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020011636 }
11637
11638 dev = set->crtc->dev;
11639
11640 ret = -ENOMEM;
11641 config = kzalloc(sizeof(*config), GFP_KERNEL);
11642 if (!config)
11643 goto out_config;
11644
11645 ret = intel_set_config_save_state(dev, config);
11646 if (ret)
11647 goto out_config;
11648
11649 save_set.crtc = set->crtc;
11650 save_set.mode = &set->crtc->mode;
11651 save_set.x = set->crtc->x;
11652 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070011653 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020011654
11655 /* Compute whether we need a full modeset, only an fb base update or no
11656 * change at all. In the future we might also check whether only the
11657 * mode changed, e.g. for LVDS where we only change the panel fitter in
11658 * such cases. */
11659 intel_set_config_compute_mode_changes(set, config);
11660
Daniel Vetter9a935852012-07-05 22:34:27 +020011661 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +020011662 if (ret)
11663 goto fail;
11664
Jesse Barnes50f52752014-11-07 13:11:00 -080011665 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
11666 set->fb,
11667 &modeset_pipes,
11668 &prepare_pipes,
11669 &disable_pipes);
Jesse Barnes20664592014-11-05 14:26:09 -080011670 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080011671 ret = PTR_ERR(pipe_config);
Jesse Barnes50f52752014-11-07 13:11:00 -080011672 goto fail;
Jesse Barnes20664592014-11-05 14:26:09 -080011673 } else if (pipe_config) {
Ville Syrjäläb9950a12014-11-21 21:00:36 +020011674 if (pipe_config->has_audio !=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011675 to_intel_crtc(set->crtc)->config->has_audio)
Jesse Barnes20664592014-11-05 14:26:09 -080011676 config->mode_changed = true;
11677
Jesse Barnesaf15d2c2014-12-01 09:54:28 -080011678 /*
11679 * Note we have an issue here with infoframes: current code
11680 * only updates them on the full mode set path per hw
11681 * requirements. So here we should be checking for any
11682 * required changes and forcing a mode set.
11683 */
Jesse Barnes20664592014-11-05 14:26:09 -080011684 }
Jesse Barnes50f52752014-11-07 13:11:00 -080011685
11686 /* set_mode will free it in the mode_changed case */
11687 if (!config->mode_changed)
11688 kfree(pipe_config);
11689
Jesse Barnes1f9954d2014-11-05 14:26:10 -080011690 intel_update_pipe_size(to_intel_crtc(set->crtc));
11691
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011692 if (config->mode_changed) {
Jesse Barnes50f52752014-11-07 13:11:00 -080011693 ret = intel_set_mode_pipes(set->crtc, set->mode,
11694 set->x, set->y, set->fb, pipe_config,
11695 modeset_pipes, prepare_pipes,
11696 disable_pipes);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011697 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070011698 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011699 struct drm_plane *primary = set->crtc->primary;
11700 int vdisplay, hdisplay;
Matt Roper3b150f02014-05-29 08:06:53 -070011701
Gustavo Padovan455a6802014-12-01 15:40:11 -080011702 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
11703 ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
11704 0, 0, hdisplay, vdisplay,
11705 set->x << 16, set->y << 16,
11706 hdisplay << 16, vdisplay << 16);
Matt Roper3b150f02014-05-29 08:06:53 -070011707
11708 /*
11709 * We need to make sure the primary plane is re-enabled if it
11710 * has previously been turned off.
11711 */
11712 if (!intel_crtc->primary_enabled && ret == 0) {
11713 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a62014-08-08 21:51:11 +030011714 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070011715 }
11716
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011717 /*
11718 * In the fastboot case this may be our only check of the
11719 * state after boot. It would be better to only do it on
11720 * the first update, but we don't have a nice way of doing that
11721 * (and really, set_config isn't used much for high freq page
11722 * flipping, so increasing its cost here shouldn't be a big
11723 * deal).
11724 */
Jani Nikulad330a952014-01-21 11:24:25 +020011725 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011726 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020011727 }
11728
Chris Wilson2d05eae2013-05-03 17:36:25 +010011729 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020011730 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11731 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020011732fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010011733 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011734
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011735 /*
11736 * HACK: if the pipe was on, but we didn't have a framebuffer,
11737 * force the pipe off to avoid oopsing in the modeset code
11738 * due to fb==NULL. This should only happen during boot since
11739 * we don't yet reconstruct the FB from the hardware state.
11740 */
11741 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11742 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11743
Chris Wilson2d05eae2013-05-03 17:36:25 +010011744 /* Try to restore the config */
11745 if (config->mode_changed &&
11746 intel_set_mode(save_set.crtc, save_set.mode,
11747 save_set.x, save_set.y, save_set.fb))
11748 DRM_ERROR("failed to restore config after modeset failure\n");
11749 }
Daniel Vetter50f56112012-07-02 09:35:43 +020011750
Daniel Vetterd9e55602012-07-04 22:16:09 +020011751out_config:
11752 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011753 return ret;
11754}
11755
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011756static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011757 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020011758 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011759 .destroy = intel_crtc_destroy,
11760 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080011761 .atomic_duplicate_state = intel_crtc_duplicate_state,
11762 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011763};
11764
Daniel Vetter53589012013-06-05 13:34:16 +020011765static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11766 struct intel_shared_dpll *pll,
11767 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011768{
Daniel Vetter53589012013-06-05 13:34:16 +020011769 uint32_t val;
11770
Daniel Vetterf458ebb2014-09-30 10:56:39 +020011771 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030011772 return false;
11773
Daniel Vetter53589012013-06-05 13:34:16 +020011774 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020011775 hw_state->dpll = val;
11776 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11777 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020011778
11779 return val & DPLL_VCO_ENABLE;
11780}
11781
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011782static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11783 struct intel_shared_dpll *pll)
11784{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011785 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
11786 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011787}
11788
Daniel Vettere7b903d2013-06-05 13:34:14 +020011789static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11790 struct intel_shared_dpll *pll)
11791{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011792 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020011793 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020011794
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011795 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011796
11797 /* Wait for the clocks to stabilize. */
11798 POSTING_READ(PCH_DPLL(pll->id));
11799 udelay(150);
11800
11801 /* The pixel multiplier can only be updated once the
11802 * DPLL is enabled and the clocks are stable.
11803 *
11804 * So write it again.
11805 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011806 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011807 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011808 udelay(200);
11809}
11810
11811static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11812 struct intel_shared_dpll *pll)
11813{
11814 struct drm_device *dev = dev_priv->dev;
11815 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011816
11817 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011818 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020011819 if (intel_crtc_to_shared_dpll(crtc) == pll)
11820 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
11821 }
11822
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011823 I915_WRITE(PCH_DPLL(pll->id), 0);
11824 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011825 udelay(200);
11826}
11827
Daniel Vetter46edb022013-06-05 13:34:12 +020011828static char *ibx_pch_dpll_names[] = {
11829 "PCH DPLL A",
11830 "PCH DPLL B",
11831};
11832
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011833static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011834{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011835 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011836 int i;
11837
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011838 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011839
Daniel Vettere72f9fb2013-06-05 13:34:06 +020011840 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020011841 dev_priv->shared_dplls[i].id = i;
11842 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011843 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011844 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11845 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020011846 dev_priv->shared_dplls[i].get_hw_state =
11847 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011848 }
11849}
11850
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011851static void intel_shared_dpll_init(struct drm_device *dev)
11852{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011853 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011854
Daniel Vetter9cd86932014-06-25 22:01:57 +030011855 if (HAS_DDI(dev))
11856 intel_ddi_pll_init(dev);
11857 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011858 ibx_pch_dpll_init(dev);
11859 else
11860 dev_priv->num_shared_dpll = 0;
11861
11862 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011863}
11864
Matt Roper6beb8c232014-12-01 15:40:14 -080011865/**
11866 * intel_prepare_plane_fb - Prepare fb for usage on plane
11867 * @plane: drm plane to prepare for
11868 * @fb: framebuffer to prepare for presentation
11869 *
11870 * Prepares a framebuffer for usage on a display plane. Generally this
11871 * involves pinning the underlying object and updating the frontbuffer tracking
11872 * bits. Some older platforms need special physical address handling for
11873 * cursor planes.
11874 *
11875 * Returns 0 on success, negative error code on failure.
11876 */
11877int
11878intel_prepare_plane_fb(struct drm_plane *plane,
11879 struct drm_framebuffer *fb)
Matt Roper465c1202014-05-29 08:06:54 -070011880{
11881 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080011882 struct intel_plane *intel_plane = to_intel_plane(plane);
11883 enum pipe pipe = intel_plane->pipe;
11884 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11885 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
11886 unsigned frontbuffer_bits = 0;
11887 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070011888
Matt Roperea2c67b2014-12-23 10:41:52 -080011889 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070011890 return 0;
11891
Matt Roper6beb8c232014-12-01 15:40:14 -080011892 switch (plane->type) {
11893 case DRM_PLANE_TYPE_PRIMARY:
11894 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
11895 break;
11896 case DRM_PLANE_TYPE_CURSOR:
11897 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
11898 break;
11899 case DRM_PLANE_TYPE_OVERLAY:
11900 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
11901 break;
11902 }
Matt Roper465c1202014-05-29 08:06:54 -070011903
Matt Roper4c345742014-07-09 16:22:10 -070011904 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070011905
Matt Roper6beb8c232014-12-01 15:40:14 -080011906 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
11907 INTEL_INFO(dev)->cursor_needs_physical) {
11908 int align = IS_I830(dev) ? 16 * 1024 : 256;
11909 ret = i915_gem_object_attach_phys(obj, align);
11910 if (ret)
11911 DRM_DEBUG_KMS("failed to attach phys object\n");
11912 } else {
11913 ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
11914 }
11915
11916 if (ret == 0)
11917 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
11918
11919 mutex_unlock(&dev->struct_mutex);
11920
11921 return ret;
11922}
11923
Matt Roper38f3ce32014-12-02 07:45:25 -080011924/**
11925 * intel_cleanup_plane_fb - Cleans up an fb after plane use
11926 * @plane: drm plane to clean up for
11927 * @fb: old framebuffer that was on plane
11928 *
11929 * Cleans up a framebuffer that has just been removed from a plane.
11930 */
11931void
11932intel_cleanup_plane_fb(struct drm_plane *plane,
11933 struct drm_framebuffer *fb)
11934{
11935 struct drm_device *dev = plane->dev;
11936 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11937
11938 if (WARN_ON(!obj))
11939 return;
11940
11941 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
11942 !INTEL_INFO(dev)->cursor_needs_physical) {
11943 mutex_lock(&dev->struct_mutex);
11944 intel_unpin_fb_obj(obj);
11945 mutex_unlock(&dev->struct_mutex);
11946 }
Matt Roper465c1202014-05-29 08:06:54 -070011947}
11948
11949static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011950intel_check_primary_plane(struct drm_plane *plane,
11951 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070011952{
Matt Roper32b7eee2014-12-24 07:59:06 -080011953 struct drm_device *dev = plane->dev;
11954 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080011955 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080011956 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080011957 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011958 struct drm_rect *dest = &state->dst;
11959 struct drm_rect *src = &state->src;
11960 const struct drm_rect *clip = &state->clip;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011961 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011962
Matt Roperea2c67b2014-12-23 10:41:52 -080011963 crtc = crtc ? crtc : plane->crtc;
11964 intel_crtc = to_intel_crtc(crtc);
11965
Matt Roperc59cb172014-12-01 15:40:16 -080011966 ret = drm_plane_helper_check_update(plane, crtc, fb,
11967 src, dest, clip,
11968 DRM_PLANE_HELPER_NO_SCALING,
11969 DRM_PLANE_HELPER_NO_SCALING,
11970 false, true, &state->visible);
11971 if (ret)
11972 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011973
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011974 if (intel_crtc->active) {
Matt Roper32b7eee2014-12-24 07:59:06 -080011975 intel_crtc->atomic.wait_for_flips = true;
11976
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011977 /*
11978 * FBC does not work on some platforms for rotated
11979 * planes, so disable it when rotation is not 0 and
11980 * update it when rotation is set back to 0.
11981 *
11982 * FIXME: This is redundant with the fbc update done in
11983 * the primary plane enable function except that that
11984 * one is done too late. We eventually need to unify
11985 * this.
11986 */
11987 if (intel_crtc->primary_enabled &&
11988 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020011989 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080011990 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080011991 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011992 }
11993
11994 if (state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080011995 /*
11996 * BDW signals flip done immediately if the plane
11997 * is disabled, even if the plane enable is already
11998 * armed to occur at the next vblank :(
11999 */
12000 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
12001 intel_crtc->atomic.wait_vblank = true;
12002 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012003
Matt Roper32b7eee2014-12-24 07:59:06 -080012004 intel_crtc->atomic.fb_bits |=
12005 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
12006
12007 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000012008
12009 /* Update watermarks on tiling changes. */
12010 if (!plane->state->fb || !state->base.fb ||
12011 plane->state->fb->modifier[0] !=
12012 state->base.fb->modifier[0])
12013 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080012014 }
12015
12016 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070012017}
12018
Sonika Jindal48404c12014-08-22 14:06:04 +053012019static void
12020intel_commit_primary_plane(struct drm_plane *plane,
12021 struct intel_plane_state *state)
12022{
Matt Roper2b875c22014-12-01 15:40:13 -080012023 struct drm_crtc *crtc = state->base.crtc;
12024 struct drm_framebuffer *fb = state->base.fb;
12025 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053012026 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080012027 struct intel_crtc *intel_crtc;
Sonika Jindal48404c12014-08-22 14:06:04 +053012028 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Sonika Jindalce54d852014-08-21 11:44:39 +053012029 struct intel_plane *intel_plane = to_intel_plane(plane);
12030 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080012031
Matt Roperea2c67b2014-12-23 10:41:52 -080012032 crtc = crtc ? crtc : plane->crtc;
12033 intel_crtc = to_intel_crtc(crtc);
12034
Matt Ropercf4c7c12014-12-04 10:27:42 -080012035 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053012036 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070012037 crtc->y = src->y1 >> 16;
12038
Sonika Jindalce54d852014-08-21 11:44:39 +053012039 intel_plane->obj = obj;
Matt Roper465c1202014-05-29 08:06:54 -070012040
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012041 if (intel_crtc->active) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012042 if (state->visible) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012043 /* FIXME: kill this fastboot hack */
12044 intel_update_pipe_size(intel_crtc);
12045
12046 intel_crtc->primary_enabled = true;
12047
12048 dev_priv->display.update_primary_plane(crtc, plane->fb,
12049 crtc->x, crtc->y);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012050 } else {
12051 /*
12052 * If clipping results in a non-visible primary plane,
12053 * we'll disable the primary plane. Note that this is
12054 * a bit different than what happens if userspace
12055 * explicitly disables the plane by passing fb=0
12056 * because plane->fb still gets set and pinned.
12057 */
12058 intel_disable_primary_hw_plane(plane, crtc);
12059 }
Matt Roper32b7eee2014-12-24 07:59:06 -080012060 }
12061}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012062
Matt Roper32b7eee2014-12-24 07:59:06 -080012063static void intel_begin_crtc_commit(struct drm_crtc *crtc)
12064{
12065 struct drm_device *dev = crtc->dev;
12066 struct drm_i915_private *dev_priv = dev->dev_private;
12067 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080012068 struct intel_plane *intel_plane;
12069 struct drm_plane *p;
12070 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012071
Matt Roperea2c67b2014-12-23 10:41:52 -080012072 /* Track fb's for any planes being disabled */
12073 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
12074 intel_plane = to_intel_plane(p);
12075
12076 if (intel_crtc->atomic.disabled_planes &
12077 (1 << drm_plane_index(p))) {
12078 switch (p->type) {
12079 case DRM_PLANE_TYPE_PRIMARY:
12080 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
12081 break;
12082 case DRM_PLANE_TYPE_CURSOR:
12083 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
12084 break;
12085 case DRM_PLANE_TYPE_OVERLAY:
12086 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
12087 break;
12088 }
12089
12090 mutex_lock(&dev->struct_mutex);
12091 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
12092 mutex_unlock(&dev->struct_mutex);
12093 }
12094 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012095
Matt Roper32b7eee2014-12-24 07:59:06 -080012096 if (intel_crtc->atomic.wait_for_flips)
12097 intel_crtc_wait_for_pending_flips(crtc);
12098
12099 if (intel_crtc->atomic.disable_fbc)
12100 intel_fbc_disable(dev);
12101
12102 if (intel_crtc->atomic.pre_disable_primary)
12103 intel_pre_disable_primary(crtc);
12104
12105 if (intel_crtc->atomic.update_wm)
12106 intel_update_watermarks(crtc);
12107
12108 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080012109
12110 /* Perform vblank evasion around commit operation */
12111 if (intel_crtc->active)
12112 intel_crtc->atomic.evade =
12113 intel_pipe_update_start(intel_crtc,
12114 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080012115}
12116
12117static void intel_finish_crtc_commit(struct drm_crtc *crtc)
12118{
12119 struct drm_device *dev = crtc->dev;
12120 struct drm_i915_private *dev_priv = dev->dev_private;
12121 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12122 struct drm_plane *p;
12123
Matt Roperc34c9ee2014-12-23 10:41:50 -080012124 if (intel_crtc->atomic.evade)
12125 intel_pipe_update_end(intel_crtc,
12126 intel_crtc->atomic.start_vbl_count);
12127
Matt Roper32b7eee2014-12-24 07:59:06 -080012128 intel_runtime_pm_put(dev_priv);
12129
12130 if (intel_crtc->atomic.wait_vblank)
12131 intel_wait_for_vblank(dev, intel_crtc->pipe);
12132
12133 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
12134
12135 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012136 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020012137 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030012138 mutex_unlock(&dev->struct_mutex);
12139 }
Matt Roper465c1202014-05-29 08:06:54 -070012140
Matt Roper32b7eee2014-12-24 07:59:06 -080012141 if (intel_crtc->atomic.post_enable_primary)
12142 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012143
Matt Roper32b7eee2014-12-24 07:59:06 -080012144 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
12145 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
12146 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
12147 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012148
Matt Roper32b7eee2014-12-24 07:59:06 -080012149 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030012150}
12151
Matt Ropercf4c7c12014-12-04 10:27:42 -080012152/**
Matt Roper4a3b8762014-12-23 10:41:51 -080012153 * intel_plane_destroy - destroy a plane
12154 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080012155 *
Matt Roper4a3b8762014-12-23 10:41:51 -080012156 * Common destruction function for all types of planes (primary, cursor,
12157 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080012158 */
Matt Roper4a3b8762014-12-23 10:41:51 -080012159void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070012160{
12161 struct intel_plane *intel_plane = to_intel_plane(plane);
12162 drm_plane_cleanup(plane);
12163 kfree(intel_plane);
12164}
12165
Matt Roper65a3fea2015-01-21 16:35:42 -080012166const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper3f678c92015-01-30 16:22:37 -080012167 .update_plane = drm_atomic_helper_update_plane,
12168 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070012169 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080012170 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080012171 .atomic_get_property = intel_plane_atomic_get_property,
12172 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080012173 .atomic_duplicate_state = intel_plane_duplicate_state,
12174 .atomic_destroy_state = intel_plane_destroy_state,
12175
Matt Roper465c1202014-05-29 08:06:54 -070012176};
12177
12178static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
12179 int pipe)
12180{
12181 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080012182 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070012183 const uint32_t *intel_primary_formats;
12184 int num_formats;
12185
12186 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
12187 if (primary == NULL)
12188 return NULL;
12189
Matt Roper8e7d6882015-01-21 16:35:41 -080012190 state = intel_create_plane_state(&primary->base);
12191 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080012192 kfree(primary);
12193 return NULL;
12194 }
Matt Roper8e7d6882015-01-21 16:35:41 -080012195 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080012196
Matt Roper465c1202014-05-29 08:06:54 -070012197 primary->can_scale = false;
12198 primary->max_downscale = 1;
12199 primary->pipe = pipe;
12200 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080012201 primary->check_plane = intel_check_primary_plane;
12202 primary->commit_plane = intel_commit_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070012203 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
12204 primary->plane = !pipe;
12205
12206 if (INTEL_INFO(dev)->gen <= 3) {
12207 intel_primary_formats = intel_primary_formats_gen2;
12208 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
12209 } else {
12210 intel_primary_formats = intel_primary_formats_gen4;
12211 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
12212 }
12213
12214 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080012215 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070012216 intel_primary_formats, num_formats,
12217 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053012218
12219 if (INTEL_INFO(dev)->gen >= 4) {
12220 if (!dev->mode_config.rotation_property)
12221 dev->mode_config.rotation_property =
12222 drm_mode_create_rotation_property(dev,
12223 BIT(DRM_ROTATE_0) |
12224 BIT(DRM_ROTATE_180));
12225 if (dev->mode_config.rotation_property)
12226 drm_object_attach_property(&primary->base.base,
12227 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080012228 state->base.rotation);
Sonika Jindal48404c12014-08-22 14:06:04 +053012229 }
12230
Matt Roperea2c67b2014-12-23 10:41:52 -080012231 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
12232
Matt Roper465c1202014-05-29 08:06:54 -070012233 return &primary->base;
12234}
12235
Matt Roper3d7d6512014-06-10 08:28:13 -070012236static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030012237intel_check_cursor_plane(struct drm_plane *plane,
12238 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070012239{
Matt Roper2b875c22014-12-01 15:40:13 -080012240 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012241 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080012242 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012243 struct drm_rect *dest = &state->dst;
12244 struct drm_rect *src = &state->src;
12245 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012246 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080012247 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012248 unsigned stride;
12249 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012250
Matt Roperea2c67b2014-12-23 10:41:52 -080012251 crtc = crtc ? crtc : plane->crtc;
12252 intel_crtc = to_intel_crtc(crtc);
12253
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012254 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030012255 src, dest, clip,
12256 DRM_PLANE_HELPER_NO_SCALING,
12257 DRM_PLANE_HELPER_NO_SCALING,
12258 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012259 if (ret)
12260 return ret;
12261
12262
12263 /* if we want to turn off the cursor ignore width and height */
12264 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080012265 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012266
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012267 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080012268 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
12269 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
12270 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012271 return -EINVAL;
12272 }
12273
Matt Roperea2c67b2014-12-23 10:41:52 -080012274 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
12275 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012276 DRM_DEBUG_KMS("buffer is too small\n");
12277 return -ENOMEM;
12278 }
12279
Gustavo Padovane391ea82014-09-24 14:20:25 -030012280 if (fb == crtc->cursor->fb)
12281 return 0;
12282
Tvrtko Ursulin6a418fc2015-02-10 17:16:14 +000012283 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012284 DRM_DEBUG_KMS("cursor cannot be tiled\n");
12285 ret = -EINVAL;
12286 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012287
Matt Roper32b7eee2014-12-24 07:59:06 -080012288finish:
12289 if (intel_crtc->active) {
Matt Roperea2c67b2014-12-23 10:41:52 -080012290 if (intel_crtc->cursor_width != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080012291 intel_crtc->atomic.update_wm = true;
12292
12293 intel_crtc->atomic.fb_bits |=
12294 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
12295 }
12296
Gustavo Padovan757f9a32014-09-24 14:20:24 -030012297 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012298}
12299
Matt Roperf4a2cf22014-12-01 15:40:12 -080012300static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030012301intel_commit_cursor_plane(struct drm_plane *plane,
12302 struct intel_plane_state *state)
12303{
Matt Roper2b875c22014-12-01 15:40:13 -080012304 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080012305 struct drm_device *dev = plane->dev;
12306 struct intel_crtc *intel_crtc;
Sonika Jindala919db92014-10-23 07:41:33 -070012307 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper2b875c22014-12-01 15:40:13 -080012308 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080012309 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070012310
Matt Roperea2c67b2014-12-23 10:41:52 -080012311 crtc = crtc ? crtc : plane->crtc;
12312 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070012313
Matt Roperea2c67b2014-12-23 10:41:52 -080012314 plane->fb = state->base.fb;
12315 crtc->cursor_x = state->base.crtc_x;
12316 crtc->cursor_y = state->base.crtc_y;
12317
Sonika Jindala919db92014-10-23 07:41:33 -070012318 intel_plane->obj = obj;
12319
Gustavo Padovana912f122014-12-01 15:40:10 -080012320 if (intel_crtc->cursor_bo == obj)
12321 goto update;
12322
Matt Roperf4a2cf22014-12-01 15:40:12 -080012323 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080012324 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080012325 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080012326 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080012327 else
Gustavo Padovana912f122014-12-01 15:40:10 -080012328 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080012329
Gustavo Padovana912f122014-12-01 15:40:10 -080012330 intel_crtc->cursor_addr = addr;
12331 intel_crtc->cursor_bo = obj;
12332update:
Matt Roperea2c67b2014-12-23 10:41:52 -080012333 intel_crtc->cursor_width = state->base.crtc_w;
12334 intel_crtc->cursor_height = state->base.crtc_h;
Gustavo Padovana912f122014-12-01 15:40:10 -080012335
Matt Roper32b7eee2014-12-24 07:59:06 -080012336 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030012337 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070012338}
Gustavo Padovan852e7872014-09-05 17:22:31 -030012339
Matt Roper3d7d6512014-06-10 08:28:13 -070012340static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12341 int pipe)
12342{
12343 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080012344 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070012345
12346 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12347 if (cursor == NULL)
12348 return NULL;
12349
Matt Roper8e7d6882015-01-21 16:35:41 -080012350 state = intel_create_plane_state(&cursor->base);
12351 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080012352 kfree(cursor);
12353 return NULL;
12354 }
Matt Roper8e7d6882015-01-21 16:35:41 -080012355 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080012356
Matt Roper3d7d6512014-06-10 08:28:13 -070012357 cursor->can_scale = false;
12358 cursor->max_downscale = 1;
12359 cursor->pipe = pipe;
12360 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080012361 cursor->check_plane = intel_check_cursor_plane;
12362 cursor->commit_plane = intel_commit_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070012363
12364 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080012365 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070012366 intel_cursor_formats,
12367 ARRAY_SIZE(intel_cursor_formats),
12368 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070012369
12370 if (INTEL_INFO(dev)->gen >= 4) {
12371 if (!dev->mode_config.rotation_property)
12372 dev->mode_config.rotation_property =
12373 drm_mode_create_rotation_property(dev,
12374 BIT(DRM_ROTATE_0) |
12375 BIT(DRM_ROTATE_180));
12376 if (dev->mode_config.rotation_property)
12377 drm_object_attach_property(&cursor->base.base,
12378 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080012379 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070012380 }
12381
Matt Roperea2c67b2014-12-23 10:41:52 -080012382 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
12383
Matt Roper3d7d6512014-06-10 08:28:13 -070012384 return &cursor->base;
12385}
12386
Hannes Ederb358d0a2008-12-18 21:18:47 +010012387static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080012388{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012389 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080012390 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012391 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070012392 struct drm_plane *primary = NULL;
12393 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070012394 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080012395
Daniel Vetter955382f2013-09-19 14:05:45 +020012396 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080012397 if (intel_crtc == NULL)
12398 return;
12399
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012400 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
12401 if (!crtc_state)
12402 goto fail;
12403 intel_crtc_set_state(intel_crtc, crtc_state);
Matt Roper07878242015-02-25 11:43:26 -080012404 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012405
Matt Roper465c1202014-05-29 08:06:54 -070012406 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070012407 if (!primary)
12408 goto fail;
12409
12410 cursor = intel_cursor_plane_create(dev, pipe);
12411 if (!cursor)
12412 goto fail;
12413
Matt Roper465c1202014-05-29 08:06:54 -070012414 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070012415 cursor, &intel_crtc_funcs);
12416 if (ret)
12417 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080012418
12419 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080012420 for (i = 0; i < 256; i++) {
12421 intel_crtc->lut_r[i] = i;
12422 intel_crtc->lut_g[i] = i;
12423 intel_crtc->lut_b[i] = i;
12424 }
12425
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020012426 /*
12427 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020012428 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020012429 */
Jesse Barnes80824002009-09-10 15:28:06 -070012430 intel_crtc->pipe = pipe;
12431 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010012432 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080012433 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010012434 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070012435 }
12436
Chris Wilson4b0e3332014-05-30 16:35:26 +030012437 intel_crtc->cursor_base = ~0;
12438 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030012439 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030012440
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080012441 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
12442 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
12443 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
12444 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
12445
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020012446 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
12447
Jesse Barnes79e53942008-11-07 14:24:08 -080012448 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020012449
12450 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070012451 return;
12452
12453fail:
12454 if (primary)
12455 drm_plane_cleanup(primary);
12456 if (cursor)
12457 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012458 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070012459 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080012460}
12461
Jesse Barnes752aa882013-10-31 18:55:49 +020012462enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12463{
12464 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012465 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020012466
Rob Clark51fd3712013-11-19 12:10:12 -050012467 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020012468
Ville Syrjäläd3babd32014-11-07 11:16:01 +020012469 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020012470 return INVALID_PIPE;
12471
12472 return to_intel_crtc(encoder->crtc)->pipe;
12473}
12474
Carl Worth08d7b3d2009-04-29 14:43:54 -070012475int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000012476 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070012477{
Carl Worth08d7b3d2009-04-29 14:43:54 -070012478 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040012479 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020012480 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012481
Rob Clark7707e652014-07-17 23:30:04 -040012482 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070012483
Rob Clark7707e652014-07-17 23:30:04 -040012484 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070012485 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030012486 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012487 }
12488
Rob Clark7707e652014-07-17 23:30:04 -040012489 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020012490 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012491
Daniel Vetterc05422d2009-08-11 16:05:30 +020012492 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012493}
12494
Daniel Vetter66a92782012-07-12 20:08:18 +020012495static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080012496{
Daniel Vetter66a92782012-07-12 20:08:18 +020012497 struct drm_device *dev = encoder->base.dev;
12498 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080012499 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080012500 int entry = 0;
12501
Damien Lespiaub2784e12014-08-05 11:29:37 +010012502 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012503 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020012504 index_mask |= (1 << entry);
12505
Jesse Barnes79e53942008-11-07 14:24:08 -080012506 entry++;
12507 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010012508
Jesse Barnes79e53942008-11-07 14:24:08 -080012509 return index_mask;
12510}
12511
Chris Wilson4d302442010-12-14 19:21:29 +000012512static bool has_edp_a(struct drm_device *dev)
12513{
12514 struct drm_i915_private *dev_priv = dev->dev_private;
12515
12516 if (!IS_MOBILE(dev))
12517 return false;
12518
12519 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12520 return false;
12521
Damien Lespiaue3589902014-02-07 19:12:50 +000012522 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000012523 return false;
12524
12525 return true;
12526}
12527
Jesse Barnes84b4e042014-06-25 08:24:29 -070012528static bool intel_crt_present(struct drm_device *dev)
12529{
12530 struct drm_i915_private *dev_priv = dev->dev_private;
12531
Damien Lespiau884497e2013-12-03 13:56:23 +000012532 if (INTEL_INFO(dev)->gen >= 9)
12533 return false;
12534
Damien Lespiaucf404ce2014-10-01 20:04:15 +010012535 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070012536 return false;
12537
12538 if (IS_CHERRYVIEW(dev))
12539 return false;
12540
12541 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12542 return false;
12543
12544 return true;
12545}
12546
Jesse Barnes79e53942008-11-07 14:24:08 -080012547static void intel_setup_outputs(struct drm_device *dev)
12548{
Eric Anholt725e30a2009-01-22 13:01:02 -080012549 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010012550 struct intel_encoder *encoder;
Matt Roperc6f95f22015-01-22 16:50:32 -080012551 struct drm_connector *connector;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012552 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080012553
Daniel Vetterc9093352013-06-06 22:22:47 +020012554 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012555
Jesse Barnes84b4e042014-06-25 08:24:29 -070012556 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020012557 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012558
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012559 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030012560 int found;
12561
12562 /* Haswell uses DDI functions to detect digital outputs */
12563 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12564 /* DDI A only supports eDP */
12565 if (found)
12566 intel_ddi_init(dev, PORT_A);
12567
12568 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12569 * register */
12570 found = I915_READ(SFUSE_STRAP);
12571
12572 if (found & SFUSE_STRAP_DDIB_DETECTED)
12573 intel_ddi_init(dev, PORT_B);
12574 if (found & SFUSE_STRAP_DDIC_DETECTED)
12575 intel_ddi_init(dev, PORT_C);
12576 if (found & SFUSE_STRAP_DDID_DETECTED)
12577 intel_ddi_init(dev, PORT_D);
12578 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012579 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020012580 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020012581
12582 if (has_edp_a(dev))
12583 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012584
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012585 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080012586 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010012587 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012588 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012589 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012590 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012591 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012592 }
12593
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012594 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012595 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012596
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012597 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012598 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012599
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012600 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012601 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012602
Daniel Vetter270b3042012-10-27 15:52:05 +020012603 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012604 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070012605 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012606 /*
12607 * The DP_DETECTED bit is the latched state of the DDC
12608 * SDA pin at boot. However since eDP doesn't require DDC
12609 * (no way to plug in a DP->HDMI dongle) the DDC pins for
12610 * eDP ports may have been muxed to an alternate function.
12611 * Thus we can't rely on the DP_DETECTED bit alone to detect
12612 * eDP ports. Consult the VBT as well as DP_DETECTED to
12613 * detect eDP ports.
12614 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020012615 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
12616 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030012617 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12618 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012619 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
12620 intel_dp_is_edp(dev, PORT_B))
12621 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030012622
Ville Syrjäläd2182a62015-01-09 14:21:14 +020012623 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
12624 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070012625 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12626 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012627 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
12628 intel_dp_is_edp(dev, PORT_C))
12629 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053012630
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012631 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012632 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012633 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12634 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012635 /* eDP not supported on port D, so don't check VBT */
12636 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12637 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012638 }
12639
Jani Nikula3cfca972013-08-27 15:12:26 +030012640 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080012641 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080012642 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080012643
Paulo Zanonie2debe92013-02-18 19:00:27 -030012644 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012645 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012646 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012647 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12648 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012649 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012650 }
Ma Ling27185ae2009-08-24 13:50:23 +080012651
Imre Deake7281ea2013-05-08 13:14:08 +030012652 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012653 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080012654 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040012655
12656 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040012657
Paulo Zanonie2debe92013-02-18 19:00:27 -030012658 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012659 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012660 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012661 }
Ma Ling27185ae2009-08-24 13:50:23 +080012662
Paulo Zanonie2debe92013-02-18 19:00:27 -030012663 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080012664
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012665 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12666 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012667 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012668 }
Imre Deake7281ea2013-05-08 13:14:08 +030012669 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012670 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080012671 }
Ma Ling27185ae2009-08-24 13:50:23 +080012672
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012673 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030012674 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012675 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070012676 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080012677 intel_dvo_init(dev);
12678
Zhenyu Wang103a1962009-11-27 11:44:36 +080012679 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080012680 intel_tv_init(dev);
12681
Matt Roperc6f95f22015-01-22 16:50:32 -080012682 /*
12683 * FIXME: We don't have full atomic support yet, but we want to be
12684 * able to enable/test plane updates via the atomic interface in the
12685 * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
12686 * will take some atomic codepaths to lookup properties during
12687 * drmModeGetConnector() that unconditionally dereference
12688 * connector->state.
12689 *
12690 * We create a dummy connector state here for each connector to ensure
12691 * the DRM core doesn't try to dereference a NULL connector->state.
12692 * The actual connector properties will never be updated or contain
12693 * useful information, but since we're doing this specifically for
12694 * testing/debug of the plane operations (and only when a specific
12695 * kernel module option is given), that shouldn't really matter.
12696 *
12697 * Once atomic support for crtc's + connectors lands, this loop should
12698 * be removed since we'll be setting up real connector state, which
12699 * will contain Intel-specific properties.
12700 */
12701 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
12702 list_for_each_entry(connector,
12703 &dev->mode_config.connector_list,
12704 head) {
12705 if (!WARN_ON(connector->state)) {
12706 connector->state =
12707 kzalloc(sizeof(*connector->state),
12708 GFP_KERNEL);
12709 }
12710 }
12711 }
12712
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080012713 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070012714
Damien Lespiaub2784e12014-08-05 11:29:37 +010012715 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010012716 encoder->base.possible_crtcs = encoder->crtc_mask;
12717 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020012718 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080012719 }
Chris Wilson47356eb2011-01-11 17:06:04 +000012720
Paulo Zanonidde86e22012-12-01 12:04:25 -020012721 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020012722
12723 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012724}
12725
12726static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12727{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012728 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080012729 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080012730
Daniel Vetteref2d6332014-02-10 18:00:38 +010012731 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012732 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010012733 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012734 drm_gem_object_unreference(&intel_fb->obj->base);
12735 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012736 kfree(intel_fb);
12737}
12738
12739static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000012740 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080012741 unsigned int *handle)
12742{
12743 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000012744 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080012745
Chris Wilson05394f32010-11-08 19:18:58 +000012746 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080012747}
12748
12749static const struct drm_framebuffer_funcs intel_fb_funcs = {
12750 .destroy = intel_user_framebuffer_destroy,
12751 .create_handle = intel_user_framebuffer_create_handle,
12752};
12753
Damien Lespiaub3218032015-02-27 11:15:18 +000012754static
12755u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
12756 uint32_t pixel_format)
12757{
12758 u32 gen = INTEL_INFO(dev)->gen;
12759
12760 if (gen >= 9) {
12761 /* "The stride in bytes must not exceed the of the size of 8K
12762 * pixels and 32K bytes."
12763 */
12764 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
12765 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
12766 return 32*1024;
12767 } else if (gen >= 4) {
12768 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
12769 return 16*1024;
12770 else
12771 return 32*1024;
12772 } else if (gen >= 3) {
12773 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
12774 return 8*1024;
12775 else
12776 return 16*1024;
12777 } else {
12778 /* XXX DSPC is limited to 4k tiled */
12779 return 8*1024;
12780 }
12781}
12782
Daniel Vetterb5ea6422014-03-02 21:18:00 +010012783static int intel_framebuffer_init(struct drm_device *dev,
12784 struct intel_framebuffer *intel_fb,
12785 struct drm_mode_fb_cmd2 *mode_cmd,
12786 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080012787{
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080012788 int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080012789 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000012790 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080012791
Daniel Vetterdd4916c2013-10-09 21:23:51 +020012792 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12793
Daniel Vetter2a80ead2015-02-10 17:16:06 +000012794 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
12795 /* Enforce that fb modifier and tiling mode match, but only for
12796 * X-tiled. This is needed for FBC. */
12797 if (!!(obj->tiling_mode == I915_TILING_X) !=
12798 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
12799 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
12800 return -EINVAL;
12801 }
12802 } else {
12803 if (obj->tiling_mode == I915_TILING_X)
12804 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
12805 else if (obj->tiling_mode == I915_TILING_Y) {
12806 DRM_DEBUG("No Y tiling for legacy addfb\n");
12807 return -EINVAL;
12808 }
12809 }
12810
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000012811 /* Passed in modifier sanity checking. */
12812 switch (mode_cmd->modifier[0]) {
12813 case I915_FORMAT_MOD_Y_TILED:
12814 case I915_FORMAT_MOD_Yf_TILED:
12815 if (INTEL_INFO(dev)->gen < 9) {
12816 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
12817 mode_cmd->modifier[0]);
12818 return -EINVAL;
12819 }
12820 case DRM_FORMAT_MOD_NONE:
12821 case I915_FORMAT_MOD_X_TILED:
12822 break;
12823 default:
12824 DRM_ERROR("Unsupported fb modifier 0x%llx!\n",
12825 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010012826 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012827 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012828
Damien Lespiaub3218032015-02-27 11:15:18 +000012829 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
12830 mode_cmd->pixel_format);
12831 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
12832 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
12833 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010012834 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012835 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012836
Damien Lespiaub3218032015-02-27 11:15:18 +000012837 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
12838 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010012839 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000012840 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
12841 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000012842 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010012843 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012844 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012845 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012846
Daniel Vetter2a80ead2015-02-10 17:16:06 +000012847 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012848 mode_cmd->pitches[0] != obj->stride) {
12849 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12850 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012851 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012852 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012853
Ville Syrjälä57779d02012-10-31 17:50:14 +020012854 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012855 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020012856 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020012857 case DRM_FORMAT_RGB565:
12858 case DRM_FORMAT_XRGB8888:
12859 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020012860 break;
12861 case DRM_FORMAT_XRGB1555:
12862 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012863 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012864 DRM_DEBUG("unsupported pixel format: %s\n",
12865 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012866 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012867 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020012868 break;
12869 case DRM_FORMAT_XBGR8888:
12870 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020012871 case DRM_FORMAT_XRGB2101010:
12872 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020012873 case DRM_FORMAT_XBGR2101010:
12874 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012875 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012876 DRM_DEBUG("unsupported pixel format: %s\n",
12877 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012878 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012879 }
Jesse Barnesb5626742011-06-24 12:19:27 -070012880 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020012881 case DRM_FORMAT_YUYV:
12882 case DRM_FORMAT_UYVY:
12883 case DRM_FORMAT_YVYU:
12884 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012885 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012886 DRM_DEBUG("unsupported pixel format: %s\n",
12887 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012888 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012889 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012890 break;
12891 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012892 DRM_DEBUG("unsupported pixel format: %s\n",
12893 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010012894 return -EINVAL;
12895 }
12896
Ville Syrjälä90f9a332012-10-31 17:50:19 +020012897 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12898 if (mode_cmd->offsets[0] != 0)
12899 return -EINVAL;
12900
Damien Lespiauec2c9812015-01-20 12:51:45 +000012901 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000012902 mode_cmd->pixel_format,
12903 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020012904 /* FIXME drm helper for size checks (especially planar formats)? */
12905 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12906 return -EINVAL;
12907
Daniel Vetterc7d73f62012-12-13 23:38:38 +010012908 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12909 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020012910 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010012911
Jesse Barnes79e53942008-11-07 14:24:08 -080012912 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12913 if (ret) {
12914 DRM_ERROR("framebuffer init failed %d\n", ret);
12915 return ret;
12916 }
12917
Jesse Barnes79e53942008-11-07 14:24:08 -080012918 return 0;
12919}
12920
Jesse Barnes79e53942008-11-07 14:24:08 -080012921static struct drm_framebuffer *
12922intel_user_framebuffer_create(struct drm_device *dev,
12923 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012924 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080012925{
Chris Wilson05394f32010-11-08 19:18:58 +000012926 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080012927
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012928 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12929 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000012930 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010012931 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080012932
Chris Wilsond2dff872011-04-19 08:36:26 +010012933 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080012934}
12935
Daniel Vetter4520f532013-10-09 09:18:51 +020012936#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020012937static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020012938{
12939}
12940#endif
12941
Jesse Barnes79e53942008-11-07 14:24:08 -080012942static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080012943 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020012944 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080012945 .atomic_check = intel_atomic_check,
12946 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080012947};
12948
Jesse Barnese70236a2009-09-21 10:42:27 -070012949/* Set up chip specific display functions */
12950static void intel_init_display(struct drm_device *dev)
12951{
12952 struct drm_i915_private *dev_priv = dev->dev_private;
12953
Daniel Vetteree9300b2013-06-03 22:40:22 +020012954 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12955 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030012956 else if (IS_CHERRYVIEW(dev))
12957 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020012958 else if (IS_VALLEYVIEW(dev))
12959 dev_priv->display.find_dpll = vlv_find_best_dpll;
12960 else if (IS_PINEVIEW(dev))
12961 dev_priv->display.find_dpll = pnv_find_best_dpll;
12962 else
12963 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12964
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000012965 if (INTEL_INFO(dev)->gen >= 9) {
12966 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000012967 dev_priv->display.get_initial_plane_config =
12968 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000012969 dev_priv->display.crtc_compute_clock =
12970 haswell_crtc_compute_clock;
12971 dev_priv->display.crtc_enable = haswell_crtc_enable;
12972 dev_priv->display.crtc_disable = haswell_crtc_disable;
12973 dev_priv->display.off = ironlake_crtc_off;
12974 dev_priv->display.update_primary_plane =
12975 skylake_update_primary_plane;
12976 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012977 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000012978 dev_priv->display.get_initial_plane_config =
12979 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020012980 dev_priv->display.crtc_compute_clock =
12981 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020012982 dev_priv->display.crtc_enable = haswell_crtc_enable;
12983 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030012984 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000012985 dev_priv->display.update_primary_plane =
12986 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030012987 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012988 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000012989 dev_priv->display.get_initial_plane_config =
12990 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020012991 dev_priv->display.crtc_compute_clock =
12992 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020012993 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12994 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012995 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012996 dev_priv->display.update_primary_plane =
12997 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070012998 } else if (IS_VALLEYVIEW(dev)) {
12999 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013000 dev_priv->display.get_initial_plane_config =
13001 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020013002 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070013003 dev_priv->display.crtc_enable = valleyview_crtc_enable;
13004 dev_priv->display.crtc_disable = i9xx_crtc_disable;
13005 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013006 dev_priv->display.update_primary_plane =
13007 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070013008 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013009 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013010 dev_priv->display.get_initial_plane_config =
13011 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020013012 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020013013 dev_priv->display.crtc_enable = i9xx_crtc_enable;
13014 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013015 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070013016 dev_priv->display.update_primary_plane =
13017 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070013018 }
Jesse Barnese70236a2009-09-21 10:42:27 -070013019
Jesse Barnese70236a2009-09-21 10:42:27 -070013020 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070013021 if (IS_VALLEYVIEW(dev))
13022 dev_priv->display.get_display_clock_speed =
13023 valleyview_get_display_clock_speed;
13024 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070013025 dev_priv->display.get_display_clock_speed =
13026 i945_get_display_clock_speed;
13027 else if (IS_I915G(dev))
13028 dev_priv->display.get_display_clock_speed =
13029 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020013030 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070013031 dev_priv->display.get_display_clock_speed =
13032 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020013033 else if (IS_PINEVIEW(dev))
13034 dev_priv->display.get_display_clock_speed =
13035 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070013036 else if (IS_I915GM(dev))
13037 dev_priv->display.get_display_clock_speed =
13038 i915gm_get_display_clock_speed;
13039 else if (IS_I865G(dev))
13040 dev_priv->display.get_display_clock_speed =
13041 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020013042 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070013043 dev_priv->display.get_display_clock_speed =
13044 i855_get_display_clock_speed;
13045 else /* 852, 830 */
13046 dev_priv->display.get_display_clock_speed =
13047 i830_get_display_clock_speed;
13048
Jani Nikula7c10a2b2014-10-27 16:26:43 +020013049 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013050 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013051 } else if (IS_GEN6(dev)) {
13052 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013053 } else if (IS_IVYBRIDGE(dev)) {
13054 /* FIXME: detect B0+ stepping and use auto training */
13055 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013056 dev_priv->display.modeset_global_resources =
13057 ivb_modeset_global_resources;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030013058 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053013059 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080013060 } else if (IS_VALLEYVIEW(dev)) {
13061 dev_priv->display.modeset_global_resources =
13062 valleyview_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070013063 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013064
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013065 switch (INTEL_INFO(dev)->gen) {
13066 case 2:
13067 dev_priv->display.queue_flip = intel_gen2_queue_flip;
13068 break;
13069
13070 case 3:
13071 dev_priv->display.queue_flip = intel_gen3_queue_flip;
13072 break;
13073
13074 case 4:
13075 case 5:
13076 dev_priv->display.queue_flip = intel_gen4_queue_flip;
13077 break;
13078
13079 case 6:
13080 dev_priv->display.queue_flip = intel_gen6_queue_flip;
13081 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070013082 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070013083 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070013084 dev_priv->display.queue_flip = intel_gen7_queue_flip;
13085 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000013086 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000013087 /* Drop through - unsupported since execlist only. */
13088 default:
13089 /* Default just returns -ENODEV to indicate unsupported */
13090 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070013091 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020013092
13093 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030013094
13095 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070013096}
13097
Jesse Barnesb690e962010-07-19 13:53:12 -070013098/*
13099 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
13100 * resume, or other times. This quirk makes sure that's the case for
13101 * affected systems.
13102 */
Akshay Joshi0206e352011-08-16 15:34:10 -040013103static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070013104{
13105 struct drm_i915_private *dev_priv = dev->dev_private;
13106
13107 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013108 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070013109}
13110
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030013111static void quirk_pipeb_force(struct drm_device *dev)
13112{
13113 struct drm_i915_private *dev_priv = dev->dev_private;
13114
13115 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
13116 DRM_INFO("applying pipe b force quirk\n");
13117}
13118
Keith Packard435793d2011-07-12 14:56:22 -070013119/*
13120 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
13121 */
13122static void quirk_ssc_force_disable(struct drm_device *dev)
13123{
13124 struct drm_i915_private *dev_priv = dev->dev_private;
13125 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013126 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070013127}
13128
Carsten Emde4dca20e2012-03-15 15:56:26 +010013129/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010013130 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
13131 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010013132 */
13133static void quirk_invert_brightness(struct drm_device *dev)
13134{
13135 struct drm_i915_private *dev_priv = dev->dev_private;
13136 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020013137 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070013138}
13139
Scot Doyle9c72cc62014-07-03 23:27:50 +000013140/* Some VBT's incorrectly indicate no backlight is present */
13141static void quirk_backlight_present(struct drm_device *dev)
13142{
13143 struct drm_i915_private *dev_priv = dev->dev_private;
13144 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
13145 DRM_INFO("applying backlight present quirk\n");
13146}
13147
Jesse Barnesb690e962010-07-19 13:53:12 -070013148struct intel_quirk {
13149 int device;
13150 int subsystem_vendor;
13151 int subsystem_device;
13152 void (*hook)(struct drm_device *dev);
13153};
13154
Egbert Eich5f85f172012-10-14 15:46:38 +020013155/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
13156struct intel_dmi_quirk {
13157 void (*hook)(struct drm_device *dev);
13158 const struct dmi_system_id (*dmi_id_list)[];
13159};
13160
13161static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
13162{
13163 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
13164 return 1;
13165}
13166
13167static const struct intel_dmi_quirk intel_dmi_quirks[] = {
13168 {
13169 .dmi_id_list = &(const struct dmi_system_id[]) {
13170 {
13171 .callback = intel_dmi_reverse_brightness,
13172 .ident = "NCR Corporation",
13173 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
13174 DMI_MATCH(DMI_PRODUCT_NAME, ""),
13175 },
13176 },
13177 { } /* terminating entry */
13178 },
13179 .hook = quirk_invert_brightness,
13180 },
13181};
13182
Ben Widawskyc43b5632012-04-16 14:07:40 -070013183static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070013184 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040013185 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070013186
Jesse Barnesb690e962010-07-19 13:53:12 -070013187 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
13188 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
13189
Jesse Barnesb690e962010-07-19 13:53:12 -070013190 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
13191 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
13192
Ville Syrjälä5f080c02014-08-15 01:22:06 +030013193 /* 830 needs to leave pipe A & dpll A up */
13194 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
13195
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030013196 /* 830 needs to leave pipe B & dpll B up */
13197 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
13198
Keith Packard435793d2011-07-12 14:56:22 -070013199 /* Lenovo U160 cannot use SSC on LVDS */
13200 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020013201
13202 /* Sony Vaio Y cannot use SSC on LVDS */
13203 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010013204
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010013205 /* Acer Aspire 5734Z must invert backlight brightness */
13206 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
13207
13208 /* Acer/eMachines G725 */
13209 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
13210
13211 /* Acer/eMachines e725 */
13212 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
13213
13214 /* Acer/Packard Bell NCL20 */
13215 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
13216
13217 /* Acer Aspire 4736Z */
13218 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020013219
13220 /* Acer Aspire 5336 */
13221 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000013222
13223 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
13224 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000013225
Scot Doyledfb3d47b2014-08-21 16:08:02 +000013226 /* Acer C720 Chromebook (Core i3 4005U) */
13227 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
13228
jens steinb2a96012014-10-28 20:25:53 +010013229 /* Apple Macbook 2,1 (Core 2 T7400) */
13230 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
13231
Scot Doyled4967d82014-07-03 23:27:52 +000013232 /* Toshiba CB35 Chromebook (Celeron 2955U) */
13233 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000013234
13235 /* HP Chromebook 14 (Celeron 2955U) */
13236 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070013237};
13238
13239static void intel_init_quirks(struct drm_device *dev)
13240{
13241 struct pci_dev *d = dev->pdev;
13242 int i;
13243
13244 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
13245 struct intel_quirk *q = &intel_quirks[i];
13246
13247 if (d->device == q->device &&
13248 (d->subsystem_vendor == q->subsystem_vendor ||
13249 q->subsystem_vendor == PCI_ANY_ID) &&
13250 (d->subsystem_device == q->subsystem_device ||
13251 q->subsystem_device == PCI_ANY_ID))
13252 q->hook(dev);
13253 }
Egbert Eich5f85f172012-10-14 15:46:38 +020013254 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
13255 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
13256 intel_dmi_quirks[i].hook(dev);
13257 }
Jesse Barnesb690e962010-07-19 13:53:12 -070013258}
13259
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013260/* Disable the VGA plane that we never use */
13261static void i915_disable_vga(struct drm_device *dev)
13262{
13263 struct drm_i915_private *dev_priv = dev->dev_private;
13264 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020013265 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013266
Ville Syrjälä2b37c612014-01-22 21:32:38 +020013267 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013268 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070013269 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013270 sr1 = inb(VGA_SR_DATA);
13271 outb(sr1 | 1<<5, VGA_SR_DATA);
13272 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
13273 udelay(300);
13274
Ville Syrjälä01f5a622014-12-16 18:38:37 +020013275 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013276 POSTING_READ(vga_reg);
13277}
13278
Daniel Vetterf8175862012-04-10 15:50:11 +020013279void intel_modeset_init_hw(struct drm_device *dev)
13280{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030013281 intel_prepare_ddi(dev);
13282
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030013283 if (IS_VALLEYVIEW(dev))
13284 vlv_update_cdclk(dev);
13285
Daniel Vetterf8175862012-04-10 15:50:11 +020013286 intel_init_clock_gating(dev);
13287
Daniel Vetter8090c6b2012-06-24 16:42:32 +020013288 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020013289}
13290
Jesse Barnes79e53942008-11-07 14:24:08 -080013291void intel_modeset_init(struct drm_device *dev)
13292{
Jesse Barnes652c3932009-08-17 13:31:43 -070013293 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000013294 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000013295 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080013296 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080013297
13298 drm_mode_config_init(dev);
13299
13300 dev->mode_config.min_width = 0;
13301 dev->mode_config.min_height = 0;
13302
Dave Airlie019d96c2011-09-29 16:20:42 +010013303 dev->mode_config.preferred_depth = 24;
13304 dev->mode_config.prefer_shadow = 1;
13305
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000013306 dev->mode_config.allow_fb_modifiers = true;
13307
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020013308 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080013309
Jesse Barnesb690e962010-07-19 13:53:12 -070013310 intel_init_quirks(dev);
13311
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030013312 intel_init_pm(dev);
13313
Ben Widawskye3c74752013-04-05 13:12:39 -070013314 if (INTEL_INFO(dev)->num_pipes == 0)
13315 return;
13316
Jesse Barnese70236a2009-09-21 10:42:27 -070013317 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020013318 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070013319
Chris Wilsona6c45cf2010-09-17 00:32:17 +010013320 if (IS_GEN2(dev)) {
13321 dev->mode_config.max_width = 2048;
13322 dev->mode_config.max_height = 2048;
13323 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070013324 dev->mode_config.max_width = 4096;
13325 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080013326 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010013327 dev->mode_config.max_width = 8192;
13328 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080013329 }
Damien Lespiau068be562014-03-28 14:17:49 +000013330
Ville Syrjälädc41c152014-08-13 11:57:05 +030013331 if (IS_845G(dev) || IS_I865G(dev)) {
13332 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
13333 dev->mode_config.cursor_height = 1023;
13334 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000013335 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
13336 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
13337 } else {
13338 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
13339 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
13340 }
13341
Ben Widawsky5d4545a2013-01-17 12:45:15 -080013342 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080013343
Zhao Yakui28c97732009-10-09 11:39:41 +080013344 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013345 INTEL_INFO(dev)->num_pipes,
13346 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080013347
Damien Lespiau055e3932014-08-18 13:49:10 +010013348 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000013349 intel_crtc_init(dev, pipe);
Damien Lespiau1fe47782014-03-03 17:31:47 +000013350 for_each_sprite(pipe, sprite) {
13351 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070013352 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030013353 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000013354 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070013355 }
Jesse Barnes79e53942008-11-07 14:24:08 -080013356 }
13357
Jesse Barnesf42bb702013-12-16 16:34:23 -080013358 intel_init_dpio(dev);
13359
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013360 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013361
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013362 /* Just disable it once at startup */
13363 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013364 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000013365
13366 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013367 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080013368
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013369 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080013370 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013371 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080013372
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013373 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080013374 if (!crtc->active)
13375 continue;
13376
Jesse Barnes46f297f2014-03-07 08:57:48 -080013377 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080013378 * Note that reserving the BIOS fb up front prevents us
13379 * from stuffing other stolen allocations like the ring
13380 * on top. This prevents some ugliness at boot time, and
13381 * can even allow for smooth boot transitions if the BIOS
13382 * fb is large enough for the active pipe configuration.
13383 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000013384 if (dev_priv->display.get_initial_plane_config) {
13385 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080013386 &crtc->plane_config);
13387 /*
13388 * If the fb is shared between multiple heads, we'll
13389 * just get the first one.
13390 */
Jesse Barnes484b41d2014-03-07 08:57:55 -080013391 intel_find_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080013392 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080013393 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010013394}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080013395
Daniel Vetter7fad7982012-07-04 17:51:47 +020013396static void intel_enable_pipe_a(struct drm_device *dev)
13397{
13398 struct intel_connector *connector;
13399 struct drm_connector *crt = NULL;
13400 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030013401 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020013402
13403 /* We can't just switch on the pipe A, we need to set things up with a
13404 * proper mode and output configuration. As a gross hack, enable pipe A
13405 * by enabling the load detect pipe once. */
13406 list_for_each_entry(connector,
13407 &dev->mode_config.connector_list,
13408 base.head) {
13409 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
13410 crt = &connector->base;
13411 break;
13412 }
13413 }
13414
13415 if (!crt)
13416 return;
13417
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030013418 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
13419 intel_release_load_detect_pipe(crt, &load_detect_temp);
Daniel Vetter7fad7982012-07-04 17:51:47 +020013420}
13421
Daniel Vetterfa555832012-10-10 23:14:00 +020013422static bool
13423intel_check_plane_mapping(struct intel_crtc *crtc)
13424{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013425 struct drm_device *dev = crtc->base.dev;
13426 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020013427 u32 reg, val;
13428
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013429 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020013430 return true;
13431
13432 reg = DSPCNTR(!crtc->plane);
13433 val = I915_READ(reg);
13434
13435 if ((val & DISPLAY_PLANE_ENABLE) &&
13436 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
13437 return false;
13438
13439 return true;
13440}
13441
Daniel Vetter24929352012-07-02 20:28:59 +020013442static void intel_sanitize_crtc(struct intel_crtc *crtc)
13443{
13444 struct drm_device *dev = crtc->base.dev;
13445 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020013446 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020013447
Daniel Vetter24929352012-07-02 20:28:59 +020013448 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013449 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020013450 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
13451
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030013452 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010013453 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030013454 if (crtc->active) {
13455 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010013456 drm_crtc_vblank_on(&crtc->base);
13457 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030013458
Daniel Vetter24929352012-07-02 20:28:59 +020013459 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020013460 * disable the crtc (and hence change the state) if it is wrong. Note
13461 * that gen4+ has a fixed plane -> pipe mapping. */
13462 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020013463 struct intel_connector *connector;
13464 bool plane;
13465
Daniel Vetter24929352012-07-02 20:28:59 +020013466 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
13467 crtc->base.base.id);
13468
13469 /* Pipe has the wrong plane attached and the plane is active.
13470 * Temporarily change the plane mapping and disable everything
13471 * ... */
13472 plane = crtc->plane;
13473 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020013474 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020013475 dev_priv->display.crtc_disable(&crtc->base);
13476 crtc->plane = plane;
13477
13478 /* ... and break all links. */
13479 list_for_each_entry(connector, &dev->mode_config.connector_list,
13480 base.head) {
13481 if (connector->encoder->base.crtc != &crtc->base)
13482 continue;
13483
Egbert Eich7f1950f2014-04-25 10:56:22 +020013484 connector->base.dpms = DRM_MODE_DPMS_OFF;
13485 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020013486 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020013487 /* multiple connectors may have the same encoder:
13488 * handle them and break crtc link separately */
13489 list_for_each_entry(connector, &dev->mode_config.connector_list,
13490 base.head)
13491 if (connector->encoder->base.crtc == &crtc->base) {
13492 connector->encoder->base.crtc = NULL;
13493 connector->encoder->connectors_active = false;
13494 }
Daniel Vetter24929352012-07-02 20:28:59 +020013495
13496 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080013497 crtc->base.state->enable = false;
Daniel Vetter24929352012-07-02 20:28:59 +020013498 crtc->base.enabled = false;
13499 }
Daniel Vetter24929352012-07-02 20:28:59 +020013500
Daniel Vetter7fad7982012-07-04 17:51:47 +020013501 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
13502 crtc->pipe == PIPE_A && !crtc->active) {
13503 /* BIOS forgot to enable pipe A, this mostly happens after
13504 * resume. Force-enable the pipe to fix this, the update_dpms
13505 * call below we restore the pipe to the right state, but leave
13506 * the required bits on. */
13507 intel_enable_pipe_a(dev);
13508 }
13509
Daniel Vetter24929352012-07-02 20:28:59 +020013510 /* Adjust the state of the output pipe according to whether we
13511 * have active connectors/encoders. */
13512 intel_crtc_update_dpms(&crtc->base);
13513
Matt Roper83d65732015-02-25 13:12:16 -080013514 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020013515 struct intel_encoder *encoder;
13516
13517 /* This can happen either due to bugs in the get_hw_state
13518 * functions or because the pipe is force-enabled due to the
13519 * pipe A quirk. */
13520 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
13521 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080013522 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020013523 crtc->active ? "enabled" : "disabled");
13524
Matt Roper83d65732015-02-25 13:12:16 -080013525 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020013526 crtc->base.enabled = crtc->active;
13527
13528 /* Because we only establish the connector -> encoder ->
13529 * crtc links if something is active, this means the
13530 * crtc is now deactivated. Break the links. connector
13531 * -> encoder links are only establish when things are
13532 * actually up, hence no need to break them. */
13533 WARN_ON(crtc->active);
13534
13535 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13536 WARN_ON(encoder->connectors_active);
13537 encoder->base.crtc = NULL;
13538 }
13539 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020013540
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030013541 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010013542 /*
13543 * We start out with underrun reporting disabled to avoid races.
13544 * For correct bookkeeping mark this on active crtcs.
13545 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020013546 * Also on gmch platforms we dont have any hardware bits to
13547 * disable the underrun reporting. Which means we need to start
13548 * out with underrun reporting disabled also on inactive pipes,
13549 * since otherwise we'll complain about the garbage we read when
13550 * e.g. coming up after runtime pm.
13551 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010013552 * No protection against concurrent access is required - at
13553 * worst a fifo underrun happens which also sets this to false.
13554 */
13555 crtc->cpu_fifo_underrun_disabled = true;
13556 crtc->pch_fifo_underrun_disabled = true;
13557 }
Daniel Vetter24929352012-07-02 20:28:59 +020013558}
13559
13560static void intel_sanitize_encoder(struct intel_encoder *encoder)
13561{
13562 struct intel_connector *connector;
13563 struct drm_device *dev = encoder->base.dev;
13564
13565 /* We need to check both for a crtc link (meaning that the
13566 * encoder is active and trying to read from a pipe) and the
13567 * pipe itself being active. */
13568 bool has_active_crtc = encoder->base.crtc &&
13569 to_intel_crtc(encoder->base.crtc)->active;
13570
13571 if (encoder->connectors_active && !has_active_crtc) {
13572 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13573 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013574 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020013575
13576 /* Connector is active, but has no active pipe. This is
13577 * fallout from our resume register restoring. Disable
13578 * the encoder manually again. */
13579 if (encoder->base.crtc) {
13580 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13581 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013582 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020013583 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030013584 if (encoder->post_disable)
13585 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020013586 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020013587 encoder->base.crtc = NULL;
13588 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020013589
13590 /* Inconsistent output/port/pipe state happens presumably due to
13591 * a bug in one of the get_hw_state functions. Or someplace else
13592 * in our code, like the register restore mess on resume. Clamp
13593 * things to off as a safer default. */
13594 list_for_each_entry(connector,
13595 &dev->mode_config.connector_list,
13596 base.head) {
13597 if (connector->encoder != encoder)
13598 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020013599 connector->base.dpms = DRM_MODE_DPMS_OFF;
13600 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020013601 }
13602 }
13603 /* Enabled encoders without active connectors will be fixed in
13604 * the crtc fixup. */
13605}
13606
Imre Deak04098752014-02-18 00:02:16 +020013607void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013608{
13609 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020013610 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013611
Imre Deak04098752014-02-18 00:02:16 +020013612 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13613 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13614 i915_disable_vga(dev);
13615 }
13616}
13617
13618void i915_redisable_vga(struct drm_device *dev)
13619{
13620 struct drm_i915_private *dev_priv = dev->dev_private;
13621
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030013622 /* This function can be called both from intel_modeset_setup_hw_state or
13623 * at a very early point in our resume sequence, where the power well
13624 * structures are not yet restored. Since this function is at a very
13625 * paranoid "someone might have enabled VGA while we were not looking"
13626 * level, just check if the power well is enabled instead of trying to
13627 * follow the "don't touch the power well if we don't need it" policy
13628 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013629 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030013630 return;
13631
Imre Deak04098752014-02-18 00:02:16 +020013632 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013633}
13634
Ville Syrjälä98ec7732014-04-30 17:43:01 +030013635static bool primary_get_hw_state(struct intel_crtc *crtc)
13636{
13637 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13638
13639 if (!crtc->active)
13640 return false;
13641
13642 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13643}
13644
Daniel Vetter30e984d2013-06-05 13:34:17 +020013645static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020013646{
13647 struct drm_i915_private *dev_priv = dev->dev_private;
13648 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020013649 struct intel_crtc *crtc;
13650 struct intel_encoder *encoder;
13651 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020013652 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020013653
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013654 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013655 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020013656
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013657 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020013658
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013659 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013660 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020013661
Matt Roper83d65732015-02-25 13:12:16 -080013662 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020013663 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030013664 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020013665
13666 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13667 crtc->base.base.id,
13668 crtc->active ? "enabled" : "disabled");
13669 }
13670
Daniel Vetter53589012013-06-05 13:34:16 +020013671 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13672 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13673
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013674 pll->on = pll->get_hw_state(dev_priv, pll,
13675 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020013676 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013677 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013678 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020013679 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020013680 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013681 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020013682 }
Daniel Vetter53589012013-06-05 13:34:16 +020013683 }
Daniel Vetter53589012013-06-05 13:34:16 +020013684
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020013685 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013686 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013687
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013688 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013689 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020013690 }
13691
Damien Lespiaub2784e12014-08-05 11:29:37 +010013692 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020013693 pipe = 0;
13694
13695 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070013696 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13697 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013698 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020013699 } else {
13700 encoder->base.crtc = NULL;
13701 }
13702
13703 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010013704 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020013705 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013706 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020013707 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010013708 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020013709 }
13710
13711 list_for_each_entry(connector, &dev->mode_config.connector_list,
13712 base.head) {
13713 if (connector->get_hw_state(connector)) {
13714 connector->base.dpms = DRM_MODE_DPMS_ON;
13715 connector->encoder->connectors_active = true;
13716 connector->base.encoder = &connector->encoder->base;
13717 } else {
13718 connector->base.dpms = DRM_MODE_DPMS_OFF;
13719 connector->base.encoder = NULL;
13720 }
13721 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13722 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013723 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020013724 connector->base.encoder ? "enabled" : "disabled");
13725 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020013726}
13727
13728/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13729 * and i915 state tracking structures. */
13730void intel_modeset_setup_hw_state(struct drm_device *dev,
13731 bool force_restore)
13732{
13733 struct drm_i915_private *dev_priv = dev->dev_private;
13734 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020013735 struct intel_crtc *crtc;
13736 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020013737 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020013738
13739 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020013740
Jesse Barnesbabea612013-06-26 18:57:38 +030013741 /*
13742 * Now that we have the config, copy it to each CRTC struct
13743 * Note that this could go away if we move to using crtc_config
13744 * checking everywhere.
13745 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013746 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020013747 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013748 intel_mode_from_pipe_config(&crtc->base.mode,
13749 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030013750 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13751 crtc->base.base.id);
13752 drm_mode_debug_printmodeline(&crtc->base.mode);
13753 }
13754 }
13755
Daniel Vetter24929352012-07-02 20:28:59 +020013756 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010013757 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020013758 intel_sanitize_encoder(encoder);
13759 }
13760
Damien Lespiau055e3932014-08-18 13:49:10 +010013761 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020013762 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13763 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020013764 intel_dump_pipe_config(crtc, crtc->config,
13765 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020013766 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013767
Daniel Vetter35c95372013-07-17 06:55:04 +020013768 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13769 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13770
13771 if (!pll->on || pll->active)
13772 continue;
13773
13774 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13775
13776 pll->disable(dev_priv, pll);
13777 pll->on = false;
13778 }
13779
Pradeep Bhat30789992014-11-04 17:06:45 +000013780 if (IS_GEN9(dev))
13781 skl_wm_get_hw_state(dev);
13782 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030013783 ilk_wm_get_hw_state(dev);
13784
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010013785 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030013786 i915_redisable_vga(dev);
13787
Daniel Vetterf30da182013-04-11 20:22:50 +020013788 /*
13789 * We need to use raw interfaces for restoring state to avoid
13790 * checking (bogus) intermediate states.
13791 */
Damien Lespiau055e3932014-08-18 13:49:10 +010013792 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070013793 struct drm_crtc *crtc =
13794 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020013795
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013796 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
13797 crtc->primary->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010013798 }
13799 } else {
13800 intel_modeset_update_staged_output_state(dev);
13801 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013802
13803 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010013804}
13805
13806void intel_modeset_gem_init(struct drm_device *dev)
13807{
Jesse Barnes92122782014-10-09 12:57:42 -070013808 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080013809 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070013810 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080013811
Imre Deakae484342014-03-31 15:10:44 +030013812 mutex_lock(&dev->struct_mutex);
13813 intel_init_gt_powersave(dev);
13814 mutex_unlock(&dev->struct_mutex);
13815
Jesse Barnes92122782014-10-09 12:57:42 -070013816 /*
13817 * There may be no VBT; and if the BIOS enabled SSC we can
13818 * just keep using it to avoid unnecessary flicker. Whereas if the
13819 * BIOS isn't using it, don't assume it will work even if the VBT
13820 * indicates as much.
13821 */
13822 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
13823 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
13824 DREF_SSC1_ENABLE);
13825
Chris Wilson1833b132012-05-09 11:56:28 +010013826 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020013827
13828 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080013829
13830 /*
13831 * Make sure any fbs we allocated at startup are properly
13832 * pinned & fenced. When we do the allocation it's too early
13833 * for this.
13834 */
13835 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010013836 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070013837 obj = intel_fb_obj(c->primary->fb);
13838 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080013839 continue;
13840
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +000013841 if (intel_pin_and_fence_fb_obj(c->primary,
13842 c->primary->fb,
13843 NULL)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080013844 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13845 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100013846 drm_framebuffer_unreference(c->primary->fb);
13847 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080013848 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080013849 }
13850 }
13851 mutex_unlock(&dev->struct_mutex);
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020013852
13853 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013854}
13855
Imre Deak4932e2c2014-02-11 17:12:48 +020013856void intel_connector_unregister(struct intel_connector *intel_connector)
13857{
13858 struct drm_connector *connector = &intel_connector->base;
13859
13860 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010013861 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020013862}
13863
Jesse Barnes79e53942008-11-07 14:24:08 -080013864void intel_modeset_cleanup(struct drm_device *dev)
13865{
Jesse Barnes652c3932009-08-17 13:31:43 -070013866 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030013867 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070013868
Imre Deak2eb52522014-11-19 15:30:05 +020013869 intel_disable_gt_powersave(dev);
13870
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020013871 intel_backlight_unregister(dev);
13872
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013873 /*
13874 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020013875 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013876 * experience fancy races otherwise.
13877 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020013878 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070013879
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013880 /*
13881 * Due to the hpd irq storm handling the hotplug work can re-arm the
13882 * poll handlers. Hence disable polling after hpd handling is shut down.
13883 */
Keith Packardf87ea762010-10-03 19:36:26 -070013884 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013885
Jesse Barnes652c3932009-08-17 13:31:43 -070013886 mutex_lock(&dev->struct_mutex);
13887
Jesse Barnes723bfd72010-10-07 16:01:13 -070013888 intel_unregister_dsm_handler();
13889
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013890 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070013891
Daniel Vetter930ebb42012-06-29 23:32:16 +020013892 ironlake_teardown_rc6(dev);
13893
Kristian Høgsberg69341a52009-11-11 12:19:17 -050013894 mutex_unlock(&dev->struct_mutex);
13895
Chris Wilson1630fe72011-07-08 12:22:42 +010013896 /* flush any delayed tasks or pending work */
13897 flush_scheduled_work();
13898
Jani Nikuladb31af1d2013-11-08 16:48:53 +020013899 /* destroy the backlight and sysfs files before encoders/connectors */
13900 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020013901 struct intel_connector *intel_connector;
13902
13903 intel_connector = to_intel_connector(connector);
13904 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020013905 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030013906
Jesse Barnes79e53942008-11-07 14:24:08 -080013907 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010013908
13909 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030013910
13911 mutex_lock(&dev->struct_mutex);
13912 intel_cleanup_gt_powersave(dev);
13913 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013914}
13915
Dave Airlie28d52042009-09-21 14:33:58 +100013916/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080013917 * Return which encoder is currently attached for connector.
13918 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010013919struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080013920{
Chris Wilsondf0e9242010-09-09 16:20:55 +010013921 return &intel_attached_encoder(connector)->base;
13922}
Jesse Barnes79e53942008-11-07 14:24:08 -080013923
Chris Wilsondf0e9242010-09-09 16:20:55 +010013924void intel_connector_attach_encoder(struct intel_connector *connector,
13925 struct intel_encoder *encoder)
13926{
13927 connector->encoder = encoder;
13928 drm_mode_connector_attach_encoder(&connector->base,
13929 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080013930}
Dave Airlie28d52042009-09-21 14:33:58 +100013931
13932/*
13933 * set vga decode state - true == enable VGA decode
13934 */
13935int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13936{
13937 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000013938 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100013939 u16 gmch_ctrl;
13940
Chris Wilson75fa0412014-02-07 18:37:02 -020013941 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13942 DRM_ERROR("failed to read control word\n");
13943 return -EIO;
13944 }
13945
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020013946 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13947 return 0;
13948
Dave Airlie28d52042009-09-21 14:33:58 +100013949 if (state)
13950 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13951 else
13952 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020013953
13954 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13955 DRM_ERROR("failed to write control word\n");
13956 return -EIO;
13957 }
13958
Dave Airlie28d52042009-09-21 14:33:58 +100013959 return 0;
13960}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013961
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013962struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013963
13964 u32 power_well_driver;
13965
Chris Wilson63b66e52013-08-08 15:12:06 +020013966 int num_transcoders;
13967
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013968 struct intel_cursor_error_state {
13969 u32 control;
13970 u32 position;
13971 u32 base;
13972 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010013973 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013974
13975 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020013976 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013977 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030013978 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010013979 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013980
13981 struct intel_plane_error_state {
13982 u32 control;
13983 u32 stride;
13984 u32 size;
13985 u32 pos;
13986 u32 addr;
13987 u32 surface;
13988 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010013989 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020013990
13991 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020013992 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020013993 enum transcoder cpu_transcoder;
13994
13995 u32 conf;
13996
13997 u32 htotal;
13998 u32 hblank;
13999 u32 hsync;
14000 u32 vtotal;
14001 u32 vblank;
14002 u32 vsync;
14003 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014004};
14005
14006struct intel_display_error_state *
14007intel_display_capture_error_state(struct drm_device *dev)
14008{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014009 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014010 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020014011 int transcoders[] = {
14012 TRANSCODER_A,
14013 TRANSCODER_B,
14014 TRANSCODER_C,
14015 TRANSCODER_EDP,
14016 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014017 int i;
14018
Chris Wilson63b66e52013-08-08 15:12:06 +020014019 if (INTEL_INFO(dev)->num_pipes == 0)
14020 return NULL;
14021
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014022 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014023 if (error == NULL)
14024 return NULL;
14025
Imre Deak190be112013-11-25 17:15:31 +020014026 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014027 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
14028
Damien Lespiau055e3932014-08-18 13:49:10 +010014029 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020014030 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014031 __intel_display_power_is_enabled(dev_priv,
14032 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020014033 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014034 continue;
14035
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030014036 error->cursor[i].control = I915_READ(CURCNTR(i));
14037 error->cursor[i].position = I915_READ(CURPOS(i));
14038 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014039
14040 error->plane[i].control = I915_READ(DSPCNTR(i));
14041 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014042 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030014043 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014044 error->plane[i].pos = I915_READ(DSPPOS(i));
14045 }
Paulo Zanonica291362013-03-06 20:03:14 -030014046 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
14047 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014048 if (INTEL_INFO(dev)->gen >= 4) {
14049 error->plane[i].surface = I915_READ(DSPSURF(i));
14050 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
14051 }
14052
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014053 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030014054
Sonika Jindal3abfce72014-07-21 15:23:43 +053014055 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030014056 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020014057 }
14058
14059 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
14060 if (HAS_DDI(dev_priv->dev))
14061 error->num_transcoders++; /* Account for eDP. */
14062
14063 for (i = 0; i < error->num_transcoders; i++) {
14064 enum transcoder cpu_transcoder = transcoders[i];
14065
Imre Deakddf9c532013-11-27 22:02:02 +020014066 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014067 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020014068 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020014069 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020014070 continue;
14071
Chris Wilson63b66e52013-08-08 15:12:06 +020014072 error->transcoder[i].cpu_transcoder = cpu_transcoder;
14073
14074 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
14075 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
14076 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
14077 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
14078 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
14079 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
14080 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014081 }
14082
14083 return error;
14084}
14085
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014086#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
14087
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014088void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014089intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014090 struct drm_device *dev,
14091 struct intel_display_error_state *error)
14092{
Damien Lespiau055e3932014-08-18 13:49:10 +010014093 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014094 int i;
14095
Chris Wilson63b66e52013-08-08 15:12:06 +020014096 if (!error)
14097 return;
14098
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014099 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020014100 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014101 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030014102 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010014103 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014104 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020014105 err_printf(m, " Power: %s\n",
14106 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014107 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030014108 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014109
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014110 err_printf(m, "Plane [%d]:\n", i);
14111 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
14112 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014113 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014114 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
14115 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030014116 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030014117 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014118 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014119 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014120 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
14121 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014122 }
14123
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030014124 err_printf(m, "Cursor [%d]:\n", i);
14125 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
14126 err_printf(m, " POS: %08x\n", error->cursor[i].position);
14127 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014128 }
Chris Wilson63b66e52013-08-08 15:12:06 +020014129
14130 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010014131 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020014132 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020014133 err_printf(m, " Power: %s\n",
14134 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020014135 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
14136 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
14137 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
14138 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
14139 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
14140 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
14141 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
14142 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000014143}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014144
14145void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
14146{
14147 struct intel_crtc *crtc;
14148
14149 for_each_intel_crtc(dev, crtc) {
14150 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014151
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020014152 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014153
14154 work = crtc->unpin_work;
14155
14156 if (work && work->event &&
14157 work->event->base.file_priv == file) {
14158 kfree(work->event);
14159 work->event = NULL;
14160 }
14161
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020014162 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030014163 }
14164}