blob: 1386086ec245e4fcfadae4655a08e0d0413e97b6 [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"
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070042#include <drm/drm_plane_helper.h>
43#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080044#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080045
Matt Roper465c1202014-05-29 08:06:54 -070046/* Primary plane formats supported by all gen */
47#define COMMON_PRIMARY_FORMATS \
48 DRM_FORMAT_C8, \
49 DRM_FORMAT_RGB565, \
50 DRM_FORMAT_XRGB8888, \
51 DRM_FORMAT_ARGB8888
52
53/* Primary plane formats for gen <= 3 */
54static const uint32_t intel_primary_formats_gen2[] = {
55 COMMON_PRIMARY_FORMATS,
56 DRM_FORMAT_XRGB1555,
57 DRM_FORMAT_ARGB1555,
58};
59
60/* Primary plane formats for gen >= 4 */
61static const uint32_t intel_primary_formats_gen4[] = {
62 COMMON_PRIMARY_FORMATS, \
63 DRM_FORMAT_XBGR8888,
64 DRM_FORMAT_ABGR8888,
65 DRM_FORMAT_XRGB2101010,
66 DRM_FORMAT_ARGB2101010,
67 DRM_FORMAT_XBGR2101010,
68 DRM_FORMAT_ABGR2101010,
69};
70
Matt Roper3d7d6512014-06-10 08:28:13 -070071/* Cursor formats */
72static const uint32_t intel_cursor_formats[] = {
73 DRM_FORMAT_ARGB8888,
74};
75
Chon Ming Leeef9348c2014-04-09 13:28:18 +030076#define DIV_ROUND_CLOSEST_ULL(ll, d) \
Matt Roper465c1202014-05-29 08:06:54 -070077({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
Chon Ming Leeef9348c2014-04-09 13:28:18 +030078
Daniel Vettercc365132014-06-18 13:59:13 +020079static void intel_increase_pllclock(struct drm_device *dev,
80 enum pipe pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +010081static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080082
Jesse Barnesf1f644d2013-06-27 00:39:25 +030083static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
84 struct intel_crtc_config *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030085static void ironlake_pch_clock_get(struct intel_crtc *crtc,
86 struct intel_crtc_config *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030087
Damien Lespiaue7457a92013-08-08 22:28:59 +010088static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
89 int x, int y, struct drm_framebuffer *old_fb);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080090static int intel_framebuffer_init(struct drm_device *dev,
91 struct intel_framebuffer *ifb,
92 struct drm_mode_fb_cmd2 *mode_cmd,
93 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020094static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
95static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020096static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070097 struct intel_link_m_n *m_n,
98 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020099static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200100static void haswell_set_pipeconf(struct drm_crtc *crtc);
101static void intel_set_pipe_csc(struct drm_crtc *crtc);
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +0200102static void vlv_prepare_pll(struct intel_crtc *crtc);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +0300103static void chv_prepare_pll(struct intel_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},
393 .vco = { .min = 4860000, .max = 6700000 },
394 .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 */
414static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
415{
416 struct drm_device *dev = crtc->dev;
417 struct intel_encoder *encoder;
418
419 for_each_encoder_on_crtc(dev, crtc, encoder)
420 if (encoder->type == type)
421 return true;
422
423 return false;
424}
425
Chris Wilson1b894b52010-12-14 20:04:54 +0000426static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
427 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800428{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800429 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800430 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800431
432 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100433 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000434 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800435 limit = &intel_limits_ironlake_dual_lvds_100m;
436 else
437 limit = &intel_limits_ironlake_dual_lvds;
438 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000439 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800440 limit = &intel_limits_ironlake_single_lvds_100m;
441 else
442 limit = &intel_limits_ironlake_single_lvds;
443 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200444 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800445 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800446
447 return limit;
448}
449
Ma Ling044c7c42009-03-18 20:13:23 +0800450static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
451{
452 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800453 const intel_limit_t *limit;
454
455 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100456 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700457 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800458 else
Keith Packarde4b36692009-06-05 19:22:17 -0700459 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800460 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
461 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700462 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800463 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700464 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800465 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700466 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800467
468 return limit;
469}
470
Chris Wilson1b894b52010-12-14 20:04:54 +0000471static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800472{
473 struct drm_device *dev = crtc->dev;
474 const intel_limit_t *limit;
475
Eric Anholtbad720f2009-10-22 16:11:14 -0700476 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000477 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800478 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800479 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500480 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800481 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500482 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800483 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500484 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300485 } else if (IS_CHERRYVIEW(dev)) {
486 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700487 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300488 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100489 } else if (!IS_GEN2(dev)) {
490 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
491 limit = &intel_limits_i9xx_lvds;
492 else
493 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800494 } else {
495 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700496 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200497 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700498 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200499 else
500 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800501 }
502 return limit;
503}
504
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500505/* m1 is reserved as 0 in Pineview, n is a ring counter */
506static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800507{
Shaohua Li21778322009-02-23 15:19:16 +0800508 clock->m = clock->m2 + 2;
509 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200510 if (WARN_ON(clock->n == 0 || clock->p == 0))
511 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300512 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
513 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800514}
515
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200516static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
517{
518 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
519}
520
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200521static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800522{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200523 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800524 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200525 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
526 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300527 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
528 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800529}
530
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300531static void chv_clock(int refclk, intel_clock_t *clock)
532{
533 clock->m = clock->m1 * clock->m2;
534 clock->p = clock->p1 * clock->p2;
535 if (WARN_ON(clock->n == 0 || clock->p == 0))
536 return;
537 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
538 clock->n << 22);
539 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
540}
541
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800542#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800543/**
544 * Returns whether the given set of divisors are valid for a given refclk with
545 * the given connectors.
546 */
547
Chris Wilson1b894b52010-12-14 20:04:54 +0000548static bool intel_PLL_is_valid(struct drm_device *dev,
549 const intel_limit_t *limit,
550 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800551{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300552 if (clock->n < limit->n.min || limit->n.max < clock->n)
553 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800554 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400555 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800556 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400557 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800558 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400559 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300560
561 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
562 if (clock->m1 <= clock->m2)
563 INTELPllInvalid("m1 <= m2\n");
564
565 if (!IS_VALLEYVIEW(dev)) {
566 if (clock->p < limit->p.min || limit->p.max < clock->p)
567 INTELPllInvalid("p out of range\n");
568 if (clock->m < limit->m.min || limit->m.max < clock->m)
569 INTELPllInvalid("m out of range\n");
570 }
571
Jesse Barnes79e53942008-11-07 14:24:08 -0800572 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400573 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800574 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
575 * connector, etc., rather than just a single range.
576 */
577 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400578 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800579
580 return true;
581}
582
Ma Lingd4906092009-03-18 20:13:27 +0800583static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200584i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800585 int target, int refclk, intel_clock_t *match_clock,
586 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800587{
588 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800589 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800590 int err = target;
591
Daniel Vettera210b022012-11-26 17:22:08 +0100592 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800593 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100594 * For LVDS just rely on its current settings for dual-channel.
595 * We haven't figured out how to reliably set up different
596 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800597 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100598 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800599 clock.p2 = limit->p2.p2_fast;
600 else
601 clock.p2 = limit->p2.p2_slow;
602 } else {
603 if (target < limit->p2.dot_limit)
604 clock.p2 = limit->p2.p2_slow;
605 else
606 clock.p2 = limit->p2.p2_fast;
607 }
608
Akshay Joshi0206e352011-08-16 15:34:10 -0400609 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800610
Zhao Yakui42158662009-11-20 11:24:18 +0800611 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
612 clock.m1++) {
613 for (clock.m2 = limit->m2.min;
614 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200615 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800616 break;
617 for (clock.n = limit->n.min;
618 clock.n <= limit->n.max; clock.n++) {
619 for (clock.p1 = limit->p1.min;
620 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800621 int this_err;
622
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200623 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000624 if (!intel_PLL_is_valid(dev, limit,
625 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800626 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800627 if (match_clock &&
628 clock.p != match_clock->p)
629 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800630
631 this_err = abs(clock.dot - target);
632 if (this_err < err) {
633 *best_clock = clock;
634 err = this_err;
635 }
636 }
637 }
638 }
639 }
640
641 return (err != target);
642}
643
Ma Lingd4906092009-03-18 20:13:27 +0800644static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200645pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
646 int target, int refclk, intel_clock_t *match_clock,
647 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200648{
649 struct drm_device *dev = crtc->dev;
650 intel_clock_t clock;
651 int err = target;
652
653 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
654 /*
655 * For LVDS just rely on its current settings for dual-channel.
656 * We haven't figured out how to reliably set up different
657 * single/dual channel state, if we even can.
658 */
659 if (intel_is_dual_link_lvds(dev))
660 clock.p2 = limit->p2.p2_fast;
661 else
662 clock.p2 = limit->p2.p2_slow;
663 } else {
664 if (target < limit->p2.dot_limit)
665 clock.p2 = limit->p2.p2_slow;
666 else
667 clock.p2 = limit->p2.p2_fast;
668 }
669
670 memset(best_clock, 0, sizeof(*best_clock));
671
672 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
673 clock.m1++) {
674 for (clock.m2 = limit->m2.min;
675 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200676 for (clock.n = limit->n.min;
677 clock.n <= limit->n.max; clock.n++) {
678 for (clock.p1 = limit->p1.min;
679 clock.p1 <= limit->p1.max; clock.p1++) {
680 int this_err;
681
682 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800683 if (!intel_PLL_is_valid(dev, limit,
684 &clock))
685 continue;
686 if (match_clock &&
687 clock.p != match_clock->p)
688 continue;
689
690 this_err = abs(clock.dot - target);
691 if (this_err < err) {
692 *best_clock = clock;
693 err = this_err;
694 }
695 }
696 }
697 }
698 }
699
700 return (err != target);
701}
702
Ma Lingd4906092009-03-18 20:13:27 +0800703static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200704g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
705 int target, int refclk, intel_clock_t *match_clock,
706 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800707{
708 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800709 intel_clock_t clock;
710 int max_n;
711 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400712 /* approximately equals target * 0.00585 */
713 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800714 found = false;
715
716 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100717 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800718 clock.p2 = limit->p2.p2_fast;
719 else
720 clock.p2 = limit->p2.p2_slow;
721 } else {
722 if (target < limit->p2.dot_limit)
723 clock.p2 = limit->p2.p2_slow;
724 else
725 clock.p2 = limit->p2.p2_fast;
726 }
727
728 memset(best_clock, 0, sizeof(*best_clock));
729 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200730 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800731 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200732 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800733 for (clock.m1 = limit->m1.max;
734 clock.m1 >= limit->m1.min; clock.m1--) {
735 for (clock.m2 = limit->m2.max;
736 clock.m2 >= limit->m2.min; clock.m2--) {
737 for (clock.p1 = limit->p1.max;
738 clock.p1 >= limit->p1.min; clock.p1--) {
739 int this_err;
740
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200741 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000742 if (!intel_PLL_is_valid(dev, limit,
743 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800744 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000745
746 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800747 if (this_err < err_most) {
748 *best_clock = clock;
749 err_most = this_err;
750 max_n = clock.n;
751 found = true;
752 }
753 }
754 }
755 }
756 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800757 return found;
758}
Ma Lingd4906092009-03-18 20:13:27 +0800759
Zhenyu Wang2c072452009-06-05 15:38:42 +0800760static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200761vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
762 int target, int refclk, intel_clock_t *match_clock,
763 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700764{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300765 struct drm_device *dev = crtc->dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300766 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300767 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300768 /* min update 19.2 MHz */
769 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300770 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700771
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300772 target *= 5; /* fast clock */
773
774 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700775
776 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300777 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300778 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300779 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300780 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300781 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700782 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300783 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300784 unsigned int ppm, diff;
785
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300786 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
787 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300788
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300789 vlv_clock(refclk, &clock);
790
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300791 if (!intel_PLL_is_valid(dev, limit,
792 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300793 continue;
794
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300795 diff = abs(clock.dot - target);
796 ppm = div_u64(1000000ULL * diff, target);
797
798 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300799 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300800 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300801 found = true;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300802 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300803
Ville Syrjäläc6861222013-09-24 21:26:21 +0300804 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300805 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300806 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300807 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700808 }
809 }
810 }
811 }
812 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700813
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300814 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700815}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700816
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300817static bool
818chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
819 int target, int refclk, intel_clock_t *match_clock,
820 intel_clock_t *best_clock)
821{
822 struct drm_device *dev = crtc->dev;
823 intel_clock_t clock;
824 uint64_t m2;
825 int found = false;
826
827 memset(best_clock, 0, sizeof(*best_clock));
828
829 /*
830 * Based on hardware doc, the n always set to 1, and m1 always
831 * set to 2. If requires to support 200Mhz refclk, we need to
832 * revisit this because n may not 1 anymore.
833 */
834 clock.n = 1, clock.m1 = 2;
835 target *= 5; /* fast clock */
836
837 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
838 for (clock.p2 = limit->p2.p2_fast;
839 clock.p2 >= limit->p2.p2_slow;
840 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
841
842 clock.p = clock.p1 * clock.p2;
843
844 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
845 clock.n) << 22, refclk * clock.m1);
846
847 if (m2 > INT_MAX/clock.m1)
848 continue;
849
850 clock.m2 = m2;
851
852 chv_clock(refclk, &clock);
853
854 if (!intel_PLL_is_valid(dev, limit, &clock))
855 continue;
856
857 /* based on hardware requirement, prefer bigger p
858 */
859 if (clock.p > best_clock->p) {
860 *best_clock = clock;
861 found = true;
862 }
863 }
864 }
865
866 return found;
867}
868
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300869bool intel_crtc_active(struct drm_crtc *crtc)
870{
871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
872
873 /* Be paranoid as we can arrive here with only partial
874 * state retrieved from the hardware during setup.
875 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100876 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300877 * as Haswell has gained clock readout/fastboot support.
878 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000879 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300880 * properly reconstruct framebuffers.
881 */
Matt Roperf4510a22014-04-01 15:22:40 -0700882 return intel_crtc->active && crtc->primary->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100883 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300884}
885
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200886enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
887 enum pipe pipe)
888{
889 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
890 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
891
Daniel Vetter3b117c82013-04-17 20:15:07 +0200892 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200893}
894
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200895static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
Paulo Zanonia928d532012-05-04 17:18:15 -0300896{
897 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200898 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300899
900 frame = I915_READ(frame_reg);
901
902 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
Damien Lespiau31e4b892014-08-18 13:51:00 +0100903 WARN(1, "vblank wait on pipe %c timed out\n",
904 pipe_name(pipe));
Paulo Zanonia928d532012-05-04 17:18:15 -0300905}
906
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700907/**
908 * intel_wait_for_vblank - wait for vblank on a given pipe
909 * @dev: drm device
910 * @pipe: pipe to wait for
911 *
912 * Wait for vblank to occur on a given pipe. Needed for various bits of
913 * mode setting code.
914 */
915void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800916{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700917 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800918 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700919
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200920 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
921 g4x_wait_for_vblank(dev, pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300922 return;
923 }
924
Chris Wilson300387c2010-09-05 20:25:43 +0100925 /* Clear existing vblank status. Note this will clear any other
926 * sticky status fields as well.
927 *
928 * This races with i915_driver_irq_handler() with the result
929 * that either function could miss a vblank event. Here it is not
930 * fatal, as we will either wait upon the next vblank interrupt or
931 * timeout. Generally speaking intel_wait_for_vblank() is only
932 * called during modeset at which time the GPU should be idle and
933 * should *not* be performing page flips and thus not waiting on
934 * vblanks...
935 * Currently, the result of us stealing a vblank from the irq
936 * handler is that a single frame will be skipped during swapbuffers.
937 */
938 I915_WRITE(pipestat_reg,
939 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
940
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700941 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100942 if (wait_for(I915_READ(pipestat_reg) &
943 PIPE_VBLANK_INTERRUPT_STATUS,
944 50))
Damien Lespiau31e4b892014-08-18 13:51:00 +0100945 DRM_DEBUG_KMS("vblank wait on pipe %c timed out\n",
946 pipe_name(pipe));
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700947}
948
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300949static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
950{
951 struct drm_i915_private *dev_priv = dev->dev_private;
952 u32 reg = PIPEDSL(pipe);
953 u32 line1, line2;
954 u32 line_mask;
955
956 if (IS_GEN2(dev))
957 line_mask = DSL_LINEMASK_GEN2;
958 else
959 line_mask = DSL_LINEMASK_GEN3;
960
961 line1 = I915_READ(reg) & line_mask;
962 mdelay(5);
963 line2 = I915_READ(reg) & line_mask;
964
965 return line1 == line2;
966}
967
Keith Packardab7ad7f2010-10-03 00:33:06 -0700968/*
969 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300970 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700971 *
972 * After disabling a pipe, we can't wait for vblank in the usual way,
973 * spinning on the vblank interrupt status bit, since we won't actually
974 * see an interrupt when the pipe is disabled.
975 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700976 * On Gen4 and above:
977 * wait for the pipe register state bit to turn off
978 *
979 * Otherwise:
980 * wait for the display line value to settle (it usually
981 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100982 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700983 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300984static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700985{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300986 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700987 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300988 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
989 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700990
Keith Packardab7ad7f2010-10-03 00:33:06 -0700991 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200992 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700993
Keith Packardab7ad7f2010-10-03 00:33:06 -0700994 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100995 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
996 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200997 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700998 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -0700999 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001000 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001001 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001002 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001003}
1004
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001005/*
1006 * ibx_digital_port_connected - is the specified port connected?
1007 * @dev_priv: i915 private structure
1008 * @port: the port to test
1009 *
1010 * Returns true if @port is connected, false otherwise.
1011 */
1012bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1013 struct intel_digital_port *port)
1014{
1015 u32 bit;
1016
Damien Lespiauc36346e2012-12-13 16:09:03 +00001017 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001018 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001019 case PORT_B:
1020 bit = SDE_PORTB_HOTPLUG;
1021 break;
1022 case PORT_C:
1023 bit = SDE_PORTC_HOTPLUG;
1024 break;
1025 case PORT_D:
1026 bit = SDE_PORTD_HOTPLUG;
1027 break;
1028 default:
1029 return true;
1030 }
1031 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001032 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001033 case PORT_B:
1034 bit = SDE_PORTB_HOTPLUG_CPT;
1035 break;
1036 case PORT_C:
1037 bit = SDE_PORTC_HOTPLUG_CPT;
1038 break;
1039 case PORT_D:
1040 bit = SDE_PORTD_HOTPLUG_CPT;
1041 break;
1042 default:
1043 return true;
1044 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001045 }
1046
1047 return I915_READ(SDEISR) & bit;
1048}
1049
Jesse Barnesb24e7172011-01-04 15:09:30 -08001050static const char *state_string(bool enabled)
1051{
1052 return enabled ? "on" : "off";
1053}
1054
1055/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001056void assert_pll(struct drm_i915_private *dev_priv,
1057 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001058{
1059 int reg;
1060 u32 val;
1061 bool cur_state;
1062
1063 reg = DPLL(pipe);
1064 val = I915_READ(reg);
1065 cur_state = !!(val & DPLL_VCO_ENABLE);
1066 WARN(cur_state != state,
1067 "PLL state assertion failure (expected %s, current %s)\n",
1068 state_string(state), state_string(cur_state));
1069}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001070
Jani Nikula23538ef2013-08-27 15:12:22 +03001071/* XXX: the dsi pll is shared between MIPI DSI ports */
1072static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1073{
1074 u32 val;
1075 bool cur_state;
1076
1077 mutex_lock(&dev_priv->dpio_lock);
1078 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1079 mutex_unlock(&dev_priv->dpio_lock);
1080
1081 cur_state = val & DSI_PLL_VCO_EN;
1082 WARN(cur_state != state,
1083 "DSI PLL state assertion failure (expected %s, current %s)\n",
1084 state_string(state), state_string(cur_state));
1085}
1086#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1087#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1088
Daniel Vetter55607e82013-06-16 21:42:39 +02001089struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001090intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001091{
Daniel Vettere2b78262013-06-07 23:10:03 +02001092 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1093
Daniel Vettera43f6e02013-06-07 23:10:32 +02001094 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001095 return NULL;
1096
Daniel Vettera43f6e02013-06-07 23:10:32 +02001097 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001098}
1099
Jesse Barnesb24e7172011-01-04 15:09:30 -08001100/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001101void assert_shared_dpll(struct drm_i915_private *dev_priv,
1102 struct intel_shared_dpll *pll,
1103 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001104{
Jesse Barnes040484a2011-01-03 12:14:26 -08001105 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001106 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001107
Chris Wilson92b27b02012-05-20 18:10:50 +01001108 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001109 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001110 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001111
Daniel Vetter53589012013-06-05 13:34:16 +02001112 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +01001113 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001114 "%s assertion failure (expected %s, current %s)\n",
1115 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001116}
Jesse Barnes040484a2011-01-03 12:14:26 -08001117
1118static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1119 enum pipe pipe, bool state)
1120{
1121 int reg;
1122 u32 val;
1123 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001124 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1125 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001126
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001127 if (HAS_DDI(dev_priv->dev)) {
1128 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001129 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001130 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001131 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001132 } else {
1133 reg = FDI_TX_CTL(pipe);
1134 val = I915_READ(reg);
1135 cur_state = !!(val & FDI_TX_ENABLE);
1136 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001137 WARN(cur_state != state,
1138 "FDI TX state assertion failure (expected %s, current %s)\n",
1139 state_string(state), state_string(cur_state));
1140}
1141#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1142#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1143
1144static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1145 enum pipe pipe, bool state)
1146{
1147 int reg;
1148 u32 val;
1149 bool cur_state;
1150
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001151 reg = FDI_RX_CTL(pipe);
1152 val = I915_READ(reg);
1153 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001154 WARN(cur_state != state,
1155 "FDI RX state assertion failure (expected %s, current %s)\n",
1156 state_string(state), state_string(cur_state));
1157}
1158#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1159#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1160
1161static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1162 enum pipe pipe)
1163{
1164 int reg;
1165 u32 val;
1166
1167 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001168 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001169 return;
1170
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001171 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001172 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001173 return;
1174
Jesse Barnes040484a2011-01-03 12:14:26 -08001175 reg = FDI_TX_CTL(pipe);
1176 val = I915_READ(reg);
1177 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1178}
1179
Daniel Vetter55607e82013-06-16 21:42:39 +02001180void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1181 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001182{
1183 int reg;
1184 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001185 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001186
1187 reg = FDI_RX_CTL(pipe);
1188 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001189 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1190 WARN(cur_state != state,
1191 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1192 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001193}
1194
Jesse Barnesea0760c2011-01-04 15:09:32 -08001195static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1196 enum pipe pipe)
1197{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001198 struct drm_device *dev = dev_priv->dev;
1199 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001200 u32 val;
1201 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001202 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001203
Jani Nikulabedd4db2014-08-22 15:04:13 +03001204 if (WARN_ON(HAS_DDI(dev)))
1205 return;
1206
1207 if (HAS_PCH_SPLIT(dev)) {
1208 u32 port_sel;
1209
Jesse Barnesea0760c2011-01-04 15:09:32 -08001210 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001211 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1212
1213 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1214 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1215 panel_pipe = PIPE_B;
1216 /* XXX: else fix for eDP */
1217 } else if (IS_VALLEYVIEW(dev)) {
1218 /* presumably write lock depends on pipe, not port select */
1219 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1220 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001221 } else {
1222 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001223 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1224 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001225 }
1226
1227 val = I915_READ(pp_reg);
1228 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001229 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001230 locked = false;
1231
Jesse Barnesea0760c2011-01-04 15:09:32 -08001232 WARN(panel_pipe == pipe && locked,
1233 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001234 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001235}
1236
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001237static void assert_cursor(struct drm_i915_private *dev_priv,
1238 enum pipe pipe, bool state)
1239{
1240 struct drm_device *dev = dev_priv->dev;
1241 bool cur_state;
1242
Paulo Zanonid9d82082014-02-27 16:30:56 -03001243 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001244 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001245 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001246 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001247
1248 WARN(cur_state != state,
1249 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1250 pipe_name(pipe), state_string(state), state_string(cur_state));
1251}
1252#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1253#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1254
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001255void assert_pipe(struct drm_i915_private *dev_priv,
1256 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001257{
1258 int reg;
1259 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001260 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001261 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1262 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001263
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001264 /* if we need the pipe quirk it must be always on */
1265 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1266 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001267 state = true;
1268
Imre Deakda7e29b2014-02-18 00:02:02 +02001269 if (!intel_display_power_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001270 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001271 cur_state = false;
1272 } else {
1273 reg = PIPECONF(cpu_transcoder);
1274 val = I915_READ(reg);
1275 cur_state = !!(val & PIPECONF_ENABLE);
1276 }
1277
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001278 WARN(cur_state != state,
1279 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001280 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001281}
1282
Chris Wilson931872f2012-01-16 23:01:13 +00001283static void assert_plane(struct drm_i915_private *dev_priv,
1284 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001285{
1286 int reg;
1287 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001288 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001289
1290 reg = DSPCNTR(plane);
1291 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001292 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1293 WARN(cur_state != state,
1294 "plane %c assertion failure (expected %s, current %s)\n",
1295 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001296}
1297
Chris Wilson931872f2012-01-16 23:01:13 +00001298#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1299#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1300
Jesse Barnesb24e7172011-01-04 15:09:30 -08001301static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1302 enum pipe pipe)
1303{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001304 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001305 int reg, i;
1306 u32 val;
1307 int cur_pipe;
1308
Ville Syrjälä653e1022013-06-04 13:49:05 +03001309 /* Primary planes are fixed to pipes on gen4+ */
1310 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001311 reg = DSPCNTR(pipe);
1312 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001313 WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001314 "plane %c assertion failure, should be disabled but not\n",
1315 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001316 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001317 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001318
Jesse Barnesb24e7172011-01-04 15:09:30 -08001319 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001320 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001321 reg = DSPCNTR(i);
1322 val = I915_READ(reg);
1323 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1324 DISPPLANE_SEL_PIPE_SHIFT;
1325 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001326 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1327 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001328 }
1329}
1330
Jesse Barnes19332d72013-03-28 09:55:38 -07001331static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1332 enum pipe pipe)
1333{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001334 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001335 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001336 u32 val;
1337
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001338 if (IS_VALLEYVIEW(dev)) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001339 for_each_sprite(pipe, sprite) {
1340 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001341 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001342 WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001343 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001344 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001345 }
1346 } else if (INTEL_INFO(dev)->gen >= 7) {
1347 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001348 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001349 WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001350 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001351 plane_name(pipe), pipe_name(pipe));
1352 } else if (INTEL_INFO(dev)->gen >= 5) {
1353 reg = DVSCNTR(pipe);
1354 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001355 WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001356 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1357 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001358 }
1359}
1360
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001361static void assert_vblank_disabled(struct drm_crtc *crtc)
1362{
1363 if (WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1364 drm_crtc_vblank_put(crtc);
1365}
1366
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001367static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001368{
1369 u32 val;
1370 bool enabled;
1371
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001372 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001373
Jesse Barnes92f25842011-01-04 15:09:34 -08001374 val = I915_READ(PCH_DREF_CONTROL);
1375 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1376 DREF_SUPERSPREAD_SOURCE_MASK));
1377 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1378}
1379
Daniel Vetterab9412b2013-05-03 11:49:46 +02001380static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1381 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001382{
1383 int reg;
1384 u32 val;
1385 bool enabled;
1386
Daniel Vetterab9412b2013-05-03 11:49:46 +02001387 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001388 val = I915_READ(reg);
1389 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001390 WARN(enabled,
1391 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1392 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001393}
1394
Keith Packard4e634382011-08-06 10:39:45 -07001395static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1396 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001397{
1398 if ((val & DP_PORT_EN) == 0)
1399 return false;
1400
1401 if (HAS_PCH_CPT(dev_priv->dev)) {
1402 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1403 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1404 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1405 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001406 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1407 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1408 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001409 } else {
1410 if ((val & DP_PIPE_MASK) != (pipe << 30))
1411 return false;
1412 }
1413 return true;
1414}
1415
Keith Packard1519b992011-08-06 10:35:34 -07001416static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1417 enum pipe pipe, u32 val)
1418{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001419 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001420 return false;
1421
1422 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001423 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001424 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001425 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1426 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1427 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001428 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001429 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001430 return false;
1431 }
1432 return true;
1433}
1434
1435static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1436 enum pipe pipe, u32 val)
1437{
1438 if ((val & LVDS_PORT_EN) == 0)
1439 return false;
1440
1441 if (HAS_PCH_CPT(dev_priv->dev)) {
1442 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1443 return false;
1444 } else {
1445 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1446 return false;
1447 }
1448 return true;
1449}
1450
1451static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1452 enum pipe pipe, u32 val)
1453{
1454 if ((val & ADPA_DAC_ENABLE) == 0)
1455 return false;
1456 if (HAS_PCH_CPT(dev_priv->dev)) {
1457 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1458 return false;
1459 } else {
1460 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1461 return false;
1462 }
1463 return true;
1464}
1465
Jesse Barnes291906f2011-02-02 12:28:03 -08001466static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001467 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001468{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001469 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001470 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001471 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001472 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001473
Daniel Vetter75c5da22012-09-10 21:58:29 +02001474 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1475 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001476 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001477}
1478
1479static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1480 enum pipe pipe, int reg)
1481{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001482 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001483 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001484 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001485 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001486
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001487 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001488 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001489 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001490}
1491
1492static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1493 enum pipe pipe)
1494{
1495 int reg;
1496 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001497
Keith Packardf0575e92011-07-25 22:12:43 -07001498 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1499 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1500 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001501
1502 reg = PCH_ADPA;
1503 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001504 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001505 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001506 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001507
1508 reg = PCH_LVDS;
1509 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001510 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001511 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001512 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001513
Paulo Zanonie2debe92013-02-18 19:00:27 -03001514 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1515 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1516 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001517}
1518
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001519static void intel_init_dpio(struct drm_device *dev)
1520{
1521 struct drm_i915_private *dev_priv = dev->dev_private;
1522
1523 if (!IS_VALLEYVIEW(dev))
1524 return;
1525
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001526 /*
1527 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1528 * CHV x1 PHY (DP/HDMI D)
1529 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1530 */
1531 if (IS_CHERRYVIEW(dev)) {
1532 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1533 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1534 } else {
1535 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1536 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001537}
1538
Daniel Vetter426115c2013-07-11 22:13:42 +02001539static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001540{
Daniel Vetter426115c2013-07-11 22:13:42 +02001541 struct drm_device *dev = crtc->base.dev;
1542 struct drm_i915_private *dev_priv = dev->dev_private;
1543 int reg = DPLL(crtc->pipe);
1544 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001545
Daniel Vetter426115c2013-07-11 22:13:42 +02001546 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001547
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001548 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001549 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1550
1551 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001552 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001553 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001554
Daniel Vetter426115c2013-07-11 22:13:42 +02001555 I915_WRITE(reg, dpll);
1556 POSTING_READ(reg);
1557 udelay(150);
1558
1559 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1560 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1561
1562 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1563 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001564
1565 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001566 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001567 POSTING_READ(reg);
1568 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001569 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001570 POSTING_READ(reg);
1571 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001572 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001573 POSTING_READ(reg);
1574 udelay(150); /* wait for warmup */
1575}
1576
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001577static void chv_enable_pll(struct intel_crtc *crtc)
1578{
1579 struct drm_device *dev = crtc->base.dev;
1580 struct drm_i915_private *dev_priv = dev->dev_private;
1581 int pipe = crtc->pipe;
1582 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001583 u32 tmp;
1584
1585 assert_pipe_disabled(dev_priv, crtc->pipe);
1586
1587 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1588
1589 mutex_lock(&dev_priv->dpio_lock);
1590
1591 /* Enable back the 10bit clock to display controller */
1592 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1593 tmp |= DPIO_DCLKP_EN;
1594 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1595
1596 /*
1597 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1598 */
1599 udelay(1);
1600
1601 /* Enable PLL */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001602 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001603
1604 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001605 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001606 DRM_ERROR("PLL %d failed to lock\n", pipe);
1607
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001608 /* not sure when this should be written */
1609 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1610 POSTING_READ(DPLL_MD(pipe));
1611
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001612 mutex_unlock(&dev_priv->dpio_lock);
1613}
1614
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001615static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001616{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001617 struct drm_device *dev = crtc->base.dev;
1618 struct drm_i915_private *dev_priv = dev->dev_private;
1619 int reg = DPLL(crtc->pipe);
1620 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001621
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001622 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001623
1624 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001625 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001626
1627 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001628 if (IS_MOBILE(dev) && !IS_I830(dev))
1629 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001630
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001631 I915_WRITE(reg, dpll);
1632
1633 /* Wait for the clocks to stabilize. */
1634 POSTING_READ(reg);
1635 udelay(150);
1636
1637 if (INTEL_INFO(dev)->gen >= 4) {
1638 I915_WRITE(DPLL_MD(crtc->pipe),
1639 crtc->config.dpll_hw_state.dpll_md);
1640 } else {
1641 /* The pixel multiplier can only be updated once the
1642 * DPLL is enabled and the clocks are stable.
1643 *
1644 * So write it again.
1645 */
1646 I915_WRITE(reg, dpll);
1647 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001648
1649 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001650 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001651 POSTING_READ(reg);
1652 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001653 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001654 POSTING_READ(reg);
1655 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001656 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001657 POSTING_READ(reg);
1658 udelay(150); /* wait for warmup */
1659}
1660
1661/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001662 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001663 * @dev_priv: i915 private structure
1664 * @pipe: pipe PLL to disable
1665 *
1666 * Disable the PLL for @pipe, making sure the pipe is off first.
1667 *
1668 * Note! This is for pre-ILK only.
1669 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001670static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001671{
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001672 /* Don't disable pipe or pipe PLLs if needed */
1673 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1674 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001675 return;
1676
1677 /* Make sure the pipe isn't still relying on us */
1678 assert_pipe_disabled(dev_priv, pipe);
1679
Daniel Vetter50b44a42013-06-05 13:34:33 +02001680 I915_WRITE(DPLL(pipe), 0);
1681 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001682}
1683
Jesse Barnesf6071162013-10-01 10:41:38 -07001684static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1685{
1686 u32 val = 0;
1687
1688 /* Make sure the pipe isn't still relying on us */
1689 assert_pipe_disabled(dev_priv, pipe);
1690
Imre Deake5cbfbf2014-01-09 17:08:16 +02001691 /*
1692 * Leave integrated clock source and reference clock enabled for pipe B.
1693 * The latter is needed for VGA hotplug / manual detection.
1694 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001695 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001696 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001697 I915_WRITE(DPLL(pipe), val);
1698 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001699
1700}
1701
1702static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1703{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001704 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001705 u32 val;
1706
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001707 /* Make sure the pipe isn't still relying on us */
1708 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001709
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001710 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001711 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001712 if (pipe != PIPE_A)
1713 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1714 I915_WRITE(DPLL(pipe), val);
1715 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001716
1717 mutex_lock(&dev_priv->dpio_lock);
1718
1719 /* Disable 10bit clock to display controller */
1720 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1721 val &= ~DPIO_DCLKP_EN;
1722 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1723
Ville Syrjälä61407f62014-05-27 16:32:55 +03001724 /* disable left/right clock distribution */
1725 if (pipe != PIPE_B) {
1726 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1727 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1728 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1729 } else {
1730 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1731 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1732 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1733 }
1734
Ville Syrjäläd7520482014-04-09 13:28:59 +03001735 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001736}
1737
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001738void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1739 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001740{
1741 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001742 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001743
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001744 switch (dport->port) {
1745 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001746 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001747 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001748 break;
1749 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001750 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001751 dpll_reg = DPLL(0);
1752 break;
1753 case PORT_D:
1754 port_mask = DPLL_PORTD_READY_MASK;
1755 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001756 break;
1757 default:
1758 BUG();
1759 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001760
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001761 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001762 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001763 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001764}
1765
Daniel Vetterb14b1052014-04-24 23:55:13 +02001766static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1767{
1768 struct drm_device *dev = crtc->base.dev;
1769 struct drm_i915_private *dev_priv = dev->dev_private;
1770 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1771
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001772 if (WARN_ON(pll == NULL))
1773 return;
1774
Daniel Vetterb14b1052014-04-24 23:55:13 +02001775 WARN_ON(!pll->refcount);
1776 if (pll->active == 0) {
1777 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1778 WARN_ON(pll->on);
1779 assert_shared_dpll_disabled(dev_priv, pll);
1780
1781 pll->mode_set(dev_priv, pll);
1782 }
1783}
1784
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001785/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001786 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001787 * @dev_priv: i915 private structure
1788 * @pipe: pipe PLL to enable
1789 *
1790 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1791 * drives the transcoder clock.
1792 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001793static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001794{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001795 struct drm_device *dev = crtc->base.dev;
1796 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001797 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001798
Daniel Vetter87a875b2013-06-05 13:34:19 +02001799 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001800 return;
1801
1802 if (WARN_ON(pll->refcount == 0))
1803 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001804
Damien Lespiau74dd6922014-07-29 18:06:17 +01001805 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001806 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001807 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001808
Daniel Vettercdbd2312013-06-05 13:34:03 +02001809 if (pll->active++) {
1810 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001811 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001812 return;
1813 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001814 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001815
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001816 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1817
Daniel Vetter46edb022013-06-05 13:34:12 +02001818 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001819 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001820 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001821}
1822
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001823static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001824{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001825 struct drm_device *dev = crtc->base.dev;
1826 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001827 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001828
Jesse Barnes92f25842011-01-04 15:09:34 -08001829 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001830 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001831 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001832 return;
1833
Chris Wilson48da64a2012-05-13 20:16:12 +01001834 if (WARN_ON(pll->refcount == 0))
1835 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001836
Daniel Vetter46edb022013-06-05 13:34:12 +02001837 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1838 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001839 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001840
Chris Wilson48da64a2012-05-13 20:16:12 +01001841 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001842 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001843 return;
1844 }
1845
Daniel Vettere9d69442013-06-05 13:34:15 +02001846 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001847 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001848 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001849 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001850
Daniel Vetter46edb022013-06-05 13:34:12 +02001851 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001852 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001853 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001854
1855 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001856}
1857
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001858static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1859 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001860{
Daniel Vetter23670b322012-11-01 09:15:30 +01001861 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001862 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001863 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001864 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001865
1866 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001867 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001868
1869 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001870 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001871 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001872
1873 /* FDI must be feeding us bits for PCH ports */
1874 assert_fdi_tx_enabled(dev_priv, pipe);
1875 assert_fdi_rx_enabled(dev_priv, pipe);
1876
Daniel Vetter23670b322012-11-01 09:15:30 +01001877 if (HAS_PCH_CPT(dev)) {
1878 /* Workaround: Set the timing override bit before enabling the
1879 * pch transcoder. */
1880 reg = TRANS_CHICKEN2(pipe);
1881 val = I915_READ(reg);
1882 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1883 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001884 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001885
Daniel Vetterab9412b2013-05-03 11:49:46 +02001886 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001887 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001888 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001889
1890 if (HAS_PCH_IBX(dev_priv->dev)) {
1891 /*
1892 * make the BPC in transcoder be consistent with
1893 * that in pipeconf reg.
1894 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001895 val &= ~PIPECONF_BPC_MASK;
1896 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001897 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001898
1899 val &= ~TRANS_INTERLACE_MASK;
1900 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001901 if (HAS_PCH_IBX(dev_priv->dev) &&
1902 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1903 val |= TRANS_LEGACY_INTERLACED_ILK;
1904 else
1905 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001906 else
1907 val |= TRANS_PROGRESSIVE;
1908
Jesse Barnes040484a2011-01-03 12:14:26 -08001909 I915_WRITE(reg, val | TRANS_ENABLE);
1910 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001911 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001912}
1913
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001914static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001915 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001916{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001917 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001918
1919 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001920 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001921
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001922 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001923 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001924 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001925
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001926 /* Workaround: set timing override bit. */
1927 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001928 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001929 I915_WRITE(_TRANSA_CHICKEN2, val);
1930
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001931 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001932 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001933
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001934 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1935 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001936 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001937 else
1938 val |= TRANS_PROGRESSIVE;
1939
Daniel Vetterab9412b2013-05-03 11:49:46 +02001940 I915_WRITE(LPT_TRANSCONF, val);
1941 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001942 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001943}
1944
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001945static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1946 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001947{
Daniel Vetter23670b322012-11-01 09:15:30 +01001948 struct drm_device *dev = dev_priv->dev;
1949 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001950
1951 /* FDI relies on the transcoder */
1952 assert_fdi_tx_disabled(dev_priv, pipe);
1953 assert_fdi_rx_disabled(dev_priv, pipe);
1954
Jesse Barnes291906f2011-02-02 12:28:03 -08001955 /* Ports must be off as well */
1956 assert_pch_ports_disabled(dev_priv, pipe);
1957
Daniel Vetterab9412b2013-05-03 11:49:46 +02001958 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001959 val = I915_READ(reg);
1960 val &= ~TRANS_ENABLE;
1961 I915_WRITE(reg, val);
1962 /* wait for PCH transcoder off, transcoder state */
1963 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001964 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001965
1966 if (!HAS_PCH_IBX(dev)) {
1967 /* Workaround: Clear the timing override chicken bit again. */
1968 reg = TRANS_CHICKEN2(pipe);
1969 val = I915_READ(reg);
1970 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1971 I915_WRITE(reg, val);
1972 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001973}
1974
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001975static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001976{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001977 u32 val;
1978
Daniel Vetterab9412b2013-05-03 11:49:46 +02001979 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001980 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001981 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001982 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001983 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001984 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001985
1986 /* Workaround: clear timing override bit. */
1987 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001988 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001989 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001990}
1991
1992/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001993 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001994 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001995 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001996 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001997 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001998 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001999static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002000{
Paulo Zanoni03722642014-01-17 13:51:09 -02002001 struct drm_device *dev = crtc->base.dev;
2002 struct drm_i915_private *dev_priv = dev->dev_private;
2003 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002004 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2005 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002006 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002007 int reg;
2008 u32 val;
2009
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002010 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002011 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002012 assert_sprites_disabled(dev_priv, pipe);
2013
Paulo Zanoni681e5812012-12-06 11:12:38 -02002014 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002015 pch_transcoder = TRANSCODER_A;
2016 else
2017 pch_transcoder = pipe;
2018
Jesse Barnesb24e7172011-01-04 15:09:30 -08002019 /*
2020 * A pipe without a PLL won't actually be able to drive bits from
2021 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2022 * need the check.
2023 */
2024 if (!HAS_PCH_SPLIT(dev_priv->dev))
Paulo Zanonifbf32182014-01-17 13:51:11 -02002025 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002026 assert_dsi_pll_enabled(dev_priv);
2027 else
2028 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002029 else {
Paulo Zanoni30421c42014-01-17 13:51:10 -02002030 if (crtc->config.has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002031 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002032 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002033 assert_fdi_tx_pll_enabled(dev_priv,
2034 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002035 }
2036 /* FIXME: assert CPU port conditions for SNB+ */
2037 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002038
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002039 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002040 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002041 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002042 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2043 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002044 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002045 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002046
2047 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002048 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002049}
2050
2051/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002052 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002053 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002054 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002055 * Disable the pipe of @crtc, making sure that various hardware
2056 * specific requirements are met, if applicable, e.g. plane
2057 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002058 *
2059 * Will wait until the pipe has shut down before returning.
2060 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002061static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002062{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002063 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
2064 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2065 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002066 int reg;
2067 u32 val;
2068
2069 /*
2070 * Make sure planes won't keep trying to pump pixels to us,
2071 * or we might hang the display.
2072 */
2073 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002074 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002075 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002076
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002077 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002078 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002079 if ((val & PIPECONF_ENABLE) == 0)
2080 return;
2081
Ville Syrjälä67adc642014-08-15 01:21:57 +03002082 /*
2083 * Double wide has implications for planes
2084 * so best keep it disabled when not needed.
2085 */
2086 if (crtc->config.double_wide)
2087 val &= ~PIPECONF_DOUBLE_WIDE;
2088
2089 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002090 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2091 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002092 val &= ~PIPECONF_ENABLE;
2093
2094 I915_WRITE(reg, val);
2095 if ((val & PIPECONF_ENABLE) == 0)
2096 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002097}
2098
Keith Packardd74362c2011-07-28 14:47:14 -07002099/*
2100 * Plane regs are double buffered, going from enabled->disabled needs a
2101 * trigger in order to latch. The display address reg provides this.
2102 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002103void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2104 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002105{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002106 struct drm_device *dev = dev_priv->dev;
2107 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002108
2109 I915_WRITE(reg, I915_READ(reg));
2110 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002111}
2112
Jesse Barnesb24e7172011-01-04 15:09:30 -08002113/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002114 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002115 * @plane: plane to be enabled
2116 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002117 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002118 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002119 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002120static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2121 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002122{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002123 struct drm_device *dev = plane->dev;
2124 struct drm_i915_private *dev_priv = dev->dev_private;
2125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002126
2127 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002128 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002129
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002130 if (intel_crtc->primary_enabled)
2131 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002132
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002133 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002134
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002135 dev_priv->display.update_primary_plane(crtc, plane->fb,
2136 crtc->x, crtc->y);
Ville Syrjälä33c3b0d2014-06-24 13:59:28 +03002137
2138 /*
2139 * BDW signals flip done immediately if the plane
2140 * is disabled, even if the plane enable is already
2141 * armed to occur at the next vblank :(
2142 */
2143 if (IS_BROADWELL(dev))
2144 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002145}
2146
Jesse Barnesb24e7172011-01-04 15:09:30 -08002147/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002148 * intel_disable_primary_hw_plane - disable the primary hardware plane
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002149 * @plane: plane to be disabled
2150 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002151 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002152 * Disable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002153 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002154static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2155 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002156{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002157 struct drm_device *dev = plane->dev;
2158 struct drm_i915_private *dev_priv = dev->dev_private;
2159 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2160
2161 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002162
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002163 if (!intel_crtc->primary_enabled)
2164 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002165
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002166 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002167
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002168 dev_priv->display.update_primary_plane(crtc, plane->fb,
2169 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002170}
2171
Chris Wilson693db182013-03-05 14:52:39 +00002172static bool need_vtd_wa(struct drm_device *dev)
2173{
2174#ifdef CONFIG_INTEL_IOMMU
2175 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2176 return true;
2177#endif
2178 return false;
2179}
2180
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002181static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2182{
2183 int tile_height;
2184
2185 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2186 return ALIGN(height, tile_height);
2187}
2188
Chris Wilson127bd2a2010-07-23 23:32:05 +01002189int
Chris Wilson48b956c2010-09-14 12:50:34 +01002190intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002191 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002192 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002193{
Chris Wilsonce453d82011-02-21 14:43:56 +00002194 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002195 u32 alignment;
2196 int ret;
2197
Matt Roperebcdd392014-07-09 16:22:11 -07002198 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2199
Chris Wilson05394f32010-11-08 19:18:58 +00002200 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002201 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01002202 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2203 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002204 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002205 alignment = 4 * 1024;
2206 else
2207 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002208 break;
2209 case I915_TILING_X:
2210 /* pin() will align the object as required by fence */
2211 alignment = 0;
2212 break;
2213 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02002214 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002215 return -EINVAL;
2216 default:
2217 BUG();
2218 }
2219
Chris Wilson693db182013-03-05 14:52:39 +00002220 /* Note that the w/a also requires 64 PTE of padding following the
2221 * bo. We currently fill all unused PTE with the shadow page and so
2222 * we should always have valid PTE following the scanout preventing
2223 * the VT-d warning.
2224 */
2225 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2226 alignment = 256 * 1024;
2227
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002228 /*
2229 * Global gtt pte registers are special registers which actually forward
2230 * writes to a chunk of system memory. Which means that there is no risk
2231 * that the register values disappear as soon as we call
2232 * intel_runtime_pm_put(), so it is correct to wrap only the
2233 * pin/unpin/fence and not more.
2234 */
2235 intel_runtime_pm_get(dev_priv);
2236
Chris Wilsonce453d82011-02-21 14:43:56 +00002237 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002238 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002239 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002240 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002241
2242 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2243 * fence, whereas 965+ only requires a fence if using
2244 * framebuffer compression. For simplicity, we always install
2245 * a fence as the cost is not that onerous.
2246 */
Chris Wilson06d98132012-04-17 15:31:24 +01002247 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002248 if (ret)
2249 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002250
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002251 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002252
Chris Wilsonce453d82011-02-21 14:43:56 +00002253 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002254 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002255 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002256
2257err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01002258 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002259err_interruptible:
2260 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002261 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002262 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002263}
2264
Chris Wilson1690e1e2011-12-14 13:57:08 +01002265void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2266{
Matt Roperebcdd392014-07-09 16:22:11 -07002267 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2268
Chris Wilson1690e1e2011-12-14 13:57:08 +01002269 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01002270 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002271}
2272
Daniel Vetterc2c75132012-07-05 12:17:30 +02002273/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2274 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002275unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2276 unsigned int tiling_mode,
2277 unsigned int cpp,
2278 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002279{
Chris Wilsonbc752862013-02-21 20:04:31 +00002280 if (tiling_mode != I915_TILING_NONE) {
2281 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002282
Chris Wilsonbc752862013-02-21 20:04:31 +00002283 tile_rows = *y / 8;
2284 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002285
Chris Wilsonbc752862013-02-21 20:04:31 +00002286 tiles = *x / (512/cpp);
2287 *x %= 512/cpp;
2288
2289 return tile_rows * pitch * 8 + tiles * 4096;
2290 } else {
2291 unsigned int offset;
2292
2293 offset = *y * pitch + *x * cpp;
2294 *y = 0;
2295 *x = (offset & 4095) / cpp;
2296 return offset & -4096;
2297 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002298}
2299
Jesse Barnes46f297f2014-03-07 08:57:48 -08002300int intel_format_to_fourcc(int format)
2301{
2302 switch (format) {
2303 case DISPPLANE_8BPP:
2304 return DRM_FORMAT_C8;
2305 case DISPPLANE_BGRX555:
2306 return DRM_FORMAT_XRGB1555;
2307 case DISPPLANE_BGRX565:
2308 return DRM_FORMAT_RGB565;
2309 default:
2310 case DISPPLANE_BGRX888:
2311 return DRM_FORMAT_XRGB8888;
2312 case DISPPLANE_RGBX888:
2313 return DRM_FORMAT_XBGR8888;
2314 case DISPPLANE_BGRX101010:
2315 return DRM_FORMAT_XRGB2101010;
2316 case DISPPLANE_RGBX101010:
2317 return DRM_FORMAT_XBGR2101010;
2318 }
2319}
2320
Jesse Barnes484b41d2014-03-07 08:57:55 -08002321static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -08002322 struct intel_plane_config *plane_config)
2323{
2324 struct drm_device *dev = crtc->base.dev;
2325 struct drm_i915_gem_object *obj = NULL;
2326 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2327 u32 base = plane_config->base;
2328
Chris Wilsonff2652e2014-03-10 08:07:02 +00002329 if (plane_config->size == 0)
2330 return false;
2331
Jesse Barnes46f297f2014-03-07 08:57:48 -08002332 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2333 plane_config->size);
2334 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002335 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002336
2337 if (plane_config->tiled) {
2338 obj->tiling_mode = I915_TILING_X;
Dave Airlie66e514c2014-04-03 07:51:54 +10002339 obj->stride = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002340 }
2341
Dave Airlie66e514c2014-04-03 07:51:54 +10002342 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2343 mode_cmd.width = crtc->base.primary->fb->width;
2344 mode_cmd.height = crtc->base.primary->fb->height;
2345 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002346
2347 mutex_lock(&dev->struct_mutex);
2348
Dave Airlie66e514c2014-04-03 07:51:54 +10002349 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002350 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002351 DRM_DEBUG_KMS("intel fb init failed\n");
2352 goto out_unref_obj;
2353 }
2354
Daniel Vettera071fa02014-06-18 23:28:09 +02002355 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002356 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002357
2358 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2359 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002360
2361out_unref_obj:
2362 drm_gem_object_unreference(&obj->base);
2363 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002364 return false;
2365}
2366
2367static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2368 struct intel_plane_config *plane_config)
2369{
2370 struct drm_device *dev = intel_crtc->base.dev;
2371 struct drm_crtc *c;
2372 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002373 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002374
Dave Airlie66e514c2014-04-03 07:51:54 +10002375 if (!intel_crtc->base.primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002376 return;
2377
2378 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2379 return;
2380
Dave Airlie66e514c2014-04-03 07:51:54 +10002381 kfree(intel_crtc->base.primary->fb);
2382 intel_crtc->base.primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002383
2384 /*
2385 * Failed to alloc the obj, check to see if we should share
2386 * an fb with another CRTC instead
2387 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002388 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002389 i = to_intel_crtc(c);
2390
2391 if (c == &intel_crtc->base)
2392 continue;
2393
Matt Roper2ff8fde2014-07-08 07:50:07 -07002394 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002395 continue;
2396
Matt Roper2ff8fde2014-07-08 07:50:07 -07002397 obj = intel_fb_obj(c->primary->fb);
2398 if (obj == NULL)
2399 continue;
2400
2401 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Dave Airlie66e514c2014-04-03 07:51:54 +10002402 drm_framebuffer_reference(c->primary->fb);
2403 intel_crtc->base.primary->fb = c->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002404 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002405 break;
2406 }
2407 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002408}
2409
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002410static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2411 struct drm_framebuffer *fb,
2412 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002413{
2414 struct drm_device *dev = crtc->dev;
2415 struct drm_i915_private *dev_priv = dev->dev_private;
2416 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002417 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002418 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002419 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002420 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002421 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302422 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002423
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002424 if (!intel_crtc->primary_enabled) {
2425 I915_WRITE(reg, 0);
2426 if (INTEL_INFO(dev)->gen >= 4)
2427 I915_WRITE(DSPSURF(plane), 0);
2428 else
2429 I915_WRITE(DSPADDR(plane), 0);
2430 POSTING_READ(reg);
2431 return;
2432 }
2433
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002434 obj = intel_fb_obj(fb);
2435 if (WARN_ON(obj == NULL))
2436 return;
2437
2438 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2439
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002440 dspcntr = DISPPLANE_GAMMA_ENABLE;
2441
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002442 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002443
2444 if (INTEL_INFO(dev)->gen < 4) {
2445 if (intel_crtc->pipe == PIPE_B)
2446 dspcntr |= DISPPLANE_SEL_PIPE_B;
2447
2448 /* pipesrc and dspsize control the size that is scaled from,
2449 * which should always be the user's requested size.
2450 */
2451 I915_WRITE(DSPSIZE(plane),
2452 ((intel_crtc->config.pipe_src_h - 1) << 16) |
2453 (intel_crtc->config.pipe_src_w - 1));
2454 I915_WRITE(DSPPOS(plane), 0);
2455 }
2456
Ville Syrjälä57779d02012-10-31 17:50:14 +02002457 switch (fb->pixel_format) {
2458 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002459 dspcntr |= DISPPLANE_8BPP;
2460 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002461 case DRM_FORMAT_XRGB1555:
2462 case DRM_FORMAT_ARGB1555:
2463 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002464 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002465 case DRM_FORMAT_RGB565:
2466 dspcntr |= DISPPLANE_BGRX565;
2467 break;
2468 case DRM_FORMAT_XRGB8888:
2469 case DRM_FORMAT_ARGB8888:
2470 dspcntr |= DISPPLANE_BGRX888;
2471 break;
2472 case DRM_FORMAT_XBGR8888:
2473 case DRM_FORMAT_ABGR8888:
2474 dspcntr |= DISPPLANE_RGBX888;
2475 break;
2476 case DRM_FORMAT_XRGB2101010:
2477 case DRM_FORMAT_ARGB2101010:
2478 dspcntr |= DISPPLANE_BGRX101010;
2479 break;
2480 case DRM_FORMAT_XBGR2101010:
2481 case DRM_FORMAT_ABGR2101010:
2482 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002483 break;
2484 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002485 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002486 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002487
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002488 if (INTEL_INFO(dev)->gen >= 4 &&
2489 obj->tiling_mode != I915_TILING_NONE)
2490 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002491
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002492 if (IS_G4X(dev))
2493 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2494
Ville Syrjäläb98971272014-08-27 16:51:22 +03002495 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002496
Daniel Vetterc2c75132012-07-05 12:17:30 +02002497 if (INTEL_INFO(dev)->gen >= 4) {
2498 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002499 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002500 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002501 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002502 linear_offset -= intel_crtc->dspaddr_offset;
2503 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002504 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002505 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002506
Sonika Jindal48404c12014-08-22 14:06:04 +05302507 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2508 dspcntr |= DISPPLANE_ROTATE_180;
2509
2510 x += (intel_crtc->config.pipe_src_w - 1);
2511 y += (intel_crtc->config.pipe_src_h - 1);
2512
2513 /* Finding the last pixel of the last line of the display
2514 data and adding to linear_offset*/
2515 linear_offset +=
2516 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2517 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2518 }
2519
2520 I915_WRITE(reg, dspcntr);
2521
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002522 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2523 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2524 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002525 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002526 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002527 I915_WRITE(DSPSURF(plane),
2528 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002529 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002530 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002531 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002532 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002533 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002534}
2535
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002536static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2537 struct drm_framebuffer *fb,
2538 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002539{
2540 struct drm_device *dev = crtc->dev;
2541 struct drm_i915_private *dev_priv = dev->dev_private;
2542 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002543 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002544 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002545 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002546 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002547 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302548 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002549
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002550 if (!intel_crtc->primary_enabled) {
2551 I915_WRITE(reg, 0);
2552 I915_WRITE(DSPSURF(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äfdd508a2014-08-08 21:51:11 +03002565 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002566
2567 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2568 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2569
Ville Syrjälä57779d02012-10-31 17:50:14 +02002570 switch (fb->pixel_format) {
2571 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002572 dspcntr |= DISPPLANE_8BPP;
2573 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002574 case DRM_FORMAT_RGB565:
2575 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002576 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002577 case DRM_FORMAT_XRGB8888:
2578 case DRM_FORMAT_ARGB8888:
2579 dspcntr |= DISPPLANE_BGRX888;
2580 break;
2581 case DRM_FORMAT_XBGR8888:
2582 case DRM_FORMAT_ABGR8888:
2583 dspcntr |= DISPPLANE_RGBX888;
2584 break;
2585 case DRM_FORMAT_XRGB2101010:
2586 case DRM_FORMAT_ARGB2101010:
2587 dspcntr |= DISPPLANE_BGRX101010;
2588 break;
2589 case DRM_FORMAT_XBGR2101010:
2590 case DRM_FORMAT_ABGR2101010:
2591 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002592 break;
2593 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002594 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002595 }
2596
2597 if (obj->tiling_mode != I915_TILING_NONE)
2598 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002599
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002600 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002601 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002602
Ville Syrjäläb98971272014-08-27 16:51:22 +03002603 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002604 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002605 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002606 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002607 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002608 linear_offset -= intel_crtc->dspaddr_offset;
Sonika Jindal48404c12014-08-22 14:06:04 +05302609 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2610 dspcntr |= DISPPLANE_ROTATE_180;
2611
2612 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
2613 x += (intel_crtc->config.pipe_src_w - 1);
2614 y += (intel_crtc->config.pipe_src_h - 1);
2615
2616 /* Finding the last pixel of the last line of the display
2617 data and adding to linear_offset*/
2618 linear_offset +=
2619 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2620 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2621 }
2622 }
2623
2624 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002625
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002626 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2627 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2628 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002629 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002630 I915_WRITE(DSPSURF(plane),
2631 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002632 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002633 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2634 } else {
2635 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2636 I915_WRITE(DSPLINOFF(plane), linear_offset);
2637 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002638 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002639}
2640
2641/* Assume fb object is pinned & idle & fenced and just update base pointers */
2642static int
2643intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2644 int x, int y, enum mode_set_atomic state)
2645{
2646 struct drm_device *dev = crtc->dev;
2647 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002648
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002649 if (dev_priv->display.disable_fbc)
2650 dev_priv->display.disable_fbc(dev);
Daniel Vettercc365132014-06-18 13:59:13 +02002651 intel_increase_pllclock(dev, to_intel_crtc(crtc)->pipe);
Jesse Barnes81255562010-08-02 12:07:50 -07002652
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002653 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2654
2655 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07002656}
2657
Ville Syrjälä96a02912013-02-18 19:08:49 +02002658void intel_display_handle_reset(struct drm_device *dev)
2659{
2660 struct drm_i915_private *dev_priv = dev->dev_private;
2661 struct drm_crtc *crtc;
2662
2663 /*
2664 * Flips in the rings have been nuked by the reset,
2665 * so complete all pending flips so that user space
2666 * will get its events and not get stuck.
2667 *
2668 * Also update the base address of all primary
2669 * planes to the the last fb to make sure we're
2670 * showing the correct fb after a reset.
2671 *
2672 * Need to make two loops over the crtcs so that we
2673 * don't try to grab a crtc mutex before the
2674 * pending_flip_queue really got woken up.
2675 */
2676
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002677 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002678 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2679 enum plane plane = intel_crtc->plane;
2680
2681 intel_prepare_page_flip(dev, plane);
2682 intel_finish_page_flip_plane(dev, plane);
2683 }
2684
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002685 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002686 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2687
Rob Clark51fd3712013-11-19 12:10:12 -05002688 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002689 /*
2690 * FIXME: Once we have proper support for primary planes (and
2691 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10002692 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002693 */
Matt Roperf4510a22014-04-01 15:22:40 -07002694 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07002695 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10002696 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07002697 crtc->x,
2698 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05002699 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02002700 }
2701}
2702
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002703static int
Chris Wilson14667a42012-04-03 17:58:35 +01002704intel_finish_fb(struct drm_framebuffer *old_fb)
2705{
Matt Roper2ff8fde2014-07-08 07:50:07 -07002706 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01002707 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2708 bool was_interruptible = dev_priv->mm.interruptible;
2709 int ret;
2710
Chris Wilson14667a42012-04-03 17:58:35 +01002711 /* Big Hammer, we also need to ensure that any pending
2712 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2713 * current scanout is retired before unpinning the old
2714 * framebuffer.
2715 *
2716 * This should only fail upon a hung GPU, in which case we
2717 * can safely continue.
2718 */
2719 dev_priv->mm.interruptible = false;
2720 ret = i915_gem_object_finish_gpu(obj);
2721 dev_priv->mm.interruptible = was_interruptible;
2722
2723 return ret;
2724}
2725
Chris Wilson7d5e3792014-03-04 13:15:08 +00002726static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2727{
2728 struct drm_device *dev = crtc->dev;
2729 struct drm_i915_private *dev_priv = dev->dev_private;
2730 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2731 unsigned long flags;
2732 bool pending;
2733
2734 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2735 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2736 return false;
2737
2738 spin_lock_irqsave(&dev->event_lock, flags);
2739 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2740 spin_unlock_irqrestore(&dev->event_lock, flags);
2741
2742 return pending;
2743}
2744
Chris Wilson14667a42012-04-03 17:58:35 +01002745static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002746intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002747 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002748{
2749 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002750 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02002752 enum pipe pipe = intel_crtc->pipe;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002753 struct drm_framebuffer *old_fb = crtc->primary->fb;
2754 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2755 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002756 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002757
Chris Wilson7d5e3792014-03-04 13:15:08 +00002758 if (intel_crtc_has_pending_flip(crtc)) {
2759 DRM_ERROR("pipe is still busy with an old pageflip\n");
2760 return -EBUSY;
2761 }
2762
Jesse Barnes79e53942008-11-07 14:24:08 -08002763 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002764 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002765 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002766 return 0;
2767 }
2768
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002769 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002770 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2771 plane_name(intel_crtc->plane),
2772 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002773 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002774 }
2775
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002776 mutex_lock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +02002777 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
2778 if (ret == 0)
Matt Roper91565c852014-06-24 17:05:02 -07002779 i915_gem_track_fb(old_obj, obj,
Daniel Vettera071fa02014-06-18 23:28:09 +02002780 INTEL_FRONTBUFFER_PRIMARY(pipe));
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002781 mutex_unlock(&dev->struct_mutex);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002782 if (ret != 0) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002783 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002784 return ret;
2785 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002786
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002787 /*
2788 * Update pipe size and adjust fitter if needed: the reason for this is
2789 * that in compute_mode_changes we check the native mode (not the pfit
2790 * mode) to see if we can flip rather than do a full mode set. In the
2791 * fastboot case, we'll flip, but if we don't update the pipesrc and
2792 * pfit state, we'll end up with a big fb scanned out into the wrong
2793 * sized surface.
2794 *
2795 * To fix this properly, we need to hoist the checks up into
2796 * compute_mode_changes (or above), check the actual pfit state and
2797 * whether the platform allows pfit disable with pipe active, and only
2798 * then update the pipesrc and pfit state, even on the flip path.
2799 */
Jani Nikulad330a952014-01-21 11:24:25 +02002800 if (i915.fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002801 const struct drm_display_mode *adjusted_mode =
2802 &intel_crtc->config.adjusted_mode;
2803
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002804 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002805 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2806 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002807 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002808 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2809 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2810 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2811 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2812 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2813 }
Jesse Barnes0637d602013-12-19 10:48:01 -08002814 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2815 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002816 }
2817
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002818 dev_priv->display.update_primary_plane(crtc, fb, x, y);
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002819
Daniel Vetterf99d7062014-06-19 16:01:59 +02002820 if (intel_crtc->active)
2821 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
2822
Matt Roperf4510a22014-04-01 15:22:40 -07002823 crtc->primary->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002824 crtc->x = x;
2825 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002826
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002827 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002828 if (intel_crtc->active && old_fb != fb)
2829 intel_wait_for_vblank(dev, intel_crtc->pipe);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002830 mutex_lock(&dev->struct_mutex);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002831 intel_unpin_fb_obj(old_obj);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002832 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002833 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002834
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002835 mutex_lock(&dev->struct_mutex);
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002836 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002837 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002838
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002839 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002840}
2841
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002842static void intel_fdi_normal_train(struct drm_crtc *crtc)
2843{
2844 struct drm_device *dev = crtc->dev;
2845 struct drm_i915_private *dev_priv = dev->dev_private;
2846 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2847 int pipe = intel_crtc->pipe;
2848 u32 reg, temp;
2849
2850 /* enable normal train */
2851 reg = FDI_TX_CTL(pipe);
2852 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002853 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002854 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2855 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002856 } else {
2857 temp &= ~FDI_LINK_TRAIN_NONE;
2858 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002859 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002860 I915_WRITE(reg, temp);
2861
2862 reg = FDI_RX_CTL(pipe);
2863 temp = I915_READ(reg);
2864 if (HAS_PCH_CPT(dev)) {
2865 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2866 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2867 } else {
2868 temp &= ~FDI_LINK_TRAIN_NONE;
2869 temp |= FDI_LINK_TRAIN_NONE;
2870 }
2871 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2872
2873 /* wait one idle pattern time */
2874 POSTING_READ(reg);
2875 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002876
2877 /* IVB wants error correction enabled */
2878 if (IS_IVYBRIDGE(dev))
2879 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2880 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002881}
2882
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002883static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002884{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002885 return crtc->base.enabled && crtc->active &&
2886 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002887}
2888
Daniel Vetter01a415f2012-10-27 15:58:40 +02002889static void ivb_modeset_global_resources(struct drm_device *dev)
2890{
2891 struct drm_i915_private *dev_priv = dev->dev_private;
2892 struct intel_crtc *pipe_B_crtc =
2893 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2894 struct intel_crtc *pipe_C_crtc =
2895 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2896 uint32_t temp;
2897
Daniel Vetter1e833f42013-02-19 22:31:57 +01002898 /*
2899 * When everything is off disable fdi C so that we could enable fdi B
2900 * with all lanes. Note that we don't care about enabled pipes without
2901 * an enabled pch encoder.
2902 */
2903 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2904 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002905 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2906 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2907
2908 temp = I915_READ(SOUTH_CHICKEN1);
2909 temp &= ~FDI_BC_BIFURCATION_SELECT;
2910 DRM_DEBUG_KMS("disabling fdi C rx\n");
2911 I915_WRITE(SOUTH_CHICKEN1, temp);
2912 }
2913}
2914
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002915/* The FDI link training functions for ILK/Ibexpeak. */
2916static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2917{
2918 struct drm_device *dev = crtc->dev;
2919 struct drm_i915_private *dev_priv = dev->dev_private;
2920 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2921 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002922 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002923
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03002924 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002925 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002926
Adam Jacksone1a44742010-06-25 15:32:14 -04002927 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2928 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002929 reg = FDI_RX_IMR(pipe);
2930 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002931 temp &= ~FDI_RX_SYMBOL_LOCK;
2932 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002933 I915_WRITE(reg, temp);
2934 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002935 udelay(150);
2936
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002937 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002938 reg = FDI_TX_CTL(pipe);
2939 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002940 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2941 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002942 temp &= ~FDI_LINK_TRAIN_NONE;
2943 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002944 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002945
Chris Wilson5eddb702010-09-11 13:48:45 +01002946 reg = FDI_RX_CTL(pipe);
2947 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002948 temp &= ~FDI_LINK_TRAIN_NONE;
2949 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002950 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2951
2952 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002953 udelay(150);
2954
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002955 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002956 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2957 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2958 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002959
Chris Wilson5eddb702010-09-11 13:48:45 +01002960 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002961 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002962 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002963 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2964
2965 if ((temp & FDI_RX_BIT_LOCK)) {
2966 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002967 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002968 break;
2969 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002970 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002971 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002972 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002973
2974 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002975 reg = FDI_TX_CTL(pipe);
2976 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002977 temp &= ~FDI_LINK_TRAIN_NONE;
2978 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002979 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002980
Chris Wilson5eddb702010-09-11 13:48:45 +01002981 reg = FDI_RX_CTL(pipe);
2982 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002983 temp &= ~FDI_LINK_TRAIN_NONE;
2984 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002985 I915_WRITE(reg, temp);
2986
2987 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002988 udelay(150);
2989
Chris Wilson5eddb702010-09-11 13:48:45 +01002990 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002991 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002992 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002993 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2994
2995 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002996 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002997 DRM_DEBUG_KMS("FDI train 2 done.\n");
2998 break;
2999 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003000 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003001 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003002 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003003
3004 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003005
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003006}
3007
Akshay Joshi0206e352011-08-16 15:34:10 -04003008static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003009 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3010 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3011 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3012 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3013};
3014
3015/* The FDI link training functions for SNB/Cougarpoint. */
3016static void gen6_fdi_link_train(struct drm_crtc *crtc)
3017{
3018 struct drm_device *dev = crtc->dev;
3019 struct drm_i915_private *dev_priv = dev->dev_private;
3020 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3021 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003022 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003023
Adam Jacksone1a44742010-06-25 15:32:14 -04003024 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3025 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003026 reg = FDI_RX_IMR(pipe);
3027 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003028 temp &= ~FDI_RX_SYMBOL_LOCK;
3029 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003030 I915_WRITE(reg, temp);
3031
3032 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003033 udelay(150);
3034
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003035 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003036 reg = FDI_TX_CTL(pipe);
3037 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003038 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3039 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003040 temp &= ~FDI_LINK_TRAIN_NONE;
3041 temp |= FDI_LINK_TRAIN_PATTERN_1;
3042 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3043 /* SNB-B */
3044 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003045 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003046
Daniel Vetterd74cf322012-10-26 10:58:13 +02003047 I915_WRITE(FDI_RX_MISC(pipe),
3048 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3049
Chris Wilson5eddb702010-09-11 13:48:45 +01003050 reg = FDI_RX_CTL(pipe);
3051 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003052 if (HAS_PCH_CPT(dev)) {
3053 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3054 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3055 } else {
3056 temp &= ~FDI_LINK_TRAIN_NONE;
3057 temp |= FDI_LINK_TRAIN_PATTERN_1;
3058 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003059 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3060
3061 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003062 udelay(150);
3063
Akshay Joshi0206e352011-08-16 15:34:10 -04003064 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003065 reg = FDI_TX_CTL(pipe);
3066 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003067 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3068 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003069 I915_WRITE(reg, temp);
3070
3071 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003072 udelay(500);
3073
Sean Paulfa37d392012-03-02 12:53:39 -05003074 for (retry = 0; retry < 5; retry++) {
3075 reg = FDI_RX_IIR(pipe);
3076 temp = I915_READ(reg);
3077 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3078 if (temp & FDI_RX_BIT_LOCK) {
3079 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3080 DRM_DEBUG_KMS("FDI train 1 done.\n");
3081 break;
3082 }
3083 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003084 }
Sean Paulfa37d392012-03-02 12:53:39 -05003085 if (retry < 5)
3086 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003087 }
3088 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003089 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003090
3091 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003092 reg = FDI_TX_CTL(pipe);
3093 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003094 temp &= ~FDI_LINK_TRAIN_NONE;
3095 temp |= FDI_LINK_TRAIN_PATTERN_2;
3096 if (IS_GEN6(dev)) {
3097 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3098 /* SNB-B */
3099 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3100 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003101 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003102
Chris Wilson5eddb702010-09-11 13:48:45 +01003103 reg = FDI_RX_CTL(pipe);
3104 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003105 if (HAS_PCH_CPT(dev)) {
3106 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3107 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3108 } else {
3109 temp &= ~FDI_LINK_TRAIN_NONE;
3110 temp |= FDI_LINK_TRAIN_PATTERN_2;
3111 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003112 I915_WRITE(reg, temp);
3113
3114 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003115 udelay(150);
3116
Akshay Joshi0206e352011-08-16 15:34:10 -04003117 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003118 reg = FDI_TX_CTL(pipe);
3119 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003120 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3121 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003122 I915_WRITE(reg, temp);
3123
3124 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003125 udelay(500);
3126
Sean Paulfa37d392012-03-02 12:53:39 -05003127 for (retry = 0; retry < 5; retry++) {
3128 reg = FDI_RX_IIR(pipe);
3129 temp = I915_READ(reg);
3130 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3131 if (temp & FDI_RX_SYMBOL_LOCK) {
3132 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3133 DRM_DEBUG_KMS("FDI train 2 done.\n");
3134 break;
3135 }
3136 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003137 }
Sean Paulfa37d392012-03-02 12:53:39 -05003138 if (retry < 5)
3139 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003140 }
3141 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003142 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003143
3144 DRM_DEBUG_KMS("FDI train done.\n");
3145}
3146
Jesse Barnes357555c2011-04-28 15:09:55 -07003147/* Manual link training for Ivy Bridge A0 parts */
3148static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3149{
3150 struct drm_device *dev = crtc->dev;
3151 struct drm_i915_private *dev_priv = dev->dev_private;
3152 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3153 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003154 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003155
3156 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3157 for train result */
3158 reg = FDI_RX_IMR(pipe);
3159 temp = I915_READ(reg);
3160 temp &= ~FDI_RX_SYMBOL_LOCK;
3161 temp &= ~FDI_RX_BIT_LOCK;
3162 I915_WRITE(reg, temp);
3163
3164 POSTING_READ(reg);
3165 udelay(150);
3166
Daniel Vetter01a415f2012-10-27 15:58:40 +02003167 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3168 I915_READ(FDI_RX_IIR(pipe)));
3169
Jesse Barnes139ccd32013-08-19 11:04:55 -07003170 /* Try each vswing and preemphasis setting twice before moving on */
3171 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3172 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003173 reg = FDI_TX_CTL(pipe);
3174 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003175 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3176 temp &= ~FDI_TX_ENABLE;
3177 I915_WRITE(reg, temp);
3178
3179 reg = FDI_RX_CTL(pipe);
3180 temp = I915_READ(reg);
3181 temp &= ~FDI_LINK_TRAIN_AUTO;
3182 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3183 temp &= ~FDI_RX_ENABLE;
3184 I915_WRITE(reg, temp);
3185
3186 /* enable CPU FDI TX and PCH FDI RX */
3187 reg = FDI_TX_CTL(pipe);
3188 temp = I915_READ(reg);
3189 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3190 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3191 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003192 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003193 temp |= snb_b_fdi_train_param[j/2];
3194 temp |= FDI_COMPOSITE_SYNC;
3195 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3196
3197 I915_WRITE(FDI_RX_MISC(pipe),
3198 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3199
3200 reg = FDI_RX_CTL(pipe);
3201 temp = I915_READ(reg);
3202 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3203 temp |= FDI_COMPOSITE_SYNC;
3204 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3205
3206 POSTING_READ(reg);
3207 udelay(1); /* should be 0.5us */
3208
3209 for (i = 0; i < 4; i++) {
3210 reg = FDI_RX_IIR(pipe);
3211 temp = I915_READ(reg);
3212 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3213
3214 if (temp & FDI_RX_BIT_LOCK ||
3215 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3216 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3217 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3218 i);
3219 break;
3220 }
3221 udelay(1); /* should be 0.5us */
3222 }
3223 if (i == 4) {
3224 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3225 continue;
3226 }
3227
3228 /* Train 2 */
3229 reg = FDI_TX_CTL(pipe);
3230 temp = I915_READ(reg);
3231 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3232 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3233 I915_WRITE(reg, temp);
3234
3235 reg = FDI_RX_CTL(pipe);
3236 temp = I915_READ(reg);
3237 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3238 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003239 I915_WRITE(reg, temp);
3240
3241 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003242 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003243
Jesse Barnes139ccd32013-08-19 11:04:55 -07003244 for (i = 0; i < 4; i++) {
3245 reg = FDI_RX_IIR(pipe);
3246 temp = I915_READ(reg);
3247 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003248
Jesse Barnes139ccd32013-08-19 11:04:55 -07003249 if (temp & FDI_RX_SYMBOL_LOCK ||
3250 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3251 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3252 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3253 i);
3254 goto train_done;
3255 }
3256 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003257 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003258 if (i == 4)
3259 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003260 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003261
Jesse Barnes139ccd32013-08-19 11:04:55 -07003262train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003263 DRM_DEBUG_KMS("FDI train done.\n");
3264}
3265
Daniel Vetter88cefb62012-08-12 19:27:14 +02003266static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003267{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003268 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003269 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003270 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003271 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003272
Jesse Barnesc64e3112010-09-10 11:27:03 -07003273
Jesse Barnes0e23b992010-09-10 11:10:00 -07003274 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003275 reg = FDI_RX_CTL(pipe);
3276 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003277 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3278 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003279 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003280 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3281
3282 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003283 udelay(200);
3284
3285 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003286 temp = I915_READ(reg);
3287 I915_WRITE(reg, temp | FDI_PCDCLK);
3288
3289 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003290 udelay(200);
3291
Paulo Zanoni20749732012-11-23 15:30:38 -02003292 /* Enable CPU FDI TX PLL, always on for Ironlake */
3293 reg = FDI_TX_CTL(pipe);
3294 temp = I915_READ(reg);
3295 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3296 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003297
Paulo Zanoni20749732012-11-23 15:30:38 -02003298 POSTING_READ(reg);
3299 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003300 }
3301}
3302
Daniel Vetter88cefb62012-08-12 19:27:14 +02003303static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3304{
3305 struct drm_device *dev = intel_crtc->base.dev;
3306 struct drm_i915_private *dev_priv = dev->dev_private;
3307 int pipe = intel_crtc->pipe;
3308 u32 reg, temp;
3309
3310 /* Switch from PCDclk to Rawclk */
3311 reg = FDI_RX_CTL(pipe);
3312 temp = I915_READ(reg);
3313 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3314
3315 /* Disable CPU FDI TX PLL */
3316 reg = FDI_TX_CTL(pipe);
3317 temp = I915_READ(reg);
3318 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3319
3320 POSTING_READ(reg);
3321 udelay(100);
3322
3323 reg = FDI_RX_CTL(pipe);
3324 temp = I915_READ(reg);
3325 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3326
3327 /* Wait for the clocks to turn off. */
3328 POSTING_READ(reg);
3329 udelay(100);
3330}
3331
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003332static void ironlake_fdi_disable(struct drm_crtc *crtc)
3333{
3334 struct drm_device *dev = crtc->dev;
3335 struct drm_i915_private *dev_priv = dev->dev_private;
3336 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3337 int pipe = intel_crtc->pipe;
3338 u32 reg, temp;
3339
3340 /* disable CPU FDI tx and PCH FDI rx */
3341 reg = FDI_TX_CTL(pipe);
3342 temp = I915_READ(reg);
3343 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3344 POSTING_READ(reg);
3345
3346 reg = FDI_RX_CTL(pipe);
3347 temp = I915_READ(reg);
3348 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003349 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003350 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3351
3352 POSTING_READ(reg);
3353 udelay(100);
3354
3355 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003356 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003357 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003358
3359 /* still set train pattern 1 */
3360 reg = FDI_TX_CTL(pipe);
3361 temp = I915_READ(reg);
3362 temp &= ~FDI_LINK_TRAIN_NONE;
3363 temp |= FDI_LINK_TRAIN_PATTERN_1;
3364 I915_WRITE(reg, temp);
3365
3366 reg = FDI_RX_CTL(pipe);
3367 temp = I915_READ(reg);
3368 if (HAS_PCH_CPT(dev)) {
3369 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3370 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3371 } else {
3372 temp &= ~FDI_LINK_TRAIN_NONE;
3373 temp |= FDI_LINK_TRAIN_PATTERN_1;
3374 }
3375 /* BPC in FDI rx is consistent with that in PIPECONF */
3376 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003377 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003378 I915_WRITE(reg, temp);
3379
3380 POSTING_READ(reg);
3381 udelay(100);
3382}
3383
Chris Wilson5dce5b932014-01-20 10:17:36 +00003384bool intel_has_pending_fb_unpin(struct drm_device *dev)
3385{
3386 struct intel_crtc *crtc;
3387
3388 /* Note that we don't need to be called with mode_config.lock here
3389 * as our list of CRTC objects is static for the lifetime of the
3390 * device and so cannot disappear as we iterate. Similarly, we can
3391 * happily treat the predicates as racy, atomic checks as userspace
3392 * cannot claim and pin a new fb without at least acquring the
3393 * struct_mutex and so serialising with us.
3394 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003395 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003396 if (atomic_read(&crtc->unpin_work_count) == 0)
3397 continue;
3398
3399 if (crtc->unpin_work)
3400 intel_wait_for_vblank(dev, crtc->pipe);
3401
3402 return true;
3403 }
3404
3405 return false;
3406}
3407
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003408static void page_flip_completed(struct intel_crtc *intel_crtc)
3409{
3410 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3411 struct intel_unpin_work *work = intel_crtc->unpin_work;
3412
3413 /* ensure that the unpin work is consistent wrt ->pending. */
3414 smp_rmb();
3415 intel_crtc->unpin_work = NULL;
3416
3417 if (work->event)
3418 drm_send_vblank_event(intel_crtc->base.dev,
3419 intel_crtc->pipe,
3420 work->event);
3421
3422 drm_crtc_vblank_put(&intel_crtc->base);
3423
3424 wake_up_all(&dev_priv->pending_flip_queue);
3425 queue_work(dev_priv->wq, &work->work);
3426
3427 trace_i915_flip_complete(intel_crtc->plane,
3428 work->pending_flip_obj);
3429}
3430
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003431void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003432{
Chris Wilson0f911282012-04-17 10:05:38 +01003433 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003434 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003435
Daniel Vetter2c10d572012-12-20 21:24:07 +01003436 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003437 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3438 !intel_crtc_has_pending_flip(crtc),
3439 60*HZ) == 0)) {
3440 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3441 unsigned long flags;
Daniel Vetter2c10d572012-12-20 21:24:07 +01003442
Chris Wilson9c787942014-09-05 07:13:25 +01003443 spin_lock_irqsave(&dev->event_lock, flags);
3444 if (intel_crtc->unpin_work) {
3445 WARN_ONCE(1, "Removing stuck page flip\n");
3446 page_flip_completed(intel_crtc);
3447 }
3448 spin_unlock_irqrestore(&dev->event_lock, flags);
3449 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003450
Chris Wilson975d5682014-08-20 13:13:34 +01003451 if (crtc->primary->fb) {
3452 mutex_lock(&dev->struct_mutex);
3453 intel_finish_fb(crtc->primary->fb);
3454 mutex_unlock(&dev->struct_mutex);
3455 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003456}
3457
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003458/* Program iCLKIP clock to the desired frequency */
3459static void lpt_program_iclkip(struct drm_crtc *crtc)
3460{
3461 struct drm_device *dev = crtc->dev;
3462 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01003463 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003464 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3465 u32 temp;
3466
Daniel Vetter09153002012-12-12 14:06:44 +01003467 mutex_lock(&dev_priv->dpio_lock);
3468
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003469 /* It is necessary to ungate the pixclk gate prior to programming
3470 * the divisors, and gate it back when it is done.
3471 */
3472 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3473
3474 /* Disable SSCCTL */
3475 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003476 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3477 SBI_SSCCTL_DISABLE,
3478 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003479
3480 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003481 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003482 auxdiv = 1;
3483 divsel = 0x41;
3484 phaseinc = 0x20;
3485 } else {
3486 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003487 * but the adjusted_mode->crtc_clock in in KHz. To get the
3488 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003489 * convert the virtual clock precision to KHz here for higher
3490 * precision.
3491 */
3492 u32 iclk_virtual_root_freq = 172800 * 1000;
3493 u32 iclk_pi_range = 64;
3494 u32 desired_divisor, msb_divisor_value, pi_value;
3495
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003496 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003497 msb_divisor_value = desired_divisor / iclk_pi_range;
3498 pi_value = desired_divisor % iclk_pi_range;
3499
3500 auxdiv = 0;
3501 divsel = msb_divisor_value - 2;
3502 phaseinc = pi_value;
3503 }
3504
3505 /* This should not happen with any sane values */
3506 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3507 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3508 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3509 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3510
3511 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 +03003512 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003513 auxdiv,
3514 divsel,
3515 phasedir,
3516 phaseinc);
3517
3518 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003519 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003520 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3521 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3522 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3523 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3524 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3525 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003526 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003527
3528 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003529 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003530 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3531 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003532 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003533
3534 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003535 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003536 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003537 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003538
3539 /* Wait for initialization time */
3540 udelay(24);
3541
3542 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003543
3544 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003545}
3546
Daniel Vetter275f01b22013-05-03 11:49:47 +02003547static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3548 enum pipe pch_transcoder)
3549{
3550 struct drm_device *dev = crtc->base.dev;
3551 struct drm_i915_private *dev_priv = dev->dev_private;
3552 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3553
3554 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3555 I915_READ(HTOTAL(cpu_transcoder)));
3556 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3557 I915_READ(HBLANK(cpu_transcoder)));
3558 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3559 I915_READ(HSYNC(cpu_transcoder)));
3560
3561 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3562 I915_READ(VTOTAL(cpu_transcoder)));
3563 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3564 I915_READ(VBLANK(cpu_transcoder)));
3565 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3566 I915_READ(VSYNC(cpu_transcoder)));
3567 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3568 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3569}
3570
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003571static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3572{
3573 struct drm_i915_private *dev_priv = dev->dev_private;
3574 uint32_t temp;
3575
3576 temp = I915_READ(SOUTH_CHICKEN1);
3577 if (temp & FDI_BC_BIFURCATION_SELECT)
3578 return;
3579
3580 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3581 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3582
3583 temp |= FDI_BC_BIFURCATION_SELECT;
3584 DRM_DEBUG_KMS("enabling fdi C rx\n");
3585 I915_WRITE(SOUTH_CHICKEN1, temp);
3586 POSTING_READ(SOUTH_CHICKEN1);
3587}
3588
3589static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3590{
3591 struct drm_device *dev = intel_crtc->base.dev;
3592 struct drm_i915_private *dev_priv = dev->dev_private;
3593
3594 switch (intel_crtc->pipe) {
3595 case PIPE_A:
3596 break;
3597 case PIPE_B:
3598 if (intel_crtc->config.fdi_lanes > 2)
3599 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3600 else
3601 cpt_enable_fdi_bc_bifurcation(dev);
3602
3603 break;
3604 case PIPE_C:
3605 cpt_enable_fdi_bc_bifurcation(dev);
3606
3607 break;
3608 default:
3609 BUG();
3610 }
3611}
3612
Jesse Barnesf67a5592011-01-05 10:31:48 -08003613/*
3614 * Enable PCH resources required for PCH ports:
3615 * - PCH PLLs
3616 * - FDI training & RX/TX
3617 * - update transcoder timings
3618 * - DP transcoding bits
3619 * - transcoder
3620 */
3621static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003622{
3623 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003624 struct drm_i915_private *dev_priv = dev->dev_private;
3625 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3626 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003627 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003628
Daniel Vetterab9412b2013-05-03 11:49:46 +02003629 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003630
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003631 if (IS_IVYBRIDGE(dev))
3632 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3633
Daniel Vettercd986ab2012-10-26 10:58:12 +02003634 /* Write the TU size bits before fdi link training, so that error
3635 * detection works. */
3636 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3637 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3638
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003639 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003640 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003641
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003642 /* We need to program the right clock selection before writing the pixel
3643 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003644 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003645 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003646
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003647 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003648 temp |= TRANS_DPLL_ENABLE(pipe);
3649 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003650 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003651 temp |= sel;
3652 else
3653 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003654 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003655 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003656
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003657 /* XXX: pch pll's can be enabled any time before we enable the PCH
3658 * transcoder, and we actually should do this to not upset any PCH
3659 * transcoder that already use the clock when we share it.
3660 *
3661 * Note that enable_shared_dpll tries to do the right thing, but
3662 * get_shared_dpll unconditionally resets the pll - we need that to have
3663 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02003664 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003665
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003666 /* set transcoder timing, panel must allow it */
3667 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003668 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003669
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003670 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003671
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003672 /* For PCH DP, enable TRANS_DP_CTL */
3673 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003674 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3675 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003676 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003677 reg = TRANS_DP_CTL(pipe);
3678 temp = I915_READ(reg);
3679 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003680 TRANS_DP_SYNC_MASK |
3681 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003682 temp |= (TRANS_DP_OUTPUT_ENABLE |
3683 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003684 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003685
3686 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003687 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003688 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003689 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003690
3691 switch (intel_trans_dp_port_sel(crtc)) {
3692 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003693 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003694 break;
3695 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003696 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003697 break;
3698 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003699 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003700 break;
3701 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003702 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003703 }
3704
Chris Wilson5eddb702010-09-11 13:48:45 +01003705 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003706 }
3707
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003708 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003709}
3710
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003711static void lpt_pch_enable(struct drm_crtc *crtc)
3712{
3713 struct drm_device *dev = crtc->dev;
3714 struct drm_i915_private *dev_priv = dev->dev_private;
3715 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003716 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003717
Daniel Vetterab9412b2013-05-03 11:49:46 +02003718 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003719
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003720 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003721
Paulo Zanoni0540e482012-10-31 18:12:40 -02003722 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003723 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003724
Paulo Zanoni937bb612012-10-31 18:12:47 -02003725 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003726}
3727
Daniel Vetter716c2e52014-06-25 22:02:02 +03003728void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003729{
Daniel Vettere2b78262013-06-07 23:10:03 +02003730 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003731
3732 if (pll == NULL)
3733 return;
3734
3735 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003736 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003737 return;
3738 }
3739
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003740 if (--pll->refcount == 0) {
3741 WARN_ON(pll->on);
3742 WARN_ON(pll->active);
3743 }
3744
Daniel Vettera43f6e02013-06-07 23:10:32 +02003745 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003746}
3747
Daniel Vetter716c2e52014-06-25 22:02:02 +03003748struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003749{
Daniel Vettere2b78262013-06-07 23:10:03 +02003750 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3751 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3752 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003753
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003754 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003755 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3756 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003757 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003758 }
3759
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003760 if (HAS_PCH_IBX(dev_priv->dev)) {
3761 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003762 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003763 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003764
Daniel Vetter46edb022013-06-05 13:34:12 +02003765 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3766 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003767
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003768 WARN_ON(pll->refcount);
3769
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003770 goto found;
3771 }
3772
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003773 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3774 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003775
3776 /* Only want to check enabled timings first */
3777 if (pll->refcount == 0)
3778 continue;
3779
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003780 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3781 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003782 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003783 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003784 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003785
3786 goto found;
3787 }
3788 }
3789
3790 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003791 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3792 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003793 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003794 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3795 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003796 goto found;
3797 }
3798 }
3799
3800 return NULL;
3801
3802found:
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003803 if (pll->refcount == 0)
3804 pll->hw_state = crtc->config.dpll_hw_state;
3805
Daniel Vettera43f6e02013-06-07 23:10:32 +02003806 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003807 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3808 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003809
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003810 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003811
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003812 return pll;
3813}
3814
Daniel Vettera1520312013-05-03 11:49:50 +02003815static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003816{
3817 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003818 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003819 u32 temp;
3820
3821 temp = I915_READ(dslreg);
3822 udelay(500);
3823 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003824 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003825 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003826 }
3827}
3828
Jesse Barnesb074cec2013-04-25 12:55:02 -07003829static void ironlake_pfit_enable(struct intel_crtc *crtc)
3830{
3831 struct drm_device *dev = crtc->base.dev;
3832 struct drm_i915_private *dev_priv = dev->dev_private;
3833 int pipe = crtc->pipe;
3834
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003835 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003836 /* Force use of hard-coded filter coefficients
3837 * as some pre-programmed values are broken,
3838 * e.g. x201.
3839 */
3840 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3841 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3842 PF_PIPE_SEL_IVB(pipe));
3843 else
3844 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3845 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3846 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003847 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003848}
3849
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003850static void intel_enable_planes(struct drm_crtc *crtc)
3851{
3852 struct drm_device *dev = crtc->dev;
3853 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003854 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003855 struct intel_plane *intel_plane;
3856
Matt Roperaf2b6532014-04-01 15:22:32 -07003857 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3858 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003859 if (intel_plane->pipe == pipe)
3860 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003861 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003862}
3863
3864static void intel_disable_planes(struct drm_crtc *crtc)
3865{
3866 struct drm_device *dev = crtc->dev;
3867 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003868 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003869 struct intel_plane *intel_plane;
3870
Matt Roperaf2b6532014-04-01 15:22:32 -07003871 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3872 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003873 if (intel_plane->pipe == pipe)
3874 intel_plane_disable(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003875 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003876}
3877
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003878void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003879{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003880 struct drm_device *dev = crtc->base.dev;
3881 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03003882
3883 if (!crtc->config.ips_enabled)
3884 return;
3885
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003886 /* We can only enable IPS after we enable a plane and wait for a vblank */
3887 intel_wait_for_vblank(dev, crtc->pipe);
3888
Paulo Zanonid77e4532013-09-24 13:52:55 -03003889 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003890 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003891 mutex_lock(&dev_priv->rps.hw_lock);
3892 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3893 mutex_unlock(&dev_priv->rps.hw_lock);
3894 /* Quoting Art Runyan: "its not safe to expect any particular
3895 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08003896 * mailbox." Moreover, the mailbox may return a bogus state,
3897 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003898 */
3899 } else {
3900 I915_WRITE(IPS_CTL, IPS_ENABLE);
3901 /* The bit only becomes 1 in the next vblank, so this wait here
3902 * is essentially intel_wait_for_vblank. If we don't have this
3903 * and don't wait for vblanks until the end of crtc_enable, then
3904 * the HW state readout code will complain that the expected
3905 * IPS_CTL value is not the one we read. */
3906 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3907 DRM_ERROR("Timed out waiting for IPS enable\n");
3908 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003909}
3910
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003911void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003912{
3913 struct drm_device *dev = crtc->base.dev;
3914 struct drm_i915_private *dev_priv = dev->dev_private;
3915
3916 if (!crtc->config.ips_enabled)
3917 return;
3918
3919 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07003920 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003921 mutex_lock(&dev_priv->rps.hw_lock);
3922 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3923 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07003924 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3925 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3926 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08003927 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003928 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08003929 POSTING_READ(IPS_CTL);
3930 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003931
3932 /* We need to wait for a vblank before we can disable the plane. */
3933 intel_wait_for_vblank(dev, crtc->pipe);
3934}
3935
3936/** Loads the palette/gamma unit for the CRTC with the prepared values */
3937static void intel_crtc_load_lut(struct drm_crtc *crtc)
3938{
3939 struct drm_device *dev = crtc->dev;
3940 struct drm_i915_private *dev_priv = dev->dev_private;
3941 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3942 enum pipe pipe = intel_crtc->pipe;
3943 int palreg = PALETTE(pipe);
3944 int i;
3945 bool reenable_ips = false;
3946
3947 /* The clocks have to be on to load the palette. */
3948 if (!crtc->enabled || !intel_crtc->active)
3949 return;
3950
3951 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3952 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3953 assert_dsi_pll_enabled(dev_priv);
3954 else
3955 assert_pll_enabled(dev_priv, pipe);
3956 }
3957
3958 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05303959 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03003960 palreg = LGC_PALETTE(pipe);
3961
3962 /* Workaround : Do not read or write the pipe palette/gamma data while
3963 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3964 */
Paulo Zanoni41e6fc42014-01-08 17:26:31 -02003965 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03003966 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3967 GAMMA_MODE_MODE_SPLIT)) {
3968 hsw_disable_ips(intel_crtc);
3969 reenable_ips = true;
3970 }
3971
3972 for (i = 0; i < 256; i++) {
3973 I915_WRITE(palreg + 4 * i,
3974 (intel_crtc->lut_r[i] << 16) |
3975 (intel_crtc->lut_g[i] << 8) |
3976 intel_crtc->lut_b[i]);
3977 }
3978
3979 if (reenable_ips)
3980 hsw_enable_ips(intel_crtc);
3981}
3982
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003983static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3984{
3985 if (!enable && intel_crtc->overlay) {
3986 struct drm_device *dev = intel_crtc->base.dev;
3987 struct drm_i915_private *dev_priv = dev->dev_private;
3988
3989 mutex_lock(&dev->struct_mutex);
3990 dev_priv->mm.interruptible = false;
3991 (void) intel_overlay_switch_off(intel_crtc->overlay);
3992 dev_priv->mm.interruptible = true;
3993 mutex_unlock(&dev->struct_mutex);
3994 }
3995
3996 /* Let userspace switch the overlay on again. In most cases userspace
3997 * has to recompute where to put it anyway.
3998 */
3999}
4000
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004001static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004002{
4003 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004004 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4005 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004006
Ville Syrjälä08c71e52014-08-06 14:49:45 +03004007 assert_vblank_disabled(crtc);
4008
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004009 drm_vblank_on(dev, pipe);
4010
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004011 intel_enable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004012 intel_enable_planes(crtc);
4013 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004014 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004015
4016 hsw_enable_ips(intel_crtc);
4017
4018 mutex_lock(&dev->struct_mutex);
4019 intel_update_fbc(dev);
4020 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004021
4022 /*
4023 * FIXME: Once we grow proper nuclear flip support out of this we need
4024 * to compute the mask of flip planes precisely. For the time being
4025 * consider this a flip from a NULL plane.
4026 */
4027 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004028}
4029
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004030static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004031{
4032 struct drm_device *dev = crtc->dev;
4033 struct drm_i915_private *dev_priv = dev->dev_private;
4034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4035 int pipe = intel_crtc->pipe;
4036 int plane = intel_crtc->plane;
4037
4038 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004039
4040 if (dev_priv->fbc.plane == plane)
4041 intel_disable_fbc(dev);
4042
4043 hsw_disable_ips(intel_crtc);
4044
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004045 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004046 intel_crtc_update_cursor(crtc, false);
4047 intel_disable_planes(crtc);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004048 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004049
Daniel Vetterf99d7062014-06-19 16:01:59 +02004050 /*
4051 * FIXME: Once we grow proper nuclear flip support out of this we need
4052 * to compute the mask of flip planes precisely. For the time being
4053 * consider this a flip to a NULL plane.
4054 */
4055 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
4056
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004057 drm_vblank_off(dev, pipe);
Ville Syrjälä08c71e52014-08-06 14:49:45 +03004058
4059 assert_vblank_disabled(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004060}
4061
Jesse Barnesf67a5592011-01-05 10:31:48 -08004062static void ironlake_crtc_enable(struct drm_crtc *crtc)
4063{
4064 struct drm_device *dev = crtc->dev;
4065 struct drm_i915_private *dev_priv = dev->dev_private;
4066 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004067 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004068 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004069
Daniel Vetter08a48462012-07-02 11:43:47 +02004070 WARN_ON(!crtc->enabled);
4071
Jesse Barnesf67a5592011-01-05 10:31:48 -08004072 if (intel_crtc->active)
4073 return;
4074
Daniel Vetterb14b1052014-04-24 23:55:13 +02004075 if (intel_crtc->config.has_pch_encoder)
4076 intel_prepare_shared_dpll(intel_crtc);
4077
Daniel Vetter29407aa2014-04-24 23:55:08 +02004078 if (intel_crtc->config.has_dp_encoder)
4079 intel_dp_set_m_n(intel_crtc);
4080
4081 intel_set_pipe_timings(intel_crtc);
4082
4083 if (intel_crtc->config.has_pch_encoder) {
4084 intel_cpu_transcoder_set_m_n(intel_crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07004085 &intel_crtc->config.fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004086 }
4087
4088 ironlake_set_pipeconf(crtc);
4089
Jesse Barnesf67a5592011-01-05 10:31:48 -08004090 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004091
4092 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4093 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
4094
Daniel Vetterf6736a12013-06-05 13:34:30 +02004095 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004096 if (encoder->pre_enable)
4097 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004098
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004099 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004100 /* Note: FDI PLL enabling _must_ be done before we enable the
4101 * cpu pipes, hence this is separate from all the other fdi/pch
4102 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004103 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004104 } else {
4105 assert_fdi_tx_disabled(dev_priv, pipe);
4106 assert_fdi_rx_disabled(dev_priv, pipe);
4107 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004108
Jesse Barnesb074cec2013-04-25 12:55:02 -07004109 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004110
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004111 /*
4112 * On ILK+ LUT must be loaded before the pipe is running but with
4113 * clocks enabled
4114 */
4115 intel_crtc_load_lut(crtc);
4116
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004117 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004118 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004119
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004120 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004121 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004122
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004123 for_each_encoder_on_crtc(dev, crtc, encoder)
4124 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004125
4126 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004127 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004128
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004129 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004130}
4131
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004132/* IPS only exists on ULT machines and is tied to pipe A. */
4133static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4134{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004135 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004136}
4137
Paulo Zanonie4916942013-09-20 16:21:19 -03004138/*
4139 * This implements the workaround described in the "notes" section of the mode
4140 * set sequence documentation. When going from no pipes or single pipe to
4141 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4142 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4143 */
4144static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4145{
4146 struct drm_device *dev = crtc->base.dev;
4147 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4148
4149 /* We want to get the other_active_crtc only if there's only 1 other
4150 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004151 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004152 if (!crtc_it->active || crtc_it == crtc)
4153 continue;
4154
4155 if (other_active_crtc)
4156 return;
4157
4158 other_active_crtc = crtc_it;
4159 }
4160 if (!other_active_crtc)
4161 return;
4162
4163 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4164 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4165}
4166
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004167static void haswell_crtc_enable(struct drm_crtc *crtc)
4168{
4169 struct drm_device *dev = crtc->dev;
4170 struct drm_i915_private *dev_priv = dev->dev_private;
4171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4172 struct intel_encoder *encoder;
4173 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004174
4175 WARN_ON(!crtc->enabled);
4176
4177 if (intel_crtc->active)
4178 return;
4179
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004180 if (intel_crtc_to_shared_dpll(intel_crtc))
4181 intel_enable_shared_dpll(intel_crtc);
4182
Daniel Vetter229fca92014-04-24 23:55:09 +02004183 if (intel_crtc->config.has_dp_encoder)
4184 intel_dp_set_m_n(intel_crtc);
4185
4186 intel_set_pipe_timings(intel_crtc);
4187
4188 if (intel_crtc->config.has_pch_encoder) {
4189 intel_cpu_transcoder_set_m_n(intel_crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07004190 &intel_crtc->config.fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004191 }
4192
4193 haswell_set_pipeconf(crtc);
4194
4195 intel_set_pipe_csc(crtc);
4196
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004197 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004198
4199 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004200 for_each_encoder_on_crtc(dev, crtc, encoder)
4201 if (encoder->pre_enable)
4202 encoder->pre_enable(encoder);
4203
Imre Deak4fe94672014-06-25 22:01:49 +03004204 if (intel_crtc->config.has_pch_encoder) {
4205 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4206 dev_priv->display.fdi_link_train(crtc);
4207 }
4208
Paulo Zanoni1f544382012-10-24 11:32:00 -02004209 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004210
Jesse Barnesb074cec2013-04-25 12:55:02 -07004211 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004212
4213 /*
4214 * On ILK+ LUT must be loaded before the pipe is running but with
4215 * clocks enabled
4216 */
4217 intel_crtc_load_lut(crtc);
4218
Paulo Zanoni1f544382012-10-24 11:32:00 -02004219 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004220 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004221
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004222 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004223 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004224
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004225 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004226 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004227
Dave Airlie0e32b392014-05-02 14:02:48 +10004228 if (intel_crtc->config.dp_encoder_is_mst)
4229 intel_ddi_set_vc_payload_alloc(crtc, true);
4230
Jani Nikula8807e552013-08-30 19:40:32 +03004231 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004232 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004233 intel_opregion_notify_encoder(encoder, true);
4234 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004235
Paulo Zanonie4916942013-09-20 16:21:19 -03004236 /* If we change the relative order between pipe/planes enabling, we need
4237 * to change the workaround. */
4238 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004239 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004240}
4241
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004242static void ironlake_pfit_disable(struct intel_crtc *crtc)
4243{
4244 struct drm_device *dev = crtc->base.dev;
4245 struct drm_i915_private *dev_priv = dev->dev_private;
4246 int pipe = crtc->pipe;
4247
4248 /* To avoid upsetting the power well on haswell only disable the pfit if
4249 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01004250 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004251 I915_WRITE(PF_CTL(pipe), 0);
4252 I915_WRITE(PF_WIN_POS(pipe), 0);
4253 I915_WRITE(PF_WIN_SZ(pipe), 0);
4254 }
4255}
4256
Jesse Barnes6be4a602010-09-10 10:26:01 -07004257static void ironlake_crtc_disable(struct drm_crtc *crtc)
4258{
4259 struct drm_device *dev = crtc->dev;
4260 struct drm_i915_private *dev_priv = dev->dev_private;
4261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004262 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004263 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004264 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004265
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004266 if (!intel_crtc->active)
4267 return;
4268
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004269 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004270
Daniel Vetterea9d7582012-07-10 10:42:52 +02004271 for_each_encoder_on_crtc(dev, crtc, encoder)
4272 encoder->disable(encoder);
4273
Daniel Vetterd925c592013-06-05 13:34:04 +02004274 if (intel_crtc->config.has_pch_encoder)
4275 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4276
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004277 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004278
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004279 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004280
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004281 for_each_encoder_on_crtc(dev, crtc, encoder)
4282 if (encoder->post_disable)
4283 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004284
Daniel Vetterd925c592013-06-05 13:34:04 +02004285 if (intel_crtc->config.has_pch_encoder) {
4286 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004287
Daniel Vetterd925c592013-06-05 13:34:04 +02004288 ironlake_disable_pch_transcoder(dev_priv, pipe);
4289 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004290
Daniel Vetterd925c592013-06-05 13:34:04 +02004291 if (HAS_PCH_CPT(dev)) {
4292 /* disable TRANS_DP_CTL */
4293 reg = TRANS_DP_CTL(pipe);
4294 temp = I915_READ(reg);
4295 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4296 TRANS_DP_PORT_SEL_MASK);
4297 temp |= TRANS_DP_PORT_SEL_NONE;
4298 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004299
Daniel Vetterd925c592013-06-05 13:34:04 +02004300 /* disable DPLL_SEL */
4301 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004302 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004303 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004304 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004305
4306 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004307 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004308
4309 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004310 }
4311
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004312 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004313 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004314
4315 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004316 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004317 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004318}
4319
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004320static void haswell_crtc_disable(struct drm_crtc *crtc)
4321{
4322 struct drm_device *dev = crtc->dev;
4323 struct drm_i915_private *dev_priv = dev->dev_private;
4324 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4325 struct intel_encoder *encoder;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004326 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004327
4328 if (!intel_crtc->active)
4329 return;
4330
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004331 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004332
Jani Nikula8807e552013-08-30 19:40:32 +03004333 for_each_encoder_on_crtc(dev, crtc, encoder) {
4334 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004335 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004336 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004337
Paulo Zanoni86642812013-04-12 17:57:57 -03004338 if (intel_crtc->config.has_pch_encoder)
4339 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004340 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004341
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03004342 if (intel_crtc->config.dp_encoder_is_mst)
4343 intel_ddi_set_vc_payload_alloc(crtc, false);
4344
Paulo Zanoniad80a812012-10-24 16:06:19 -02004345 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004346
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004347 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004348
Paulo Zanoni1f544382012-10-24 11:32:00 -02004349 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004350
Daniel Vetter88adfff2013-03-28 10:42:01 +01004351 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004352 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03004353 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004354 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004355 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004356
Imre Deak97b040a2014-06-25 22:01:50 +03004357 for_each_encoder_on_crtc(dev, crtc, encoder)
4358 if (encoder->post_disable)
4359 encoder->post_disable(encoder);
4360
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004361 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004362 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004363
4364 mutex_lock(&dev->struct_mutex);
4365 intel_update_fbc(dev);
4366 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004367
4368 if (intel_crtc_to_shared_dpll(intel_crtc))
4369 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004370}
4371
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004372static void ironlake_crtc_off(struct drm_crtc *crtc)
4373{
4374 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004375 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004376}
4377
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004378
Jesse Barnes2dd24552013-04-25 12:55:01 -07004379static void i9xx_pfit_enable(struct intel_crtc *crtc)
4380{
4381 struct drm_device *dev = crtc->base.dev;
4382 struct drm_i915_private *dev_priv = dev->dev_private;
4383 struct intel_crtc_config *pipe_config = &crtc->config;
4384
Daniel Vetter328d8e82013-05-08 10:36:31 +02004385 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004386 return;
4387
Daniel Vetterc0b03412013-05-28 12:05:54 +02004388 /*
4389 * The panel fitter should only be adjusted whilst the pipe is disabled,
4390 * according to register description and PRM.
4391 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004392 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4393 assert_pipe_disabled(dev_priv, crtc->pipe);
4394
Jesse Barnesb074cec2013-04-25 12:55:02 -07004395 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4396 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004397
4398 /* Border color in case we don't scale up to the full screen. Black by
4399 * default, change to something else for debugging. */
4400 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004401}
4402
Dave Airlied05410f2014-06-05 13:22:59 +10004403static enum intel_display_power_domain port_to_power_domain(enum port port)
4404{
4405 switch (port) {
4406 case PORT_A:
4407 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4408 case PORT_B:
4409 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4410 case PORT_C:
4411 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4412 case PORT_D:
4413 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4414 default:
4415 WARN_ON_ONCE(1);
4416 return POWER_DOMAIN_PORT_OTHER;
4417 }
4418}
4419
Imre Deak77d22dc2014-03-05 16:20:52 +02004420#define for_each_power_domain(domain, mask) \
4421 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4422 if ((1 << (domain)) & (mask))
4423
Imre Deak319be8a2014-03-04 19:22:57 +02004424enum intel_display_power_domain
4425intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004426{
Imre Deak319be8a2014-03-04 19:22:57 +02004427 struct drm_device *dev = intel_encoder->base.dev;
4428 struct intel_digital_port *intel_dig_port;
4429
4430 switch (intel_encoder->type) {
4431 case INTEL_OUTPUT_UNKNOWN:
4432 /* Only DDI platforms should ever use this output type */
4433 WARN_ON_ONCE(!HAS_DDI(dev));
4434 case INTEL_OUTPUT_DISPLAYPORT:
4435 case INTEL_OUTPUT_HDMI:
4436 case INTEL_OUTPUT_EDP:
4437 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10004438 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10004439 case INTEL_OUTPUT_DP_MST:
4440 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4441 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02004442 case INTEL_OUTPUT_ANALOG:
4443 return POWER_DOMAIN_PORT_CRT;
4444 case INTEL_OUTPUT_DSI:
4445 return POWER_DOMAIN_PORT_DSI;
4446 default:
4447 return POWER_DOMAIN_PORT_OTHER;
4448 }
4449}
4450
4451static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4452{
4453 struct drm_device *dev = crtc->dev;
4454 struct intel_encoder *intel_encoder;
4455 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4456 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02004457 unsigned long mask;
4458 enum transcoder transcoder;
4459
4460 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4461
4462 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4463 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Daniel Vetterfabf6e52014-05-29 14:10:22 +02004464 if (intel_crtc->config.pch_pfit.enabled ||
4465 intel_crtc->config.pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02004466 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4467
Imre Deak319be8a2014-03-04 19:22:57 +02004468 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4469 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4470
Imre Deak77d22dc2014-03-05 16:20:52 +02004471 return mask;
4472}
4473
4474void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4475 bool enable)
4476{
4477 if (dev_priv->power_domains.init_power_on == enable)
4478 return;
4479
4480 if (enable)
4481 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4482 else
4483 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4484
4485 dev_priv->power_domains.init_power_on = enable;
4486}
4487
4488static void modeset_update_crtc_power_domains(struct drm_device *dev)
4489{
4490 struct drm_i915_private *dev_priv = dev->dev_private;
4491 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4492 struct intel_crtc *crtc;
4493
4494 /*
4495 * First get all needed power domains, then put all unneeded, to avoid
4496 * any unnecessary toggling of the power wells.
4497 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004498 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004499 enum intel_display_power_domain domain;
4500
4501 if (!crtc->base.enabled)
4502 continue;
4503
Imre Deak319be8a2014-03-04 19:22:57 +02004504 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02004505
4506 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4507 intel_display_power_get(dev_priv, domain);
4508 }
4509
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004510 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004511 enum intel_display_power_domain domain;
4512
4513 for_each_power_domain(domain, crtc->enabled_power_domains)
4514 intel_display_power_put(dev_priv, domain);
4515
4516 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4517 }
4518
4519 intel_display_set_init_power(dev_priv, false);
4520}
4521
Ville Syrjälädfcab172014-06-13 13:37:47 +03004522/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004523static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004524{
Jesse Barnes586f49d2013-11-04 16:06:59 -08004525 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08004526
Jesse Barnes586f49d2013-11-04 16:06:59 -08004527 /* Obtain SKU information */
4528 mutex_lock(&dev_priv->dpio_lock);
4529 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4530 CCK_FUSE_HPLL_FREQ_MASK;
4531 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004532
Ville Syrjälädfcab172014-06-13 13:37:47 +03004533 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004534}
4535
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004536static void vlv_update_cdclk(struct drm_device *dev)
4537{
4538 struct drm_i915_private *dev_priv = dev->dev_private;
4539
4540 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
4541 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
4542 dev_priv->vlv_cdclk_freq);
4543
4544 /*
4545 * Program the gmbus_freq based on the cdclk frequency.
4546 * BSpec erroneously claims we should aim for 4MHz, but
4547 * in fact 1MHz is the correct frequency.
4548 */
4549 I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
4550}
4551
Jesse Barnes30a970c2013-11-04 13:48:12 -08004552/* Adjust CDclk dividers to allow high res or save power if possible */
4553static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4554{
4555 struct drm_i915_private *dev_priv = dev->dev_private;
4556 u32 val, cmd;
4557
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03004558 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02004559
Ville Syrjälädfcab172014-06-13 13:37:47 +03004560 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08004561 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03004562 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004563 cmd = 1;
4564 else
4565 cmd = 0;
4566
4567 mutex_lock(&dev_priv->rps.hw_lock);
4568 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4569 val &= ~DSPFREQGUAR_MASK;
4570 val |= (cmd << DSPFREQGUAR_SHIFT);
4571 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4572 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4573 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4574 50)) {
4575 DRM_ERROR("timed out waiting for CDclk change\n");
4576 }
4577 mutex_unlock(&dev_priv->rps.hw_lock);
4578
Ville Syrjälädfcab172014-06-13 13:37:47 +03004579 if (cdclk == 400000) {
Jesse Barnes30a970c2013-11-04 13:48:12 -08004580 u32 divider, vco;
4581
4582 vco = valleyview_get_vco(dev_priv);
Ville Syrjälädfcab172014-06-13 13:37:47 +03004583 divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004584
4585 mutex_lock(&dev_priv->dpio_lock);
4586 /* adjust cdclk divider */
4587 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03004588 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004589 val |= divider;
4590 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03004591
4592 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4593 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4594 50))
4595 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08004596 mutex_unlock(&dev_priv->dpio_lock);
4597 }
4598
4599 mutex_lock(&dev_priv->dpio_lock);
4600 /* adjust self-refresh exit latency value */
4601 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4602 val &= ~0x7f;
4603
4604 /*
4605 * For high bandwidth configs, we set a higher latency in the bunit
4606 * so that the core display fetch happens in time to avoid underruns.
4607 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03004608 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004609 val |= 4500 / 250; /* 4.5 usec */
4610 else
4611 val |= 3000 / 250; /* 3.0 usec */
4612 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4613 mutex_unlock(&dev_priv->dpio_lock);
4614
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004615 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004616}
4617
Ville Syrjälä383c5a62014-06-28 02:03:57 +03004618static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4619{
4620 struct drm_i915_private *dev_priv = dev->dev_private;
4621 u32 val, cmd;
4622
4623 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4624
4625 switch (cdclk) {
4626 case 400000:
4627 cmd = 3;
4628 break;
4629 case 333333:
4630 case 320000:
4631 cmd = 2;
4632 break;
4633 case 266667:
4634 cmd = 1;
4635 break;
4636 case 200000:
4637 cmd = 0;
4638 break;
4639 default:
4640 WARN_ON(1);
4641 return;
4642 }
4643
4644 mutex_lock(&dev_priv->rps.hw_lock);
4645 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4646 val &= ~DSPFREQGUAR_MASK_CHV;
4647 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4648 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4649 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4650 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4651 50)) {
4652 DRM_ERROR("timed out waiting for CDclk change\n");
4653 }
4654 mutex_unlock(&dev_priv->rps.hw_lock);
4655
4656 vlv_update_cdclk(dev);
4657}
4658
Jesse Barnes30a970c2013-11-04 13:48:12 -08004659static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4660 int max_pixclk)
4661{
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004662 int vco = valleyview_get_vco(dev_priv);
4663 int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
4664
Ville Syrjäläd49a3402014-06-28 02:03:58 +03004665 /* FIXME: Punit isn't quite ready yet */
4666 if (IS_CHERRYVIEW(dev_priv->dev))
4667 return 400000;
4668
Jesse Barnes30a970c2013-11-04 13:48:12 -08004669 /*
4670 * Really only a few cases to deal with, as only 4 CDclks are supported:
4671 * 200MHz
4672 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004673 * 320/333MHz (depends on HPLL freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004674 * 400MHz
4675 * So we check to see whether we're above 90% of the lower bin and
4676 * adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004677 *
4678 * We seem to get an unstable or solid color picture at 200MHz.
4679 * Not sure what's wrong. For now use 200MHz only when all pipes
4680 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08004681 */
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004682 if (max_pixclk > freq_320*9/10)
Ville Syrjälädfcab172014-06-13 13:37:47 +03004683 return 400000;
4684 else if (max_pixclk > 266667*9/10)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004685 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004686 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03004687 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004688 else
4689 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004690}
4691
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004692/* compute the max pixel clock for new configuration */
4693static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004694{
4695 struct drm_device *dev = dev_priv->dev;
4696 struct intel_crtc *intel_crtc;
4697 int max_pixclk = 0;
4698
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004699 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004700 if (intel_crtc->new_enabled)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004701 max_pixclk = max(max_pixclk,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004702 intel_crtc->new_config->adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004703 }
4704
4705 return max_pixclk;
4706}
4707
4708static void valleyview_modeset_global_pipes(struct drm_device *dev,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004709 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004710{
4711 struct drm_i915_private *dev_priv = dev->dev_private;
4712 struct intel_crtc *intel_crtc;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004713 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004714
Imre Deakd60c4472014-03-27 17:45:10 +02004715 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4716 dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004717 return;
4718
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004719 /* disable/enable all currently active pipes while we change cdclk */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004720 for_each_intel_crtc(dev, intel_crtc)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004721 if (intel_crtc->base.enabled)
4722 *prepare_pipes |= (1 << intel_crtc->pipe);
4723}
4724
4725static void valleyview_modeset_global_resources(struct drm_device *dev)
4726{
4727 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004728 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004729 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4730
Ville Syrjälä383c5a62014-06-28 02:03:57 +03004731 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
4732 if (IS_CHERRYVIEW(dev))
4733 cherryview_set_cdclk(dev, req_cdclk);
4734 else
4735 valleyview_set_cdclk(dev, req_cdclk);
4736 }
4737
Imre Deak77961eb2014-03-05 16:20:56 +02004738 modeset_update_crtc_power_domains(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004739}
4740
Jesse Barnes89b667f2013-04-18 14:51:36 -07004741static void valleyview_crtc_enable(struct drm_crtc *crtc)
4742{
4743 struct drm_device *dev = crtc->dev;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004744 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4745 struct intel_encoder *encoder;
4746 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03004747 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004748
4749 WARN_ON(!crtc->enabled);
4750
4751 if (intel_crtc->active)
4752 return;
4753
Shobhit Kumar8525a232014-06-25 12:20:39 +05304754 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4755
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03004756 if (!is_dsi) {
4757 if (IS_CHERRYVIEW(dev))
4758 chv_prepare_pll(intel_crtc);
4759 else
4760 vlv_prepare_pll(intel_crtc);
4761 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02004762
4763 if (intel_crtc->config.has_dp_encoder)
4764 intel_dp_set_m_n(intel_crtc);
4765
4766 intel_set_pipe_timings(intel_crtc);
4767
Daniel Vetter5b18e572014-04-24 23:55:06 +02004768 i9xx_set_pipeconf(intel_crtc);
4769
Jesse Barnes89b667f2013-04-18 14:51:36 -07004770 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004771
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004772 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4773
Jesse Barnes89b667f2013-04-18 14:51:36 -07004774 for_each_encoder_on_crtc(dev, crtc, encoder)
4775 if (encoder->pre_pll_enable)
4776 encoder->pre_pll_enable(encoder);
4777
Chon Ming Lee9d556c92014-05-02 14:27:47 +03004778 if (!is_dsi) {
4779 if (IS_CHERRYVIEW(dev))
4780 chv_enable_pll(intel_crtc);
4781 else
4782 vlv_enable_pll(intel_crtc);
4783 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07004784
4785 for_each_encoder_on_crtc(dev, crtc, encoder)
4786 if (encoder->pre_enable)
4787 encoder->pre_enable(encoder);
4788
Jesse Barnes2dd24552013-04-25 12:55:01 -07004789 i9xx_pfit_enable(intel_crtc);
4790
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004791 intel_crtc_load_lut(crtc);
4792
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004793 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004794 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004795
Jani Nikula50049452013-07-30 12:20:32 +03004796 for_each_encoder_on_crtc(dev, crtc, encoder)
4797 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004798
4799 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004800
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004801 /* Underruns don't raise interrupts, so check manually. */
4802 i9xx_check_fifo_underruns(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004803}
4804
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004805static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4806{
4807 struct drm_device *dev = crtc->base.dev;
4808 struct drm_i915_private *dev_priv = dev->dev_private;
4809
4810 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4811 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4812}
4813
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004814static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004815{
4816 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08004817 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004818 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004819 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004820
Daniel Vetter08a48462012-07-02 11:43:47 +02004821 WARN_ON(!crtc->enabled);
4822
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004823 if (intel_crtc->active)
4824 return;
4825
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004826 i9xx_set_pll_dividers(intel_crtc);
4827
Daniel Vetter5b18e572014-04-24 23:55:06 +02004828 if (intel_crtc->config.has_dp_encoder)
4829 intel_dp_set_m_n(intel_crtc);
4830
4831 intel_set_pipe_timings(intel_crtc);
4832
Daniel Vetter5b18e572014-04-24 23:55:06 +02004833 i9xx_set_pipeconf(intel_crtc);
4834
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004835 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004836
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004837 if (!IS_GEN2(dev))
4838 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4839
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004840 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004841 if (encoder->pre_enable)
4842 encoder->pre_enable(encoder);
4843
Daniel Vetterf6736a12013-06-05 13:34:30 +02004844 i9xx_enable_pll(intel_crtc);
4845
Jesse Barnes2dd24552013-04-25 12:55:01 -07004846 i9xx_pfit_enable(intel_crtc);
4847
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004848 intel_crtc_load_lut(crtc);
4849
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004850 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004851 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004852
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004853 for_each_encoder_on_crtc(dev, crtc, encoder)
4854 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004855
4856 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004857
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004858 /*
4859 * Gen2 reports pipe underruns whenever all planes are disabled.
4860 * So don't enable underrun reporting before at least some planes
4861 * are enabled.
4862 * FIXME: Need to fix the logic to work when we turn off all planes
4863 * but leave the pipe running.
4864 */
4865 if (IS_GEN2(dev))
4866 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4867
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004868 /* Underruns don't raise interrupts, so check manually. */
4869 i9xx_check_fifo_underruns(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004870}
4871
Daniel Vetter87476d62013-04-11 16:29:06 +02004872static void i9xx_pfit_disable(struct intel_crtc *crtc)
4873{
4874 struct drm_device *dev = crtc->base.dev;
4875 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004876
4877 if (!crtc->config.gmch_pfit.control)
4878 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004879
4880 assert_pipe_disabled(dev_priv, crtc->pipe);
4881
Daniel Vetter328d8e82013-05-08 10:36:31 +02004882 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4883 I915_READ(PFIT_CONTROL));
4884 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004885}
4886
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004887static void i9xx_crtc_disable(struct drm_crtc *crtc)
4888{
4889 struct drm_device *dev = crtc->dev;
4890 struct drm_i915_private *dev_priv = dev->dev_private;
4891 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004892 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004893 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004894
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004895 if (!intel_crtc->active)
4896 return;
4897
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004898 /*
4899 * Gen2 reports pipe underruns whenever all planes are disabled.
4900 * So diasble underrun reporting before all the planes get disabled.
4901 * FIXME: Need to fix the logic to work when we turn off all planes
4902 * but leave the pipe running.
4903 */
4904 if (IS_GEN2(dev))
4905 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4906
Imre Deak564ed192014-06-13 14:54:21 +03004907 /*
4908 * Vblank time updates from the shadow to live plane control register
4909 * are blocked if the memory self-refresh mode is active at that
4910 * moment. So to make sure the plane gets truly disabled, disable
4911 * first the self-refresh mode. The self-refresh enable bit in turn
4912 * will be checked/applied by the HW only at the next frame start
4913 * event which is after the vblank start event, so we need to have a
4914 * wait-for-vblank between disabling the plane and the pipe.
4915 */
4916 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004917 intel_crtc_disable_planes(crtc);
4918
Daniel Vetterea9d7582012-07-10 10:42:52 +02004919 for_each_encoder_on_crtc(dev, crtc, encoder)
4920 encoder->disable(encoder);
4921
Ville Syrjälä6304cd92014-04-25 13:30:12 +03004922 /*
4923 * On gen2 planes are double buffered but the pipe isn't, so we must
4924 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03004925 * We also need to wait on all gmch platforms because of the
4926 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03004927 */
Imre Deak564ed192014-06-13 14:54:21 +03004928 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03004929
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004930 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004931
Daniel Vetter87476d62013-04-11 16:29:06 +02004932 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004933
Jesse Barnes89b667f2013-04-18 14:51:36 -07004934 for_each_encoder_on_crtc(dev, crtc, encoder)
4935 if (encoder->post_disable)
4936 encoder->post_disable(encoder);
4937
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03004938 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4939 if (IS_CHERRYVIEW(dev))
4940 chv_disable_pll(dev_priv, pipe);
4941 else if (IS_VALLEYVIEW(dev))
4942 vlv_disable_pll(dev_priv, pipe);
4943 else
4944 i9xx_disable_pll(dev_priv, pipe);
4945 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004946
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004947 if (!IS_GEN2(dev))
4948 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4949
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004950 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004951 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004952
Daniel Vetterefa96242014-04-24 23:55:02 +02004953 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004954 intel_update_fbc(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02004955 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004956}
4957
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004958static void i9xx_crtc_off(struct drm_crtc *crtc)
4959{
4960}
4961
Daniel Vetter976f8a22012-07-08 22:34:21 +02004962static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4963 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004964{
4965 struct drm_device *dev = crtc->dev;
4966 struct drm_i915_master_private *master_priv;
4967 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4968 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004969
4970 if (!dev->primary->master)
4971 return;
4972
4973 master_priv = dev->primary->master->driver_priv;
4974 if (!master_priv->sarea_priv)
4975 return;
4976
Jesse Barnes79e53942008-11-07 14:24:08 -08004977 switch (pipe) {
4978 case 0:
4979 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4980 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4981 break;
4982 case 1:
4983 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4984 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4985 break;
4986 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004987 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004988 break;
4989 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004990}
4991
Borun Fub04c5bd2014-07-12 10:02:27 +05304992/* Master function to enable/disable CRTC and corresponding power wells */
4993void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004994{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004995 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004996 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02004997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02004998 enum intel_display_power_domain domain;
4999 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005000
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005001 if (enable) {
5002 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005003 domains = get_crtc_power_domains(crtc);
5004 for_each_power_domain(domain, domains)
5005 intel_display_power_get(dev_priv, domain);
5006 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005007
5008 dev_priv->display.crtc_enable(crtc);
5009 }
5010 } else {
5011 if (intel_crtc->active) {
5012 dev_priv->display.crtc_disable(crtc);
5013
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005014 domains = intel_crtc->enabled_power_domains;
5015 for_each_power_domain(domain, domains)
5016 intel_display_power_put(dev_priv, domain);
5017 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005018 }
5019 }
Borun Fub04c5bd2014-07-12 10:02:27 +05305020}
5021
5022/**
5023 * Sets the power management mode of the pipe and plane.
5024 */
5025void intel_crtc_update_dpms(struct drm_crtc *crtc)
5026{
5027 struct drm_device *dev = crtc->dev;
5028 struct intel_encoder *intel_encoder;
5029 bool enable = false;
5030
5031 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5032 enable |= intel_encoder->connectors_active;
5033
5034 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005035
5036 intel_crtc_update_sarea(crtc, enable);
5037}
5038
Daniel Vetter976f8a22012-07-08 22:34:21 +02005039static void intel_crtc_disable(struct drm_crtc *crtc)
5040{
5041 struct drm_device *dev = crtc->dev;
5042 struct drm_connector *connector;
5043 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2ff8fde2014-07-08 07:50:07 -07005044 struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
Daniel Vettera071fa02014-06-18 23:28:09 +02005045 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005046
5047 /* crtc should still be enabled when we disable it. */
5048 WARN_ON(!crtc->enabled);
5049
5050 dev_priv->display.crtc_disable(crtc);
5051 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005052 dev_priv->display.off(crtc);
5053
Matt Roperf4510a22014-04-01 15:22:40 -07005054 if (crtc->primary->fb) {
Chris Wilsoncdd59982010-09-08 16:30:16 +01005055 mutex_lock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +02005056 intel_unpin_fb_obj(old_obj);
5057 i915_gem_track_fb(old_obj, NULL,
5058 INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilsoncdd59982010-09-08 16:30:16 +01005059 mutex_unlock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07005060 crtc->primary->fb = NULL;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005061 }
5062
5063 /* Update computed state. */
5064 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5065 if (!connector->encoder || !connector->encoder->crtc)
5066 continue;
5067
5068 if (connector->encoder->crtc != crtc)
5069 continue;
5070
5071 connector->dpms = DRM_MODE_DPMS_OFF;
5072 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01005073 }
5074}
5075
Chris Wilsonea5b2132010-08-04 13:50:23 +01005076void intel_encoder_destroy(struct drm_encoder *encoder)
5077{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005078 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01005079
Chris Wilsonea5b2132010-08-04 13:50:23 +01005080 drm_encoder_cleanup(encoder);
5081 kfree(intel_encoder);
5082}
5083
Damien Lespiau92373292013-08-08 22:28:57 +01005084/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005085 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5086 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01005087static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005088{
5089 if (mode == DRM_MODE_DPMS_ON) {
5090 encoder->connectors_active = true;
5091
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005092 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005093 } else {
5094 encoder->connectors_active = false;
5095
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005096 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005097 }
5098}
5099
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005100/* Cross check the actual hw state with our own modeset state tracking (and it's
5101 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02005102static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005103{
5104 if (connector->get_hw_state(connector)) {
5105 struct intel_encoder *encoder = connector->encoder;
5106 struct drm_crtc *crtc;
5107 bool encoder_enabled;
5108 enum pipe pipe;
5109
5110 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5111 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03005112 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005113
Dave Airlie0e32b392014-05-02 14:02:48 +10005114 /* there is no real hw state for MST connectors */
5115 if (connector->mst_port)
5116 return;
5117
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005118 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5119 "wrong connector dpms state\n");
5120 WARN(connector->base.encoder != &encoder->base,
5121 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005122
Dave Airlie36cd7442014-05-02 13:44:18 +10005123 if (encoder) {
5124 WARN(!encoder->connectors_active,
5125 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005126
Dave Airlie36cd7442014-05-02 13:44:18 +10005127 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5128 WARN(!encoder_enabled, "encoder not enabled\n");
5129 if (WARN_ON(!encoder->base.crtc))
5130 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005131
Dave Airlie36cd7442014-05-02 13:44:18 +10005132 crtc = encoder->base.crtc;
5133
5134 WARN(!crtc->enabled, "crtc not enabled\n");
5135 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5136 WARN(pipe != to_intel_crtc(crtc)->pipe,
5137 "encoder active on the wrong pipe\n");
5138 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005139 }
5140}
5141
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005142/* Even simpler default implementation, if there's really no special case to
5143 * consider. */
5144void intel_connector_dpms(struct drm_connector *connector, int mode)
5145{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005146 /* All the simple cases only support two dpms states. */
5147 if (mode != DRM_MODE_DPMS_ON)
5148 mode = DRM_MODE_DPMS_OFF;
5149
5150 if (mode == connector->dpms)
5151 return;
5152
5153 connector->dpms = mode;
5154
5155 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01005156 if (connector->encoder)
5157 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005158
Daniel Vetterb9805142012-08-31 17:37:33 +02005159 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005160}
5161
Daniel Vetterf0947c32012-07-02 13:10:34 +02005162/* Simple connector->get_hw_state implementation for encoders that support only
5163 * one connector and no cloning and hence the encoder state determines the state
5164 * of the connector. */
5165bool intel_connector_get_hw_state(struct intel_connector *connector)
5166{
Daniel Vetter24929352012-07-02 20:28:59 +02005167 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005168 struct intel_encoder *encoder = connector->encoder;
5169
5170 return encoder->get_hw_state(encoder, &pipe);
5171}
5172
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005173static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5174 struct intel_crtc_config *pipe_config)
5175{
5176 struct drm_i915_private *dev_priv = dev->dev_private;
5177 struct intel_crtc *pipe_B_crtc =
5178 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5179
5180 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5181 pipe_name(pipe), pipe_config->fdi_lanes);
5182 if (pipe_config->fdi_lanes > 4) {
5183 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5184 pipe_name(pipe), pipe_config->fdi_lanes);
5185 return false;
5186 }
5187
Paulo Zanonibafb6552013-11-02 21:07:44 -07005188 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005189 if (pipe_config->fdi_lanes > 2) {
5190 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5191 pipe_config->fdi_lanes);
5192 return false;
5193 } else {
5194 return true;
5195 }
5196 }
5197
5198 if (INTEL_INFO(dev)->num_pipes == 2)
5199 return true;
5200
5201 /* Ivybridge 3 pipe is really complicated */
5202 switch (pipe) {
5203 case PIPE_A:
5204 return true;
5205 case PIPE_B:
5206 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5207 pipe_config->fdi_lanes > 2) {
5208 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5209 pipe_name(pipe), pipe_config->fdi_lanes);
5210 return false;
5211 }
5212 return true;
5213 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01005214 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005215 pipe_B_crtc->config.fdi_lanes <= 2) {
5216 if (pipe_config->fdi_lanes > 2) {
5217 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5218 pipe_name(pipe), pipe_config->fdi_lanes);
5219 return false;
5220 }
5221 } else {
5222 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5223 return false;
5224 }
5225 return true;
5226 default:
5227 BUG();
5228 }
5229}
5230
Daniel Vettere29c22c2013-02-21 00:00:16 +01005231#define RETRY 1
5232static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5233 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02005234{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005235 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005236 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02005237 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01005238 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005239
Daniel Vettere29c22c2013-02-21 00:00:16 +01005240retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02005241 /* FDI is a binary signal running at ~2.7GHz, encoding
5242 * each output octet as 10 bits. The actual frequency
5243 * is stored as a divider into a 100MHz clock, and the
5244 * mode pixel clock is stored in units of 1KHz.
5245 * Hence the bw of each lane in terms of the mode signal
5246 * is:
5247 */
5248 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5249
Damien Lespiau241bfc32013-09-25 16:45:37 +01005250 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005251
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005252 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005253 pipe_config->pipe_bpp);
5254
5255 pipe_config->fdi_lanes = lane;
5256
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005257 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005258 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005259
Daniel Vettere29c22c2013-02-21 00:00:16 +01005260 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5261 intel_crtc->pipe, pipe_config);
5262 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5263 pipe_config->pipe_bpp -= 2*3;
5264 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5265 pipe_config->pipe_bpp);
5266 needs_recompute = true;
5267 pipe_config->bw_constrained = true;
5268
5269 goto retry;
5270 }
5271
5272 if (needs_recompute)
5273 return RETRY;
5274
5275 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005276}
5277
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005278static void hsw_compute_ips_config(struct intel_crtc *crtc,
5279 struct intel_crtc_config *pipe_config)
5280{
Jani Nikulad330a952014-01-21 11:24:25 +02005281 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03005282 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07005283 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005284}
5285
Daniel Vettera43f6e02013-06-07 23:10:32 +02005286static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01005287 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08005288{
Daniel Vettera43f6e02013-06-07 23:10:32 +02005289 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005290 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01005291
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005292 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005293 if (INTEL_INFO(dev)->gen < 4) {
5294 struct drm_i915_private *dev_priv = dev->dev_private;
5295 int clock_limit =
5296 dev_priv->display.get_display_clock_speed(dev);
5297
5298 /*
5299 * Enable pixel doubling when the dot clock
5300 * is > 90% of the (display) core speed.
5301 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03005302 * GDG double wide on either pipe,
5303 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005304 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03005305 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01005306 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005307 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005308 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005309 }
5310
Damien Lespiau241bfc32013-09-25 16:45:37 +01005311 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005312 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005313 }
Chris Wilson89749352010-09-12 18:25:19 +01005314
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005315 /*
5316 * Pipe horizontal size must be even in:
5317 * - DVO ganged mode
5318 * - LVDS dual channel mode
5319 * - Double wide pipe
5320 */
5321 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5322 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5323 pipe_config->pipe_src_w &= ~1;
5324
Damien Lespiau8693a822013-05-03 18:48:11 +01005325 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5326 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03005327 */
5328 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5329 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005330 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03005331
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005332 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005333 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005334 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005335 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5336 * for lvds. */
5337 pipe_config->pipe_bpp = 8*3;
5338 }
5339
Damien Lespiauf5adf942013-06-24 18:29:34 +01005340 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005341 hsw_compute_ips_config(crtc, pipe_config);
5342
Daniel Vetter12030432014-06-25 22:02:00 +03005343 /*
5344 * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
5345 * old clock survives for now.
5346 */
5347 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005348 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005349
Daniel Vetter877d48d2013-04-19 11:24:43 +02005350 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02005351 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02005352
Daniel Vettere29c22c2013-02-21 00:00:16 +01005353 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005354}
5355
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005356static int valleyview_get_display_clock_speed(struct drm_device *dev)
5357{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005358 struct drm_i915_private *dev_priv = dev->dev_private;
5359 int vco = valleyview_get_vco(dev_priv);
5360 u32 val;
5361 int divider;
5362
Ville Syrjäläd49a3402014-06-28 02:03:58 +03005363 /* FIXME: Punit isn't quite ready yet */
5364 if (IS_CHERRYVIEW(dev))
5365 return 400000;
5366
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005367 mutex_lock(&dev_priv->dpio_lock);
5368 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5369 mutex_unlock(&dev_priv->dpio_lock);
5370
5371 divider = val & DISPLAY_FREQUENCY_VALUES;
5372
Ville Syrjälä7d007f42014-06-13 13:37:53 +03005373 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5374 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5375 "cdclk change in progress\n");
5376
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005377 return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005378}
5379
Jesse Barnese70236a2009-09-21 10:42:27 -07005380static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08005381{
Jesse Barnese70236a2009-09-21 10:42:27 -07005382 return 400000;
5383}
Jesse Barnes79e53942008-11-07 14:24:08 -08005384
Jesse Barnese70236a2009-09-21 10:42:27 -07005385static int i915_get_display_clock_speed(struct drm_device *dev)
5386{
5387 return 333000;
5388}
Jesse Barnes79e53942008-11-07 14:24:08 -08005389
Jesse Barnese70236a2009-09-21 10:42:27 -07005390static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5391{
5392 return 200000;
5393}
Jesse Barnes79e53942008-11-07 14:24:08 -08005394
Daniel Vetter257a7ff2013-07-26 08:35:42 +02005395static int pnv_get_display_clock_speed(struct drm_device *dev)
5396{
5397 u16 gcfgc = 0;
5398
5399 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5400
5401 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5402 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5403 return 267000;
5404 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5405 return 333000;
5406 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5407 return 444000;
5408 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5409 return 200000;
5410 default:
5411 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5412 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5413 return 133000;
5414 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5415 return 167000;
5416 }
5417}
5418
Jesse Barnese70236a2009-09-21 10:42:27 -07005419static int i915gm_get_display_clock_speed(struct drm_device *dev)
5420{
5421 u16 gcfgc = 0;
5422
5423 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5424
5425 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08005426 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07005427 else {
5428 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5429 case GC_DISPLAY_CLOCK_333_MHZ:
5430 return 333000;
5431 default:
5432 case GC_DISPLAY_CLOCK_190_200_MHZ:
5433 return 190000;
5434 }
5435 }
5436}
Jesse Barnes79e53942008-11-07 14:24:08 -08005437
Jesse Barnese70236a2009-09-21 10:42:27 -07005438static int i865_get_display_clock_speed(struct drm_device *dev)
5439{
5440 return 266000;
5441}
5442
5443static int i855_get_display_clock_speed(struct drm_device *dev)
5444{
5445 u16 hpllcc = 0;
5446 /* Assume that the hardware is in the high speed state. This
5447 * should be the default.
5448 */
5449 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5450 case GC_CLOCK_133_200:
5451 case GC_CLOCK_100_200:
5452 return 200000;
5453 case GC_CLOCK_166_250:
5454 return 250000;
5455 case GC_CLOCK_100_133:
5456 return 133000;
5457 }
5458
5459 /* Shouldn't happen */
5460 return 0;
5461}
5462
5463static int i830_get_display_clock_speed(struct drm_device *dev)
5464{
5465 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08005466}
5467
Zhenyu Wang2c072452009-06-05 15:38:42 +08005468static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005469intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005470{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005471 while (*num > DATA_LINK_M_N_MASK ||
5472 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08005473 *num >>= 1;
5474 *den >>= 1;
5475 }
5476}
5477
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005478static void compute_m_n(unsigned int m, unsigned int n,
5479 uint32_t *ret_m, uint32_t *ret_n)
5480{
5481 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5482 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5483 intel_reduce_m_n_ratio(ret_m, ret_n);
5484}
5485
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005486void
5487intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5488 int pixel_clock, int link_clock,
5489 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005490{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005491 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005492
5493 compute_m_n(bits_per_pixel * pixel_clock,
5494 link_clock * nlanes * 8,
5495 &m_n->gmch_m, &m_n->gmch_n);
5496
5497 compute_m_n(pixel_clock, link_clock,
5498 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005499}
5500
Chris Wilsona7615032011-01-12 17:04:08 +00005501static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5502{
Jani Nikulad330a952014-01-21 11:24:25 +02005503 if (i915.panel_use_ssc >= 0)
5504 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005505 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07005506 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00005507}
5508
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005509static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5510{
5511 struct drm_device *dev = crtc->dev;
5512 struct drm_i915_private *dev_priv = dev->dev_private;
5513 int refclk;
5514
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005515 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02005516 refclk = 100000;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005517 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005518 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005519 refclk = dev_priv->vbt.lvds_ssc_freq;
5520 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005521 } else if (!IS_GEN2(dev)) {
5522 refclk = 96000;
5523 } else {
5524 refclk = 48000;
5525 }
5526
5527 return refclk;
5528}
5529
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005530static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005531{
Daniel Vetter7df00d72013-05-21 21:54:55 +02005532 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005533}
Daniel Vetterf47709a2013-03-28 10:42:02 +01005534
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005535static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5536{
5537 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005538}
5539
Daniel Vetterf47709a2013-03-28 10:42:02 +01005540static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08005541 intel_clock_t *reduced_clock)
5542{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005543 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005544 u32 fp, fp2 = 0;
5545
5546 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005547 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005548 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005549 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005550 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005551 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005552 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005553 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005554 }
5555
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005556 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005557
Daniel Vetterf47709a2013-03-28 10:42:02 +01005558 crtc->lowfreq_avail = false;
5559 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jani Nikulad330a952014-01-21 11:24:25 +02005560 reduced_clock && i915.powersave) {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005561 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005562 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005563 } else {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005564 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005565 }
5566}
5567
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005568static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5569 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005570{
5571 u32 reg_val;
5572
5573 /*
5574 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5575 * and set it to a reasonable value instead.
5576 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005577 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005578 reg_val &= 0xffffff00;
5579 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005580 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005581
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005582 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005583 reg_val &= 0x8cffffff;
5584 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005585 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005586
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005587 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005588 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005589 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005590
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005591 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005592 reg_val &= 0x00ffffff;
5593 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005594 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005595}
5596
Daniel Vetterb5518422013-05-03 11:49:48 +02005597static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5598 struct intel_link_m_n *m_n)
5599{
5600 struct drm_device *dev = crtc->base.dev;
5601 struct drm_i915_private *dev_priv = dev->dev_private;
5602 int pipe = crtc->pipe;
5603
Daniel Vettere3b95f12013-05-03 11:49:49 +02005604 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5605 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5606 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5607 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005608}
5609
5610static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07005611 struct intel_link_m_n *m_n,
5612 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02005613{
5614 struct drm_device *dev = crtc->base.dev;
5615 struct drm_i915_private *dev_priv = dev->dev_private;
5616 int pipe = crtc->pipe;
5617 enum transcoder transcoder = crtc->config.cpu_transcoder;
5618
5619 if (INTEL_INFO(dev)->gen >= 5) {
5620 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5621 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5622 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5623 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07005624 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5625 * for gen < 8) and if DRRS is supported (to make sure the
5626 * registers are not unnecessarily accessed).
5627 */
5628 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
5629 crtc->config.has_drrs) {
5630 I915_WRITE(PIPE_DATA_M2(transcoder),
5631 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5632 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5633 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5634 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5635 }
Daniel Vetterb5518422013-05-03 11:49:48 +02005636 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02005637 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5638 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5639 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5640 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005641 }
5642}
5643
Vandana Kannanf769cd22014-08-05 07:51:22 -07005644void intel_dp_set_m_n(struct intel_crtc *crtc)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005645{
5646 if (crtc->config.has_pch_encoder)
5647 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5648 else
Vandana Kannanf769cd22014-08-05 07:51:22 -07005649 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
5650 &crtc->config.dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005651}
5652
Daniel Vetterf47709a2013-03-28 10:42:02 +01005653static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005654{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005655 u32 dpll, dpll_md;
5656
5657 /*
5658 * Enable DPIO clock input. We should never disable the reference
5659 * clock for pipe B, since VGA hotplug / manual detection depends
5660 * on it.
5661 */
5662 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5663 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5664 /* We should never disable this, set it here for state tracking */
5665 if (crtc->pipe == PIPE_B)
5666 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5667 dpll |= DPLL_VCO_ENABLE;
5668 crtc->config.dpll_hw_state.dpll = dpll;
5669
5670 dpll_md = (crtc->config.pixel_multiplier - 1)
5671 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5672 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5673}
5674
5675static void vlv_prepare_pll(struct intel_crtc *crtc)
5676{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005677 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005678 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005679 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005680 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005681 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005682 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005683
Daniel Vetter09153002012-12-12 14:06:44 +01005684 mutex_lock(&dev_priv->dpio_lock);
5685
Daniel Vetterf47709a2013-03-28 10:42:02 +01005686 bestn = crtc->config.dpll.n;
5687 bestm1 = crtc->config.dpll.m1;
5688 bestm2 = crtc->config.dpll.m2;
5689 bestp1 = crtc->config.dpll.p1;
5690 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005691
Jesse Barnes89b667f2013-04-18 14:51:36 -07005692 /* See eDP HDMI DPIO driver vbios notes doc */
5693
5694 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005695 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005696 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005697
5698 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005699 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005700
5701 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005702 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005703 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005704 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005705
5706 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005707 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005708
5709 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005710 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5711 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5712 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005713 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07005714
5715 /*
5716 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5717 * but we don't support that).
5718 * Note: don't use the DAC post divider as it seems unstable.
5719 */
5720 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005721 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005722
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005723 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005724 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005725
Jesse Barnes89b667f2013-04-18 14:51:36 -07005726 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02005727 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03005728 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07005729 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005730 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03005731 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005732 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005733 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005734 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005735
Jesse Barnes89b667f2013-04-18 14:51:36 -07005736 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5737 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5738 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005739 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005740 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005741 0x0df40000);
5742 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005743 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005744 0x0df70000);
5745 } else { /* HDMI or VGA */
5746 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005747 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005748 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005749 0x0df70000);
5750 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005751 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005752 0x0df40000);
5753 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005754
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005755 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005756 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5757 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5758 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5759 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005760 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005761
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005762 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01005763 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005764}
5765
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005766static void chv_update_pll(struct intel_crtc *crtc)
5767{
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005768 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5769 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5770 DPLL_VCO_ENABLE;
5771 if (crtc->pipe != PIPE_A)
5772 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5773
5774 crtc->config.dpll_hw_state.dpll_md =
5775 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5776}
5777
5778static void chv_prepare_pll(struct intel_crtc *crtc)
5779{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005780 struct drm_device *dev = crtc->base.dev;
5781 struct drm_i915_private *dev_priv = dev->dev_private;
5782 int pipe = crtc->pipe;
5783 int dpll_reg = DPLL(crtc->pipe);
5784 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ville Syrjälä580d3812014-04-09 13:29:00 +03005785 u32 loopfilter, intcoeff;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005786 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5787 int refclk;
5788
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005789 bestn = crtc->config.dpll.n;
5790 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5791 bestm1 = crtc->config.dpll.m1;
5792 bestm2 = crtc->config.dpll.m2 >> 22;
5793 bestp1 = crtc->config.dpll.p1;
5794 bestp2 = crtc->config.dpll.p2;
5795
5796 /*
5797 * Enable Refclk and SSC
5798 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03005799 I915_WRITE(dpll_reg,
5800 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5801
5802 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005803
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005804 /* p1 and p2 divider */
5805 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5806 5 << DPIO_CHV_S1_DIV_SHIFT |
5807 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5808 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5809 1 << DPIO_CHV_K_DIV_SHIFT);
5810
5811 /* Feedback post-divider - m2 */
5812 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5813
5814 /* Feedback refclk divider - n and m1 */
5815 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5816 DPIO_CHV_M1_DIV_BY_2 |
5817 1 << DPIO_CHV_N_DIV_SHIFT);
5818
5819 /* M2 fraction division */
5820 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5821
5822 /* M2 fraction division enable */
5823 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5824 DPIO_CHV_FRAC_DIV_EN |
5825 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5826
5827 /* Loop filter */
5828 refclk = i9xx_get_refclk(&crtc->base, 0);
5829 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5830 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5831 if (refclk == 100000)
5832 intcoeff = 11;
5833 else if (refclk == 38400)
5834 intcoeff = 10;
5835 else
5836 intcoeff = 9;
5837 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5838 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5839
5840 /* AFC Recal */
5841 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5842 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5843 DPIO_AFC_RECAL);
5844
5845 mutex_unlock(&dev_priv->dpio_lock);
5846}
5847
Daniel Vetterf47709a2013-03-28 10:42:02 +01005848static void i9xx_update_pll(struct intel_crtc *crtc,
5849 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005850 int num_connectors)
5851{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005852 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005853 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005854 u32 dpll;
5855 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005856 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005857
Daniel Vetterf47709a2013-03-28 10:42:02 +01005858 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305859
Daniel Vetterf47709a2013-03-28 10:42:02 +01005860 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5861 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005862
5863 dpll = DPLL_VGA_MODE_DIS;
5864
Daniel Vetterf47709a2013-03-28 10:42:02 +01005865 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005866 dpll |= DPLLB_MODE_LVDS;
5867 else
5868 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005869
Daniel Vetteref1b4602013-06-01 17:17:04 +02005870 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02005871 dpll |= (crtc->config.pixel_multiplier - 1)
5872 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005873 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02005874
5875 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005876 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005877
Daniel Vetterf47709a2013-03-28 10:42:02 +01005878 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02005879 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005880
5881 /* compute bitmask from p1 value */
5882 if (IS_PINEVIEW(dev))
5883 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5884 else {
5885 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5886 if (IS_G4X(dev) && reduced_clock)
5887 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5888 }
5889 switch (clock->p2) {
5890 case 5:
5891 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5892 break;
5893 case 7:
5894 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5895 break;
5896 case 10:
5897 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5898 break;
5899 case 14:
5900 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5901 break;
5902 }
5903 if (INTEL_INFO(dev)->gen >= 4)
5904 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5905
Daniel Vetter09ede542013-04-30 14:01:45 +02005906 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005907 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005908 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005909 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5910 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5911 else
5912 dpll |= PLL_REF_INPUT_DREFCLK;
5913
5914 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005915 crtc->config.dpll_hw_state.dpll = dpll;
5916
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005917 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02005918 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5919 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005920 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005921 }
5922}
5923
Daniel Vetterf47709a2013-03-28 10:42:02 +01005924static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01005925 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005926 int num_connectors)
5927{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005928 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005929 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005930 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005931 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005932
Daniel Vetterf47709a2013-03-28 10:42:02 +01005933 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305934
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005935 dpll = DPLL_VGA_MODE_DIS;
5936
Daniel Vetterf47709a2013-03-28 10:42:02 +01005937 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005938 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5939 } else {
5940 if (clock->p1 == 2)
5941 dpll |= PLL_P1_DIVIDE_BY_TWO;
5942 else
5943 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5944 if (clock->p2 == 4)
5945 dpll |= PLL_P2_DIVIDE_BY_4;
5946 }
5947
Daniel Vetter4a33e482013-07-06 12:52:05 +02005948 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5949 dpll |= DPLL_DVO_2X_MODE;
5950
Daniel Vetterf47709a2013-03-28 10:42:02 +01005951 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005952 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5953 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5954 else
5955 dpll |= PLL_REF_INPUT_DREFCLK;
5956
5957 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005958 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005959}
5960
Daniel Vetter8a654f32013-06-01 17:16:22 +02005961static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005962{
5963 struct drm_device *dev = intel_crtc->base.dev;
5964 struct drm_i915_private *dev_priv = dev->dev_private;
5965 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005966 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02005967 struct drm_display_mode *adjusted_mode =
5968 &intel_crtc->config.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02005969 uint32_t crtc_vtotal, crtc_vblank_end;
5970 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005971
5972 /* We need to be careful not to changed the adjusted mode, for otherwise
5973 * the hw state checker will get angry at the mismatch. */
5974 crtc_vtotal = adjusted_mode->crtc_vtotal;
5975 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005976
Ville Syrjälä609aeac2014-03-28 23:29:30 +02005977 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005978 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005979 crtc_vtotal -= 1;
5980 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02005981
5982 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5983 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5984 else
5985 vsyncshift = adjusted_mode->crtc_hsync_start -
5986 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02005987 if (vsyncshift < 0)
5988 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005989 }
5990
5991 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005992 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005993
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005994 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005995 (adjusted_mode->crtc_hdisplay - 1) |
5996 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005997 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005998 (adjusted_mode->crtc_hblank_start - 1) |
5999 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006000 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006001 (adjusted_mode->crtc_hsync_start - 1) |
6002 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6003
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006004 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006005 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006006 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006007 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006008 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006009 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006010 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006011 (adjusted_mode->crtc_vsync_start - 1) |
6012 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6013
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006014 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6015 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6016 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6017 * bits. */
6018 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6019 (pipe == PIPE_B || pipe == PIPE_C))
6020 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6021
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006022 /* pipesrc controls the size that is scaled from, which should
6023 * always be the user's requested size.
6024 */
6025 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006026 ((intel_crtc->config.pipe_src_w - 1) << 16) |
6027 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006028}
6029
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006030static void intel_get_pipe_timings(struct intel_crtc *crtc,
6031 struct intel_crtc_config *pipe_config)
6032{
6033 struct drm_device *dev = crtc->base.dev;
6034 struct drm_i915_private *dev_priv = dev->dev_private;
6035 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6036 uint32_t tmp;
6037
6038 tmp = I915_READ(HTOTAL(cpu_transcoder));
6039 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6040 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
6041 tmp = I915_READ(HBLANK(cpu_transcoder));
6042 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6043 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
6044 tmp = I915_READ(HSYNC(cpu_transcoder));
6045 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6046 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
6047
6048 tmp = I915_READ(VTOTAL(cpu_transcoder));
6049 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6050 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
6051 tmp = I915_READ(VBLANK(cpu_transcoder));
6052 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6053 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
6054 tmp = I915_READ(VSYNC(cpu_transcoder));
6055 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6056 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
6057
6058 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
6059 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6060 pipe_config->adjusted_mode.crtc_vtotal += 1;
6061 pipe_config->adjusted_mode.crtc_vblank_end += 1;
6062 }
6063
6064 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006065 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6066 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6067
6068 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
6069 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006070}
6071
Daniel Vetterf6a83282014-02-11 15:28:57 -08006072void intel_mode_from_pipe_config(struct drm_display_mode *mode,
6073 struct intel_crtc_config *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03006074{
Daniel Vetterf6a83282014-02-11 15:28:57 -08006075 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
6076 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
6077 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
6078 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006079
Daniel Vetterf6a83282014-02-11 15:28:57 -08006080 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
6081 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
6082 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
6083 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006084
Daniel Vetterf6a83282014-02-11 15:28:57 -08006085 mode->flags = pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006086
Daniel Vetterf6a83282014-02-11 15:28:57 -08006087 mode->clock = pipe_config->adjusted_mode.crtc_clock;
6088 mode->flags |= pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006089}
6090
Daniel Vetter84b046f2013-02-19 18:48:54 +01006091static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6092{
6093 struct drm_device *dev = intel_crtc->base.dev;
6094 struct drm_i915_private *dev_priv = dev->dev_private;
6095 uint32_t pipeconf;
6096
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006097 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006098
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03006099 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6100 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6101 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02006102
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006103 if (intel_crtc->config.double_wide)
6104 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006105
Daniel Vetterff9ce462013-04-24 14:57:17 +02006106 /* only g4x and later have fancy bpc/dither controls */
6107 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006108 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6109 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
6110 pipeconf |= PIPECONF_DITHER_EN |
6111 PIPECONF_DITHER_TYPE_SP;
6112
6113 switch (intel_crtc->config.pipe_bpp) {
6114 case 18:
6115 pipeconf |= PIPECONF_6BPC;
6116 break;
6117 case 24:
6118 pipeconf |= PIPECONF_8BPC;
6119 break;
6120 case 30:
6121 pipeconf |= PIPECONF_10BPC;
6122 break;
6123 default:
6124 /* Case prevented by intel_choose_pipe_bpp_dither. */
6125 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01006126 }
6127 }
6128
6129 if (HAS_PIPE_CXSR(dev)) {
6130 if (intel_crtc->lowfreq_avail) {
6131 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6132 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6133 } else {
6134 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01006135 }
6136 }
6137
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006138 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6139 if (INTEL_INFO(dev)->gen < 4 ||
6140 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
6141 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6142 else
6143 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6144 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01006145 pipeconf |= PIPECONF_PROGRESSIVE;
6146
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006147 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
6148 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03006149
Daniel Vetter84b046f2013-02-19 18:48:54 +01006150 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6151 POSTING_READ(PIPECONF(intel_crtc->pipe));
6152}
6153
Eric Anholtf564048e2011-03-30 13:01:02 -07006154static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07006155 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006156 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006157{
6158 struct drm_device *dev = crtc->dev;
6159 struct drm_i915_private *dev_priv = dev->dev_private;
6160 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eric Anholtc751ce42010-03-25 11:48:48 -07006161 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07006162 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02006163 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006164 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01006165 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08006166 const intel_limit_t *limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08006167
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02006168 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01006169 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006170 case INTEL_OUTPUT_LVDS:
6171 is_lvds = true;
6172 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006173 case INTEL_OUTPUT_DSI:
6174 is_dsi = true;
6175 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006176 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006177
Eric Anholtc751ce42010-03-25 11:48:48 -07006178 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08006179 }
6180
Jani Nikulaf2335332013-09-13 11:03:09 +03006181 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02006182 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006183
Jani Nikulaf2335332013-09-13 11:03:09 +03006184 if (!intel_crtc->config.clock_set) {
6185 refclk = i9xx_get_refclk(crtc, num_connectors);
6186
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006187 /*
6188 * Returns a set of divisors for the desired target clock with
6189 * the given refclk, or FALSE. The returned values represent
6190 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6191 * 2) / p1 / p2.
6192 */
6193 limit = intel_limit(crtc, refclk);
6194 ok = dev_priv->display.find_dpll(limit, crtc,
6195 intel_crtc->config.port_clock,
6196 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03006197 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006198 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6199 return -EINVAL;
6200 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006201
Jani Nikulaf2335332013-09-13 11:03:09 +03006202 if (is_lvds && dev_priv->lvds_downclock_avail) {
6203 /*
6204 * Ensure we match the reduced clock's P to the target
6205 * clock. If the clocks don't match, we can't switch
6206 * the display clock by using the FP0/FP1. In such case
6207 * we will disable the LVDS downclock feature.
6208 */
6209 has_reduced_clock =
6210 dev_priv->display.find_dpll(limit, crtc,
6211 dev_priv->lvds_downclock,
6212 refclk, &clock,
6213 &reduced_clock);
6214 }
6215 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01006216 intel_crtc->config.dpll.n = clock.n;
6217 intel_crtc->config.dpll.m1 = clock.m1;
6218 intel_crtc->config.dpll.m2 = clock.m2;
6219 intel_crtc->config.dpll.p1 = clock.p1;
6220 intel_crtc->config.dpll.p2 = clock.p2;
6221 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006222
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006223 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02006224 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306225 has_reduced_clock ? &reduced_clock : NULL,
6226 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006227 } else if (IS_CHERRYVIEW(dev)) {
6228 chv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006229 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03006230 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006231 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01006232 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006233 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02006234 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006235 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006236
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02006237 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07006238}
6239
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006240static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6241 struct intel_crtc_config *pipe_config)
6242{
6243 struct drm_device *dev = crtc->base.dev;
6244 struct drm_i915_private *dev_priv = dev->dev_private;
6245 uint32_t tmp;
6246
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02006247 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6248 return;
6249
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006250 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02006251 if (!(tmp & PFIT_ENABLE))
6252 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006253
Daniel Vetter06922822013-07-11 13:35:40 +02006254 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006255 if (INTEL_INFO(dev)->gen < 4) {
6256 if (crtc->pipe != PIPE_B)
6257 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006258 } else {
6259 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6260 return;
6261 }
6262
Daniel Vetter06922822013-07-11 13:35:40 +02006263 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006264 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6265 if (INTEL_INFO(dev)->gen < 5)
6266 pipe_config->gmch_pfit.lvds_border_bits =
6267 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6268}
6269
Jesse Barnesacbec812013-09-20 11:29:32 -07006270static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6271 struct intel_crtc_config *pipe_config)
6272{
6273 struct drm_device *dev = crtc->base.dev;
6274 struct drm_i915_private *dev_priv = dev->dev_private;
6275 int pipe = pipe_config->cpu_transcoder;
6276 intel_clock_t clock;
6277 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07006278 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07006279
Shobhit Kumarf573de52014-07-30 20:32:37 +05306280 /* In case of MIPI DPLL will not even be used */
6281 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6282 return;
6283
Jesse Barnesacbec812013-09-20 11:29:32 -07006284 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006285 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07006286 mutex_unlock(&dev_priv->dpio_lock);
6287
6288 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6289 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6290 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6291 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6292 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6293
Ville Syrjäläf6466282013-10-14 14:50:31 +03006294 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07006295
Ville Syrjäläf6466282013-10-14 14:50:31 +03006296 /* clock.dot is the fast clock */
6297 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07006298}
6299
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006300static void i9xx_get_plane_config(struct intel_crtc *crtc,
6301 struct intel_plane_config *plane_config)
6302{
6303 struct drm_device *dev = crtc->base.dev;
6304 struct drm_i915_private *dev_priv = dev->dev_private;
6305 u32 val, base, offset;
6306 int pipe = crtc->pipe, plane = crtc->plane;
6307 int fourcc, pixel_format;
6308 int aligned_height;
6309
Dave Airlie66e514c2014-04-03 07:51:54 +10006310 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6311 if (!crtc->base.primary->fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006312 DRM_DEBUG_KMS("failed to alloc fb\n");
6313 return;
6314 }
6315
6316 val = I915_READ(DSPCNTR(plane));
6317
6318 if (INTEL_INFO(dev)->gen >= 4)
6319 if (val & DISPPLANE_TILED)
6320 plane_config->tiled = true;
6321
6322 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6323 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10006324 crtc->base.primary->fb->pixel_format = fourcc;
6325 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006326 drm_format_plane_cpp(fourcc, 0) * 8;
6327
6328 if (INTEL_INFO(dev)->gen >= 4) {
6329 if (plane_config->tiled)
6330 offset = I915_READ(DSPTILEOFF(plane));
6331 else
6332 offset = I915_READ(DSPLINOFF(plane));
6333 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6334 } else {
6335 base = I915_READ(DSPADDR(plane));
6336 }
6337 plane_config->base = base;
6338
6339 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10006340 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6341 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006342
6343 val = I915_READ(DSPSTRIDE(pipe));
Rafael Barbalho026b96e2014-07-28 19:56:27 +01006344 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006345
Dave Airlie66e514c2014-04-03 07:51:54 +10006346 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006347 plane_config->tiled);
6348
Fabian Frederick1267a262014-07-01 20:39:41 +02006349 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
6350 aligned_height);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006351
6352 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
Dave Airlie66e514c2014-04-03 07:51:54 +10006353 pipe, plane, crtc->base.primary->fb->width,
6354 crtc->base.primary->fb->height,
6355 crtc->base.primary->fb->bits_per_pixel, base,
6356 crtc->base.primary->fb->pitches[0],
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006357 plane_config->size);
6358
6359}
6360
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006361static void chv_crtc_clock_get(struct intel_crtc *crtc,
6362 struct intel_crtc_config *pipe_config)
6363{
6364 struct drm_device *dev = crtc->base.dev;
6365 struct drm_i915_private *dev_priv = dev->dev_private;
6366 int pipe = pipe_config->cpu_transcoder;
6367 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6368 intel_clock_t clock;
6369 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6370 int refclk = 100000;
6371
6372 mutex_lock(&dev_priv->dpio_lock);
6373 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6374 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6375 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6376 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6377 mutex_unlock(&dev_priv->dpio_lock);
6378
6379 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6380 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6381 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6382 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6383 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6384
6385 chv_clock(refclk, &clock);
6386
6387 /* clock.dot is the fast clock */
6388 pipe_config->port_clock = clock.dot / 5;
6389}
6390
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006391static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6392 struct intel_crtc_config *pipe_config)
6393{
6394 struct drm_device *dev = crtc->base.dev;
6395 struct drm_i915_private *dev_priv = dev->dev_private;
6396 uint32_t tmp;
6397
Imre Deakb5482bd2014-03-05 16:20:55 +02006398 if (!intel_display_power_enabled(dev_priv,
6399 POWER_DOMAIN_PIPE(crtc->pipe)))
6400 return false;
6401
Daniel Vettere143a212013-07-04 12:01:15 +02006402 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006403 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006404
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006405 tmp = I915_READ(PIPECONF(crtc->pipe));
6406 if (!(tmp & PIPECONF_ENABLE))
6407 return false;
6408
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006409 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6410 switch (tmp & PIPECONF_BPC_MASK) {
6411 case PIPECONF_6BPC:
6412 pipe_config->pipe_bpp = 18;
6413 break;
6414 case PIPECONF_8BPC:
6415 pipe_config->pipe_bpp = 24;
6416 break;
6417 case PIPECONF_10BPC:
6418 pipe_config->pipe_bpp = 30;
6419 break;
6420 default:
6421 break;
6422 }
6423 }
6424
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02006425 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6426 pipe_config->limited_color_range = true;
6427
Ville Syrjälä282740f2013-09-04 18:30:03 +03006428 if (INTEL_INFO(dev)->gen < 4)
6429 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6430
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006431 intel_get_pipe_timings(crtc, pipe_config);
6432
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006433 i9xx_get_pfit_config(crtc, pipe_config);
6434
Daniel Vetter6c49f242013-06-06 12:45:25 +02006435 if (INTEL_INFO(dev)->gen >= 4) {
6436 tmp = I915_READ(DPLL_MD(crtc->pipe));
6437 pipe_config->pixel_multiplier =
6438 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6439 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006440 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02006441 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6442 tmp = I915_READ(DPLL(crtc->pipe));
6443 pipe_config->pixel_multiplier =
6444 ((tmp & SDVO_MULTIPLIER_MASK)
6445 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6446 } else {
6447 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6448 * port and will be fixed up in the encoder->get_config
6449 * function. */
6450 pipe_config->pixel_multiplier = 1;
6451 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006452 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6453 if (!IS_VALLEYVIEW(dev)) {
6454 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6455 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03006456 } else {
6457 /* Mask out read-only status bits. */
6458 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6459 DPLL_PORTC_READY_MASK |
6460 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006461 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02006462
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006463 if (IS_CHERRYVIEW(dev))
6464 chv_crtc_clock_get(crtc, pipe_config);
6465 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07006466 vlv_crtc_clock_get(crtc, pipe_config);
6467 else
6468 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03006469
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006470 return true;
6471}
6472
Paulo Zanonidde86e22012-12-01 12:04:25 -02006473static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07006474{
6475 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006476 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006477 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006478 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006479 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006480 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07006481 bool has_ck505 = false;
6482 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006483
6484 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01006485 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07006486 switch (encoder->type) {
6487 case INTEL_OUTPUT_LVDS:
6488 has_panel = true;
6489 has_lvds = true;
6490 break;
6491 case INTEL_OUTPUT_EDP:
6492 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03006493 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07006494 has_cpu_edp = true;
6495 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006496 }
6497 }
6498
Keith Packard99eb6a02011-09-26 14:29:12 -07006499 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006500 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07006501 can_ssc = has_ck505;
6502 } else {
6503 has_ck505 = false;
6504 can_ssc = true;
6505 }
6506
Imre Deak2de69052013-05-08 13:14:04 +03006507 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6508 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006509
6510 /* Ironlake: try to setup display ref clock before DPLL
6511 * enabling. This is only under driver's control after
6512 * PCH B stepping, previous chipset stepping should be
6513 * ignoring this setting.
6514 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006515 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006516
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006517 /* As we must carefully and slowly disable/enable each source in turn,
6518 * compute the final state we want first and check if we need to
6519 * make any changes at all.
6520 */
6521 final = val;
6522 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07006523 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006524 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07006525 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006526 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6527
6528 final &= ~DREF_SSC_SOURCE_MASK;
6529 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6530 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006531
Keith Packard199e5d72011-09-22 12:01:57 -07006532 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006533 final |= DREF_SSC_SOURCE_ENABLE;
6534
6535 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6536 final |= DREF_SSC1_ENABLE;
6537
6538 if (has_cpu_edp) {
6539 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6540 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6541 else
6542 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6543 } else
6544 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6545 } else {
6546 final |= DREF_SSC_SOURCE_DISABLE;
6547 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6548 }
6549
6550 if (final == val)
6551 return;
6552
6553 /* Always enable nonspread source */
6554 val &= ~DREF_NONSPREAD_SOURCE_MASK;
6555
6556 if (has_ck505)
6557 val |= DREF_NONSPREAD_CK505_ENABLE;
6558 else
6559 val |= DREF_NONSPREAD_SOURCE_ENABLE;
6560
6561 if (has_panel) {
6562 val &= ~DREF_SSC_SOURCE_MASK;
6563 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006564
Keith Packard199e5d72011-09-22 12:01:57 -07006565 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07006566 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006567 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006568 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02006569 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006570 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006571
6572 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006573 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006574 POSTING_READ(PCH_DREF_CONTROL);
6575 udelay(200);
6576
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006577 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006578
6579 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07006580 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07006581 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006582 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006583 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02006584 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006585 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07006586 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006587 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006588
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006589 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006590 POSTING_READ(PCH_DREF_CONTROL);
6591 udelay(200);
6592 } else {
6593 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6594
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006595 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07006596
6597 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006598 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006599
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006600 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006601 POSTING_READ(PCH_DREF_CONTROL);
6602 udelay(200);
6603
6604 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006605 val &= ~DREF_SSC_SOURCE_MASK;
6606 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006607
6608 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006609 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006610
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006611 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006612 POSTING_READ(PCH_DREF_CONTROL);
6613 udelay(200);
6614 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006615
6616 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006617}
6618
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006619static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02006620{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006621 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006622
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006623 tmp = I915_READ(SOUTH_CHICKEN2);
6624 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6625 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006626
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006627 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6628 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6629 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02006630
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006631 tmp = I915_READ(SOUTH_CHICKEN2);
6632 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6633 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006634
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006635 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6636 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6637 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006638}
6639
6640/* WaMPhyProgramming:hsw */
6641static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6642{
6643 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006644
6645 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6646 tmp &= ~(0xFF << 24);
6647 tmp |= (0x12 << 24);
6648 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6649
Paulo Zanonidde86e22012-12-01 12:04:25 -02006650 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6651 tmp |= (1 << 11);
6652 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6653
6654 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6655 tmp |= (1 << 11);
6656 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6657
Paulo Zanonidde86e22012-12-01 12:04:25 -02006658 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6659 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6660 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6661
6662 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6663 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6664 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6665
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006666 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6667 tmp &= ~(7 << 13);
6668 tmp |= (5 << 13);
6669 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006670
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006671 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6672 tmp &= ~(7 << 13);
6673 tmp |= (5 << 13);
6674 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006675
6676 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6677 tmp &= ~0xFF;
6678 tmp |= 0x1C;
6679 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6680
6681 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6682 tmp &= ~0xFF;
6683 tmp |= 0x1C;
6684 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6685
6686 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6687 tmp &= ~(0xFF << 16);
6688 tmp |= (0x1C << 16);
6689 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6690
6691 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6692 tmp &= ~(0xFF << 16);
6693 tmp |= (0x1C << 16);
6694 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6695
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006696 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6697 tmp |= (1 << 27);
6698 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006699
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006700 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6701 tmp |= (1 << 27);
6702 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006703
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006704 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6705 tmp &= ~(0xF << 28);
6706 tmp |= (4 << 28);
6707 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006708
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006709 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6710 tmp &= ~(0xF << 28);
6711 tmp |= (4 << 28);
6712 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006713}
6714
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006715/* Implements 3 different sequences from BSpec chapter "Display iCLK
6716 * Programming" based on the parameters passed:
6717 * - Sequence to enable CLKOUT_DP
6718 * - Sequence to enable CLKOUT_DP without spread
6719 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6720 */
6721static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6722 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006723{
6724 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006725 uint32_t reg, tmp;
6726
6727 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6728 with_spread = true;
6729 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6730 with_fdi, "LP PCH doesn't have FDI\n"))
6731 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006732
6733 mutex_lock(&dev_priv->dpio_lock);
6734
6735 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6736 tmp &= ~SBI_SSCCTL_DISABLE;
6737 tmp |= SBI_SSCCTL_PATHALT;
6738 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6739
6740 udelay(24);
6741
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006742 if (with_spread) {
6743 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6744 tmp &= ~SBI_SSCCTL_PATHALT;
6745 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006746
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006747 if (with_fdi) {
6748 lpt_reset_fdi_mphy(dev_priv);
6749 lpt_program_fdi_mphy(dev_priv);
6750 }
6751 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02006752
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006753 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6754 SBI_GEN0 : SBI_DBUFF0;
6755 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6756 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6757 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01006758
6759 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006760}
6761
Paulo Zanoni47701c32013-07-23 11:19:25 -03006762/* Sequence to disable CLKOUT_DP */
6763static void lpt_disable_clkout_dp(struct drm_device *dev)
6764{
6765 struct drm_i915_private *dev_priv = dev->dev_private;
6766 uint32_t reg, tmp;
6767
6768 mutex_lock(&dev_priv->dpio_lock);
6769
6770 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6771 SBI_GEN0 : SBI_DBUFF0;
6772 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6773 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6774 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6775
6776 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6777 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6778 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6779 tmp |= SBI_SSCCTL_PATHALT;
6780 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6781 udelay(32);
6782 }
6783 tmp |= SBI_SSCCTL_DISABLE;
6784 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6785 }
6786
6787 mutex_unlock(&dev_priv->dpio_lock);
6788}
6789
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006790static void lpt_init_pch_refclk(struct drm_device *dev)
6791{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006792 struct intel_encoder *encoder;
6793 bool has_vga = false;
6794
Damien Lespiaub2784e12014-08-05 11:29:37 +01006795 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006796 switch (encoder->type) {
6797 case INTEL_OUTPUT_ANALOG:
6798 has_vga = true;
6799 break;
6800 }
6801 }
6802
Paulo Zanoni47701c32013-07-23 11:19:25 -03006803 if (has_vga)
6804 lpt_enable_clkout_dp(dev, true, true);
6805 else
6806 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006807}
6808
Paulo Zanonidde86e22012-12-01 12:04:25 -02006809/*
6810 * Initialize reference clocks when the driver loads
6811 */
6812void intel_init_pch_refclk(struct drm_device *dev)
6813{
6814 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6815 ironlake_init_pch_refclk(dev);
6816 else if (HAS_PCH_LPT(dev))
6817 lpt_init_pch_refclk(dev);
6818}
6819
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006820static int ironlake_get_refclk(struct drm_crtc *crtc)
6821{
6822 struct drm_device *dev = crtc->dev;
6823 struct drm_i915_private *dev_priv = dev->dev_private;
6824 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006825 int num_connectors = 0;
6826 bool is_lvds = false;
6827
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02006828 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006829 switch (encoder->type) {
6830 case INTEL_OUTPUT_LVDS:
6831 is_lvds = true;
6832 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006833 }
6834 num_connectors++;
6835 }
6836
6837 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006838 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006839 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006840 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006841 }
6842
6843 return 120000;
6844}
6845
Daniel Vetter6ff93602013-04-19 11:24:36 +02006846static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03006847{
6848 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
6849 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6850 int pipe = intel_crtc->pipe;
6851 uint32_t val;
6852
Daniel Vetter78114072013-06-13 00:54:57 +02006853 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03006854
Daniel Vetter965e0c42013-03-27 00:44:57 +01006855 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03006856 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006857 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006858 break;
6859 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006860 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006861 break;
6862 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006863 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006864 break;
6865 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006866 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006867 break;
6868 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03006869 /* Case prevented by intel_choose_pipe_bpp_dither. */
6870 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03006871 }
6872
Daniel Vetterd8b32242013-04-25 17:54:44 +02006873 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03006874 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6875
Daniel Vetter6ff93602013-04-19 11:24:36 +02006876 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03006877 val |= PIPECONF_INTERLACED_ILK;
6878 else
6879 val |= PIPECONF_PROGRESSIVE;
6880
Daniel Vetter50f3b012013-03-27 00:44:56 +01006881 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006882 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006883
Paulo Zanonic8203562012-09-12 10:06:29 -03006884 I915_WRITE(PIPECONF(pipe), val);
6885 POSTING_READ(PIPECONF(pipe));
6886}
6887
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006888/*
6889 * Set up the pipe CSC unit.
6890 *
6891 * Currently only full range RGB to limited range RGB conversion
6892 * is supported, but eventually this should handle various
6893 * RGB<->YCbCr scenarios as well.
6894 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01006895static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006896{
6897 struct drm_device *dev = crtc->dev;
6898 struct drm_i915_private *dev_priv = dev->dev_private;
6899 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6900 int pipe = intel_crtc->pipe;
6901 uint16_t coeff = 0x7800; /* 1.0 */
6902
6903 /*
6904 * TODO: Check what kind of values actually come out of the pipe
6905 * with these coeff/postoff values and adjust to get the best
6906 * accuracy. Perhaps we even need to take the bpc value into
6907 * consideration.
6908 */
6909
Daniel Vetter50f3b012013-03-27 00:44:56 +01006910 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006911 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6912
6913 /*
6914 * GY/GU and RY/RU should be the other way around according
6915 * to BSpec, but reality doesn't agree. Just set them up in
6916 * a way that results in the correct picture.
6917 */
6918 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6919 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6920
6921 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6922 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6923
6924 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6925 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6926
6927 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6928 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6929 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6930
6931 if (INTEL_INFO(dev)->gen > 6) {
6932 uint16_t postoff = 0;
6933
Daniel Vetter50f3b012013-03-27 00:44:56 +01006934 if (intel_crtc->config.limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02006935 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006936
6937 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6938 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6939 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6940
6941 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6942 } else {
6943 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6944
Daniel Vetter50f3b012013-03-27 00:44:56 +01006945 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006946 mode |= CSC_BLACK_SCREEN_OFFSET;
6947
6948 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6949 }
6950}
6951
Daniel Vetter6ff93602013-04-19 11:24:36 +02006952static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006953{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006954 struct drm_device *dev = crtc->dev;
6955 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006956 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006957 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006958 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006959 uint32_t val;
6960
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006961 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006962
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006963 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006964 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6965
Daniel Vetter6ff93602013-04-19 11:24:36 +02006966 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006967 val |= PIPECONF_INTERLACED_ILK;
6968 else
6969 val |= PIPECONF_PROGRESSIVE;
6970
Paulo Zanoni702e7a52012-10-23 18:29:59 -02006971 I915_WRITE(PIPECONF(cpu_transcoder), val);
6972 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006973
6974 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6975 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006976
6977 if (IS_BROADWELL(dev)) {
6978 val = 0;
6979
6980 switch (intel_crtc->config.pipe_bpp) {
6981 case 18:
6982 val |= PIPEMISC_DITHER_6_BPC;
6983 break;
6984 case 24:
6985 val |= PIPEMISC_DITHER_8_BPC;
6986 break;
6987 case 30:
6988 val |= PIPEMISC_DITHER_10_BPC;
6989 break;
6990 case 36:
6991 val |= PIPEMISC_DITHER_12_BPC;
6992 break;
6993 default:
6994 /* Case prevented by pipe_config_set_bpp. */
6995 BUG();
6996 }
6997
6998 if (intel_crtc->config.dither)
6999 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7000
7001 I915_WRITE(PIPEMISC(pipe), val);
7002 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007003}
7004
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007005static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007006 intel_clock_t *clock,
7007 bool *has_reduced_clock,
7008 intel_clock_t *reduced_clock)
7009{
7010 struct drm_device *dev = crtc->dev;
7011 struct drm_i915_private *dev_priv = dev->dev_private;
7012 struct intel_encoder *intel_encoder;
7013 int refclk;
7014 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02007015 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007016
7017 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7018 switch (intel_encoder->type) {
7019 case INTEL_OUTPUT_LVDS:
7020 is_lvds = true;
7021 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007022 }
7023 }
7024
7025 refclk = ironlake_get_refclk(crtc);
7026
7027 /*
7028 * Returns a set of divisors for the desired target clock with the given
7029 * refclk, or FALSE. The returned values represent the clock equation:
7030 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7031 */
7032 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02007033 ret = dev_priv->display.find_dpll(limit, crtc,
7034 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007035 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007036 if (!ret)
7037 return false;
7038
7039 if (is_lvds && dev_priv->lvds_downclock_avail) {
7040 /*
7041 * Ensure we match the reduced clock's P to the target clock.
7042 * If the clocks don't match, we can't switch the display clock
7043 * by using the FP0/FP1. In such case we will disable the LVDS
7044 * downclock feature.
7045 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02007046 *has_reduced_clock =
7047 dev_priv->display.find_dpll(limit, crtc,
7048 dev_priv->lvds_downclock,
7049 refclk, clock,
7050 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007051 }
7052
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007053 return true;
7054}
7055
Paulo Zanonid4b19312012-11-29 11:29:32 -02007056int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7057{
7058 /*
7059 * Account for spread spectrum to avoid
7060 * oversubscribing the link. Max center spread
7061 * is 2.5%; use 5% for safety's sake.
7062 */
7063 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02007064 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02007065}
7066
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007067static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02007068{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007069 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007070}
7071
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007072static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007073 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007074 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007075{
7076 struct drm_crtc *crtc = &intel_crtc->base;
7077 struct drm_device *dev = crtc->dev;
7078 struct drm_i915_private *dev_priv = dev->dev_private;
7079 struct intel_encoder *intel_encoder;
7080 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007081 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02007082 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007083
7084 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7085 switch (intel_encoder->type) {
7086 case INTEL_OUTPUT_LVDS:
7087 is_lvds = true;
7088 break;
7089 case INTEL_OUTPUT_SDVO:
7090 case INTEL_OUTPUT_HDMI:
7091 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007092 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007093 }
7094
7095 num_connectors++;
7096 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007097
Chris Wilsonc1858122010-12-03 21:35:48 +00007098 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07007099 factor = 21;
7100 if (is_lvds) {
7101 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007102 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02007103 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07007104 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02007105 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07007106 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00007107
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007108 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02007109 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00007110
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007111 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7112 *fp2 |= FP_CB_TUNE;
7113
Chris Wilson5eddb702010-09-11 13:48:45 +01007114 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08007115
Eric Anholta07d6782011-03-30 13:01:08 -07007116 if (is_lvds)
7117 dpll |= DPLLB_MODE_LVDS;
7118 else
7119 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007120
Daniel Vetteref1b4602013-06-01 17:17:04 +02007121 dpll |= (intel_crtc->config.pixel_multiplier - 1)
7122 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007123
7124 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007125 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02007126 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007127 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08007128
Eric Anholta07d6782011-03-30 13:01:08 -07007129 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007130 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007131 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007132 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007133
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007134 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07007135 case 5:
7136 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7137 break;
7138 case 7:
7139 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7140 break;
7141 case 10:
7142 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7143 break;
7144 case 14:
7145 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7146 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007147 }
7148
Daniel Vetterb4c09f32013-04-30 14:01:42 +02007149 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007150 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08007151 else
7152 dpll |= PLL_REF_INPUT_DREFCLK;
7153
Daniel Vetter959e16d2013-06-05 13:34:21 +02007154 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007155}
7156
Jesse Barnes79e53942008-11-07 14:24:08 -08007157static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08007158 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02007159 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08007160{
7161 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007162 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007163 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007164 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007165 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03007166 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01007167 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007168 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02007169 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007170
7171 for_each_encoder_on_crtc(dev, crtc, encoder) {
7172 switch (encoder->type) {
7173 case INTEL_OUTPUT_LVDS:
7174 is_lvds = true;
7175 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007176 }
7177
7178 num_connectors++;
7179 }
7180
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007181 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7182 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
7183
Daniel Vetterff9a6752013-06-01 17:16:21 +02007184 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007185 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02007186 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007187 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7188 return -EINVAL;
7189 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01007190 /* Compat-code for transition, will disappear. */
7191 if (!intel_crtc->config.clock_set) {
7192 intel_crtc->config.dpll.n = clock.n;
7193 intel_crtc->config.dpll.m1 = clock.m1;
7194 intel_crtc->config.dpll.m2 = clock.m2;
7195 intel_crtc->config.dpll.p1 = clock.p1;
7196 intel_crtc->config.dpll.p2 = clock.p2;
7197 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007198
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007199 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01007200 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007201 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007202 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007203 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007204
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007205 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007206 &fp, &reduced_clock,
7207 has_reduced_clock ? &fp2 : NULL);
7208
Daniel Vetter959e16d2013-06-05 13:34:21 +02007209 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007210 intel_crtc->config.dpll_hw_state.fp0 = fp;
7211 if (has_reduced_clock)
7212 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7213 else
7214 intel_crtc->config.dpll_hw_state.fp1 = fp;
7215
Daniel Vetterb89a1d32013-06-05 13:34:24 +02007216 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007217 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03007218 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Daniel Vetter29407aa2014-04-24 23:55:08 +02007219 pipe_name(intel_crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07007220 return -EINVAL;
7221 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007222 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02007223 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007224
Jani Nikulad330a952014-01-21 11:24:25 +02007225 if (is_lvds && has_reduced_clock && i915.powersave)
Daniel Vetterbcd644e2013-06-05 13:34:22 +02007226 intel_crtc->lowfreq_avail = true;
7227 else
7228 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007229
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007230 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007231}
7232
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007233static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7234 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02007235{
7236 struct drm_device *dev = crtc->base.dev;
7237 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007238 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02007239
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007240 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7241 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7242 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7243 & ~TU_SIZE_MASK;
7244 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7245 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7246 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7247}
7248
7249static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7250 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007251 struct intel_link_m_n *m_n,
7252 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007253{
7254 struct drm_device *dev = crtc->base.dev;
7255 struct drm_i915_private *dev_priv = dev->dev_private;
7256 enum pipe pipe = crtc->pipe;
7257
7258 if (INTEL_INFO(dev)->gen >= 5) {
7259 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7260 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7261 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7262 & ~TU_SIZE_MASK;
7263 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7264 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7265 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007266 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7267 * gen < 8) and if DRRS is supported (to make sure the
7268 * registers are not unnecessarily read).
7269 */
7270 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
7271 crtc->config.has_drrs) {
7272 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7273 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7274 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7275 & ~TU_SIZE_MASK;
7276 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7277 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7278 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7279 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007280 } else {
7281 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7282 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7283 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7284 & ~TU_SIZE_MASK;
7285 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7286 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7287 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7288 }
7289}
7290
7291void intel_dp_get_m_n(struct intel_crtc *crtc,
7292 struct intel_crtc_config *pipe_config)
7293{
7294 if (crtc->config.has_pch_encoder)
7295 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7296 else
7297 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007298 &pipe_config->dp_m_n,
7299 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007300}
7301
Daniel Vetter72419202013-04-04 13:28:53 +02007302static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7303 struct intel_crtc_config *pipe_config)
7304{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007305 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007306 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02007307}
7308
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007309static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7310 struct intel_crtc_config *pipe_config)
7311{
7312 struct drm_device *dev = crtc->base.dev;
7313 struct drm_i915_private *dev_priv = dev->dev_private;
7314 uint32_t tmp;
7315
7316 tmp = I915_READ(PF_CTL(crtc->pipe));
7317
7318 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01007319 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007320 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7321 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02007322
7323 /* We currently do not free assignements of panel fitters on
7324 * ivb/hsw (since we don't use the higher upscaling modes which
7325 * differentiates them) so just WARN about this case for now. */
7326 if (IS_GEN7(dev)) {
7327 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7328 PF_PIPE_SEL_IVB(crtc->pipe));
7329 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007330 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007331}
7332
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007333static void ironlake_get_plane_config(struct intel_crtc *crtc,
7334 struct intel_plane_config *plane_config)
7335{
7336 struct drm_device *dev = crtc->base.dev;
7337 struct drm_i915_private *dev_priv = dev->dev_private;
7338 u32 val, base, offset;
7339 int pipe = crtc->pipe, plane = crtc->plane;
7340 int fourcc, pixel_format;
7341 int aligned_height;
7342
Dave Airlie66e514c2014-04-03 07:51:54 +10007343 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7344 if (!crtc->base.primary->fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007345 DRM_DEBUG_KMS("failed to alloc fb\n");
7346 return;
7347 }
7348
7349 val = I915_READ(DSPCNTR(plane));
7350
7351 if (INTEL_INFO(dev)->gen >= 4)
7352 if (val & DISPPLANE_TILED)
7353 plane_config->tiled = true;
7354
7355 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7356 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10007357 crtc->base.primary->fb->pixel_format = fourcc;
7358 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007359 drm_format_plane_cpp(fourcc, 0) * 8;
7360
7361 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7362 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7363 offset = I915_READ(DSPOFFSET(plane));
7364 } else {
7365 if (plane_config->tiled)
7366 offset = I915_READ(DSPTILEOFF(plane));
7367 else
7368 offset = I915_READ(DSPLINOFF(plane));
7369 }
7370 plane_config->base = base;
7371
7372 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10007373 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7374 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007375
7376 val = I915_READ(DSPSTRIDE(pipe));
Rafael Barbalho026b96e2014-07-28 19:56:27 +01007377 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007378
Dave Airlie66e514c2014-04-03 07:51:54 +10007379 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007380 plane_config->tiled);
7381
Fabian Frederick1267a262014-07-01 20:39:41 +02007382 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
7383 aligned_height);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007384
7385 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
Dave Airlie66e514c2014-04-03 07:51:54 +10007386 pipe, plane, crtc->base.primary->fb->width,
7387 crtc->base.primary->fb->height,
7388 crtc->base.primary->fb->bits_per_pixel, base,
7389 crtc->base.primary->fb->pitches[0],
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007390 plane_config->size);
7391}
7392
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007393static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7394 struct intel_crtc_config *pipe_config)
7395{
7396 struct drm_device *dev = crtc->base.dev;
7397 struct drm_i915_private *dev_priv = dev->dev_private;
7398 uint32_t tmp;
7399
Paulo Zanoni930e8c92014-07-04 13:38:34 -03007400 if (!intel_display_power_enabled(dev_priv,
7401 POWER_DOMAIN_PIPE(crtc->pipe)))
7402 return false;
7403
Daniel Vettere143a212013-07-04 12:01:15 +02007404 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007405 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007406
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007407 tmp = I915_READ(PIPECONF(crtc->pipe));
7408 if (!(tmp & PIPECONF_ENABLE))
7409 return false;
7410
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007411 switch (tmp & PIPECONF_BPC_MASK) {
7412 case PIPECONF_6BPC:
7413 pipe_config->pipe_bpp = 18;
7414 break;
7415 case PIPECONF_8BPC:
7416 pipe_config->pipe_bpp = 24;
7417 break;
7418 case PIPECONF_10BPC:
7419 pipe_config->pipe_bpp = 30;
7420 break;
7421 case PIPECONF_12BPC:
7422 pipe_config->pipe_bpp = 36;
7423 break;
7424 default:
7425 break;
7426 }
7427
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007428 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7429 pipe_config->limited_color_range = true;
7430
Daniel Vetterab9412b2013-05-03 11:49:46 +02007431 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02007432 struct intel_shared_dpll *pll;
7433
Daniel Vetter88adfff2013-03-28 10:42:01 +01007434 pipe_config->has_pch_encoder = true;
7435
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007436 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7437 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7438 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007439
7440 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007441
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007442 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02007443 pipe_config->shared_dpll =
7444 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007445 } else {
7446 tmp = I915_READ(PCH_DPLL_SEL);
7447 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7448 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7449 else
7450 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7451 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02007452
7453 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7454
7455 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7456 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02007457
7458 tmp = pipe_config->dpll_hw_state.dpll;
7459 pipe_config->pixel_multiplier =
7460 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7461 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007462
7463 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007464 } else {
7465 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007466 }
7467
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007468 intel_get_pipe_timings(crtc, pipe_config);
7469
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007470 ironlake_get_pfit_config(crtc, pipe_config);
7471
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007472 return true;
7473}
7474
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007475static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7476{
7477 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007478 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007479
Damien Lespiaud3fcc802014-05-13 23:32:22 +01007480 for_each_intel_crtc(dev, crtc)
Paulo Zanoni798183c2013-12-06 20:29:01 -02007481 WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007482 pipe_name(crtc->pipe));
7483
7484 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
Daniel Vetter8cc3e162014-06-25 22:01:46 +03007485 WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
7486 WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
7487 WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007488 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7489 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7490 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03007491 if (IS_HASWELL(dev))
7492 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7493 "CPU PWM2 enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007494 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7495 "PCH PWM1 enabled\n");
7496 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7497 "Utility pin enabled\n");
7498 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7499
Paulo Zanoni9926ada2014-04-01 19:39:47 -03007500 /*
7501 * In theory we can still leave IRQs enabled, as long as only the HPD
7502 * interrupts remain enabled. We used to check for that, but since it's
7503 * gen-specific and since we only disable LCPLL after we fully disable
7504 * the interrupts, the check below should be enough.
7505 */
Jesse Barnes9df7575f2014-06-20 09:29:20 -07007506 WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007507}
7508
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007509static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
7510{
7511 struct drm_device *dev = dev_priv->dev;
7512
7513 if (IS_HASWELL(dev))
7514 return I915_READ(D_COMP_HSW);
7515 else
7516 return I915_READ(D_COMP_BDW);
7517}
7518
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007519static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7520{
7521 struct drm_device *dev = dev_priv->dev;
7522
7523 if (IS_HASWELL(dev)) {
7524 mutex_lock(&dev_priv->rps.hw_lock);
7525 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7526 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03007527 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007528 mutex_unlock(&dev_priv->rps.hw_lock);
7529 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007530 I915_WRITE(D_COMP_BDW, val);
7531 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007532 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007533}
7534
7535/*
7536 * This function implements pieces of two sequences from BSpec:
7537 * - Sequence for display software to disable LCPLL
7538 * - Sequence for display software to allow package C8+
7539 * The steps implemented here are just the steps that actually touch the LCPLL
7540 * register. Callers should take care of disabling all the display engine
7541 * functions, doing the mode unset, fixing interrupts, etc.
7542 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007543static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7544 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007545{
7546 uint32_t val;
7547
7548 assert_can_disable_lcpll(dev_priv);
7549
7550 val = I915_READ(LCPLL_CTL);
7551
7552 if (switch_to_fclk) {
7553 val |= LCPLL_CD_SOURCE_FCLK;
7554 I915_WRITE(LCPLL_CTL, val);
7555
7556 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7557 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7558 DRM_ERROR("Switching to FCLK failed\n");
7559
7560 val = I915_READ(LCPLL_CTL);
7561 }
7562
7563 val |= LCPLL_PLL_DISABLE;
7564 I915_WRITE(LCPLL_CTL, val);
7565 POSTING_READ(LCPLL_CTL);
7566
7567 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7568 DRM_ERROR("LCPLL still locked\n");
7569
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007570 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007571 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007572 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007573 ndelay(100);
7574
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007575 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
7576 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007577 DRM_ERROR("D_COMP RCOMP still in progress\n");
7578
7579 if (allow_power_down) {
7580 val = I915_READ(LCPLL_CTL);
7581 val |= LCPLL_POWER_DOWN_ALLOW;
7582 I915_WRITE(LCPLL_CTL, val);
7583 POSTING_READ(LCPLL_CTL);
7584 }
7585}
7586
7587/*
7588 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7589 * source.
7590 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007591static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007592{
7593 uint32_t val;
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007594 unsigned long irqflags;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007595
7596 val = I915_READ(LCPLL_CTL);
7597
7598 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7599 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7600 return;
7601
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007602 /*
7603 * Make sure we're not on PC8 state before disabling PC8, otherwise
7604 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7605 *
7606 * The other problem is that hsw_restore_lcpll() is called as part of
7607 * the runtime PM resume sequence, so we can't just call
7608 * gen6_gt_force_wake_get() because that function calls
7609 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7610 * while we are on the resume sequence. So to solve this problem we have
7611 * to call special forcewake code that doesn't touch runtime PM and
7612 * doesn't enable the forcewake delayed work.
7613 */
7614 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7615 if (dev_priv->uncore.forcewake_count++ == 0)
7616 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7617 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Paulo Zanoni215733f2013-08-19 13:18:07 -03007618
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007619 if (val & LCPLL_POWER_DOWN_ALLOW) {
7620 val &= ~LCPLL_POWER_DOWN_ALLOW;
7621 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02007622 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007623 }
7624
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007625 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007626 val |= D_COMP_COMP_FORCE;
7627 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007628 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007629
7630 val = I915_READ(LCPLL_CTL);
7631 val &= ~LCPLL_PLL_DISABLE;
7632 I915_WRITE(LCPLL_CTL, val);
7633
7634 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7635 DRM_ERROR("LCPLL not locked yet\n");
7636
7637 if (val & LCPLL_CD_SOURCE_FCLK) {
7638 val = I915_READ(LCPLL_CTL);
7639 val &= ~LCPLL_CD_SOURCE_FCLK;
7640 I915_WRITE(LCPLL_CTL, val);
7641
7642 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7643 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7644 DRM_ERROR("Switching back to LCPLL failed\n");
7645 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03007646
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007647 /* See the big comment above. */
7648 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7649 if (--dev_priv->uncore.forcewake_count == 0)
7650 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7651 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007652}
7653
Paulo Zanoni765dab672014-03-07 20:08:18 -03007654/*
7655 * Package states C8 and deeper are really deep PC states that can only be
7656 * reached when all the devices on the system allow it, so even if the graphics
7657 * device allows PC8+, it doesn't mean the system will actually get to these
7658 * states. Our driver only allows PC8+ when going into runtime PM.
7659 *
7660 * The requirements for PC8+ are that all the outputs are disabled, the power
7661 * well is disabled and most interrupts are disabled, and these are also
7662 * requirements for runtime PM. When these conditions are met, we manually do
7663 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7664 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7665 * hang the machine.
7666 *
7667 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7668 * the state of some registers, so when we come back from PC8+ we need to
7669 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7670 * need to take care of the registers kept by RC6. Notice that this happens even
7671 * if we don't put the device in PCI D3 state (which is what currently happens
7672 * because of the runtime PM support).
7673 *
7674 * For more, read "Display Sequences for Package C8" on the hardware
7675 * documentation.
7676 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007677void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007678{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007679 struct drm_device *dev = dev_priv->dev;
7680 uint32_t val;
7681
Paulo Zanonic67a4702013-08-19 13:18:09 -03007682 DRM_DEBUG_KMS("Enabling package C8+\n");
7683
Paulo Zanonic67a4702013-08-19 13:18:09 -03007684 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7685 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7686 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7687 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7688 }
7689
7690 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007691 hsw_disable_lcpll(dev_priv, true, true);
7692}
7693
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007694void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007695{
7696 struct drm_device *dev = dev_priv->dev;
7697 uint32_t val;
7698
Paulo Zanonic67a4702013-08-19 13:18:09 -03007699 DRM_DEBUG_KMS("Disabling package C8+\n");
7700
7701 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007702 lpt_init_pch_refclk(dev);
7703
7704 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7705 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7706 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7707 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7708 }
7709
7710 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007711}
7712
Paulo Zanoni9a952a02014-03-07 20:12:34 -03007713static void snb_modeset_global_resources(struct drm_device *dev)
7714{
7715 modeset_update_crtc_power_domains(dev);
7716}
7717
Imre Deak4f074122013-10-16 17:25:51 +03007718static void haswell_modeset_global_resources(struct drm_device *dev)
7719{
Paulo Zanonida723562013-12-19 11:54:51 -02007720 modeset_update_crtc_power_domains(dev);
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02007721}
7722
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007723static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007724 int x, int y,
7725 struct drm_framebuffer *fb)
7726{
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007728
Paulo Zanoni566b7342013-11-25 15:27:08 -02007729 if (!intel_ddi_pll_select(intel_crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03007730 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03007731
Daniel Vetter644cef32014-04-24 23:55:07 +02007732 intel_crtc->lowfreq_avail = false;
7733
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007734 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007735}
7736
Damien Lespiau7d2c8172014-07-29 18:06:18 +01007737static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
7738 enum port port,
7739 struct intel_crtc_config *pipe_config)
7740{
7741 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
7742
7743 switch (pipe_config->ddi_pll_sel) {
7744 case PORT_CLK_SEL_WRPLL1:
7745 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
7746 break;
7747 case PORT_CLK_SEL_WRPLL2:
7748 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
7749 break;
7750 }
7751}
7752
Daniel Vetter26804af2014-06-25 22:01:55 +03007753static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
7754 struct intel_crtc_config *pipe_config)
7755{
7756 struct drm_device *dev = crtc->base.dev;
7757 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03007758 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03007759 enum port port;
7760 uint32_t tmp;
7761
7762 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
7763
7764 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
7765
Damien Lespiau7d2c8172014-07-29 18:06:18 +01007766 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03007767
Daniel Vetterd452c5b2014-07-04 11:27:39 -03007768 if (pipe_config->shared_dpll >= 0) {
7769 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7770
7771 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7772 &pipe_config->dpll_hw_state));
7773 }
7774
Daniel Vetter26804af2014-06-25 22:01:55 +03007775 /*
7776 * Haswell has only FDI/PCH transcoder A. It is which is connected to
7777 * DDI E. So just check whether this pipe is wired to DDI E and whether
7778 * the PCH transcoder is on.
7779 */
7780 if ((port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
7781 pipe_config->has_pch_encoder = true;
7782
7783 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7784 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7785 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7786
7787 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7788 }
7789}
7790
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007791static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7792 struct intel_crtc_config *pipe_config)
7793{
7794 struct drm_device *dev = crtc->base.dev;
7795 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007796 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007797 uint32_t tmp;
7798
Imre Deakb5482bd2014-03-05 16:20:55 +02007799 if (!intel_display_power_enabled(dev_priv,
7800 POWER_DOMAIN_PIPE(crtc->pipe)))
7801 return false;
7802
Daniel Vettere143a212013-07-04 12:01:15 +02007803 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007804 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7805
Daniel Vettereccb1402013-05-22 00:50:22 +02007806 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7807 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7808 enum pipe trans_edp_pipe;
7809 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7810 default:
7811 WARN(1, "unknown pipe linked to edp transcoder\n");
7812 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7813 case TRANS_DDI_EDP_INPUT_A_ON:
7814 trans_edp_pipe = PIPE_A;
7815 break;
7816 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7817 trans_edp_pipe = PIPE_B;
7818 break;
7819 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7820 trans_edp_pipe = PIPE_C;
7821 break;
7822 }
7823
7824 if (trans_edp_pipe == crtc->pipe)
7825 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7826 }
7827
Imre Deakda7e29b2014-02-18 00:02:02 +02007828 if (!intel_display_power_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02007829 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03007830 return false;
7831
Daniel Vettereccb1402013-05-22 00:50:22 +02007832 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007833 if (!(tmp & PIPECONF_ENABLE))
7834 return false;
7835
Daniel Vetter26804af2014-06-25 22:01:55 +03007836 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007837
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007838 intel_get_pipe_timings(crtc, pipe_config);
7839
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007840 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Imre Deakda7e29b2014-02-18 00:02:02 +02007841 if (intel_display_power_enabled(dev_priv, pfit_domain))
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007842 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01007843
Jesse Barnese59150d2014-01-07 13:30:45 -08007844 if (IS_HASWELL(dev))
7845 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7846 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007847
Daniel Vetter6c49f242013-06-06 12:45:25 +02007848 pipe_config->pixel_multiplier = 1;
7849
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007850 return true;
7851}
7852
Jani Nikula1a915102013-10-16 12:34:48 +03007853static struct {
7854 int clock;
7855 u32 config;
7856} hdmi_audio_clock[] = {
7857 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7858 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7859 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7860 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7861 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7862 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7863 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7864 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7865 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7866 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7867};
7868
7869/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7870static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7871{
7872 int i;
7873
7874 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7875 if (mode->clock == hdmi_audio_clock[i].clock)
7876 break;
7877 }
7878
7879 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7880 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7881 i = 1;
7882 }
7883
7884 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7885 hdmi_audio_clock[i].clock,
7886 hdmi_audio_clock[i].config);
7887
7888 return hdmi_audio_clock[i].config;
7889}
7890
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007891static bool intel_eld_uptodate(struct drm_connector *connector,
7892 int reg_eldv, uint32_t bits_eldv,
7893 int reg_elda, uint32_t bits_elda,
7894 int reg_edid)
7895{
7896 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7897 uint8_t *eld = connector->eld;
7898 uint32_t i;
7899
7900 i = I915_READ(reg_eldv);
7901 i &= bits_eldv;
7902
7903 if (!eld[0])
7904 return !i;
7905
7906 if (!i)
7907 return false;
7908
7909 i = I915_READ(reg_elda);
7910 i &= ~bits_elda;
7911 I915_WRITE(reg_elda, i);
7912
7913 for (i = 0; i < eld[2]; i++)
7914 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7915 return false;
7916
7917 return true;
7918}
7919
Wu Fengguange0dac652011-09-05 14:25:34 +08007920static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007921 struct drm_crtc *crtc,
7922 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007923{
7924 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7925 uint8_t *eld = connector->eld;
7926 uint32_t eldv;
7927 uint32_t len;
7928 uint32_t i;
7929
7930 i = I915_READ(G4X_AUD_VID_DID);
7931
7932 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7933 eldv = G4X_ELDV_DEVCL_DEVBLC;
7934 else
7935 eldv = G4X_ELDV_DEVCTG;
7936
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007937 if (intel_eld_uptodate(connector,
7938 G4X_AUD_CNTL_ST, eldv,
7939 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7940 G4X_HDMIW_HDMIEDID))
7941 return;
7942
Wu Fengguange0dac652011-09-05 14:25:34 +08007943 i = I915_READ(G4X_AUD_CNTL_ST);
7944 i &= ~(eldv | G4X_ELD_ADDR);
7945 len = (i >> 9) & 0x1f; /* ELD buffer size */
7946 I915_WRITE(G4X_AUD_CNTL_ST, i);
7947
7948 if (!eld[0])
7949 return;
7950
7951 len = min_t(uint8_t, eld[2], len);
7952 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7953 for (i = 0; i < len; i++)
7954 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7955
7956 i = I915_READ(G4X_AUD_CNTL_ST);
7957 i |= eldv;
7958 I915_WRITE(G4X_AUD_CNTL_ST, i);
7959}
7960
Wang Xingchao83358c852012-08-16 22:43:37 +08007961static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007962 struct drm_crtc *crtc,
7963 struct drm_display_mode *mode)
Wang Xingchao83358c852012-08-16 22:43:37 +08007964{
7965 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7966 uint8_t *eld = connector->eld;
Wang Xingchao83358c852012-08-16 22:43:37 +08007967 uint32_t eldv;
7968 uint32_t i;
7969 int len;
7970 int pipe = to_intel_crtc(crtc)->pipe;
7971 int tmp;
7972
7973 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7974 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7975 int aud_config = HSW_AUD_CFG(pipe);
7976 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7977
Wang Xingchao83358c852012-08-16 22:43:37 +08007978 /* Audio output enable */
7979 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7980 tmp = I915_READ(aud_cntrl_st2);
7981 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7982 I915_WRITE(aud_cntrl_st2, tmp);
Daniel Vetterc7905792014-04-16 16:56:09 +02007983 POSTING_READ(aud_cntrl_st2);
Wang Xingchao83358c852012-08-16 22:43:37 +08007984
Daniel Vetterc7905792014-04-16 16:56:09 +02007985 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Wang Xingchao83358c852012-08-16 22:43:37 +08007986
7987 /* Set ELD valid state */
7988 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007989 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007990 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7991 I915_WRITE(aud_cntrl_st2, tmp);
7992 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007993 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007994
7995 /* Enable HDMI mode */
7996 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007997 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007998 /* clear N_programing_enable and N_value_index */
7999 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
8000 I915_WRITE(aud_config, tmp);
8001
8002 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
8003
8004 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
8005
8006 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8007 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8008 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
8009 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03008010 } else {
8011 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8012 }
Wang Xingchao83358c852012-08-16 22:43:37 +08008013
8014 if (intel_eld_uptodate(connector,
8015 aud_cntrl_st2, eldv,
8016 aud_cntl_st, IBX_ELD_ADDRESS,
8017 hdmiw_hdmiedid))
8018 return;
8019
8020 i = I915_READ(aud_cntrl_st2);
8021 i &= ~eldv;
8022 I915_WRITE(aud_cntrl_st2, i);
8023
8024 if (!eld[0])
8025 return;
8026
8027 i = I915_READ(aud_cntl_st);
8028 i &= ~IBX_ELD_ADDRESS;
8029 I915_WRITE(aud_cntl_st, i);
8030 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
8031 DRM_DEBUG_DRIVER("port num:%d\n", i);
8032
8033 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8034 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8035 for (i = 0; i < len; i++)
8036 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8037
8038 i = I915_READ(aud_cntrl_st2);
8039 i |= eldv;
8040 I915_WRITE(aud_cntrl_st2, i);
8041
8042}
8043
Wu Fengguange0dac652011-09-05 14:25:34 +08008044static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03008045 struct drm_crtc *crtc,
8046 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08008047{
8048 struct drm_i915_private *dev_priv = connector->dev->dev_private;
8049 uint8_t *eld = connector->eld;
8050 uint32_t eldv;
8051 uint32_t i;
8052 int len;
8053 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06008054 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08008055 int aud_cntl_st;
8056 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08008057 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08008058
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08008059 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08008060 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
8061 aud_config = IBX_AUD_CFG(pipe);
8062 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08008063 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04008064 } else if (IS_VALLEYVIEW(connector->dev)) {
8065 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
8066 aud_config = VLV_AUD_CFG(pipe);
8067 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
8068 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08008069 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08008070 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
8071 aud_config = CPT_AUD_CFG(pipe);
8072 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08008073 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08008074 }
8075
Wang Xingchao9b138a82012-08-09 16:52:18 +08008076 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08008077
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04008078 if (IS_VALLEYVIEW(connector->dev)) {
8079 struct intel_encoder *intel_encoder;
8080 struct intel_digital_port *intel_dig_port;
8081
8082 intel_encoder = intel_attached_encoder(connector);
8083 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
8084 i = intel_dig_port->port;
8085 } else {
8086 i = I915_READ(aud_cntl_st);
8087 i = (i >> 29) & DIP_PORT_SEL_MASK;
8088 /* DIP_Port_Select, 0x1 = PortB */
8089 }
8090
Wu Fengguange0dac652011-09-05 14:25:34 +08008091 if (!i) {
8092 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
8093 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08008094 eldv = IBX_ELD_VALIDB;
8095 eldv |= IBX_ELD_VALIDB << 4;
8096 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08008097 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03008098 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08008099 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08008100 }
8101
Wu Fengguang3a9627f2011-12-09 20:42:19 +08008102 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8103 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8104 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06008105 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03008106 } else {
8107 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8108 }
Wu Fengguang3a9627f2011-12-09 20:42:19 +08008109
8110 if (intel_eld_uptodate(connector,
8111 aud_cntrl_st2, eldv,
8112 aud_cntl_st, IBX_ELD_ADDRESS,
8113 hdmiw_hdmiedid))
8114 return;
8115
Wu Fengguange0dac652011-09-05 14:25:34 +08008116 i = I915_READ(aud_cntrl_st2);
8117 i &= ~eldv;
8118 I915_WRITE(aud_cntrl_st2, i);
8119
8120 if (!eld[0])
8121 return;
8122
Wu Fengguange0dac652011-09-05 14:25:34 +08008123 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08008124 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08008125 I915_WRITE(aud_cntl_st, i);
8126
8127 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8128 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8129 for (i = 0; i < len; i++)
8130 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8131
8132 i = I915_READ(aud_cntrl_st2);
8133 i |= eldv;
8134 I915_WRITE(aud_cntrl_st2, i);
8135}
8136
8137void intel_write_eld(struct drm_encoder *encoder,
8138 struct drm_display_mode *mode)
8139{
8140 struct drm_crtc *crtc = encoder->crtc;
8141 struct drm_connector *connector;
8142 struct drm_device *dev = encoder->dev;
8143 struct drm_i915_private *dev_priv = dev->dev_private;
8144
8145 connector = drm_select_eld(encoder, mode);
8146 if (!connector)
8147 return;
8148
8149 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8150 connector->base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03008151 connector->name,
Wu Fengguange0dac652011-09-05 14:25:34 +08008152 connector->encoder->base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +03008153 connector->encoder->name);
Wu Fengguange0dac652011-09-05 14:25:34 +08008154
8155 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
8156
8157 if (dev_priv->display.write_eld)
Jani Nikula34427052013-10-16 12:34:47 +03008158 dev_priv->display.write_eld(connector, crtc, mode);
Wu Fengguange0dac652011-09-05 14:25:34 +08008159}
8160
Chris Wilson560b85b2010-08-07 11:01:38 +01008161static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8162{
8163 struct drm_device *dev = crtc->dev;
8164 struct drm_i915_private *dev_priv = dev->dev_private;
8165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03008166 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01008167
Ville Syrjälädc41c152014-08-13 11:57:05 +03008168 if (base) {
8169 unsigned int width = intel_crtc->cursor_width;
8170 unsigned int height = intel_crtc->cursor_height;
8171 unsigned int stride = roundup_pow_of_two(width) * 4;
8172
8173 switch (stride) {
8174 default:
8175 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8176 width, stride);
8177 stride = 256;
8178 /* fallthrough */
8179 case 256:
8180 case 512:
8181 case 1024:
8182 case 2048:
8183 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008184 }
8185
Ville Syrjälädc41c152014-08-13 11:57:05 +03008186 cntl |= CURSOR_ENABLE |
8187 CURSOR_GAMMA_ENABLE |
8188 CURSOR_FORMAT_ARGB |
8189 CURSOR_STRIDE(stride);
8190
8191 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008192 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008193
Ville Syrjälädc41c152014-08-13 11:57:05 +03008194 if (intel_crtc->cursor_cntl != 0 &&
8195 (intel_crtc->cursor_base != base ||
8196 intel_crtc->cursor_size != size ||
8197 intel_crtc->cursor_cntl != cntl)) {
8198 /* On these chipsets we can only modify the base/size/stride
8199 * whilst the cursor is disabled.
8200 */
8201 I915_WRITE(_CURACNTR, 0);
8202 POSTING_READ(_CURACNTR);
8203 intel_crtc->cursor_cntl = 0;
8204 }
8205
8206 if (intel_crtc->cursor_base != base)
8207 I915_WRITE(_CURABASE, base);
8208
8209 if (intel_crtc->cursor_size != size) {
8210 I915_WRITE(CURSIZE, size);
8211 intel_crtc->cursor_size = size;
8212 }
8213
Chris Wilson4b0e3332014-05-30 16:35:26 +03008214 if (intel_crtc->cursor_cntl != cntl) {
8215 I915_WRITE(_CURACNTR, cntl);
8216 POSTING_READ(_CURACNTR);
8217 intel_crtc->cursor_cntl = cntl;
8218 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008219}
8220
8221static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
8222{
8223 struct drm_device *dev = crtc->dev;
8224 struct drm_i915_private *dev_priv = dev->dev_private;
8225 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8226 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008227 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01008228
Chris Wilson4b0e3332014-05-30 16:35:26 +03008229 cntl = 0;
8230 if (base) {
8231 cntl = MCURSOR_GAMMA_ENABLE;
8232 switch (intel_crtc->cursor_width) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308233 case 64:
8234 cntl |= CURSOR_MODE_64_ARGB_AX;
8235 break;
8236 case 128:
8237 cntl |= CURSOR_MODE_128_ARGB_AX;
8238 break;
8239 case 256:
8240 cntl |= CURSOR_MODE_256_ARGB_AX;
8241 break;
8242 default:
8243 WARN_ON(1);
8244 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01008245 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008246 cntl |= pipe << 28; /* Connect to correct pipe */
Chris Wilson560b85b2010-08-07 11:01:38 +01008247 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008248 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8249 cntl |= CURSOR_PIPE_CSC_ENABLE;
8250
8251 if (intel_crtc->cursor_cntl != cntl) {
8252 I915_WRITE(CURCNTR(pipe), cntl);
8253 POSTING_READ(CURCNTR(pipe));
8254 intel_crtc->cursor_cntl = cntl;
8255 }
8256
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008257 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008258 I915_WRITE(CURBASE(pipe), base);
8259 POSTING_READ(CURBASE(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008260}
8261
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008262/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01008263static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8264 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008265{
8266 struct drm_device *dev = crtc->dev;
8267 struct drm_i915_private *dev_priv = dev->dev_private;
8268 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8269 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07008270 int x = crtc->cursor_x;
8271 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008272 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008273
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008274 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008275 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008276
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008277 if (x >= intel_crtc->config.pipe_src_w)
8278 base = 0;
8279
8280 if (y >= intel_crtc->config.pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008281 base = 0;
8282
8283 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008284 if (x + intel_crtc->cursor_width <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008285 base = 0;
8286
8287 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8288 x = -x;
8289 }
8290 pos |= x << CURSOR_X_SHIFT;
8291
8292 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008293 if (y + intel_crtc->cursor_height <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008294 base = 0;
8295
8296 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8297 y = -y;
8298 }
8299 pos |= y << CURSOR_Y_SHIFT;
8300
Chris Wilson4b0e3332014-05-30 16:35:26 +03008301 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008302 return;
8303
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008304 I915_WRITE(CURPOS(pipe), pos);
8305
Ville Syrjälä8ac54662014-08-12 19:39:54 +03008306 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008307 i845_update_cursor(crtc, base);
8308 else
8309 i9xx_update_cursor(crtc, base);
Chris Wilson4b0e3332014-05-30 16:35:26 +03008310 intel_crtc->cursor_base = base;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008311}
8312
Ville Syrjälädc41c152014-08-13 11:57:05 +03008313static bool cursor_size_ok(struct drm_device *dev,
8314 uint32_t width, uint32_t height)
8315{
8316 if (width == 0 || height == 0)
8317 return false;
8318
8319 /*
8320 * 845g/865g are special in that they are only limited by
8321 * the width of their cursors, the height is arbitrary up to
8322 * the precision of the register. Everything else requires
8323 * square cursors, limited to a few power-of-two sizes.
8324 */
8325 if (IS_845G(dev) || IS_I865G(dev)) {
8326 if ((width & 63) != 0)
8327 return false;
8328
8329 if (width > (IS_845G(dev) ? 64 : 512))
8330 return false;
8331
8332 if (height > 1023)
8333 return false;
8334 } else {
8335 switch (width | height) {
8336 case 256:
8337 case 128:
8338 if (IS_GEN2(dev))
8339 return false;
8340 case 64:
8341 break;
8342 default:
8343 return false;
8344 }
8345 }
8346
8347 return true;
8348}
8349
Matt Ropere3287952014-06-10 08:28:12 -07008350/*
8351 * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
8352 *
8353 * Note that the object's reference will be consumed if the update fails. If
8354 * the update succeeds, the reference of the old object (if any) will be
8355 * consumed.
8356 */
8357static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
8358 struct drm_i915_gem_object *obj,
8359 uint32_t width, uint32_t height)
Jesse Barnes79e53942008-11-07 14:24:08 -08008360{
8361 struct drm_device *dev = crtc->dev;
8362 struct drm_i915_private *dev_priv = dev->dev_private;
8363 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02008364 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälädc41c152014-08-13 11:57:05 +03008365 unsigned old_width, stride;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008366 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008367 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008368
Jesse Barnes79e53942008-11-07 14:24:08 -08008369 /* if we want to turn off the cursor ignore width and height */
Matt Ropere3287952014-06-10 08:28:12 -07008370 if (!obj) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008371 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008372 addr = 0;
Pierre Willenbrock50044172009-02-23 10:12:15 +10008373 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008374 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08008375 }
8376
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308377 /* Check for which cursor types we support */
Ville Syrjälädc41c152014-08-13 11:57:05 +03008378 if (!cursor_size_ok(dev, width, height)) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308379 DRM_DEBUG("Cursor dimension not supported\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08008380 return -EINVAL;
8381 }
8382
Ville Syrjälädc41c152014-08-13 11:57:05 +03008383 stride = roundup_pow_of_two(width) * 4;
8384 if (obj->base.size < stride * height) {
Matt Ropere3287952014-06-10 08:28:12 -07008385 DRM_DEBUG_KMS("buffer is too small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10008386 ret = -ENOMEM;
8387 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008388 }
8389
Dave Airlie71acb5e2008-12-30 20:31:46 +10008390 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008391 mutex_lock(&dev->struct_mutex);
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008392 if (!INTEL_INFO(dev)->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00008393 unsigned alignment;
8394
Chris Wilsond9e86c02010-11-10 16:40:20 +00008395 if (obj->tiling_mode) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008396 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Chris Wilsond9e86c02010-11-10 16:40:20 +00008397 ret = -EINVAL;
8398 goto fail_locked;
8399 }
8400
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008401 /*
8402 * Global gtt pte registers are special registers which actually
8403 * forward writes to a chunk of system memory. Which means that
8404 * there is no risk that the register values disappear as soon
8405 * as we call intel_runtime_pm_put(), so it is correct to wrap
8406 * only the pin/unpin/fence and not more.
8407 */
8408 intel_runtime_pm_get(dev_priv);
8409
Chris Wilson693db182013-03-05 14:52:39 +00008410 /* Note that the w/a also requires 2 PTE of padding following
8411 * the bo. We currently fill all unused PTE with the shadow
8412 * page and so we should always have valid PTE following the
8413 * cursor preventing the VT-d warning.
8414 */
8415 alignment = 0;
8416 if (need_vtd_wa(dev))
8417 alignment = 64*1024;
8418
8419 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01008420 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008421 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008422 intel_runtime_pm_put(dev_priv);
Chris Wilson2da3b9b2011-04-14 09:41:17 +01008423 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008424 }
8425
Chris Wilsond9e86c02010-11-10 16:40:20 +00008426 ret = i915_gem_object_put_fence(obj);
8427 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008428 DRM_DEBUG_KMS("failed to release fence for cursor");
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008429 intel_runtime_pm_put(dev_priv);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008430 goto fail_unpin;
8431 }
8432
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008433 addr = i915_gem_obj_ggtt_offset(obj);
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008434
8435 intel_runtime_pm_put(dev_priv);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008436 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01008437 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson00731152014-05-21 12:42:56 +01008438 ret = i915_gem_object_attach_phys(obj, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008439 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008440 DRM_DEBUG_KMS("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008441 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10008442 }
Chris Wilson00731152014-05-21 12:42:56 +01008443 addr = obj->phys_handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008444 }
8445
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008446 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008447 if (intel_crtc->cursor_bo) {
Chris Wilson00731152014-05-21 12:42:56 +01008448 if (!INTEL_INFO(dev)->cursor_needs_physical)
Chris Wilsoncc98b412013-08-09 12:25:09 +01008449 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008450 }
Jesse Barnes80824002009-09-10 15:28:06 -07008451
Daniel Vettera071fa02014-06-18 23:28:09 +02008452 i915_gem_track_fb(intel_crtc->cursor_bo, obj,
8453 INTEL_FRONTBUFFER_CURSOR(pipe));
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008454 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008455
Chris Wilson64f962e2014-03-26 12:38:15 +00008456 old_width = intel_crtc->cursor_width;
8457
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008458 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00008459 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008460 intel_crtc->cursor_width = width;
8461 intel_crtc->cursor_height = height;
8462
Chris Wilson64f962e2014-03-26 12:38:15 +00008463 if (intel_crtc->active) {
8464 if (old_width != width)
8465 intel_update_watermarks(crtc);
Ville Syrjäläf2f5f7712013-09-17 18:33:44 +03008466 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Chris Wilson64f962e2014-03-26 12:38:15 +00008467 }
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008468
Daniel Vetterf99d7062014-06-19 16:01:59 +02008469 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
8470
Jesse Barnes79e53942008-11-07 14:24:08 -08008471 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008472fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01008473 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008474fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10008475 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00008476fail:
Chris Wilson05394f32010-11-08 19:18:58 +00008477 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10008478 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008479}
8480
Jesse Barnes79e53942008-11-07 14:24:08 -08008481static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01008482 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08008483{
James Simmons72034252010-08-03 01:33:19 +01008484 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008485 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008486
James Simmons72034252010-08-03 01:33:19 +01008487 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008488 intel_crtc->lut_r[i] = red[i] >> 8;
8489 intel_crtc->lut_g[i] = green[i] >> 8;
8490 intel_crtc->lut_b[i] = blue[i] >> 8;
8491 }
8492
8493 intel_crtc_load_lut(crtc);
8494}
8495
Jesse Barnes79e53942008-11-07 14:24:08 -08008496/* VESA 640x480x72Hz mode to set on the pipe */
8497static struct drm_display_mode load_detect_mode = {
8498 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8499 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8500};
8501
Daniel Vettera8bb6812014-02-10 18:00:39 +01008502struct drm_framebuffer *
8503__intel_framebuffer_create(struct drm_device *dev,
8504 struct drm_mode_fb_cmd2 *mode_cmd,
8505 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01008506{
8507 struct intel_framebuffer *intel_fb;
8508 int ret;
8509
8510 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8511 if (!intel_fb) {
8512 drm_gem_object_unreference_unlocked(&obj->base);
8513 return ERR_PTR(-ENOMEM);
8514 }
8515
8516 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008517 if (ret)
8518 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01008519
8520 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008521err:
8522 drm_gem_object_unreference_unlocked(&obj->base);
8523 kfree(intel_fb);
8524
8525 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01008526}
8527
Daniel Vetterb5ea6422014-03-02 21:18:00 +01008528static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01008529intel_framebuffer_create(struct drm_device *dev,
8530 struct drm_mode_fb_cmd2 *mode_cmd,
8531 struct drm_i915_gem_object *obj)
8532{
8533 struct drm_framebuffer *fb;
8534 int ret;
8535
8536 ret = i915_mutex_lock_interruptible(dev);
8537 if (ret)
8538 return ERR_PTR(ret);
8539 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8540 mutex_unlock(&dev->struct_mutex);
8541
8542 return fb;
8543}
8544
Chris Wilsond2dff872011-04-19 08:36:26 +01008545static u32
8546intel_framebuffer_pitch_for_width(int width, int bpp)
8547{
8548 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8549 return ALIGN(pitch, 64);
8550}
8551
8552static u32
8553intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8554{
8555 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02008556 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01008557}
8558
8559static struct drm_framebuffer *
8560intel_framebuffer_create_for_mode(struct drm_device *dev,
8561 struct drm_display_mode *mode,
8562 int depth, int bpp)
8563{
8564 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00008565 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01008566
8567 obj = i915_gem_alloc_object(dev,
8568 intel_framebuffer_size_for_mode(mode, bpp));
8569 if (obj == NULL)
8570 return ERR_PTR(-ENOMEM);
8571
8572 mode_cmd.width = mode->hdisplay;
8573 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008574 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8575 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00008576 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01008577
8578 return intel_framebuffer_create(dev, &mode_cmd, obj);
8579}
8580
8581static struct drm_framebuffer *
8582mode_fits_in_fbdev(struct drm_device *dev,
8583 struct drm_display_mode *mode)
8584{
Daniel Vetter4520f532013-10-09 09:18:51 +02008585#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01008586 struct drm_i915_private *dev_priv = dev->dev_private;
8587 struct drm_i915_gem_object *obj;
8588 struct drm_framebuffer *fb;
8589
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008590 if (!dev_priv->fbdev)
8591 return NULL;
8592
8593 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01008594 return NULL;
8595
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008596 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008597 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01008598
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008599 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008600 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8601 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01008602 return NULL;
8603
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008604 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01008605 return NULL;
8606
8607 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02008608#else
8609 return NULL;
8610#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01008611}
8612
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008613bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01008614 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05008615 struct intel_load_detect_pipe *old,
8616 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008617{
8618 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008619 struct intel_encoder *intel_encoder =
8620 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08008621 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008622 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008623 struct drm_crtc *crtc = NULL;
8624 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02008625 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05008626 struct drm_mode_config *config = &dev->mode_config;
8627 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008628
Chris Wilsond2dff872011-04-19 08:36:26 +01008629 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008630 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008631 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008632
Rob Clark51fd3712013-11-19 12:10:12 -05008633retry:
8634 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8635 if (ret)
8636 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008637
Jesse Barnes79e53942008-11-07 14:24:08 -08008638 /*
8639 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01008640 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008641 * - if the connector already has an assigned crtc, use it (but make
8642 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01008643 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008644 * - try to find the first unused crtc that can drive this connector,
8645 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08008646 */
8647
8648 /* See if we already have a CRTC for this connector */
8649 if (encoder->crtc) {
8650 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01008651
Rob Clark51fd3712013-11-19 12:10:12 -05008652 ret = drm_modeset_lock(&crtc->mutex, ctx);
8653 if (ret)
8654 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01008655
Daniel Vetter24218aa2012-08-12 19:27:11 +02008656 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008657 old->load_detect_temp = false;
8658
8659 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008660 if (connector->dpms != DRM_MODE_DPMS_ON)
8661 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01008662
Chris Wilson71731882011-04-19 23:10:58 +01008663 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08008664 }
8665
8666 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008667 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008668 i++;
8669 if (!(encoder->possible_crtcs & (1 << i)))
8670 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +03008671 if (possible_crtc->enabled)
8672 continue;
8673 /* This can occur when applying the pipe A quirk on resume. */
8674 if (to_intel_crtc(possible_crtc)->new_enabled)
8675 continue;
8676
8677 crtc = possible_crtc;
8678 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008679 }
8680
8681 /*
8682 * If we didn't find an unused CRTC, don't use any.
8683 */
8684 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01008685 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05008686 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08008687 }
8688
Rob Clark51fd3712013-11-19 12:10:12 -05008689 ret = drm_modeset_lock(&crtc->mutex, ctx);
8690 if (ret)
8691 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02008692 intel_encoder->new_crtc = to_intel_crtc(crtc);
8693 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008694
8695 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008696 intel_crtc->new_enabled = true;
8697 intel_crtc->new_config = &intel_crtc->config;
Daniel Vetter24218aa2012-08-12 19:27:11 +02008698 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008699 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01008700 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08008701
Chris Wilson64927112011-04-20 07:25:26 +01008702 if (!mode)
8703 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08008704
Chris Wilsond2dff872011-04-19 08:36:26 +01008705 /* We need a framebuffer large enough to accommodate all accesses
8706 * that the plane may generate whilst we perform load detection.
8707 * We can not rely on the fbcon either being present (we get called
8708 * during its initialisation to detect all boot displays, or it may
8709 * not even exist) or that it is large enough to satisfy the
8710 * requested mode.
8711 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02008712 fb = mode_fits_in_fbdev(dev, mode);
8713 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008714 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008715 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8716 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01008717 } else
8718 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008719 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008720 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008721 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008722 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008723
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008724 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01008725 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01008726 if (old->release_fb)
8727 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008728 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008729 }
Chris Wilson71731882011-04-19 23:10:58 +01008730
Jesse Barnes79e53942008-11-07 14:24:08 -08008731 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008732 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01008733 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008734
8735 fail:
8736 intel_crtc->new_enabled = crtc->enabled;
8737 if (intel_crtc->new_enabled)
8738 intel_crtc->new_config = &intel_crtc->config;
8739 else
8740 intel_crtc->new_config = NULL;
Rob Clark51fd3712013-11-19 12:10:12 -05008741fail_unlock:
8742 if (ret == -EDEADLK) {
8743 drm_modeset_backoff(ctx);
8744 goto retry;
8745 }
8746
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008747 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08008748}
8749
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008750void intel_release_load_detect_pipe(struct drm_connector *connector,
Ville Syrjälä208bf9f2014-08-11 13:15:35 +03008751 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08008752{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008753 struct intel_encoder *intel_encoder =
8754 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01008755 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01008756 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008757 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008758
Chris Wilsond2dff872011-04-19 08:36:26 +01008759 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008760 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008761 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008762
Chris Wilson8261b192011-04-19 23:18:09 +01008763 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02008764 to_intel_connector(connector)->new_encoder = NULL;
8765 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008766 intel_crtc->new_enabled = false;
8767 intel_crtc->new_config = NULL;
Daniel Vetterfc303102012-07-09 10:40:58 +02008768 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01008769
Daniel Vetter36206362012-12-10 20:42:17 +01008770 if (old->release_fb) {
8771 drm_framebuffer_unregister_private(old->release_fb);
8772 drm_framebuffer_unreference(old->release_fb);
8773 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008774
Chris Wilson0622a532011-04-21 09:32:11 +01008775 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008776 }
8777
Eric Anholtc751ce42010-03-25 11:48:48 -07008778 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008779 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8780 connector->funcs->dpms(connector, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008781}
8782
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008783static int i9xx_pll_refclk(struct drm_device *dev,
8784 const struct intel_crtc_config *pipe_config)
8785{
8786 struct drm_i915_private *dev_priv = dev->dev_private;
8787 u32 dpll = pipe_config->dpll_hw_state.dpll;
8788
8789 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008790 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008791 else if (HAS_PCH_SPLIT(dev))
8792 return 120000;
8793 else if (!IS_GEN2(dev))
8794 return 96000;
8795 else
8796 return 48000;
8797}
8798
Jesse Barnes79e53942008-11-07 14:24:08 -08008799/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008800static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8801 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08008802{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008803 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008804 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008805 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008806 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008807 u32 fp;
8808 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008809 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08008810
8811 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03008812 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008813 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03008814 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008815
8816 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008817 if (IS_PINEVIEW(dev)) {
8818 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8819 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08008820 } else {
8821 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8822 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8823 }
8824
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008825 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008826 if (IS_PINEVIEW(dev))
8827 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8828 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08008829 else
8830 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08008831 DPLL_FPA01_P1_POST_DIV_SHIFT);
8832
8833 switch (dpll & DPLL_MODE_MASK) {
8834 case DPLLB_MODE_DAC_SERIAL:
8835 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8836 5 : 10;
8837 break;
8838 case DPLLB_MODE_LVDS:
8839 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8840 7 : 14;
8841 break;
8842 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08008843 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08008844 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008845 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008846 }
8847
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008848 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008849 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008850 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008851 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008852 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02008853 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008854 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08008855
8856 if (is_lvds) {
8857 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8858 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008859
8860 if (lvds & LVDS_CLKB_POWER_UP)
8861 clock.p2 = 7;
8862 else
8863 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08008864 } else {
8865 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8866 clock.p1 = 2;
8867 else {
8868 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8869 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8870 }
8871 if (dpll & PLL_P2_DIVIDE_BY_4)
8872 clock.p2 = 4;
8873 else
8874 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08008875 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008876
8877 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008878 }
8879
Ville Syrjälä18442d02013-09-13 16:00:08 +03008880 /*
8881 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01008882 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03008883 * encoder's get_config() function.
8884 */
8885 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008886}
8887
Ville Syrjälä6878da02013-09-13 15:59:11 +03008888int intel_dotclock_calculate(int link_freq,
8889 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008890{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008891 /*
8892 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008893 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008894 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008895 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008896 *
8897 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008898 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08008899 */
8900
Ville Syrjälä6878da02013-09-13 15:59:11 +03008901 if (!m_n->link_n)
8902 return 0;
8903
8904 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8905}
8906
Ville Syrjälä18442d02013-09-13 16:00:08 +03008907static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8908 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03008909{
8910 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008911
8912 /* read out port_clock from the DPLL */
8913 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03008914
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008915 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03008916 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01008917 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03008918 * agree once we know their relationship in the encoder's
8919 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008920 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01008921 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03008922 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8923 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08008924}
8925
8926/** Returns the currently programmed mode of the given pipe. */
8927struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8928 struct drm_crtc *crtc)
8929{
Jesse Barnes548f2452011-02-17 10:40:53 -08008930 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008931 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02008932 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008933 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008934 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008935 int htot = I915_READ(HTOTAL(cpu_transcoder));
8936 int hsync = I915_READ(HSYNC(cpu_transcoder));
8937 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8938 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008939 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008940
8941 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8942 if (!mode)
8943 return NULL;
8944
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008945 /*
8946 * Construct a pipe_config sufficient for getting the clock info
8947 * back out of crtc_clock_get.
8948 *
8949 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8950 * to use a real value here instead.
8951 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008952 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008953 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008954 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8955 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8956 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008957 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8958
Ville Syrjälä773ae032013-09-23 17:48:20 +03008959 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008960 mode->hdisplay = (htot & 0xffff) + 1;
8961 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8962 mode->hsync_start = (hsync & 0xffff) + 1;
8963 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8964 mode->vdisplay = (vtot & 0xffff) + 1;
8965 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8966 mode->vsync_start = (vsync & 0xffff) + 1;
8967 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8968
8969 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008970
8971 return mode;
8972}
8973
Daniel Vettercc365132014-06-18 13:59:13 +02008974static void intel_increase_pllclock(struct drm_device *dev,
8975 enum pipe pipe)
Jesse Barnes652c3932009-08-17 13:31:43 -07008976{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008977 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008978 int dpll_reg = DPLL(pipe);
8979 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07008980
Sonika Jindalbaff2962014-07-22 11:16:35 +05308981 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008982 return;
8983
8984 if (!dev_priv->lvds_downclock_avail)
8985 return;
8986
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008987 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008988 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08008989 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008990
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008991 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008992
8993 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8994 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008995 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008996
Jesse Barnes652c3932009-08-17 13:31:43 -07008997 dpll = I915_READ(dpll_reg);
8998 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08008999 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009000 }
Jesse Barnes652c3932009-08-17 13:31:43 -07009001}
9002
9003static void intel_decrease_pllclock(struct drm_crtc *crtc)
9004{
9005 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03009006 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07009007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009008
Sonika Jindalbaff2962014-07-22 11:16:35 +05309009 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07009010 return;
9011
9012 if (!dev_priv->lvds_downclock_avail)
9013 return;
9014
9015 /*
9016 * Since this is called by a timer, we should never get here in
9017 * the manual case.
9018 */
9019 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01009020 int pipe = intel_crtc->pipe;
9021 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02009022 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01009023
Zhao Yakui44d98a62009-10-09 11:39:40 +08009024 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009025
Sean Paul8ac5a6d2012-02-13 13:14:51 -05009026 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009027
Chris Wilson074b5e12012-05-02 12:07:06 +01009028 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07009029 dpll |= DISPLAY_RATE_SELECT_FPA1;
9030 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009031 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009032 dpll = I915_READ(dpll_reg);
9033 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08009034 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009035 }
9036
9037}
9038
Chris Wilsonf047e392012-07-21 12:31:41 +01009039void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07009040{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009041 struct drm_i915_private *dev_priv = dev->dev_private;
9042
Chris Wilsonf62a0072014-02-21 17:55:39 +00009043 if (dev_priv->mm.busy)
9044 return;
9045
Paulo Zanoni43694d62014-03-07 20:08:08 -03009046 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009047 i915_update_gfx_val(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00009048 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01009049}
9050
9051void intel_mark_idle(struct drm_device *dev)
9052{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009053 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00009054 struct drm_crtc *crtc;
9055
Chris Wilsonf62a0072014-02-21 17:55:39 +00009056 if (!dev_priv->mm.busy)
9057 return;
9058
9059 dev_priv->mm.busy = false;
9060
Jani Nikulad330a952014-01-21 11:24:25 +02009061 if (!i915.powersave)
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03009062 goto out;
Chris Wilson725a5b52013-01-08 11:02:57 +00009063
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009064 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07009065 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00009066 continue;
9067
9068 intel_decrease_pllclock(crtc);
9069 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009070
Damien Lespiau3d13ef22014-02-07 19:12:47 +00009071 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009072 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03009073
9074out:
Paulo Zanoni43694d62014-03-07 20:08:08 -03009075 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01009076}
9077
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -07009078
Daniel Vetterf99d7062014-06-19 16:01:59 +02009079/**
9080 * intel_mark_fb_busy - mark given planes as busy
9081 * @dev: DRM device
9082 * @frontbuffer_bits: bits for the affected planes
9083 * @ring: optional ring for asynchronous commands
9084 *
9085 * This function gets called every time the screen contents change. It can be
9086 * used to keep e.g. the update rate at the nominal refresh rate with DRRS.
9087 */
9088static void intel_mark_fb_busy(struct drm_device *dev,
9089 unsigned frontbuffer_bits,
9090 struct intel_engine_cs *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01009091{
Damien Lespiau055e3932014-08-18 13:49:10 +01009092 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettercc365132014-06-18 13:59:13 +02009093 enum pipe pipe;
Jesse Barnes652c3932009-08-17 13:31:43 -07009094
Jani Nikulad330a952014-01-21 11:24:25 +02009095 if (!i915.powersave)
Jesse Barnes652c3932009-08-17 13:31:43 -07009096 return;
9097
Damien Lespiau055e3932014-08-18 13:49:10 +01009098 for_each_pipe(dev_priv, pipe) {
Daniel Vetterf99d7062014-06-19 16:01:59 +02009099 if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)))
Jesse Barnes652c3932009-08-17 13:31:43 -07009100 continue;
9101
Daniel Vettercc365132014-06-18 13:59:13 +02009102 intel_increase_pllclock(dev, pipe);
Chris Wilsonc65355b2013-06-06 16:53:41 -03009103 if (ring && intel_fbc_enabled(dev))
9104 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07009105 }
Jesse Barnes652c3932009-08-17 13:31:43 -07009106}
9107
Daniel Vetterf99d7062014-06-19 16:01:59 +02009108/**
9109 * intel_fb_obj_invalidate - invalidate frontbuffer object
9110 * @obj: GEM object to invalidate
9111 * @ring: set for asynchronous rendering
9112 *
9113 * This function gets called every time rendering on the given object starts and
9114 * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
9115 * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed
9116 * until the rendering completes or a flip on this frontbuffer plane is
9117 * scheduled.
9118 */
9119void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
9120 struct intel_engine_cs *ring)
9121{
9122 struct drm_device *dev = obj->base.dev;
9123 struct drm_i915_private *dev_priv = dev->dev_private;
9124
9125 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
9126
9127 if (!obj->frontbuffer_bits)
9128 return;
9129
9130 if (ring) {
9131 mutex_lock(&dev_priv->fb_tracking.lock);
9132 dev_priv->fb_tracking.busy_bits
9133 |= obj->frontbuffer_bits;
9134 dev_priv->fb_tracking.flip_bits
9135 &= ~obj->frontbuffer_bits;
9136 mutex_unlock(&dev_priv->fb_tracking.lock);
9137 }
9138
9139 intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring);
9140
Daniel Vetter9ca15302014-07-11 10:30:16 -07009141 intel_edp_psr_invalidate(dev, obj->frontbuffer_bits);
Daniel Vetterf99d7062014-06-19 16:01:59 +02009142}
9143
9144/**
9145 * intel_frontbuffer_flush - flush frontbuffer
9146 * @dev: DRM device
9147 * @frontbuffer_bits: frontbuffer plane tracking bits
9148 *
9149 * This function gets called every time rendering on the given planes has
9150 * completed and frontbuffer caching can be started again. Flushes will get
9151 * delayed if they're blocked by some oustanding asynchronous rendering.
9152 *
9153 * Can be called without any locks held.
9154 */
9155void intel_frontbuffer_flush(struct drm_device *dev,
9156 unsigned frontbuffer_bits)
9157{
9158 struct drm_i915_private *dev_priv = dev->dev_private;
9159
9160 /* Delay flushing when rings are still busy.*/
9161 mutex_lock(&dev_priv->fb_tracking.lock);
9162 frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
9163 mutex_unlock(&dev_priv->fb_tracking.lock);
9164
9165 intel_mark_fb_busy(dev, frontbuffer_bits, NULL);
9166
Daniel Vetter9ca15302014-07-11 10:30:16 -07009167 intel_edp_psr_flush(dev, frontbuffer_bits);
Rodrigo Vivic5ad0112014-08-04 03:51:38 -07009168
Ville Syrjäläc317adc2014-09-03 14:09:50 +03009169 /*
9170 * FIXME: Unconditional fbc flushing here is a rather gross hack and
9171 * needs to be reworked into a proper frontbuffer tracking scheme like
9172 * psr employs.
9173 */
9174 if (IS_BROADWELL(dev))
Rodrigo Vivic5ad0112014-08-04 03:51:38 -07009175 gen8_fbc_sw_flush(dev, FBC_REND_CACHE_CLEAN);
Daniel Vetterf99d7062014-06-19 16:01:59 +02009176}
9177
9178/**
9179 * intel_fb_obj_flush - flush frontbuffer object
9180 * @obj: GEM object to flush
9181 * @retire: set when retiring asynchronous rendering
9182 *
9183 * This function gets called every time rendering on the given object has
9184 * completed and frontbuffer caching can be started again. If @retire is true
9185 * then any delayed flushes will be unblocked.
9186 */
9187void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
9188 bool retire)
9189{
9190 struct drm_device *dev = obj->base.dev;
9191 struct drm_i915_private *dev_priv = dev->dev_private;
9192 unsigned frontbuffer_bits;
9193
9194 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
9195
9196 if (!obj->frontbuffer_bits)
9197 return;
9198
9199 frontbuffer_bits = obj->frontbuffer_bits;
9200
9201 if (retire) {
9202 mutex_lock(&dev_priv->fb_tracking.lock);
9203 /* Filter out new bits since rendering started. */
9204 frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
9205
9206 dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
9207 mutex_unlock(&dev_priv->fb_tracking.lock);
9208 }
9209
9210 intel_frontbuffer_flush(dev, frontbuffer_bits);
9211}
9212
9213/**
9214 * intel_frontbuffer_flip_prepare - prepare asnychronous frontbuffer flip
9215 * @dev: DRM device
9216 * @frontbuffer_bits: frontbuffer plane tracking bits
9217 *
9218 * This function gets called after scheduling a flip on @obj. The actual
9219 * frontbuffer flushing will be delayed until completion is signalled with
9220 * intel_frontbuffer_flip_complete. If an invalidate happens in between this
9221 * flush will be cancelled.
9222 *
9223 * Can be called without any locks held.
9224 */
9225void intel_frontbuffer_flip_prepare(struct drm_device *dev,
9226 unsigned frontbuffer_bits)
9227{
9228 struct drm_i915_private *dev_priv = dev->dev_private;
9229
9230 mutex_lock(&dev_priv->fb_tracking.lock);
9231 dev_priv->fb_tracking.flip_bits
9232 |= frontbuffer_bits;
9233 mutex_unlock(&dev_priv->fb_tracking.lock);
9234}
9235
9236/**
9237 * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flush
9238 * @dev: DRM device
9239 * @frontbuffer_bits: frontbuffer plane tracking bits
9240 *
9241 * This function gets called after the flip has been latched and will complete
9242 * on the next vblank. It will execute the fush if it hasn't been cancalled yet.
9243 *
9244 * Can be called without any locks held.
9245 */
9246void intel_frontbuffer_flip_complete(struct drm_device *dev,
9247 unsigned frontbuffer_bits)
9248{
9249 struct drm_i915_private *dev_priv = dev->dev_private;
9250
9251 mutex_lock(&dev_priv->fb_tracking.lock);
9252 /* Mask any cancelled flips. */
9253 frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
9254 dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
9255 mutex_unlock(&dev_priv->fb_tracking.lock);
9256
9257 intel_frontbuffer_flush(dev, frontbuffer_bits);
9258}
9259
Jesse Barnes79e53942008-11-07 14:24:08 -08009260static void intel_crtc_destroy(struct drm_crtc *crtc)
9261{
9262 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009263 struct drm_device *dev = crtc->dev;
9264 struct intel_unpin_work *work;
9265 unsigned long flags;
9266
9267 spin_lock_irqsave(&dev->event_lock, flags);
9268 work = intel_crtc->unpin_work;
9269 intel_crtc->unpin_work = NULL;
9270 spin_unlock_irqrestore(&dev->event_lock, flags);
9271
9272 if (work) {
9273 cancel_work_sync(&work->work);
9274 kfree(work);
9275 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009276
9277 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009278
Jesse Barnes79e53942008-11-07 14:24:08 -08009279 kfree(intel_crtc);
9280}
9281
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009282static void intel_unpin_work_fn(struct work_struct *__work)
9283{
9284 struct intel_unpin_work *work =
9285 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009286 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +02009287 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009288
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009289 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01009290 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00009291 drm_gem_object_unreference(&work->pending_flip_obj->base);
9292 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00009293
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009294 intel_update_fbc(dev);
9295 mutex_unlock(&dev->struct_mutex);
9296
Daniel Vetterf99d7062014-06-19 16:01:59 +02009297 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
9298
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009299 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9300 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9301
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009302 kfree(work);
9303}
9304
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009305static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01009306 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009307{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9309 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009310 unsigned long flags;
9311
9312 /* Ignore early vblank irqs */
9313 if (intel_crtc == NULL)
9314 return;
9315
9316 spin_lock_irqsave(&dev->event_lock, flags);
9317 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00009318
9319 /* Ensure we don't miss a work->pending update ... */
9320 smp_rmb();
9321
9322 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009323 spin_unlock_irqrestore(&dev->event_lock, flags);
9324 return;
9325 }
9326
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009327 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01009328
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009329 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009330}
9331
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009332void intel_finish_page_flip(struct drm_device *dev, int pipe)
9333{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009334 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009335 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9336
Mario Kleiner49b14a52010-12-09 07:00:07 +01009337 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009338}
9339
9340void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9341{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009342 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009343 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9344
Mario Kleiner49b14a52010-12-09 07:00:07 +01009345 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009346}
9347
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009348/* Is 'a' after or equal to 'b'? */
9349static bool g4x_flip_count_after_eq(u32 a, u32 b)
9350{
9351 return !((a - b) & 0x80000000);
9352}
9353
9354static bool page_flip_finished(struct intel_crtc *crtc)
9355{
9356 struct drm_device *dev = crtc->base.dev;
9357 struct drm_i915_private *dev_priv = dev->dev_private;
9358
9359 /*
9360 * The relevant registers doen't exist on pre-ctg.
9361 * As the flip done interrupt doesn't trigger for mmio
9362 * flips on gmch platforms, a flip count check isn't
9363 * really needed there. But since ctg has the registers,
9364 * include it in the check anyway.
9365 */
9366 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9367 return true;
9368
9369 /*
9370 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9371 * used the same base address. In that case the mmio flip might
9372 * have completed, but the CS hasn't even executed the flip yet.
9373 *
9374 * A flip count check isn't enough as the CS might have updated
9375 * the base address just after start of vblank, but before we
9376 * managed to process the interrupt. This means we'd complete the
9377 * CS flip too soon.
9378 *
9379 * Combining both checks should get us a good enough result. It may
9380 * still happen that the CS flip has been executed, but has not
9381 * yet actually completed. But in case the base address is the same
9382 * anyway, we don't really care.
9383 */
9384 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9385 crtc->unpin_work->gtt_offset &&
9386 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9387 crtc->unpin_work->flip_count);
9388}
9389
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009390void intel_prepare_page_flip(struct drm_device *dev, int plane)
9391{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009392 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009393 struct intel_crtc *intel_crtc =
9394 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9395 unsigned long flags;
9396
Chris Wilsone7d841c2012-12-03 11:36:30 +00009397 /* NB: An MMIO update of the plane base pointer will also
9398 * generate a page-flip completion irq, i.e. every modeset
9399 * is also accompanied by a spurious intel_prepare_page_flip().
9400 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009401 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009402 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00009403 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009404 spin_unlock_irqrestore(&dev->event_lock, flags);
9405}
9406
Robin Schroereba905b2014-05-18 02:24:50 +02009407static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00009408{
9409 /* Ensure that the work item is consistent when activating it ... */
9410 smp_wmb();
9411 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9412 /* and that it is marked active as soon as the irq could fire. */
9413 smp_wmb();
9414}
9415
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009416static int intel_gen2_queue_flip(struct drm_device *dev,
9417 struct drm_crtc *crtc,
9418 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009419 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009420 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009421 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009422{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009423 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009424 u32 flip_mask;
9425 int ret;
9426
Daniel Vetter6d90c952012-04-26 23:28:05 +02009427 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009428 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009429 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009430
9431 /* Can't queue multiple flips, so wait for the previous
9432 * one to finish before executing the next.
9433 */
9434 if (intel_crtc->plane)
9435 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9436 else
9437 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009438 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9439 intel_ring_emit(ring, MI_NOOP);
9440 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9441 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9442 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009443 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009444 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00009445
9446 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009447 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009448 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009449}
9450
9451static int intel_gen3_queue_flip(struct drm_device *dev,
9452 struct drm_crtc *crtc,
9453 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009454 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009455 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009456 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009457{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009458 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009459 u32 flip_mask;
9460 int ret;
9461
Daniel Vetter6d90c952012-04-26 23:28:05 +02009462 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009463 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009464 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009465
9466 if (intel_crtc->plane)
9467 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9468 else
9469 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009470 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9471 intel_ring_emit(ring, MI_NOOP);
9472 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9473 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9474 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009475 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009476 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009477
Chris Wilsone7d841c2012-12-03 11:36:30 +00009478 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009479 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009480 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009481}
9482
9483static int intel_gen4_queue_flip(struct drm_device *dev,
9484 struct drm_crtc *crtc,
9485 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009486 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009487 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009488 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009489{
9490 struct drm_i915_private *dev_priv = dev->dev_private;
9491 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9492 uint32_t pf, pipesrc;
9493 int ret;
9494
Daniel Vetter6d90c952012-04-26 23:28:05 +02009495 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009496 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009497 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009498
9499 /* i965+ uses the linear or tiled offsets from the
9500 * Display Registers (which do not change across a page-flip)
9501 * so we need only reprogram the base address.
9502 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02009503 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9504 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9505 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009506 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +02009507 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009508
9509 /* XXX Enabling the panel-fitter across page-flip is so far
9510 * untested on non-native modes, so ignore it for now.
9511 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9512 */
9513 pf = 0;
9514 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009515 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009516
9517 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009518 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009519 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009520}
9521
9522static int intel_gen6_queue_flip(struct drm_device *dev,
9523 struct drm_crtc *crtc,
9524 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009525 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009526 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009527 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009528{
9529 struct drm_i915_private *dev_priv = dev->dev_private;
9530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9531 uint32_t pf, pipesrc;
9532 int ret;
9533
Daniel Vetter6d90c952012-04-26 23:28:05 +02009534 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009535 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009536 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009537
Daniel Vetter6d90c952012-04-26 23:28:05 +02009538 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9539 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9540 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009541 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009542
Chris Wilson99d9acd2012-04-17 20:37:00 +01009543 /* Contrary to the suggestions in the documentation,
9544 * "Enable Panel Fitter" does not seem to be required when page
9545 * flipping with a non-native mode, and worse causes a normal
9546 * modeset to fail.
9547 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9548 */
9549 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009550 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009551 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009552
9553 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009554 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009555 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009556}
9557
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009558static int intel_gen7_queue_flip(struct drm_device *dev,
9559 struct drm_crtc *crtc,
9560 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009561 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009562 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009563 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009564{
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009565 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009566 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01009567 int len, ret;
9568
Robin Schroereba905b2014-05-18 02:24:50 +02009569 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009570 case PLANE_A:
9571 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9572 break;
9573 case PLANE_B:
9574 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9575 break;
9576 case PLANE_C:
9577 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9578 break;
9579 default:
9580 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009581 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009582 }
9583
Chris Wilsonffe74d72013-08-26 20:58:12 +01009584 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01009585 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01009586 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01009587 /*
9588 * On Gen 8, SRM is now taking an extra dword to accommodate
9589 * 48bits addresses, and we need a NOOP for the batch size to
9590 * stay even.
9591 */
9592 if (IS_GEN8(dev))
9593 len += 2;
9594 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009595
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009596 /*
9597 * BSpec MI_DISPLAY_FLIP for IVB:
9598 * "The full packet must be contained within the same cache line."
9599 *
9600 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9601 * cacheline, if we ever start emitting more commands before
9602 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9603 * then do the cacheline alignment, and finally emit the
9604 * MI_DISPLAY_FLIP.
9605 */
9606 ret = intel_ring_cacheline_align(ring);
9607 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009608 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009609
Chris Wilsonffe74d72013-08-26 20:58:12 +01009610 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009611 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009612 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009613
Chris Wilsonffe74d72013-08-26 20:58:12 +01009614 /* Unmask the flip-done completion message. Note that the bspec says that
9615 * we should do this for both the BCS and RCS, and that we must not unmask
9616 * more than one flip event at any time (or ensure that one flip message
9617 * can be sent by waiting for flip-done prior to queueing new flips).
9618 * Experimentation says that BCS works despite DERRMR masking all
9619 * flip-done completion events and that unmasking all planes at once
9620 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9621 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9622 */
9623 if (ring->id == RCS) {
9624 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9625 intel_ring_emit(ring, DERRMR);
9626 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9627 DERRMR_PIPEB_PRI_FLIP_DONE |
9628 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +01009629 if (IS_GEN8(dev))
9630 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9631 MI_SRM_LRM_GLOBAL_GTT);
9632 else
9633 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9634 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01009635 intel_ring_emit(ring, DERRMR);
9636 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +01009637 if (IS_GEN8(dev)) {
9638 intel_ring_emit(ring, 0);
9639 intel_ring_emit(ring, MI_NOOP);
9640 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009641 }
9642
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009643 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009644 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009645 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009646 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00009647
9648 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009649 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009650 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009651}
9652
Sourab Gupta84c33a62014-06-02 16:47:17 +05309653static bool use_mmio_flip(struct intel_engine_cs *ring,
9654 struct drm_i915_gem_object *obj)
9655{
9656 /*
9657 * This is not being used for older platforms, because
9658 * non-availability of flip done interrupt forces us to use
9659 * CS flips. Older platforms derive flip done using some clever
9660 * tricks involving the flip_pending status bits and vblank irqs.
9661 * So using MMIO flips there would disrupt this mechanism.
9662 */
9663
Chris Wilson8e09bf82014-07-08 10:40:30 +01009664 if (ring == NULL)
9665 return true;
9666
Sourab Gupta84c33a62014-06-02 16:47:17 +05309667 if (INTEL_INFO(ring->dev)->gen < 5)
9668 return false;
9669
9670 if (i915.use_mmio_flip < 0)
9671 return false;
9672 else if (i915.use_mmio_flip > 0)
9673 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +01009674 else if (i915.enable_execlists)
9675 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +05309676 else
9677 return ring != obj->ring;
9678}
9679
9680static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9681{
9682 struct drm_device *dev = intel_crtc->base.dev;
9683 struct drm_i915_private *dev_priv = dev->dev_private;
9684 struct intel_framebuffer *intel_fb =
9685 to_intel_framebuffer(intel_crtc->base.primary->fb);
9686 struct drm_i915_gem_object *obj = intel_fb->obj;
9687 u32 dspcntr;
9688 u32 reg;
9689
9690 intel_mark_page_flip_active(intel_crtc);
9691
9692 reg = DSPCNTR(intel_crtc->plane);
9693 dspcntr = I915_READ(reg);
9694
9695 if (INTEL_INFO(dev)->gen >= 4) {
9696 if (obj->tiling_mode != I915_TILING_NONE)
9697 dspcntr |= DISPPLANE_TILED;
9698 else
9699 dspcntr &= ~DISPPLANE_TILED;
9700 }
9701 I915_WRITE(reg, dspcntr);
9702
9703 I915_WRITE(DSPSURF(intel_crtc->plane),
9704 intel_crtc->unpin_work->gtt_offset);
9705 POSTING_READ(DSPSURF(intel_crtc->plane));
9706}
9707
9708static int intel_postpone_flip(struct drm_i915_gem_object *obj)
9709{
9710 struct intel_engine_cs *ring;
9711 int ret;
9712
9713 lockdep_assert_held(&obj->base.dev->struct_mutex);
9714
9715 if (!obj->last_write_seqno)
9716 return 0;
9717
9718 ring = obj->ring;
9719
9720 if (i915_seqno_passed(ring->get_seqno(ring, true),
9721 obj->last_write_seqno))
9722 return 0;
9723
9724 ret = i915_gem_check_olr(ring, obj->last_write_seqno);
9725 if (ret)
9726 return ret;
9727
9728 if (WARN_ON(!ring->irq_get(ring)))
9729 return 0;
9730
9731 return 1;
9732}
9733
9734void intel_notify_mmio_flip(struct intel_engine_cs *ring)
9735{
9736 struct drm_i915_private *dev_priv = to_i915(ring->dev);
9737 struct intel_crtc *intel_crtc;
9738 unsigned long irq_flags;
9739 u32 seqno;
9740
9741 seqno = ring->get_seqno(ring, false);
9742
9743 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9744 for_each_intel_crtc(ring->dev, intel_crtc) {
9745 struct intel_mmio_flip *mmio_flip;
9746
9747 mmio_flip = &intel_crtc->mmio_flip;
9748 if (mmio_flip->seqno == 0)
9749 continue;
9750
9751 if (ring->id != mmio_flip->ring_id)
9752 continue;
9753
9754 if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
9755 intel_do_mmio_flip(intel_crtc);
9756 mmio_flip->seqno = 0;
9757 ring->irq_put(ring);
9758 }
9759 }
9760 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9761}
9762
9763static int intel_queue_mmio_flip(struct drm_device *dev,
9764 struct drm_crtc *crtc,
9765 struct drm_framebuffer *fb,
9766 struct drm_i915_gem_object *obj,
9767 struct intel_engine_cs *ring,
9768 uint32_t flags)
9769{
9770 struct drm_i915_private *dev_priv = dev->dev_private;
9771 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9772 unsigned long irq_flags;
9773 int ret;
9774
9775 if (WARN_ON(intel_crtc->mmio_flip.seqno))
9776 return -EBUSY;
9777
9778 ret = intel_postpone_flip(obj);
9779 if (ret < 0)
9780 return ret;
9781 if (ret == 0) {
9782 intel_do_mmio_flip(intel_crtc);
9783 return 0;
9784 }
9785
9786 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9787 intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
9788 intel_crtc->mmio_flip.ring_id = obj->ring->id;
9789 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9790
9791 /*
9792 * Double check to catch cases where irq fired before
9793 * mmio flip data was ready
9794 */
9795 intel_notify_mmio_flip(obj->ring);
9796 return 0;
9797}
9798
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009799static int intel_default_queue_flip(struct drm_device *dev,
9800 struct drm_crtc *crtc,
9801 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009802 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009803 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009804 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009805{
9806 return -ENODEV;
9807}
9808
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009809static bool __intel_pageflip_stall_check(struct drm_device *dev,
9810 struct drm_crtc *crtc)
9811{
9812 struct drm_i915_private *dev_priv = dev->dev_private;
9813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9814 struct intel_unpin_work *work = intel_crtc->unpin_work;
9815 u32 addr;
9816
9817 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9818 return true;
9819
9820 if (!work->enable_stall_check)
9821 return false;
9822
9823 if (work->flip_ready_vblank == 0) {
9824 if (work->flip_queued_ring &&
9825 !i915_seqno_passed(work->flip_queued_ring->get_seqno(work->flip_queued_ring, true),
9826 work->flip_queued_seqno))
9827 return false;
9828
9829 work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9830 }
9831
9832 if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
9833 return false;
9834
9835 /* Potential stall - if we see that the flip has happened,
9836 * assume a missed interrupt. */
9837 if (INTEL_INFO(dev)->gen >= 4)
9838 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9839 else
9840 addr = I915_READ(DSPADDR(intel_crtc->plane));
9841
9842 /* There is a potential issue here with a false positive after a flip
9843 * to the same address. We could address this by checking for a
9844 * non-incrementing frame counter.
9845 */
9846 return addr == work->gtt_offset;
9847}
9848
9849void intel_check_page_flip(struct drm_device *dev, int pipe)
9850{
9851 struct drm_i915_private *dev_priv = dev->dev_private;
9852 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9854 unsigned long flags;
9855
9856 if (crtc == NULL)
9857 return;
9858
9859 spin_lock_irqsave(&dev->event_lock, flags);
9860 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9861 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
9862 intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
9863 page_flip_completed(intel_crtc);
9864 }
9865 spin_unlock_irqrestore(&dev->event_lock, flags);
9866}
9867
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009868static int intel_crtc_page_flip(struct drm_crtc *crtc,
9869 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009870 struct drm_pending_vblank_event *event,
9871 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009872{
9873 struct drm_device *dev = crtc->dev;
9874 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -07009875 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -07009876 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009877 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02009878 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009879 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009880 struct intel_engine_cs *ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009881 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01009882 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009883
Daisy Sunc76bb612014-08-11 11:08:38 -07009884 //trigger software GT busyness calculation
9885 gen8_flip_interrupt(dev);
9886
Matt Roper2ff8fde2014-07-08 07:50:07 -07009887 /*
9888 * drm_mode_page_flip_ioctl() should already catch this, but double
9889 * check to be safe. In the future we may enable pageflipping from
9890 * a disabled primary plane.
9891 */
9892 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9893 return -EBUSY;
9894
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009895 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -07009896 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009897 return -EINVAL;
9898
9899 /*
9900 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9901 * Note that pitch changes could also affect these register.
9902 */
9903 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -07009904 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9905 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009906 return -EINVAL;
9907
Chris Wilsonf900db42014-02-20 09:26:13 +00009908 if (i915_terminally_wedged(&dev_priv->gpu_error))
9909 goto out_hang;
9910
Daniel Vetterb14c5672013-09-19 12:18:32 +02009911 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009912 if (work == NULL)
9913 return -ENOMEM;
9914
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009915 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009916 work->crtc = crtc;
Matt Roper2ff8fde2014-07-08 07:50:07 -07009917 work->old_fb_obj = intel_fb_obj(old_fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009918 INIT_WORK(&work->work, intel_unpin_work_fn);
9919
Daniel Vetter87b6b102014-05-15 15:33:46 +02009920 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009921 if (ret)
9922 goto free_work;
9923
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009924 /* We borrow the event spin lock for protecting unpin_work */
9925 spin_lock_irqsave(&dev->event_lock, flags);
9926 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009927 /* Before declaring the flip queue wedged, check if
9928 * the hardware completed the operation behind our backs.
9929 */
9930 if (__intel_pageflip_stall_check(dev, crtc)) {
9931 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
9932 page_flip_completed(intel_crtc);
9933 } else {
9934 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
9935 spin_unlock_irqrestore(&dev->event_lock, flags);
Chris Wilson468f0b42010-05-27 13:18:13 +01009936
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009937 drm_crtc_vblank_put(crtc);
9938 kfree(work);
9939 return -EBUSY;
9940 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009941 }
9942 intel_crtc->unpin_work = work;
9943 spin_unlock_irqrestore(&dev->event_lock, flags);
9944
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009945 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9946 flush_workqueue(dev_priv->wq);
9947
Chris Wilson79158102012-05-23 11:13:58 +01009948 ret = i915_mutex_lock_interruptible(dev);
9949 if (ret)
9950 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009951
Jesse Barnes75dfca82010-02-10 15:09:44 -08009952 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00009953 drm_gem_object_reference(&work->old_fb_obj->base);
9954 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009955
Matt Roperf4510a22014-04-01 15:22:40 -07009956 crtc->primary->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01009957
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009958 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009959
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009960 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02009961 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009962
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009963 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +02009964 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009965
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009966 if (IS_VALLEYVIEW(dev)) {
9967 ring = &dev_priv->ring[BCS];
Chris Wilson8e09bf82014-07-08 10:40:30 +01009968 if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
9969 /* vlv: DISPLAY_FLIP fails to change tiling */
9970 ring = NULL;
Chris Wilson2a92d5b2014-07-08 10:40:29 +01009971 } else if (IS_IVYBRIDGE(dev)) {
9972 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009973 } else if (INTEL_INFO(dev)->gen >= 7) {
9974 ring = obj->ring;
9975 if (ring == NULL || ring->id != RCS)
9976 ring = &dev_priv->ring[BCS];
9977 } else {
9978 ring = &dev_priv->ring[RCS];
9979 }
9980
9981 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009982 if (ret)
9983 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009984
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009985 work->gtt_offset =
9986 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9987
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009988 if (use_mmio_flip(ring, obj)) {
Sourab Gupta84c33a62014-06-02 16:47:17 +05309989 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9990 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009991 if (ret)
9992 goto cleanup_unpin;
9993
9994 work->flip_queued_seqno = obj->last_write_seqno;
9995 work->flip_queued_ring = obj->ring;
9996 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +05309997 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009998 page_flip_flags);
9999 if (ret)
10000 goto cleanup_unpin;
10001
10002 work->flip_queued_seqno = intel_ring_get_seqno(ring);
10003 work->flip_queued_ring = ring;
10004 }
10005
10006 work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
10007 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010008
Daniel Vettera071fa02014-06-18 23:28:09 +020010009 i915_gem_track_fb(work->old_fb_obj, obj,
10010 INTEL_FRONTBUFFER_PRIMARY(pipe));
10011
Chris Wilson7782de32011-07-08 12:22:41 +010010012 intel_disable_fbc(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010013 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010014 mutex_unlock(&dev->struct_mutex);
10015
Jesse Barnese5510fa2010-07-01 16:48:37 -070010016 trace_i915_flip_request(intel_crtc->plane, obj);
10017
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010018 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010010019
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010020cleanup_unpin:
10021 intel_unpin_fb_obj(obj);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010022cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010023 atomic_dec(&intel_crtc->unpin_work_count);
Matt Roperf4510a22014-04-01 15:22:40 -070010024 crtc->primary->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +000010025 drm_gem_object_unreference(&work->old_fb_obj->base);
10026 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +010010027 mutex_unlock(&dev->struct_mutex);
10028
Chris Wilson79158102012-05-23 11:13:58 +010010029cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +010010030 spin_lock_irqsave(&dev->event_lock, flags);
10031 intel_crtc->unpin_work = NULL;
10032 spin_unlock_irqrestore(&dev->event_lock, flags);
10033
Daniel Vetter87b6b102014-05-15 15:33:46 +020010034 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010035free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010010036 kfree(work);
10037
Chris Wilsonf900db42014-02-20 09:26:13 +000010038 if (ret == -EIO) {
10039out_hang:
10040 intel_crtc_wait_for_pending_flips(crtc);
10041 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
10042 if (ret == 0 && event)
Daniel Vettera071fa02014-06-18 23:28:09 +020010043 drm_send_vblank_event(dev, pipe, event);
Chris Wilsonf900db42014-02-20 09:26:13 +000010044 }
Chris Wilson96b099f2010-06-07 14:03:04 +010010045 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010046}
10047
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010048static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010049 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10050 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010051};
10052
Daniel Vetter9a935852012-07-05 22:34:27 +020010053/**
10054 * intel_modeset_update_staged_output_state
10055 *
10056 * Updates the staged output configuration state, e.g. after we've read out the
10057 * current hw state.
10058 */
10059static void intel_modeset_update_staged_output_state(struct drm_device *dev)
10060{
Ville Syrjälä76688512014-01-10 11:28:06 +020010061 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010062 struct intel_encoder *encoder;
10063 struct intel_connector *connector;
10064
10065 list_for_each_entry(connector, &dev->mode_config.connector_list,
10066 base.head) {
10067 connector->new_encoder =
10068 to_intel_encoder(connector->base.encoder);
10069 }
10070
Damien Lespiaub2784e12014-08-05 11:29:37 +010010071 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010072 encoder->new_crtc =
10073 to_intel_crtc(encoder->base.crtc);
10074 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010075
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010076 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010077 crtc->new_enabled = crtc->base.enabled;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010078
10079 if (crtc->new_enabled)
10080 crtc->new_config = &crtc->config;
10081 else
10082 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010083 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010084}
10085
10086/**
10087 * intel_modeset_commit_output_state
10088 *
10089 * This function copies the stage display pipe configuration to the real one.
10090 */
10091static void intel_modeset_commit_output_state(struct drm_device *dev)
10092{
Ville Syrjälä76688512014-01-10 11:28:06 +020010093 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010094 struct intel_encoder *encoder;
10095 struct intel_connector *connector;
10096
10097 list_for_each_entry(connector, &dev->mode_config.connector_list,
10098 base.head) {
10099 connector->base.encoder = &connector->new_encoder->base;
10100 }
10101
Damien Lespiaub2784e12014-08-05 11:29:37 +010010102 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010103 encoder->base.crtc = &encoder->new_crtc->base;
10104 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010105
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010106 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010107 crtc->base.enabled = crtc->new_enabled;
10108 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010109}
10110
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010111static void
Robin Schroereba905b2014-05-18 02:24:50 +020010112connected_sink_compute_bpp(struct intel_connector *connector,
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010113 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010114{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010115 int bpp = pipe_config->pipe_bpp;
10116
10117 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10118 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010119 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010120
10121 /* Don't use an invalid EDID bpc value */
10122 if (connector->base.display_info.bpc &&
10123 connector->base.display_info.bpc * 3 < bpp) {
10124 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10125 bpp, connector->base.display_info.bpc*3);
10126 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10127 }
10128
10129 /* Clamp bpp to 8 on screens without EDID 1.4 */
10130 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10131 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10132 bpp);
10133 pipe_config->pipe_bpp = 24;
10134 }
10135}
10136
10137static int
10138compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10139 struct drm_framebuffer *fb,
10140 struct intel_crtc_config *pipe_config)
10141{
10142 struct drm_device *dev = crtc->base.dev;
10143 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010144 int bpp;
10145
Daniel Vetterd42264b2013-03-28 16:38:08 +010010146 switch (fb->pixel_format) {
10147 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010148 bpp = 8*3; /* since we go through a colormap */
10149 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010150 case DRM_FORMAT_XRGB1555:
10151 case DRM_FORMAT_ARGB1555:
10152 /* checked in intel_framebuffer_init already */
10153 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10154 return -EINVAL;
10155 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010156 bpp = 6*3; /* min is 18bpp */
10157 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010158 case DRM_FORMAT_XBGR8888:
10159 case DRM_FORMAT_ABGR8888:
10160 /* checked in intel_framebuffer_init already */
10161 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10162 return -EINVAL;
10163 case DRM_FORMAT_XRGB8888:
10164 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010165 bpp = 8*3;
10166 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010167 case DRM_FORMAT_XRGB2101010:
10168 case DRM_FORMAT_ARGB2101010:
10169 case DRM_FORMAT_XBGR2101010:
10170 case DRM_FORMAT_ABGR2101010:
10171 /* checked in intel_framebuffer_init already */
10172 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +010010173 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010174 bpp = 10*3;
10175 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +010010176 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010177 default:
10178 DRM_DEBUG_KMS("unsupported depth\n");
10179 return -EINVAL;
10180 }
10181
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010182 pipe_config->pipe_bpp = bpp;
10183
10184 /* Clamp display bpp to EDID value */
10185 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010186 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +020010187 if (!connector->new_encoder ||
10188 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010189 continue;
10190
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010191 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010192 }
10193
10194 return bpp;
10195}
10196
Daniel Vetter644db712013-09-19 14:53:58 +020010197static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10198{
10199 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10200 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010010201 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020010202 mode->crtc_hdisplay, mode->crtc_hsync_start,
10203 mode->crtc_hsync_end, mode->crtc_htotal,
10204 mode->crtc_vdisplay, mode->crtc_vsync_start,
10205 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10206}
10207
Daniel Vetterc0b03412013-05-28 12:05:54 +020010208static void intel_dump_pipe_config(struct intel_crtc *crtc,
10209 struct intel_crtc_config *pipe_config,
10210 const char *context)
10211{
10212 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10213 context, pipe_name(crtc->pipe));
10214
10215 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10216 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10217 pipe_config->pipe_bpp, pipe_config->dither);
10218 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10219 pipe_config->has_pch_encoder,
10220 pipe_config->fdi_lanes,
10221 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10222 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10223 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010224 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10225 pipe_config->has_dp_encoder,
10226 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10227 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10228 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010229
10230 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10231 pipe_config->has_dp_encoder,
10232 pipe_config->dp_m2_n2.gmch_m,
10233 pipe_config->dp_m2_n2.gmch_n,
10234 pipe_config->dp_m2_n2.link_m,
10235 pipe_config->dp_m2_n2.link_n,
10236 pipe_config->dp_m2_n2.tu);
10237
Daniel Vetterc0b03412013-05-28 12:05:54 +020010238 DRM_DEBUG_KMS("requested mode:\n");
10239 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
10240 DRM_DEBUG_KMS("adjusted mode:\n");
10241 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +020010242 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030010243 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010244 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10245 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010246 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10247 pipe_config->gmch_pfit.control,
10248 pipe_config->gmch_pfit.pgm_ratios,
10249 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010250 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020010251 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010252 pipe_config->pch_pfit.size,
10253 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010254 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030010255 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010256}
10257
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010258static bool encoders_cloneable(const struct intel_encoder *a,
10259 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010260{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010261 /* masks could be asymmetric, so check both ways */
10262 return a == b || (a->cloneable & (1 << b->type) &&
10263 b->cloneable & (1 << a->type));
10264}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010265
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010266static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10267 struct intel_encoder *encoder)
10268{
10269 struct drm_device *dev = crtc->base.dev;
10270 struct intel_encoder *source_encoder;
10271
Damien Lespiaub2784e12014-08-05 11:29:37 +010010272 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010273 if (source_encoder->new_crtc != crtc)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010274 continue;
10275
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010276 if (!encoders_cloneable(encoder, source_encoder))
10277 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010278 }
10279
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010280 return true;
10281}
10282
10283static bool check_encoder_cloning(struct intel_crtc *crtc)
10284{
10285 struct drm_device *dev = crtc->base.dev;
10286 struct intel_encoder *encoder;
10287
Damien Lespiaub2784e12014-08-05 11:29:37 +010010288 for_each_intel_encoder(dev, encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010289 if (encoder->new_crtc != crtc)
10290 continue;
10291
10292 if (!check_single_encoder_cloning(crtc, encoder))
10293 return false;
10294 }
10295
10296 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010297}
10298
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010299static struct intel_crtc_config *
10300intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010301 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010302 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +020010303{
10304 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +020010305 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010306 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +010010307 int plane_bpp, ret = -EINVAL;
10308 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020010309
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010310 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010311 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10312 return ERR_PTR(-EINVAL);
10313 }
10314
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010315 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10316 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020010317 return ERR_PTR(-ENOMEM);
10318
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010319 drm_mode_copy(&pipe_config->adjusted_mode, mode);
10320 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010321
Daniel Vettere143a212013-07-04 12:01:15 +020010322 pipe_config->cpu_transcoder =
10323 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010324 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010325
Imre Deak2960bc92013-07-30 13:36:32 +030010326 /*
10327 * Sanitize sync polarity flags based on requested ones. If neither
10328 * positive or negative polarity is requested, treat this as meaning
10329 * negative polarity.
10330 */
10331 if (!(pipe_config->adjusted_mode.flags &
10332 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
10333 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
10334
10335 if (!(pipe_config->adjusted_mode.flags &
10336 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
10337 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
10338
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010339 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10340 * plane pixel format and any sink constraints into account. Returns the
10341 * source plane bpp so that dithering can be selected on mismatches
10342 * after encoders and crtc also have had their say. */
10343 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10344 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010345 if (plane_bpp < 0)
10346 goto fail;
10347
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010348 /*
10349 * Determine the real pipe dimensions. Note that stereo modes can
10350 * increase the actual pipe size due to the frame doubling and
10351 * insertion of additional space for blanks between the frame. This
10352 * is stored in the crtc timings. We use the requested mode to do this
10353 * computation to clearly distinguish it from the adjusted mode, which
10354 * can be changed by the connectors in the below retry loop.
10355 */
10356 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
10357 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
10358 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
10359
Daniel Vettere29c22c2013-02-21 00:00:16 +010010360encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020010361 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020010362 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020010363 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010364
Daniel Vetter135c81b2013-07-21 21:37:09 +020010365 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +010010366 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020010367
Daniel Vetter7758a112012-07-08 19:40:39 +020010368 /* Pass our mode to the connectors and the CRTC to give them a chance to
10369 * adjust it according to limitations or connector properties, and also
10370 * a chance to reject the mode entirely.
10371 */
Damien Lespiaub2784e12014-08-05 11:29:37 +010010372 for_each_intel_encoder(dev, encoder) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010373
10374 if (&encoder->new_crtc->base != crtc)
10375 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010010376
Daniel Vetterefea6e82013-07-21 21:36:59 +020010377 if (!(encoder->compute_config(encoder, pipe_config))) {
10378 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020010379 goto fail;
10380 }
10381 }
10382
Daniel Vetterff9a6752013-06-01 17:16:21 +020010383 /* Set default port clock if not overwritten by the encoder. Needs to be
10384 * done afterwards in case the encoder adjusts the mode. */
10385 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +010010386 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
10387 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010388
Daniel Vettera43f6e02013-06-07 23:10:32 +020010389 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010390 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010391 DRM_DEBUG_KMS("CRTC fixup failed\n");
10392 goto fail;
10393 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010010394
10395 if (ret == RETRY) {
10396 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10397 ret = -EINVAL;
10398 goto fail;
10399 }
10400
10401 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10402 retry = false;
10403 goto encoder_retry;
10404 }
10405
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010406 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10407 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10408 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10409
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010410 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020010411fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010412 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010413 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020010414}
10415
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010416/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10417 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10418static void
10419intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10420 unsigned *prepare_pipes, unsigned *disable_pipes)
10421{
10422 struct intel_crtc *intel_crtc;
10423 struct drm_device *dev = crtc->dev;
10424 struct intel_encoder *encoder;
10425 struct intel_connector *connector;
10426 struct drm_crtc *tmp_crtc;
10427
10428 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
10429
10430 /* Check which crtcs have changed outputs connected to them, these need
10431 * to be part of the prepare_pipes mask. We don't (yet) support global
10432 * modeset across multiple crtcs, so modeset_pipes will only have one
10433 * bit set at most. */
10434 list_for_each_entry(connector, &dev->mode_config.connector_list,
10435 base.head) {
10436 if (connector->base.encoder == &connector->new_encoder->base)
10437 continue;
10438
10439 if (connector->base.encoder) {
10440 tmp_crtc = connector->base.encoder->crtc;
10441
10442 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10443 }
10444
10445 if (connector->new_encoder)
10446 *prepare_pipes |=
10447 1 << connector->new_encoder->new_crtc->pipe;
10448 }
10449
Damien Lespiaub2784e12014-08-05 11:29:37 +010010450 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010451 if (encoder->base.crtc == &encoder->new_crtc->base)
10452 continue;
10453
10454 if (encoder->base.crtc) {
10455 tmp_crtc = encoder->base.crtc;
10456
10457 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10458 }
10459
10460 if (encoder->new_crtc)
10461 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
10462 }
10463
Ville Syrjälä76688512014-01-10 11:28:06 +020010464 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010465 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010466 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010467 continue;
10468
Ville Syrjälä76688512014-01-10 11:28:06 +020010469 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010470 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020010471 else
10472 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010473 }
10474
10475
10476 /* set_mode is also used to update properties on life display pipes. */
10477 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020010478 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010479 *prepare_pipes |= 1 << intel_crtc->pipe;
10480
Daniel Vetterb6c51642013-04-12 18:48:43 +020010481 /*
10482 * For simplicity do a full modeset on any pipe where the output routing
10483 * changed. We could be more clever, but that would require us to be
10484 * more careful with calling the relevant encoder->mode_set functions.
10485 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010486 if (*prepare_pipes)
10487 *modeset_pipes = *prepare_pipes;
10488
10489 /* ... and mask these out. */
10490 *modeset_pipes &= ~(*disable_pipes);
10491 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020010492
10493 /*
10494 * HACK: We don't (yet) fully support global modesets. intel_set_config
10495 * obies this rule, but the modeset restore mode of
10496 * intel_modeset_setup_hw_state does not.
10497 */
10498 *modeset_pipes &= 1 << intel_crtc->pipe;
10499 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020010500
10501 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10502 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010503}
10504
Daniel Vetterea9d7582012-07-10 10:42:52 +020010505static bool intel_crtc_in_use(struct drm_crtc *crtc)
10506{
10507 struct drm_encoder *encoder;
10508 struct drm_device *dev = crtc->dev;
10509
10510 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10511 if (encoder->crtc == crtc)
10512 return true;
10513
10514 return false;
10515}
10516
10517static void
10518intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10519{
10520 struct intel_encoder *intel_encoder;
10521 struct intel_crtc *intel_crtc;
10522 struct drm_connector *connector;
10523
Damien Lespiaub2784e12014-08-05 11:29:37 +010010524 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020010525 if (!intel_encoder->base.crtc)
10526 continue;
10527
10528 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10529
10530 if (prepare_pipes & (1 << intel_crtc->pipe))
10531 intel_encoder->connectors_active = false;
10532 }
10533
10534 intel_modeset_commit_output_state(dev);
10535
Ville Syrjälä76688512014-01-10 11:28:06 +020010536 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010537 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010538 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010539 WARN_ON(intel_crtc->new_config &&
10540 intel_crtc->new_config != &intel_crtc->config);
10541 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010542 }
10543
10544 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10545 if (!connector->encoder || !connector->encoder->crtc)
10546 continue;
10547
10548 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10549
10550 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020010551 struct drm_property *dpms_property =
10552 dev->mode_config.dpms_property;
10553
Daniel Vetterea9d7582012-07-10 10:42:52 +020010554 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050010555 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020010556 dpms_property,
10557 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010558
10559 intel_encoder = to_intel_encoder(connector->encoder);
10560 intel_encoder->connectors_active = true;
10561 }
10562 }
10563
10564}
10565
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010566static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010567{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010568 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010569
10570 if (clock1 == clock2)
10571 return true;
10572
10573 if (!clock1 || !clock2)
10574 return false;
10575
10576 diff = abs(clock1 - clock2);
10577
10578 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10579 return true;
10580
10581 return false;
10582}
10583
Daniel Vetter25c5b262012-07-08 22:08:04 +020010584#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10585 list_for_each_entry((intel_crtc), \
10586 &(dev)->mode_config.crtc_list, \
10587 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020010588 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020010589
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010590static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010591intel_pipe_config_compare(struct drm_device *dev,
10592 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010593 struct intel_crtc_config *pipe_config)
10594{
Daniel Vetter66e985c2013-06-05 13:34:20 +020010595#define PIPE_CONF_CHECK_X(name) \
10596 if (current_config->name != pipe_config->name) { \
10597 DRM_ERROR("mismatch in " #name " " \
10598 "(expected 0x%08x, found 0x%08x)\n", \
10599 current_config->name, \
10600 pipe_config->name); \
10601 return false; \
10602 }
10603
Daniel Vetter08a24032013-04-19 11:25:34 +020010604#define PIPE_CONF_CHECK_I(name) \
10605 if (current_config->name != pipe_config->name) { \
10606 DRM_ERROR("mismatch in " #name " " \
10607 "(expected %i, found %i)\n", \
10608 current_config->name, \
10609 pipe_config->name); \
10610 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010010611 }
10612
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010613/* This is required for BDW+ where there is only one set of registers for
10614 * switching between high and low RR.
10615 * This macro can be used whenever a comparison has to be made between one
10616 * hw state and multiple sw state variables.
10617 */
10618#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10619 if ((current_config->name != pipe_config->name) && \
10620 (current_config->alt_name != pipe_config->name)) { \
10621 DRM_ERROR("mismatch in " #name " " \
10622 "(expected %i or %i, found %i)\n", \
10623 current_config->name, \
10624 current_config->alt_name, \
10625 pipe_config->name); \
10626 return false; \
10627 }
10628
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010629#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10630 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070010631 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010632 "(expected %i, found %i)\n", \
10633 current_config->name & (mask), \
10634 pipe_config->name & (mask)); \
10635 return false; \
10636 }
10637
Ville Syrjälä5e550652013-09-06 23:29:07 +030010638#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10639 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10640 DRM_ERROR("mismatch in " #name " " \
10641 "(expected %i, found %i)\n", \
10642 current_config->name, \
10643 pipe_config->name); \
10644 return false; \
10645 }
10646
Daniel Vetterbb760062013-06-06 14:55:52 +020010647#define PIPE_CONF_QUIRK(quirk) \
10648 ((current_config->quirks | pipe_config->quirks) & (quirk))
10649
Daniel Vettereccb1402013-05-22 00:50:22 +020010650 PIPE_CONF_CHECK_I(cpu_transcoder);
10651
Daniel Vetter08a24032013-04-19 11:25:34 +020010652 PIPE_CONF_CHECK_I(has_pch_encoder);
10653 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020010654 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10655 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10656 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10657 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10658 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020010659
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010660 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010661
10662 if (INTEL_INFO(dev)->gen < 8) {
10663 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10664 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10665 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10666 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10667 PIPE_CONF_CHECK_I(dp_m_n.tu);
10668
10669 if (current_config->has_drrs) {
10670 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10671 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10672 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10673 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10674 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10675 }
10676 } else {
10677 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10678 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10679 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10680 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10681 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10682 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010683
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010684 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10685 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10686 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10687 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10688 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10689 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10690
10691 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10692 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10693 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10694 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10695 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10696 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10697
Daniel Vetterc93f54c2013-06-27 19:47:19 +020010698 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020010699 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020010700 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10701 IS_VALLEYVIEW(dev))
10702 PIPE_CONF_CHECK_I(limited_color_range);
Daniel Vetter6c49f242013-06-06 12:45:25 +020010703
Daniel Vetter9ed109a2014-04-24 23:54:52 +020010704 PIPE_CONF_CHECK_I(has_audio);
10705
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010706 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10707 DRM_MODE_FLAG_INTERLACE);
10708
Daniel Vetterbb760062013-06-06 14:55:52 +020010709 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10710 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10711 DRM_MODE_FLAG_PHSYNC);
10712 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10713 DRM_MODE_FLAG_NHSYNC);
10714 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10715 DRM_MODE_FLAG_PVSYNC);
10716 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10717 DRM_MODE_FLAG_NVSYNC);
10718 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010719
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010720 PIPE_CONF_CHECK_I(pipe_src_w);
10721 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010722
Daniel Vetter99535992014-04-13 12:00:33 +020010723 /*
10724 * FIXME: BIOS likes to set up a cloned config with lvds+external
10725 * screen. Since we don't yet re-compute the pipe config when moving
10726 * just the lvds port away to another pipe the sw tracking won't match.
10727 *
10728 * Proper atomic modesets with recomputed global state will fix this.
10729 * Until then just don't check gmch state for inherited modes.
10730 */
10731 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10732 PIPE_CONF_CHECK_I(gmch_pfit.control);
10733 /* pfit ratios are autocomputed by the hw on gen4+ */
10734 if (INTEL_INFO(dev)->gen < 4)
10735 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10736 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10737 }
10738
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010739 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10740 if (current_config->pch_pfit.enabled) {
10741 PIPE_CONF_CHECK_I(pch_pfit.pos);
10742 PIPE_CONF_CHECK_I(pch_pfit.size);
10743 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010744
Jesse Barnese59150d2014-01-07 13:30:45 -080010745 /* BDW+ don't expose a synchronous way to read the state */
10746 if (IS_HASWELL(dev))
10747 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010748
Ville Syrjälä282740f2013-09-04 18:30:03 +030010749 PIPE_CONF_CHECK_I(double_wide);
10750
Daniel Vetter26804af2014-06-25 22:01:55 +030010751 PIPE_CONF_CHECK_X(ddi_pll_sel);
10752
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010753 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010754 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020010755 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010756 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10757 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010758 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010759
Ville Syrjälä42571ae2013-09-06 23:29:00 +030010760 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10761 PIPE_CONF_CHECK_I(pipe_bpp);
10762
Jesse Barnesa9a7e982014-01-20 14:18:04 -080010763 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10764 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030010765
Daniel Vetter66e985c2013-06-05 13:34:20 +020010766#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020010767#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010768#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010769#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030010770#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020010771#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010772
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010773 return true;
10774}
10775
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010776static void
10777check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010778{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010779 struct intel_connector *connector;
10780
10781 list_for_each_entry(connector, &dev->mode_config.connector_list,
10782 base.head) {
10783 /* This also checks the encoder/connector hw state with the
10784 * ->get_hw_state callbacks. */
10785 intel_connector_check_state(connector);
10786
10787 WARN(&connector->new_encoder->base != connector->base.encoder,
10788 "connector's staged encoder doesn't match current encoder\n");
10789 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010790}
10791
10792static void
10793check_encoder_state(struct drm_device *dev)
10794{
10795 struct intel_encoder *encoder;
10796 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010797
Damien Lespiaub2784e12014-08-05 11:29:37 +010010798 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010799 bool enabled = false;
10800 bool active = false;
10801 enum pipe pipe, tracked_pipe;
10802
10803 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10804 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030010805 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010806
10807 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10808 "encoder's stage crtc doesn't match current crtc\n");
10809 WARN(encoder->connectors_active && !encoder->base.crtc,
10810 "encoder's active_connectors set, but no crtc\n");
10811
10812 list_for_each_entry(connector, &dev->mode_config.connector_list,
10813 base.head) {
10814 if (connector->base.encoder != &encoder->base)
10815 continue;
10816 enabled = true;
10817 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10818 active = true;
10819 }
Dave Airlie0e32b392014-05-02 14:02:48 +100010820 /*
10821 * for MST connectors if we unplug the connector is gone
10822 * away but the encoder is still connected to a crtc
10823 * until a modeset happens in response to the hotplug.
10824 */
10825 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10826 continue;
10827
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010828 WARN(!!encoder->base.crtc != enabled,
10829 "encoder's enabled state mismatch "
10830 "(expected %i, found %i)\n",
10831 !!encoder->base.crtc, enabled);
10832 WARN(active && !encoder->base.crtc,
10833 "active encoder with no crtc\n");
10834
10835 WARN(encoder->connectors_active != active,
10836 "encoder's computed active state doesn't match tracked active state "
10837 "(expected %i, found %i)\n", active, encoder->connectors_active);
10838
10839 active = encoder->get_hw_state(encoder, &pipe);
10840 WARN(active != encoder->connectors_active,
10841 "encoder's hw state doesn't match sw tracking "
10842 "(expected %i, found %i)\n",
10843 encoder->connectors_active, active);
10844
10845 if (!encoder->base.crtc)
10846 continue;
10847
10848 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10849 WARN(active && pipe != tracked_pipe,
10850 "active encoder's pipe doesn't match"
10851 "(expected %i, found %i)\n",
10852 tracked_pipe, pipe);
10853
10854 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010855}
10856
10857static void
10858check_crtc_state(struct drm_device *dev)
10859{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010860 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010861 struct intel_crtc *crtc;
10862 struct intel_encoder *encoder;
10863 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010864
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010865 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010866 bool enabled = false;
10867 bool active = false;
10868
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010869 memset(&pipe_config, 0, sizeof(pipe_config));
10870
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010871 DRM_DEBUG_KMS("[CRTC:%d]\n",
10872 crtc->base.base.id);
10873
10874 WARN(crtc->active && !crtc->base.enabled,
10875 "active crtc, but not enabled in sw tracking\n");
10876
Damien Lespiaub2784e12014-08-05 11:29:37 +010010877 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010878 if (encoder->base.crtc != &crtc->base)
10879 continue;
10880 enabled = true;
10881 if (encoder->connectors_active)
10882 active = true;
10883 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010884
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010885 WARN(active != crtc->active,
10886 "crtc's computed active state doesn't match tracked active state "
10887 "(expected %i, found %i)\n", active, crtc->active);
10888 WARN(enabled != crtc->base.enabled,
10889 "crtc's computed enabled state doesn't match tracked enabled state "
10890 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10891
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010892 active = dev_priv->display.get_pipe_config(crtc,
10893 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020010894
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030010895 /* hw state is inconsistent with the pipe quirk */
10896 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
10897 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020010898 active = crtc->active;
10899
Damien Lespiaub2784e12014-08-05 11:29:37 +010010900 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030010901 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020010902 if (encoder->base.crtc != &crtc->base)
10903 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010010904 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020010905 encoder->get_config(encoder, &pipe_config);
10906 }
10907
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010908 WARN(crtc->active != active,
10909 "crtc active state doesn't match with hw state "
10910 "(expected %i, found %i)\n", crtc->active, active);
10911
Daniel Vetterc0b03412013-05-28 12:05:54 +020010912 if (active &&
10913 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10914 WARN(1, "pipe state doesn't match!\n");
10915 intel_dump_pipe_config(crtc, &pipe_config,
10916 "[hw state]");
10917 intel_dump_pipe_config(crtc, &crtc->config,
10918 "[sw state]");
10919 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010920 }
10921}
10922
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010923static void
10924check_shared_dpll_state(struct drm_device *dev)
10925{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010926 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010927 struct intel_crtc *crtc;
10928 struct intel_dpll_hw_state dpll_hw_state;
10929 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020010930
10931 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10932 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10933 int enabled_crtcs = 0, active_crtcs = 0;
10934 bool active;
10935
10936 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10937
10938 DRM_DEBUG_KMS("%s\n", pll->name);
10939
10940 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10941
10942 WARN(pll->active > pll->refcount,
10943 "more active pll users than references: %i vs %i\n",
10944 pll->active, pll->refcount);
10945 WARN(pll->active && !pll->on,
10946 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +020010947 WARN(pll->on && !pll->active,
10948 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010949 WARN(pll->on != active,
10950 "pll on state mismatch (expected %i, found %i)\n",
10951 pll->on, active);
10952
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010953 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020010954 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10955 enabled_crtcs++;
10956 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10957 active_crtcs++;
10958 }
10959 WARN(pll->active != active_crtcs,
10960 "pll active crtcs mismatch (expected %i, found %i)\n",
10961 pll->active, active_crtcs);
10962 WARN(pll->refcount != enabled_crtcs,
10963 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10964 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010965
10966 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10967 sizeof(dpll_hw_state)),
10968 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010969 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010970}
10971
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010972void
10973intel_modeset_check_state(struct drm_device *dev)
10974{
10975 check_connector_state(dev);
10976 check_encoder_state(dev);
10977 check_crtc_state(dev);
10978 check_shared_dpll_state(dev);
10979}
10980
Ville Syrjälä18442d02013-09-13 16:00:08 +030010981void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10982 int dotclock)
10983{
10984 /*
10985 * FDI already provided one idea for the dotclock.
10986 * Yell if the encoder disagrees.
10987 */
Damien Lespiau241bfc32013-09-25 16:45:37 +010010988 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010989 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +010010990 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010991}
10992
Ville Syrjälä80715b22014-05-15 20:23:23 +030010993static void update_scanline_offset(struct intel_crtc *crtc)
10994{
10995 struct drm_device *dev = crtc->base.dev;
10996
10997 /*
10998 * The scanline counter increments at the leading edge of hsync.
10999 *
11000 * On most platforms it starts counting from vtotal-1 on the
11001 * first active line. That means the scanline counter value is
11002 * always one less than what we would expect. Ie. just after
11003 * start of vblank, which also occurs at start of hsync (on the
11004 * last active line), the scanline counter will read vblank_start-1.
11005 *
11006 * On gen2 the scanline counter starts counting from 1 instead
11007 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11008 * to keep the value positive), instead of adding one.
11009 *
11010 * On HSW+ the behaviour of the scanline counter depends on the output
11011 * type. For DP ports it behaves like most other platforms, but on HDMI
11012 * there's an extra 1 line difference. So we need to add two instead of
11013 * one to the value.
11014 */
11015 if (IS_GEN2(dev)) {
11016 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
11017 int vtotal;
11018
11019 vtotal = mode->crtc_vtotal;
11020 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11021 vtotal /= 2;
11022
11023 crtc->scanline_offset = vtotal - 1;
11024 } else if (HAS_DDI(dev) &&
11025 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
11026 crtc->scanline_offset = 2;
11027 } else
11028 crtc->scanline_offset = 1;
11029}
11030
Daniel Vetterf30da182013-04-11 20:22:50 +020011031static int __intel_set_mode(struct drm_crtc *crtc,
11032 struct drm_display_mode *mode,
11033 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +020011034{
11035 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030011036 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011037 struct drm_display_mode *saved_mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011038 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020011039 struct intel_crtc *intel_crtc;
11040 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011041 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020011042
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011043 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011044 if (!saved_mode)
11045 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020011046
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011047 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +020011048 &prepare_pipes, &disable_pipes);
11049
Tim Gardner3ac18232012-12-07 07:54:26 -070011050 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011051
Daniel Vetter25c5b262012-07-08 22:08:04 +020011052 /* Hack: Because we don't (yet) support global modeset on multiple
11053 * crtcs, we don't keep track of the new mode for more than one crtc.
11054 * Hence simply check whether any bit is set in modeset_pipes in all the
11055 * pieces of code that are not yet converted to deal with mutliple crtcs
11056 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +020011057 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011058 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011059 if (IS_ERR(pipe_config)) {
11060 ret = PTR_ERR(pipe_config);
11061 pipe_config = NULL;
11062
Tim Gardner3ac18232012-12-07 07:54:26 -070011063 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +020011064 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011065 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11066 "[modeset]");
Ville Syrjälä50741ab2014-01-10 11:28:07 +020011067 to_intel_crtc(crtc)->new_config = pipe_config;
Daniel Vettera6778b32012-07-02 09:56:42 +020011068 }
11069
Jesse Barnes30a970c2013-11-04 13:48:12 -080011070 /*
11071 * See if the config requires any additional preparation, e.g.
11072 * to adjust global state with pipes off. We need to do this
11073 * here so we can get the modeset_pipe updated config for the new
11074 * mode set on this crtc. For other crtcs we need to use the
11075 * adjusted_mode bits in the crtc directly.
11076 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020011077 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +020011078 valleyview_modeset_global_pipes(dev, &prepare_pipes);
Jesse Barnes30a970c2013-11-04 13:48:12 -080011079
Ville Syrjäläc164f832013-11-05 22:34:12 +020011080 /* may have added more to prepare_pipes than we should */
11081 prepare_pipes &= ~disable_pipes;
11082 }
11083
Daniel Vetter460da9162013-03-27 00:44:51 +010011084 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11085 intel_crtc_disable(&intel_crtc->base);
11086
Daniel Vetterea9d7582012-07-10 10:42:52 +020011087 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11088 if (intel_crtc->base.enabled)
11089 dev_priv->display.crtc_disable(&intel_crtc->base);
11090 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011091
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020011092 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11093 * to set it here already despite that we pass it down the callchain.
11094 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011095 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020011096 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011097 /* mode_set/enable/disable functions rely on a correct pipe
11098 * config. */
11099 to_intel_crtc(crtc)->config = *pipe_config;
Ville Syrjälä50741ab2014-01-10 11:28:07 +020011100 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020011101
11102 /*
11103 * Calculate and store various constants which
11104 * are later needed by vblank and swap-completion
11105 * timestamping. They are derived from true hwmode.
11106 */
11107 drm_calc_timestamping_constants(crtc,
11108 &pipe_config->adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011109 }
Daniel Vetter7758a112012-07-08 19:40:39 +020011110
Daniel Vetterea9d7582012-07-10 10:42:52 +020011111 /* Only after disabling all output pipelines that will be changed can we
11112 * update the the output configuration. */
11113 intel_modeset_update_state(dev, prepare_pipes);
11114
Daniel Vetter47fab732012-10-26 10:58:18 +020011115 if (dev_priv->display.modeset_global_resources)
11116 dev_priv->display.modeset_global_resources(dev);
11117
Daniel Vettera6778b32012-07-02 09:56:42 +020011118 /* Set up the DPLL and any encoders state that needs to adjust or depend
11119 * on the DPLL.
11120 */
Daniel Vetter25c5b262012-07-08 22:08:04 +020011121 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070011122 struct drm_framebuffer *old_fb = crtc->primary->fb;
11123 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
11124 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Daniel Vetter4c107942014-04-24 23:55:05 +020011125
11126 mutex_lock(&dev->struct_mutex);
11127 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vettera071fa02014-06-18 23:28:09 +020011128 obj,
Daniel Vetter4c107942014-04-24 23:55:05 +020011129 NULL);
11130 if (ret != 0) {
11131 DRM_ERROR("pin & fence failed\n");
11132 mutex_unlock(&dev->struct_mutex);
11133 goto done;
11134 }
Matt Roper2ff8fde2014-07-08 07:50:07 -070011135 if (old_fb)
Daniel Vettera071fa02014-06-18 23:28:09 +020011136 intel_unpin_fb_obj(old_obj);
Daniel Vettera071fa02014-06-18 23:28:09 +020011137 i915_gem_track_fb(old_obj, obj,
11138 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
Daniel Vetter4c107942014-04-24 23:55:05 +020011139 mutex_unlock(&dev->struct_mutex);
11140
11141 crtc->primary->fb = fb;
11142 crtc->x = x;
11143 crtc->y = y;
11144
Daniel Vetter4271b752014-04-24 23:55:00 +020011145 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
11146 x, y, fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011147 if (ret)
11148 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +020011149 }
11150
11151 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030011152 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11153 update_scanline_offset(intel_crtc);
11154
Daniel Vetter25c5b262012-07-08 22:08:04 +020011155 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030011156 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011157
Daniel Vettera6778b32012-07-02 09:56:42 +020011158 /* FIXME: add subpixel order */
11159done:
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011160 if (ret && crtc->enabled)
Tim Gardner3ac18232012-12-07 07:54:26 -070011161 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011162
Tim Gardner3ac18232012-12-07 07:54:26 -070011163out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011164 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -070011165 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020011166 return ret;
11167}
11168
Damien Lespiaue7457a92013-08-08 22:28:59 +010011169static int intel_set_mode(struct drm_crtc *crtc,
11170 struct drm_display_mode *mode,
11171 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +020011172{
11173 int ret;
11174
11175 ret = __intel_set_mode(crtc, mode, x, y, fb);
11176
11177 if (ret == 0)
11178 intel_modeset_check_state(crtc->dev);
11179
11180 return ret;
11181}
11182
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011183void intel_crtc_restore_mode(struct drm_crtc *crtc)
11184{
Matt Roperf4510a22014-04-01 15:22:40 -070011185 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011186}
11187
Daniel Vetter25c5b262012-07-08 22:08:04 +020011188#undef for_each_intel_crtc_masked
11189
Daniel Vetterd9e55602012-07-04 22:16:09 +020011190static void intel_set_config_free(struct intel_set_config *config)
11191{
11192 if (!config)
11193 return;
11194
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011195 kfree(config->save_connector_encoders);
11196 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020011197 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020011198 kfree(config);
11199}
11200
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011201static int intel_set_config_save_state(struct drm_device *dev,
11202 struct intel_set_config *config)
11203{
Ville Syrjälä76688512014-01-10 11:28:06 +020011204 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011205 struct drm_encoder *encoder;
11206 struct drm_connector *connector;
11207 int count;
11208
Ville Syrjälä76688512014-01-10 11:28:06 +020011209 config->save_crtc_enabled =
11210 kcalloc(dev->mode_config.num_crtc,
11211 sizeof(bool), GFP_KERNEL);
11212 if (!config->save_crtc_enabled)
11213 return -ENOMEM;
11214
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011215 config->save_encoder_crtcs =
11216 kcalloc(dev->mode_config.num_encoder,
11217 sizeof(struct drm_crtc *), GFP_KERNEL);
11218 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011219 return -ENOMEM;
11220
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011221 config->save_connector_encoders =
11222 kcalloc(dev->mode_config.num_connector,
11223 sizeof(struct drm_encoder *), GFP_KERNEL);
11224 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011225 return -ENOMEM;
11226
11227 /* Copy data. Note that driver private data is not affected.
11228 * Should anything bad happen only the expected state is
11229 * restored, not the drivers personal bookkeeping.
11230 */
11231 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010011232 for_each_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011233 config->save_crtc_enabled[count++] = crtc->enabled;
11234 }
11235
11236 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011237 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011238 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011239 }
11240
11241 count = 0;
11242 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011243 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011244 }
11245
11246 return 0;
11247}
11248
11249static void intel_set_config_restore_state(struct drm_device *dev,
11250 struct intel_set_config *config)
11251{
Ville Syrjälä76688512014-01-10 11:28:06 +020011252 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011253 struct intel_encoder *encoder;
11254 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011255 int count;
11256
11257 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011258 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011259 crtc->new_enabled = config->save_crtc_enabled[count++];
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011260
11261 if (crtc->new_enabled)
11262 crtc->new_config = &crtc->config;
11263 else
11264 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020011265 }
11266
11267 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010011268 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011269 encoder->new_crtc =
11270 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011271 }
11272
11273 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020011274 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11275 connector->new_encoder =
11276 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011277 }
11278}
11279
Imre Deake3de42b2013-05-03 19:44:07 +020011280static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010011281is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020011282{
11283 int i;
11284
Chris Wilson2e57f472013-07-17 12:14:40 +010011285 if (set->num_connectors == 0)
11286 return false;
11287
11288 if (WARN_ON(set->connectors == NULL))
11289 return false;
11290
11291 for (i = 0; i < set->num_connectors; i++)
11292 if (set->connectors[i]->encoder &&
11293 set->connectors[i]->encoder->crtc == set->crtc &&
11294 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020011295 return true;
11296
11297 return false;
11298}
11299
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011300static void
11301intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11302 struct intel_set_config *config)
11303{
11304
11305 /* We should be able to check here if the fb has the same properties
11306 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010011307 if (is_crtc_connector_off(set)) {
11308 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070011309 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070011310 /*
11311 * If we have no fb, we can only flip as long as the crtc is
11312 * active, otherwise we need a full mode set. The crtc may
11313 * be active if we've only disabled the primary plane, or
11314 * in fastboot situations.
11315 */
Matt Roperf4510a22014-04-01 15:22:40 -070011316 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030011317 struct intel_crtc *intel_crtc =
11318 to_intel_crtc(set->crtc);
11319
Matt Roper3b150f02014-05-29 08:06:53 -070011320 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030011321 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11322 config->fb_changed = true;
11323 } else {
11324 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11325 config->mode_changed = true;
11326 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011327 } else if (set->fb == NULL) {
11328 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010011329 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070011330 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011331 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020011332 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011333 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020011334 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011335 }
11336
Daniel Vetter835c5872012-07-10 18:11:08 +020011337 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011338 config->fb_changed = true;
11339
11340 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11341 DRM_DEBUG_KMS("modes are different, full mode set\n");
11342 drm_mode_debug_printmodeline(&set->crtc->mode);
11343 drm_mode_debug_printmodeline(set->mode);
11344 config->mode_changed = true;
11345 }
Chris Wilsona1d95702013-08-13 18:48:47 +010011346
11347 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11348 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011349}
11350
Daniel Vetter2e431052012-07-04 22:42:15 +020011351static int
Daniel Vetter9a935852012-07-05 22:34:27 +020011352intel_modeset_stage_output_state(struct drm_device *dev,
11353 struct drm_mode_set *set,
11354 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +020011355{
Daniel Vetter9a935852012-07-05 22:34:27 +020011356 struct intel_connector *connector;
11357 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020011358 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030011359 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020011360
Damien Lespiau9abdda72013-02-13 13:29:23 +000011361 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020011362 * of connectors. For paranoia, double-check this. */
11363 WARN_ON(!set->fb && (set->num_connectors != 0));
11364 WARN_ON(set->fb && (set->num_connectors == 0));
11365
Daniel Vetter9a935852012-07-05 22:34:27 +020011366 list_for_each_entry(connector, &dev->mode_config.connector_list,
11367 base.head) {
11368 /* Otherwise traverse passed in connector list and get encoders
11369 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020011370 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011371 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100011372 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020011373 break;
11374 }
11375 }
11376
Daniel Vetter9a935852012-07-05 22:34:27 +020011377 /* If we disable the crtc, disable all its connectors. Also, if
11378 * the connector is on the changing crtc but not on the new
11379 * connector list, disable it. */
11380 if ((!set->fb || ro == set->num_connectors) &&
11381 connector->base.encoder &&
11382 connector->base.encoder->crtc == set->crtc) {
11383 connector->new_encoder = NULL;
11384
11385 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11386 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011387 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020011388 }
11389
11390
11391 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +020011392 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011393 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011394 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011395 }
11396 /* connector->new_encoder is now updated for all connectors. */
11397
11398 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +020011399 list_for_each_entry(connector, &dev->mode_config.connector_list,
11400 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011401 struct drm_crtc *new_crtc;
11402
Daniel Vetter9a935852012-07-05 22:34:27 +020011403 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020011404 continue;
11405
Daniel Vetter9a935852012-07-05 22:34:27 +020011406 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020011407
11408 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011409 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020011410 new_crtc = set->crtc;
11411 }
11412
11413 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010011414 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11415 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011416 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020011417 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011418 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020011419
11420 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11421 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011422 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020011423 new_crtc->base.id);
11424 }
11425
11426 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010011427 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011428 int num_connectors = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020011429 list_for_each_entry(connector,
11430 &dev->mode_config.connector_list,
11431 base.head) {
11432 if (connector->new_encoder == encoder) {
11433 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011434 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020011435 }
11436 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011437
11438 if (num_connectors == 0)
11439 encoder->new_crtc = NULL;
11440 else if (num_connectors > 1)
11441 return -EINVAL;
11442
Daniel Vetter9a935852012-07-05 22:34:27 +020011443 /* Only now check for crtc changes so we don't miss encoders
11444 * that will be disabled. */
11445 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +020011446 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011447 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011448 }
11449 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011450 /* Now we've also updated encoder->new_crtc for all encoders. */
Dave Airlie0e32b392014-05-02 14:02:48 +100011451 list_for_each_entry(connector, &dev->mode_config.connector_list,
11452 base.head) {
11453 if (connector->new_encoder)
11454 if (connector->new_encoder != connector->encoder)
11455 connector->encoder = connector->new_encoder;
11456 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011457 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011458 crtc->new_enabled = false;
11459
Damien Lespiaub2784e12014-08-05 11:29:37 +010011460 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011461 if (encoder->new_crtc == crtc) {
11462 crtc->new_enabled = true;
11463 break;
11464 }
11465 }
11466
11467 if (crtc->new_enabled != crtc->base.enabled) {
11468 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11469 crtc->new_enabled ? "en" : "dis");
11470 config->mode_changed = true;
11471 }
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011472
11473 if (crtc->new_enabled)
11474 crtc->new_config = &crtc->config;
11475 else
11476 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020011477 }
11478
Daniel Vetter2e431052012-07-04 22:42:15 +020011479 return 0;
11480}
11481
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011482static void disable_crtc_nofb(struct intel_crtc *crtc)
11483{
11484 struct drm_device *dev = crtc->base.dev;
11485 struct intel_encoder *encoder;
11486 struct intel_connector *connector;
11487
11488 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11489 pipe_name(crtc->pipe));
11490
11491 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11492 if (connector->new_encoder &&
11493 connector->new_encoder->new_crtc == crtc)
11494 connector->new_encoder = NULL;
11495 }
11496
Damien Lespiaub2784e12014-08-05 11:29:37 +010011497 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011498 if (encoder->new_crtc == crtc)
11499 encoder->new_crtc = NULL;
11500 }
11501
11502 crtc->new_enabled = false;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011503 crtc->new_config = NULL;
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011504}
11505
Daniel Vetter2e431052012-07-04 22:42:15 +020011506static int intel_crtc_set_config(struct drm_mode_set *set)
11507{
11508 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020011509 struct drm_mode_set save_set;
11510 struct intel_set_config *config;
11511 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020011512
Daniel Vetter8d3e3752012-07-05 16:09:09 +020011513 BUG_ON(!set);
11514 BUG_ON(!set->crtc);
11515 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020011516
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010011517 /* Enforce sane interface api - has been abused by the fb helper. */
11518 BUG_ON(!set->mode && set->fb);
11519 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020011520
Daniel Vetter2e431052012-07-04 22:42:15 +020011521 if (set->fb) {
11522 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11523 set->crtc->base.id, set->fb->base.id,
11524 (int)set->num_connectors, set->x, set->y);
11525 } else {
11526 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020011527 }
11528
11529 dev = set->crtc->dev;
11530
11531 ret = -ENOMEM;
11532 config = kzalloc(sizeof(*config), GFP_KERNEL);
11533 if (!config)
11534 goto out_config;
11535
11536 ret = intel_set_config_save_state(dev, config);
11537 if (ret)
11538 goto out_config;
11539
11540 save_set.crtc = set->crtc;
11541 save_set.mode = &set->crtc->mode;
11542 save_set.x = set->crtc->x;
11543 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070011544 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020011545
11546 /* Compute whether we need a full modeset, only an fb base update or no
11547 * change at all. In the future we might also check whether only the
11548 * mode changed, e.g. for LVDS where we only change the panel fitter in
11549 * such cases. */
11550 intel_set_config_compute_mode_changes(set, config);
11551
Daniel Vetter9a935852012-07-05 22:34:27 +020011552 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +020011553 if (ret)
11554 goto fail;
11555
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011556 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011557 ret = intel_set_mode(set->crtc, set->mode,
11558 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011559 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070011560 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
11561
Ville Syrjälä4878cae2013-02-18 19:08:48 +020011562 intel_crtc_wait_for_pending_flips(set->crtc);
11563
Daniel Vetter4f660f42012-07-02 09:47:37 +020011564 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +020011565 set->x, set->y, set->fb);
Matt Roper3b150f02014-05-29 08:06:53 -070011566
11567 /*
11568 * We need to make sure the primary plane is re-enabled if it
11569 * has previously been turned off.
11570 */
11571 if (!intel_crtc->primary_enabled && ret == 0) {
11572 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030011573 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070011574 }
11575
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011576 /*
11577 * In the fastboot case this may be our only check of the
11578 * state after boot. It would be better to only do it on
11579 * the first update, but we don't have a nice way of doing that
11580 * (and really, set_config isn't used much for high freq page
11581 * flipping, so increasing its cost here shouldn't be a big
11582 * deal).
11583 */
Jani Nikulad330a952014-01-21 11:24:25 +020011584 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011585 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020011586 }
11587
Chris Wilson2d05eae2013-05-03 17:36:25 +010011588 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020011589 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11590 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020011591fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010011592 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011593
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011594 /*
11595 * HACK: if the pipe was on, but we didn't have a framebuffer,
11596 * force the pipe off to avoid oopsing in the modeset code
11597 * due to fb==NULL. This should only happen during boot since
11598 * we don't yet reconstruct the FB from the hardware state.
11599 */
11600 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11601 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11602
Chris Wilson2d05eae2013-05-03 17:36:25 +010011603 /* Try to restore the config */
11604 if (config->mode_changed &&
11605 intel_set_mode(save_set.crtc, save_set.mode,
11606 save_set.x, save_set.y, save_set.fb))
11607 DRM_ERROR("failed to restore config after modeset failure\n");
11608 }
Daniel Vetter50f56112012-07-02 09:35:43 +020011609
Daniel Vetterd9e55602012-07-04 22:16:09 +020011610out_config:
11611 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011612 return ret;
11613}
11614
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011615static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011616 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020011617 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011618 .destroy = intel_crtc_destroy,
11619 .page_flip = intel_crtc_page_flip,
11620};
11621
Daniel Vetter53589012013-06-05 13:34:16 +020011622static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11623 struct intel_shared_dpll *pll,
11624 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011625{
Daniel Vetter53589012013-06-05 13:34:16 +020011626 uint32_t val;
11627
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030011628 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_PLLS))
11629 return false;
11630
Daniel Vetter53589012013-06-05 13:34:16 +020011631 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020011632 hw_state->dpll = val;
11633 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11634 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020011635
11636 return val & DPLL_VCO_ENABLE;
11637}
11638
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011639static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11640 struct intel_shared_dpll *pll)
11641{
11642 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
11643 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
11644}
11645
Daniel Vettere7b903d2013-06-05 13:34:14 +020011646static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11647 struct intel_shared_dpll *pll)
11648{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011649 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020011650 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020011651
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011652 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11653
11654 /* Wait for the clocks to stabilize. */
11655 POSTING_READ(PCH_DPLL(pll->id));
11656 udelay(150);
11657
11658 /* The pixel multiplier can only be updated once the
11659 * DPLL is enabled and the clocks are stable.
11660 *
11661 * So write it again.
11662 */
11663 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11664 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011665 udelay(200);
11666}
11667
11668static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11669 struct intel_shared_dpll *pll)
11670{
11671 struct drm_device *dev = dev_priv->dev;
11672 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011673
11674 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011675 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020011676 if (intel_crtc_to_shared_dpll(crtc) == pll)
11677 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
11678 }
11679
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011680 I915_WRITE(PCH_DPLL(pll->id), 0);
11681 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011682 udelay(200);
11683}
11684
Daniel Vetter46edb022013-06-05 13:34:12 +020011685static char *ibx_pch_dpll_names[] = {
11686 "PCH DPLL A",
11687 "PCH DPLL B",
11688};
11689
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011690static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011691{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011692 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011693 int i;
11694
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011695 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011696
Daniel Vettere72f9fb2013-06-05 13:34:06 +020011697 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020011698 dev_priv->shared_dplls[i].id = i;
11699 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011700 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011701 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11702 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020011703 dev_priv->shared_dplls[i].get_hw_state =
11704 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011705 }
11706}
11707
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011708static void intel_shared_dpll_init(struct drm_device *dev)
11709{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011710 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011711
Daniel Vetter9cd86932014-06-25 22:01:57 +030011712 if (HAS_DDI(dev))
11713 intel_ddi_pll_init(dev);
11714 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011715 ibx_pch_dpll_init(dev);
11716 else
11717 dev_priv->num_shared_dpll = 0;
11718
11719 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011720}
11721
Matt Roper465c1202014-05-29 08:06:54 -070011722static int
11723intel_primary_plane_disable(struct drm_plane *plane)
11724{
11725 struct drm_device *dev = plane->dev;
Matt Roper465c1202014-05-29 08:06:54 -070011726 struct intel_crtc *intel_crtc;
11727
11728 if (!plane->fb)
11729 return 0;
11730
11731 BUG_ON(!plane->crtc);
11732
11733 intel_crtc = to_intel_crtc(plane->crtc);
11734
11735 /*
11736 * Even though we checked plane->fb above, it's still possible that
11737 * the primary plane has been implicitly disabled because the crtc
11738 * coordinates given weren't visible, or because we detected
11739 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11740 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11741 * In either case, we need to unpin the FB and let the fb pointer get
11742 * updated, but otherwise we don't need to touch the hardware.
11743 */
11744 if (!intel_crtc->primary_enabled)
11745 goto disable_unpin;
11746
11747 intel_crtc_wait_for_pending_flips(plane->crtc);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030011748 intel_disable_primary_hw_plane(plane, plane->crtc);
11749
Matt Roper465c1202014-05-29 08:06:54 -070011750disable_unpin:
Matt Roper4c345742014-07-09 16:22:10 -070011751 mutex_lock(&dev->struct_mutex);
Matt Roper2ff8fde2014-07-08 07:50:07 -070011752 i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
Daniel Vettera071fa02014-06-18 23:28:09 +020011753 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
Matt Roper2ff8fde2014-07-08 07:50:07 -070011754 intel_unpin_fb_obj(intel_fb_obj(plane->fb));
Matt Roper4c345742014-07-09 16:22:10 -070011755 mutex_unlock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070011756 plane->fb = NULL;
11757
11758 return 0;
11759}
11760
11761static int
11762intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11763 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11764 unsigned int crtc_w, unsigned int crtc_h,
11765 uint32_t src_x, uint32_t src_y,
11766 uint32_t src_w, uint32_t src_h)
11767{
11768 struct drm_device *dev = crtc->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053011769 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper465c1202014-05-29 08:06:54 -070011770 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper2ff8fde2014-07-08 07:50:07 -070011771 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11772 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
Matt Roper465c1202014-05-29 08:06:54 -070011773 struct drm_rect dest = {
11774 /* integer pixels */
11775 .x1 = crtc_x,
11776 .y1 = crtc_y,
11777 .x2 = crtc_x + crtc_w,
11778 .y2 = crtc_y + crtc_h,
11779 };
11780 struct drm_rect src = {
11781 /* 16.16 fixed point */
11782 .x1 = src_x,
11783 .y1 = src_y,
11784 .x2 = src_x + src_w,
11785 .y2 = src_y + src_h,
11786 };
11787 const struct drm_rect clip = {
11788 /* integer pixels */
11789 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
11790 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
11791 };
Sonika Jindalce54d852014-08-21 11:44:39 +053011792 const struct {
11793 int crtc_x, crtc_y;
11794 unsigned int crtc_w, crtc_h;
11795 uint32_t src_x, src_y, src_w, src_h;
11796 } orig = {
11797 .crtc_x = crtc_x,
11798 .crtc_y = crtc_y,
11799 .crtc_w = crtc_w,
11800 .crtc_h = crtc_h,
11801 .src_x = src_x,
11802 .src_y = src_y,
11803 .src_w = src_w,
11804 .src_h = src_h,
11805 };
11806 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper465c1202014-05-29 08:06:54 -070011807 bool visible;
11808 int ret;
11809
11810 ret = drm_plane_helper_check_update(plane, crtc, fb,
11811 &src, &dest, &clip,
11812 DRM_PLANE_HELPER_NO_SCALING,
11813 DRM_PLANE_HELPER_NO_SCALING,
11814 false, true, &visible);
11815
11816 if (ret)
11817 return ret;
11818
11819 /*
11820 * If the CRTC isn't enabled, we're just pinning the framebuffer,
11821 * updating the fb pointer, and returning without touching the
11822 * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
11823 * turn on the display with all planes setup as desired.
11824 */
11825 if (!crtc->enabled) {
Matt Roper4c345742014-07-09 16:22:10 -070011826 mutex_lock(&dev->struct_mutex);
11827
Matt Roper465c1202014-05-29 08:06:54 -070011828 /*
11829 * If we already called setplane while the crtc was disabled,
11830 * we may have an fb pinned; unpin it.
11831 */
11832 if (plane->fb)
Daniel Vettera071fa02014-06-18 23:28:09 +020011833 intel_unpin_fb_obj(old_obj);
11834
11835 i915_gem_track_fb(old_obj, obj,
11836 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
Matt Roper465c1202014-05-29 08:06:54 -070011837
11838 /* Pin and return without programming hardware */
Matt Roper4c345742014-07-09 16:22:10 -070011839 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
11840 mutex_unlock(&dev->struct_mutex);
11841
11842 return ret;
Matt Roper465c1202014-05-29 08:06:54 -070011843 }
11844
11845 intel_crtc_wait_for_pending_flips(crtc);
11846
11847 /*
11848 * If clipping results in a non-visible primary plane, we'll disable
11849 * the primary plane. Note that this is a bit different than what
11850 * happens if userspace explicitly disables the plane by passing fb=0
11851 * because plane->fb still gets set and pinned.
11852 */
11853 if (!visible) {
Matt Roper4c345742014-07-09 16:22:10 -070011854 mutex_lock(&dev->struct_mutex);
11855
Matt Roper465c1202014-05-29 08:06:54 -070011856 /*
11857 * Try to pin the new fb first so that we can bail out if we
11858 * fail.
11859 */
11860 if (plane->fb != fb) {
Daniel Vettera071fa02014-06-18 23:28:09 +020011861 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
Matt Roper4c345742014-07-09 16:22:10 -070011862 if (ret) {
11863 mutex_unlock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070011864 return ret;
Matt Roper4c345742014-07-09 16:22:10 -070011865 }
Matt Roper465c1202014-05-29 08:06:54 -070011866 }
11867
Daniel Vettera071fa02014-06-18 23:28:09 +020011868 i915_gem_track_fb(old_obj, obj,
11869 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
11870
Matt Roper465c1202014-05-29 08:06:54 -070011871 if (intel_crtc->primary_enabled)
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030011872 intel_disable_primary_hw_plane(plane, crtc);
Matt Roper465c1202014-05-29 08:06:54 -070011873
11874
11875 if (plane->fb != fb)
11876 if (plane->fb)
Daniel Vettera071fa02014-06-18 23:28:09 +020011877 intel_unpin_fb_obj(old_obj);
Matt Roper465c1202014-05-29 08:06:54 -070011878
Matt Roper4c345742014-07-09 16:22:10 -070011879 mutex_unlock(&dev->struct_mutex);
11880
Sonika Jindalce54d852014-08-21 11:44:39 +053011881 } else {
Sonika Jindal48404c12014-08-22 14:06:04 +053011882 if (intel_crtc && intel_crtc->active &&
11883 intel_crtc->primary_enabled) {
11884 /*
11885 * FBC does not work on some platforms for rotated
11886 * planes, so disable it when rotation is not 0 and
11887 * update it when rotation is set back to 0.
11888 *
11889 * FIXME: This is redundant with the fbc update done in
11890 * the primary plane enable function except that that
11891 * one is done too late. We eventually need to unify
11892 * this.
11893 */
11894 if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11895 dev_priv->fbc.plane == intel_crtc->plane &&
11896 intel_plane->rotation != BIT(DRM_ROTATE_0)) {
11897 intel_disable_fbc(dev);
11898 }
11899 }
Sonika Jindalce54d852014-08-21 11:44:39 +053011900 ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
11901 if (ret)
11902 return ret;
11903
11904 if (!intel_crtc->primary_enabled)
11905 intel_enable_primary_hw_plane(plane, crtc);
Matt Roper465c1202014-05-29 08:06:54 -070011906 }
11907
Sonika Jindalce54d852014-08-21 11:44:39 +053011908 intel_plane->crtc_x = orig.crtc_x;
11909 intel_plane->crtc_y = orig.crtc_y;
11910 intel_plane->crtc_w = orig.crtc_w;
11911 intel_plane->crtc_h = orig.crtc_h;
11912 intel_plane->src_x = orig.src_x;
11913 intel_plane->src_y = orig.src_y;
11914 intel_plane->src_w = orig.src_w;
11915 intel_plane->src_h = orig.src_h;
11916 intel_plane->obj = obj;
Matt Roper465c1202014-05-29 08:06:54 -070011917
11918 return 0;
11919}
11920
Matt Roper3d7d6512014-06-10 08:28:13 -070011921/* Common destruction function for both primary and cursor planes */
11922static void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070011923{
11924 struct intel_plane *intel_plane = to_intel_plane(plane);
11925 drm_plane_cleanup(plane);
11926 kfree(intel_plane);
11927}
11928
11929static const struct drm_plane_funcs intel_primary_plane_funcs = {
11930 .update_plane = intel_primary_plane_setplane,
11931 .disable_plane = intel_primary_plane_disable,
Matt Roper3d7d6512014-06-10 08:28:13 -070011932 .destroy = intel_plane_destroy,
Sonika Jindal48404c12014-08-22 14:06:04 +053011933 .set_property = intel_plane_set_property
Matt Roper465c1202014-05-29 08:06:54 -070011934};
11935
11936static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11937 int pipe)
11938{
11939 struct intel_plane *primary;
11940 const uint32_t *intel_primary_formats;
11941 int num_formats;
11942
11943 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11944 if (primary == NULL)
11945 return NULL;
11946
11947 primary->can_scale = false;
11948 primary->max_downscale = 1;
11949 primary->pipe = pipe;
11950 primary->plane = pipe;
Sonika Jindal48404c12014-08-22 14:06:04 +053011951 primary->rotation = BIT(DRM_ROTATE_0);
Matt Roper465c1202014-05-29 08:06:54 -070011952 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11953 primary->plane = !pipe;
11954
11955 if (INTEL_INFO(dev)->gen <= 3) {
11956 intel_primary_formats = intel_primary_formats_gen2;
11957 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11958 } else {
11959 intel_primary_formats = intel_primary_formats_gen4;
11960 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11961 }
11962
11963 drm_universal_plane_init(dev, &primary->base, 0,
11964 &intel_primary_plane_funcs,
11965 intel_primary_formats, num_formats,
11966 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053011967
11968 if (INTEL_INFO(dev)->gen >= 4) {
11969 if (!dev->mode_config.rotation_property)
11970 dev->mode_config.rotation_property =
11971 drm_mode_create_rotation_property(dev,
11972 BIT(DRM_ROTATE_0) |
11973 BIT(DRM_ROTATE_180));
11974 if (dev->mode_config.rotation_property)
11975 drm_object_attach_property(&primary->base.base,
11976 dev->mode_config.rotation_property,
11977 primary->rotation);
11978 }
11979
Matt Roper465c1202014-05-29 08:06:54 -070011980 return &primary->base;
11981}
11982
Matt Roper3d7d6512014-06-10 08:28:13 -070011983static int
11984intel_cursor_plane_disable(struct drm_plane *plane)
11985{
11986 if (!plane->fb)
11987 return 0;
11988
11989 BUG_ON(!plane->crtc);
11990
11991 return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
11992}
11993
11994static int
11995intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
11996 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11997 unsigned int crtc_w, unsigned int crtc_h,
11998 uint32_t src_x, uint32_t src_y,
11999 uint32_t src_w, uint32_t src_h)
12000{
12001 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12002 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
12003 struct drm_i915_gem_object *obj = intel_fb->obj;
12004 struct drm_rect dest = {
12005 /* integer pixels */
12006 .x1 = crtc_x,
12007 .y1 = crtc_y,
12008 .x2 = crtc_x + crtc_w,
12009 .y2 = crtc_y + crtc_h,
12010 };
12011 struct drm_rect src = {
12012 /* 16.16 fixed point */
12013 .x1 = src_x,
12014 .y1 = src_y,
12015 .x2 = src_x + src_w,
12016 .y2 = src_y + src_h,
12017 };
12018 const struct drm_rect clip = {
12019 /* integer pixels */
Ville Syrjälä1add1432014-08-12 19:39:52 +030012020 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
12021 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
Matt Roper3d7d6512014-06-10 08:28:13 -070012022 };
12023 bool visible;
12024 int ret;
12025
12026 ret = drm_plane_helper_check_update(plane, crtc, fb,
12027 &src, &dest, &clip,
12028 DRM_PLANE_HELPER_NO_SCALING,
12029 DRM_PLANE_HELPER_NO_SCALING,
12030 true, true, &visible);
12031 if (ret)
12032 return ret;
12033
12034 crtc->cursor_x = crtc_x;
12035 crtc->cursor_y = crtc_y;
12036 if (fb != crtc->cursor->fb) {
12037 return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
12038 } else {
12039 intel_crtc_update_cursor(crtc, visible);
Daniel Vetter4ed91092014-08-08 20:27:01 +020012040
12041 intel_frontbuffer_flip(crtc->dev,
12042 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
12043
Matt Roper3d7d6512014-06-10 08:28:13 -070012044 return 0;
12045 }
12046}
12047static const struct drm_plane_funcs intel_cursor_plane_funcs = {
12048 .update_plane = intel_cursor_plane_update,
12049 .disable_plane = intel_cursor_plane_disable,
12050 .destroy = intel_plane_destroy,
12051};
12052
12053static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12054 int pipe)
12055{
12056 struct intel_plane *cursor;
12057
12058 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12059 if (cursor == NULL)
12060 return NULL;
12061
12062 cursor->can_scale = false;
12063 cursor->max_downscale = 1;
12064 cursor->pipe = pipe;
12065 cursor->plane = pipe;
12066
12067 drm_universal_plane_init(dev, &cursor->base, 0,
12068 &intel_cursor_plane_funcs,
12069 intel_cursor_formats,
12070 ARRAY_SIZE(intel_cursor_formats),
12071 DRM_PLANE_TYPE_CURSOR);
12072 return &cursor->base;
12073}
12074
Hannes Ederb358d0a2008-12-18 21:18:47 +010012075static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080012076{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012077 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080012078 struct intel_crtc *intel_crtc;
Matt Roper3d7d6512014-06-10 08:28:13 -070012079 struct drm_plane *primary = NULL;
12080 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070012081 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080012082
Daniel Vetter955382f2013-09-19 14:05:45 +020012083 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080012084 if (intel_crtc == NULL)
12085 return;
12086
Matt Roper465c1202014-05-29 08:06:54 -070012087 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070012088 if (!primary)
12089 goto fail;
12090
12091 cursor = intel_cursor_plane_create(dev, pipe);
12092 if (!cursor)
12093 goto fail;
12094
Matt Roper465c1202014-05-29 08:06:54 -070012095 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070012096 cursor, &intel_crtc_funcs);
12097 if (ret)
12098 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080012099
12100 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080012101 for (i = 0; i < 256; i++) {
12102 intel_crtc->lut_r[i] = i;
12103 intel_crtc->lut_g[i] = i;
12104 intel_crtc->lut_b[i] = i;
12105 }
12106
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020012107 /*
12108 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020012109 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020012110 */
Jesse Barnes80824002009-09-10 15:28:06 -070012111 intel_crtc->pipe = pipe;
12112 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010012113 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080012114 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010012115 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070012116 }
12117
Chris Wilson4b0e3332014-05-30 16:35:26 +030012118 intel_crtc->cursor_base = ~0;
12119 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030012120 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030012121
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080012122 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
12123 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
12124 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
12125 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
12126
Jesse Barnes79e53942008-11-07 14:24:08 -080012127 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020012128
12129 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070012130 return;
12131
12132fail:
12133 if (primary)
12134 drm_plane_cleanup(primary);
12135 if (cursor)
12136 drm_plane_cleanup(cursor);
12137 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080012138}
12139
Jesse Barnes752aa882013-10-31 18:55:49 +020012140enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12141{
12142 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012143 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020012144
Rob Clark51fd3712013-11-19 12:10:12 -050012145 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020012146
12147 if (!encoder)
12148 return INVALID_PIPE;
12149
12150 return to_intel_crtc(encoder->crtc)->pipe;
12151}
12152
Carl Worth08d7b3d2009-04-29 14:43:54 -070012153int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000012154 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070012155{
Carl Worth08d7b3d2009-04-29 14:43:54 -070012156 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040012157 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020012158 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012159
Daniel Vetter1cff8f62012-04-24 09:55:08 +020012160 if (!drm_core_check_feature(dev, DRIVER_MODESET))
12161 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012162
Rob Clark7707e652014-07-17 23:30:04 -040012163 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070012164
Rob Clark7707e652014-07-17 23:30:04 -040012165 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070012166 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030012167 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012168 }
12169
Rob Clark7707e652014-07-17 23:30:04 -040012170 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020012171 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012172
Daniel Vetterc05422d2009-08-11 16:05:30 +020012173 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012174}
12175
Daniel Vetter66a92782012-07-12 20:08:18 +020012176static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080012177{
Daniel Vetter66a92782012-07-12 20:08:18 +020012178 struct drm_device *dev = encoder->base.dev;
12179 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080012180 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080012181 int entry = 0;
12182
Damien Lespiaub2784e12014-08-05 11:29:37 +010012183 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012184 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020012185 index_mask |= (1 << entry);
12186
Jesse Barnes79e53942008-11-07 14:24:08 -080012187 entry++;
12188 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010012189
Jesse Barnes79e53942008-11-07 14:24:08 -080012190 return index_mask;
12191}
12192
Chris Wilson4d302442010-12-14 19:21:29 +000012193static bool has_edp_a(struct drm_device *dev)
12194{
12195 struct drm_i915_private *dev_priv = dev->dev_private;
12196
12197 if (!IS_MOBILE(dev))
12198 return false;
12199
12200 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12201 return false;
12202
Damien Lespiaue3589902014-02-07 19:12:50 +000012203 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000012204 return false;
12205
12206 return true;
12207}
12208
Damien Lespiauba0fbca2014-01-08 14:18:23 +000012209const char *intel_output_name(int output)
12210{
12211 static const char *names[] = {
12212 [INTEL_OUTPUT_UNUSED] = "Unused",
12213 [INTEL_OUTPUT_ANALOG] = "Analog",
12214 [INTEL_OUTPUT_DVO] = "DVO",
12215 [INTEL_OUTPUT_SDVO] = "SDVO",
12216 [INTEL_OUTPUT_LVDS] = "LVDS",
12217 [INTEL_OUTPUT_TVOUT] = "TV",
12218 [INTEL_OUTPUT_HDMI] = "HDMI",
12219 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
12220 [INTEL_OUTPUT_EDP] = "eDP",
12221 [INTEL_OUTPUT_DSI] = "DSI",
12222 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
12223 };
12224
12225 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
12226 return "Invalid";
12227
12228 return names[output];
12229}
12230
Jesse Barnes84b4e042014-06-25 08:24:29 -070012231static bool intel_crt_present(struct drm_device *dev)
12232{
12233 struct drm_i915_private *dev_priv = dev->dev_private;
12234
12235 if (IS_ULT(dev))
12236 return false;
12237
12238 if (IS_CHERRYVIEW(dev))
12239 return false;
12240
12241 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12242 return false;
12243
12244 return true;
12245}
12246
Jesse Barnes79e53942008-11-07 14:24:08 -080012247static void intel_setup_outputs(struct drm_device *dev)
12248{
Eric Anholt725e30a2009-01-22 13:01:02 -080012249 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010012250 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012251 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080012252
Daniel Vetterc9093352013-06-06 22:22:47 +020012253 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012254
Jesse Barnes84b4e042014-06-25 08:24:29 -070012255 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020012256 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012257
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012258 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030012259 int found;
12260
12261 /* Haswell uses DDI functions to detect digital outputs */
12262 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12263 /* DDI A only supports eDP */
12264 if (found)
12265 intel_ddi_init(dev, PORT_A);
12266
12267 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12268 * register */
12269 found = I915_READ(SFUSE_STRAP);
12270
12271 if (found & SFUSE_STRAP_DDIB_DETECTED)
12272 intel_ddi_init(dev, PORT_B);
12273 if (found & SFUSE_STRAP_DDIC_DETECTED)
12274 intel_ddi_init(dev, PORT_C);
12275 if (found & SFUSE_STRAP_DDID_DETECTED)
12276 intel_ddi_init(dev, PORT_D);
12277 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012278 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020012279 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020012280
12281 if (has_edp_a(dev))
12282 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012283
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012284 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080012285 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010012286 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012287 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012288 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012289 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012290 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012291 }
12292
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012293 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012294 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012295
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012296 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012297 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012298
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012299 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012300 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012301
Daniel Vetter270b3042012-10-27 15:52:05 +020012302 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012303 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070012304 } else if (IS_VALLEYVIEW(dev)) {
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030012305 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
12306 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12307 PORT_B);
12308 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
12309 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
12310 }
12311
Jesse Barnes6f6005a2013-08-09 09:34:35 -070012312 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
12313 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12314 PORT_C);
12315 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020012316 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Jesse Barnes6f6005a2013-08-09 09:34:35 -070012317 }
Gajanan Bhat19c03922012-09-27 19:13:07 +053012318
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012319 if (IS_CHERRYVIEW(dev)) {
12320 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
12321 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12322 PORT_D);
12323 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12324 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
12325 }
12326 }
12327
Jani Nikula3cfca972013-08-27 15:12:26 +030012328 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080012329 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080012330 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080012331
Paulo Zanonie2debe92013-02-18 19:00:27 -030012332 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012333 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012334 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012335 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12336 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012337 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012338 }
Ma Ling27185ae2009-08-24 13:50:23 +080012339
Imre Deake7281ea2013-05-08 13:14:08 +030012340 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012341 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080012342 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040012343
12344 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040012345
Paulo Zanonie2debe92013-02-18 19:00:27 -030012346 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012347 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012348 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012349 }
Ma Ling27185ae2009-08-24 13:50:23 +080012350
Paulo Zanonie2debe92013-02-18 19:00:27 -030012351 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080012352
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012353 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12354 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012355 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012356 }
Imre Deake7281ea2013-05-08 13:14:08 +030012357 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012358 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080012359 }
Ma Ling27185ae2009-08-24 13:50:23 +080012360
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012361 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030012362 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012363 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070012364 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080012365 intel_dvo_init(dev);
12366
Zhenyu Wang103a1962009-11-27 11:44:36 +080012367 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080012368 intel_tv_init(dev);
12369
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070012370 intel_edp_psr_init(dev);
12371
Damien Lespiaub2784e12014-08-05 11:29:37 +010012372 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010012373 encoder->base.possible_crtcs = encoder->crtc_mask;
12374 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020012375 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080012376 }
Chris Wilson47356eb2011-01-11 17:06:04 +000012377
Paulo Zanonidde86e22012-12-01 12:04:25 -020012378 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020012379
12380 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012381}
12382
12383static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12384{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012385 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080012386 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080012387
Daniel Vetteref2d6332014-02-10 18:00:38 +010012388 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012389 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010012390 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012391 drm_gem_object_unreference(&intel_fb->obj->base);
12392 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012393 kfree(intel_fb);
12394}
12395
12396static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000012397 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080012398 unsigned int *handle)
12399{
12400 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000012401 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080012402
Chris Wilson05394f32010-11-08 19:18:58 +000012403 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080012404}
12405
12406static const struct drm_framebuffer_funcs intel_fb_funcs = {
12407 .destroy = intel_user_framebuffer_destroy,
12408 .create_handle = intel_user_framebuffer_create_handle,
12409};
12410
Daniel Vetterb5ea6422014-03-02 21:18:00 +010012411static int intel_framebuffer_init(struct drm_device *dev,
12412 struct intel_framebuffer *intel_fb,
12413 struct drm_mode_fb_cmd2 *mode_cmd,
12414 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080012415{
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080012416 int aligned_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010012417 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080012418 int ret;
12419
Daniel Vetterdd4916c2013-10-09 21:23:51 +020012420 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12421
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012422 if (obj->tiling_mode == I915_TILING_Y) {
12423 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010012424 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012425 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012426
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012427 if (mode_cmd->pitches[0] & 63) {
12428 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
12429 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010012430 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012431 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012432
Chris Wilsona35cdaa2013-06-25 17:26:45 +010012433 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
12434 pitch_limit = 32*1024;
12435 } else if (INTEL_INFO(dev)->gen >= 4) {
12436 if (obj->tiling_mode)
12437 pitch_limit = 16*1024;
12438 else
12439 pitch_limit = 32*1024;
12440 } else if (INTEL_INFO(dev)->gen >= 3) {
12441 if (obj->tiling_mode)
12442 pitch_limit = 8*1024;
12443 else
12444 pitch_limit = 16*1024;
12445 } else
12446 /* XXX DSPC is limited to 4k tiled */
12447 pitch_limit = 8*1024;
12448
12449 if (mode_cmd->pitches[0] > pitch_limit) {
12450 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
12451 obj->tiling_mode ? "tiled" : "linear",
12452 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012453 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012454 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012455
12456 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012457 mode_cmd->pitches[0] != obj->stride) {
12458 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12459 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012460 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012461 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012462
Ville Syrjälä57779d02012-10-31 17:50:14 +020012463 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012464 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020012465 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020012466 case DRM_FORMAT_RGB565:
12467 case DRM_FORMAT_XRGB8888:
12468 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020012469 break;
12470 case DRM_FORMAT_XRGB1555:
12471 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012472 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012473 DRM_DEBUG("unsupported pixel format: %s\n",
12474 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012475 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012476 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020012477 break;
12478 case DRM_FORMAT_XBGR8888:
12479 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020012480 case DRM_FORMAT_XRGB2101010:
12481 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020012482 case DRM_FORMAT_XBGR2101010:
12483 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012484 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012485 DRM_DEBUG("unsupported pixel format: %s\n",
12486 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012487 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012488 }
Jesse Barnesb5626742011-06-24 12:19:27 -070012489 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020012490 case DRM_FORMAT_YUYV:
12491 case DRM_FORMAT_UYVY:
12492 case DRM_FORMAT_YVYU:
12493 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012494 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012495 DRM_DEBUG("unsupported pixel format: %s\n",
12496 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012497 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012498 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012499 break;
12500 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012501 DRM_DEBUG("unsupported pixel format: %s\n",
12502 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010012503 return -EINVAL;
12504 }
12505
Ville Syrjälä90f9a332012-10-31 17:50:19 +020012506 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12507 if (mode_cmd->offsets[0] != 0)
12508 return -EINVAL;
12509
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080012510 aligned_height = intel_align_height(dev, mode_cmd->height,
12511 obj->tiling_mode);
Daniel Vetter53155c02013-10-09 21:55:33 +020012512 /* FIXME drm helper for size checks (especially planar formats)? */
12513 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12514 return -EINVAL;
12515
Daniel Vetterc7d73f62012-12-13 23:38:38 +010012516 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12517 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020012518 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010012519
Jesse Barnes79e53942008-11-07 14:24:08 -080012520 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12521 if (ret) {
12522 DRM_ERROR("framebuffer init failed %d\n", ret);
12523 return ret;
12524 }
12525
Jesse Barnes79e53942008-11-07 14:24:08 -080012526 return 0;
12527}
12528
Jesse Barnes79e53942008-11-07 14:24:08 -080012529static struct drm_framebuffer *
12530intel_user_framebuffer_create(struct drm_device *dev,
12531 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012532 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080012533{
Chris Wilson05394f32010-11-08 19:18:58 +000012534 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080012535
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012536 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12537 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000012538 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010012539 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080012540
Chris Wilsond2dff872011-04-19 08:36:26 +010012541 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080012542}
12543
Daniel Vetter4520f532013-10-09 09:18:51 +020012544#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020012545static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020012546{
12547}
12548#endif
12549
Jesse Barnes79e53942008-11-07 14:24:08 -080012550static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080012551 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020012552 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080012553};
12554
Jesse Barnese70236a2009-09-21 10:42:27 -070012555/* Set up chip specific display functions */
12556static void intel_init_display(struct drm_device *dev)
12557{
12558 struct drm_i915_private *dev_priv = dev->dev_private;
12559
Daniel Vetteree9300b2013-06-03 22:40:22 +020012560 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12561 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030012562 else if (IS_CHERRYVIEW(dev))
12563 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020012564 else if (IS_VALLEYVIEW(dev))
12565 dev_priv->display.find_dpll = vlv_find_best_dpll;
12566 else if (IS_PINEVIEW(dev))
12567 dev_priv->display.find_dpll = pnv_find_best_dpll;
12568 else
12569 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12570
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012571 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012572 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080012573 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030012574 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020012575 dev_priv->display.crtc_enable = haswell_crtc_enable;
12576 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030012577 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012578 dev_priv->display.update_primary_plane =
12579 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030012580 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012581 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080012582 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070012583 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020012584 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12585 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012586 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012587 dev_priv->display.update_primary_plane =
12588 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070012589 } else if (IS_VALLEYVIEW(dev)) {
12590 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080012591 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Jesse Barnes89b667f2013-04-18 14:51:36 -070012592 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
12593 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12594 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12595 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012596 dev_priv->display.update_primary_plane =
12597 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070012598 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012599 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080012600 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070012601 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020012602 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12603 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012604 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012605 dev_priv->display.update_primary_plane =
12606 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070012607 }
Jesse Barnese70236a2009-09-21 10:42:27 -070012608
Jesse Barnese70236a2009-09-21 10:42:27 -070012609 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070012610 if (IS_VALLEYVIEW(dev))
12611 dev_priv->display.get_display_clock_speed =
12612 valleyview_get_display_clock_speed;
12613 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070012614 dev_priv->display.get_display_clock_speed =
12615 i945_get_display_clock_speed;
12616 else if (IS_I915G(dev))
12617 dev_priv->display.get_display_clock_speed =
12618 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020012619 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070012620 dev_priv->display.get_display_clock_speed =
12621 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020012622 else if (IS_PINEVIEW(dev))
12623 dev_priv->display.get_display_clock_speed =
12624 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070012625 else if (IS_I915GM(dev))
12626 dev_priv->display.get_display_clock_speed =
12627 i915gm_get_display_clock_speed;
12628 else if (IS_I865G(dev))
12629 dev_priv->display.get_display_clock_speed =
12630 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020012631 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070012632 dev_priv->display.get_display_clock_speed =
12633 i855_get_display_clock_speed;
12634 else /* 852, 830 */
12635 dev_priv->display.get_display_clock_speed =
12636 i830_get_display_clock_speed;
12637
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012638 if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080012639 dev_priv->display.write_eld = g4x_write_eld;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012640 } else if (IS_GEN5(dev)) {
12641 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
12642 dev_priv->display.write_eld = ironlake_write_eld;
12643 } else if (IS_GEN6(dev)) {
12644 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
12645 dev_priv->display.write_eld = ironlake_write_eld;
12646 dev_priv->display.modeset_global_resources =
12647 snb_modeset_global_resources;
12648 } else if (IS_IVYBRIDGE(dev)) {
12649 /* FIXME: detect B0+ stepping and use auto training */
12650 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
12651 dev_priv->display.write_eld = ironlake_write_eld;
12652 dev_priv->display.modeset_global_resources =
12653 ivb_modeset_global_resources;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030012654 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012655 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
12656 dev_priv->display.write_eld = haswell_write_eld;
12657 dev_priv->display.modeset_global_resources =
12658 haswell_modeset_global_resources;
Jesse Barnes30a970c2013-11-04 13:48:12 -080012659 } else if (IS_VALLEYVIEW(dev)) {
12660 dev_priv->display.modeset_global_resources =
12661 valleyview_modeset_global_resources;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -040012662 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070012663 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012664
12665 /* Default just returns -ENODEV to indicate unsupported */
12666 dev_priv->display.queue_flip = intel_default_queue_flip;
12667
12668 switch (INTEL_INFO(dev)->gen) {
12669 case 2:
12670 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12671 break;
12672
12673 case 3:
12674 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12675 break;
12676
12677 case 4:
12678 case 5:
12679 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12680 break;
12681
12682 case 6:
12683 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12684 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070012685 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070012686 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070012687 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12688 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012689 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020012690
12691 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030012692
12693 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070012694}
12695
Jesse Barnesb690e962010-07-19 13:53:12 -070012696/*
12697 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12698 * resume, or other times. This quirk makes sure that's the case for
12699 * affected systems.
12700 */
Akshay Joshi0206e352011-08-16 15:34:10 -040012701static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070012702{
12703 struct drm_i915_private *dev_priv = dev->dev_private;
12704
12705 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012706 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070012707}
12708
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012709static void quirk_pipeb_force(struct drm_device *dev)
12710{
12711 struct drm_i915_private *dev_priv = dev->dev_private;
12712
12713 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
12714 DRM_INFO("applying pipe b force quirk\n");
12715}
12716
Keith Packard435793d2011-07-12 14:56:22 -070012717/*
12718 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
12719 */
12720static void quirk_ssc_force_disable(struct drm_device *dev)
12721{
12722 struct drm_i915_private *dev_priv = dev->dev_private;
12723 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012724 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070012725}
12726
Carsten Emde4dca20e2012-03-15 15:56:26 +010012727/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010012728 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
12729 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010012730 */
12731static void quirk_invert_brightness(struct drm_device *dev)
12732{
12733 struct drm_i915_private *dev_priv = dev->dev_private;
12734 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012735 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070012736}
12737
Scot Doyle9c72cc62014-07-03 23:27:50 +000012738/* Some VBT's incorrectly indicate no backlight is present */
12739static void quirk_backlight_present(struct drm_device *dev)
12740{
12741 struct drm_i915_private *dev_priv = dev->dev_private;
12742 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
12743 DRM_INFO("applying backlight present quirk\n");
12744}
12745
Jesse Barnesb690e962010-07-19 13:53:12 -070012746struct intel_quirk {
12747 int device;
12748 int subsystem_vendor;
12749 int subsystem_device;
12750 void (*hook)(struct drm_device *dev);
12751};
12752
Egbert Eich5f85f172012-10-14 15:46:38 +020012753/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
12754struct intel_dmi_quirk {
12755 void (*hook)(struct drm_device *dev);
12756 const struct dmi_system_id (*dmi_id_list)[];
12757};
12758
12759static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
12760{
12761 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
12762 return 1;
12763}
12764
12765static const struct intel_dmi_quirk intel_dmi_quirks[] = {
12766 {
12767 .dmi_id_list = &(const struct dmi_system_id[]) {
12768 {
12769 .callback = intel_dmi_reverse_brightness,
12770 .ident = "NCR Corporation",
12771 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
12772 DMI_MATCH(DMI_PRODUCT_NAME, ""),
12773 },
12774 },
12775 { } /* terminating entry */
12776 },
12777 .hook = quirk_invert_brightness,
12778 },
12779};
12780
Ben Widawskyc43b5632012-04-16 14:07:40 -070012781static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070012782 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040012783 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070012784
Jesse Barnesb690e962010-07-19 13:53:12 -070012785 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
12786 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
12787
Jesse Barnesb690e962010-07-19 13:53:12 -070012788 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
12789 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
12790
Ville Syrjälä5f080c02014-08-15 01:22:06 +030012791 /* 830 needs to leave pipe A & dpll A up */
12792 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
12793
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012794 /* 830 needs to leave pipe B & dpll B up */
12795 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
12796
Keith Packard435793d2011-07-12 14:56:22 -070012797 /* Lenovo U160 cannot use SSC on LVDS */
12798 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020012799
12800 /* Sony Vaio Y cannot use SSC on LVDS */
12801 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010012802
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010012803 /* Acer Aspire 5734Z must invert backlight brightness */
12804 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
12805
12806 /* Acer/eMachines G725 */
12807 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
12808
12809 /* Acer/eMachines e725 */
12810 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
12811
12812 /* Acer/Packard Bell NCL20 */
12813 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
12814
12815 /* Acer Aspire 4736Z */
12816 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020012817
12818 /* Acer Aspire 5336 */
12819 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000012820
12821 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
12822 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000012823
Scot Doyledfb3d47b2014-08-21 16:08:02 +000012824 /* Acer C720 Chromebook (Core i3 4005U) */
12825 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
12826
Scot Doyled4967d82014-07-03 23:27:52 +000012827 /* Toshiba CB35 Chromebook (Celeron 2955U) */
12828 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000012829
12830 /* HP Chromebook 14 (Celeron 2955U) */
12831 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070012832};
12833
12834static void intel_init_quirks(struct drm_device *dev)
12835{
12836 struct pci_dev *d = dev->pdev;
12837 int i;
12838
12839 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
12840 struct intel_quirk *q = &intel_quirks[i];
12841
12842 if (d->device == q->device &&
12843 (d->subsystem_vendor == q->subsystem_vendor ||
12844 q->subsystem_vendor == PCI_ANY_ID) &&
12845 (d->subsystem_device == q->subsystem_device ||
12846 q->subsystem_device == PCI_ANY_ID))
12847 q->hook(dev);
12848 }
Egbert Eich5f85f172012-10-14 15:46:38 +020012849 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
12850 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
12851 intel_dmi_quirks[i].hook(dev);
12852 }
Jesse Barnesb690e962010-07-19 13:53:12 -070012853}
12854
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012855/* Disable the VGA plane that we never use */
12856static void i915_disable_vga(struct drm_device *dev)
12857{
12858 struct drm_i915_private *dev_priv = dev->dev_private;
12859 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020012860 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012861
Ville Syrjälä2b37c612014-01-22 21:32:38 +020012862 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012863 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070012864 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012865 sr1 = inb(VGA_SR_DATA);
12866 outb(sr1 | 1<<5, VGA_SR_DATA);
12867 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
12868 udelay(300);
12869
Ville Syrjälä69769f92014-08-15 01:22:08 +030012870 /*
12871 * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
12872 * from S3 without preserving (some of?) the other bits.
12873 */
12874 I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012875 POSTING_READ(vga_reg);
12876}
12877
Daniel Vetterf8175862012-04-10 15:50:11 +020012878void intel_modeset_init_hw(struct drm_device *dev)
12879{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030012880 intel_prepare_ddi(dev);
12881
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030012882 if (IS_VALLEYVIEW(dev))
12883 vlv_update_cdclk(dev);
12884
Daniel Vetterf8175862012-04-10 15:50:11 +020012885 intel_init_clock_gating(dev);
12886
Daniel Vetter8090c6b2012-06-24 16:42:32 +020012887 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020012888}
12889
Imre Deak7d708ee2013-04-17 14:04:50 +030012890void intel_modeset_suspend_hw(struct drm_device *dev)
12891{
12892 intel_suspend_hw(dev);
12893}
12894
Jesse Barnes79e53942008-11-07 14:24:08 -080012895void intel_modeset_init(struct drm_device *dev)
12896{
Jesse Barnes652c3932009-08-17 13:31:43 -070012897 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000012898 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000012899 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080012900 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080012901
12902 drm_mode_config_init(dev);
12903
12904 dev->mode_config.min_width = 0;
12905 dev->mode_config.min_height = 0;
12906
Dave Airlie019d96c2011-09-29 16:20:42 +010012907 dev->mode_config.preferred_depth = 24;
12908 dev->mode_config.prefer_shadow = 1;
12909
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020012910 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080012911
Jesse Barnesb690e962010-07-19 13:53:12 -070012912 intel_init_quirks(dev);
12913
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030012914 intel_init_pm(dev);
12915
Ben Widawskye3c74752013-04-05 13:12:39 -070012916 if (INTEL_INFO(dev)->num_pipes == 0)
12917 return;
12918
Jesse Barnese70236a2009-09-21 10:42:27 -070012919 intel_init_display(dev);
12920
Chris Wilsona6c45cf2010-09-17 00:32:17 +010012921 if (IS_GEN2(dev)) {
12922 dev->mode_config.max_width = 2048;
12923 dev->mode_config.max_height = 2048;
12924 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070012925 dev->mode_config.max_width = 4096;
12926 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080012927 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010012928 dev->mode_config.max_width = 8192;
12929 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080012930 }
Damien Lespiau068be562014-03-28 14:17:49 +000012931
Ville Syrjälädc41c152014-08-13 11:57:05 +030012932 if (IS_845G(dev) || IS_I865G(dev)) {
12933 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
12934 dev->mode_config.cursor_height = 1023;
12935 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000012936 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
12937 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
12938 } else {
12939 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
12940 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
12941 }
12942
Ben Widawsky5d4545a2013-01-17 12:45:15 -080012943 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080012944
Zhao Yakui28c97732009-10-09 11:39:41 +080012945 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012946 INTEL_INFO(dev)->num_pipes,
12947 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080012948
Damien Lespiau055e3932014-08-18 13:49:10 +010012949 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000012950 intel_crtc_init(dev, pipe);
Damien Lespiau1fe47782014-03-03 17:31:47 +000012951 for_each_sprite(pipe, sprite) {
12952 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070012953 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030012954 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000012955 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070012956 }
Jesse Barnes79e53942008-11-07 14:24:08 -080012957 }
12958
Jesse Barnesf42bb702013-12-16 16:34:23 -080012959 intel_init_dpio(dev);
12960
Daniel Vettere72f9fb2013-06-05 13:34:06 +020012961 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012962
Ville Syrjälä69769f92014-08-15 01:22:08 +030012963 /* save the BIOS value before clobbering it */
12964 dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012965 /* Just disable it once at startup */
12966 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012967 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000012968
12969 /* Just in case the BIOS is doing something questionable. */
12970 intel_disable_fbc(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080012971
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012972 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080012973 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012974 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080012975
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012976 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080012977 if (!crtc->active)
12978 continue;
12979
Jesse Barnes46f297f2014-03-07 08:57:48 -080012980 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080012981 * Note that reserving the BIOS fb up front prevents us
12982 * from stuffing other stolen allocations like the ring
12983 * on top. This prevents some ugliness at boot time, and
12984 * can even allow for smooth boot transitions if the BIOS
12985 * fb is large enough for the active pipe configuration.
12986 */
12987 if (dev_priv->display.get_plane_config) {
12988 dev_priv->display.get_plane_config(crtc,
12989 &crtc->plane_config);
12990 /*
12991 * If the fb is shared between multiple heads, we'll
12992 * just get the first one.
12993 */
Jesse Barnes484b41d2014-03-07 08:57:55 -080012994 intel_find_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080012995 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080012996 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010012997}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080012998
Daniel Vetter7fad7982012-07-04 17:51:47 +020012999static void intel_enable_pipe_a(struct drm_device *dev)
13000{
13001 struct intel_connector *connector;
13002 struct drm_connector *crt = NULL;
13003 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030013004 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020013005
13006 /* We can't just switch on the pipe A, we need to set things up with a
13007 * proper mode and output configuration. As a gross hack, enable pipe A
13008 * by enabling the load detect pipe once. */
13009 list_for_each_entry(connector,
13010 &dev->mode_config.connector_list,
13011 base.head) {
13012 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
13013 crt = &connector->base;
13014 break;
13015 }
13016 }
13017
13018 if (!crt)
13019 return;
13020
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030013021 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
13022 intel_release_load_detect_pipe(crt, &load_detect_temp);
Daniel Vetter7fad7982012-07-04 17:51:47 +020013023}
13024
Daniel Vetterfa555832012-10-10 23:14:00 +020013025static bool
13026intel_check_plane_mapping(struct intel_crtc *crtc)
13027{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013028 struct drm_device *dev = crtc->base.dev;
13029 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020013030 u32 reg, val;
13031
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013032 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020013033 return true;
13034
13035 reg = DSPCNTR(!crtc->plane);
13036 val = I915_READ(reg);
13037
13038 if ((val & DISPLAY_PLANE_ENABLE) &&
13039 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
13040 return false;
13041
13042 return true;
13043}
13044
Daniel Vetter24929352012-07-02 20:28:59 +020013045static void intel_sanitize_crtc(struct intel_crtc *crtc)
13046{
13047 struct drm_device *dev = crtc->base.dev;
13048 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020013049 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020013050
Daniel Vetter24929352012-07-02 20:28:59 +020013051 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020013052 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020013053 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
13054
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030013055 /* restore vblank interrupts to correct state */
Ville Syrjäläd297e102014-08-06 14:50:01 +030013056 if (crtc->active) {
13057 update_scanline_offset(crtc);
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030013058 drm_vblank_on(dev, crtc->pipe);
Ville Syrjäläd297e102014-08-06 14:50:01 +030013059 } else
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030013060 drm_vblank_off(dev, crtc->pipe);
13061
Daniel Vetter24929352012-07-02 20:28:59 +020013062 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020013063 * disable the crtc (and hence change the state) if it is wrong. Note
13064 * that gen4+ has a fixed plane -> pipe mapping. */
13065 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020013066 struct intel_connector *connector;
13067 bool plane;
13068
Daniel Vetter24929352012-07-02 20:28:59 +020013069 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
13070 crtc->base.base.id);
13071
13072 /* Pipe has the wrong plane attached and the plane is active.
13073 * Temporarily change the plane mapping and disable everything
13074 * ... */
13075 plane = crtc->plane;
13076 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020013077 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020013078 dev_priv->display.crtc_disable(&crtc->base);
13079 crtc->plane = plane;
13080
13081 /* ... and break all links. */
13082 list_for_each_entry(connector, &dev->mode_config.connector_list,
13083 base.head) {
13084 if (connector->encoder->base.crtc != &crtc->base)
13085 continue;
13086
Egbert Eich7f1950f2014-04-25 10:56:22 +020013087 connector->base.dpms = DRM_MODE_DPMS_OFF;
13088 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020013089 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020013090 /* multiple connectors may have the same encoder:
13091 * handle them and break crtc link separately */
13092 list_for_each_entry(connector, &dev->mode_config.connector_list,
13093 base.head)
13094 if (connector->encoder->base.crtc == &crtc->base) {
13095 connector->encoder->base.crtc = NULL;
13096 connector->encoder->connectors_active = false;
13097 }
Daniel Vetter24929352012-07-02 20:28:59 +020013098
13099 WARN_ON(crtc->active);
13100 crtc->base.enabled = false;
13101 }
Daniel Vetter24929352012-07-02 20:28:59 +020013102
Daniel Vetter7fad7982012-07-04 17:51:47 +020013103 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
13104 crtc->pipe == PIPE_A && !crtc->active) {
13105 /* BIOS forgot to enable pipe A, this mostly happens after
13106 * resume. Force-enable the pipe to fix this, the update_dpms
13107 * call below we restore the pipe to the right state, but leave
13108 * the required bits on. */
13109 intel_enable_pipe_a(dev);
13110 }
13111
Daniel Vetter24929352012-07-02 20:28:59 +020013112 /* Adjust the state of the output pipe according to whether we
13113 * have active connectors/encoders. */
13114 intel_crtc_update_dpms(&crtc->base);
13115
13116 if (crtc->active != crtc->base.enabled) {
13117 struct intel_encoder *encoder;
13118
13119 /* This can happen either due to bugs in the get_hw_state
13120 * functions or because the pipe is force-enabled due to the
13121 * pipe A quirk. */
13122 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
13123 crtc->base.base.id,
13124 crtc->base.enabled ? "enabled" : "disabled",
13125 crtc->active ? "enabled" : "disabled");
13126
13127 crtc->base.enabled = crtc->active;
13128
13129 /* Because we only establish the connector -> encoder ->
13130 * crtc links if something is active, this means the
13131 * crtc is now deactivated. Break the links. connector
13132 * -> encoder links are only establish when things are
13133 * actually up, hence no need to break them. */
13134 WARN_ON(crtc->active);
13135
13136 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13137 WARN_ON(encoder->connectors_active);
13138 encoder->base.crtc = NULL;
13139 }
13140 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020013141
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030013142 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010013143 /*
13144 * We start out with underrun reporting disabled to avoid races.
13145 * For correct bookkeeping mark this on active crtcs.
13146 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020013147 * Also on gmch platforms we dont have any hardware bits to
13148 * disable the underrun reporting. Which means we need to start
13149 * out with underrun reporting disabled also on inactive pipes,
13150 * since otherwise we'll complain about the garbage we read when
13151 * e.g. coming up after runtime pm.
13152 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010013153 * No protection against concurrent access is required - at
13154 * worst a fifo underrun happens which also sets this to false.
13155 */
13156 crtc->cpu_fifo_underrun_disabled = true;
13157 crtc->pch_fifo_underrun_disabled = true;
13158 }
Daniel Vetter24929352012-07-02 20:28:59 +020013159}
13160
13161static void intel_sanitize_encoder(struct intel_encoder *encoder)
13162{
13163 struct intel_connector *connector;
13164 struct drm_device *dev = encoder->base.dev;
13165
13166 /* We need to check both for a crtc link (meaning that the
13167 * encoder is active and trying to read from a pipe) and the
13168 * pipe itself being active. */
13169 bool has_active_crtc = encoder->base.crtc &&
13170 to_intel_crtc(encoder->base.crtc)->active;
13171
13172 if (encoder->connectors_active && !has_active_crtc) {
13173 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13174 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013175 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020013176
13177 /* Connector is active, but has no active pipe. This is
13178 * fallout from our resume register restoring. Disable
13179 * the encoder manually again. */
13180 if (encoder->base.crtc) {
13181 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13182 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013183 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020013184 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030013185 if (encoder->post_disable)
13186 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020013187 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020013188 encoder->base.crtc = NULL;
13189 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020013190
13191 /* Inconsistent output/port/pipe state happens presumably due to
13192 * a bug in one of the get_hw_state functions. Or someplace else
13193 * in our code, like the register restore mess on resume. Clamp
13194 * things to off as a safer default. */
13195 list_for_each_entry(connector,
13196 &dev->mode_config.connector_list,
13197 base.head) {
13198 if (connector->encoder != encoder)
13199 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020013200 connector->base.dpms = DRM_MODE_DPMS_OFF;
13201 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020013202 }
13203 }
13204 /* Enabled encoders without active connectors will be fixed in
13205 * the crtc fixup. */
13206}
13207
Imre Deak04098752014-02-18 00:02:16 +020013208void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013209{
13210 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020013211 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013212
Imre Deak04098752014-02-18 00:02:16 +020013213 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13214 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13215 i915_disable_vga(dev);
13216 }
13217}
13218
13219void i915_redisable_vga(struct drm_device *dev)
13220{
13221 struct drm_i915_private *dev_priv = dev->dev_private;
13222
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030013223 /* This function can be called both from intel_modeset_setup_hw_state or
13224 * at a very early point in our resume sequence, where the power well
13225 * structures are not yet restored. Since this function is at a very
13226 * paranoid "someone might have enabled VGA while we were not looking"
13227 * level, just check if the power well is enabled instead of trying to
13228 * follow the "don't touch the power well if we don't need it" policy
13229 * the rest of the driver uses. */
Imre Deak04098752014-02-18 00:02:16 +020013230 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030013231 return;
13232
Imre Deak04098752014-02-18 00:02:16 +020013233 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013234}
13235
Ville Syrjälä98ec7732014-04-30 17:43:01 +030013236static bool primary_get_hw_state(struct intel_crtc *crtc)
13237{
13238 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13239
13240 if (!crtc->active)
13241 return false;
13242
13243 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13244}
13245
Daniel Vetter30e984d2013-06-05 13:34:17 +020013246static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020013247{
13248 struct drm_i915_private *dev_priv = dev->dev_private;
13249 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020013250 struct intel_crtc *crtc;
13251 struct intel_encoder *encoder;
13252 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020013253 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020013254
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013255 for_each_intel_crtc(dev, crtc) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010013256 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020013257
Daniel Vetter99535992014-04-13 12:00:33 +020013258 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
13259
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013260 crtc->active = dev_priv->display.get_pipe_config(crtc,
13261 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020013262
13263 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030013264 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020013265
13266 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13267 crtc->base.base.id,
13268 crtc->active ? "enabled" : "disabled");
13269 }
13270
Daniel Vetter53589012013-06-05 13:34:16 +020013271 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13272 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13273
13274 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
13275 pll->active = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013276 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020013277 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13278 pll->active++;
13279 }
13280 pll->refcount = pll->active;
13281
Daniel Vetter35c95372013-07-17 06:55:04 +020013282 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
13283 pll->name, pll->refcount, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013284
13285 if (pll->refcount)
13286 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020013287 }
13288
Damien Lespiaub2784e12014-08-05 11:29:37 +010013289 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020013290 pipe = 0;
13291
13292 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070013293 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13294 encoder->base.crtc = &crtc->base;
Daniel Vetter1d37b682013-11-18 09:00:59 +010013295 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020013296 } else {
13297 encoder->base.crtc = NULL;
13298 }
13299
13300 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010013301 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020013302 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013303 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020013304 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010013305 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020013306 }
13307
13308 list_for_each_entry(connector, &dev->mode_config.connector_list,
13309 base.head) {
13310 if (connector->get_hw_state(connector)) {
13311 connector->base.dpms = DRM_MODE_DPMS_ON;
13312 connector->encoder->connectors_active = true;
13313 connector->base.encoder = &connector->encoder->base;
13314 } else {
13315 connector->base.dpms = DRM_MODE_DPMS_OFF;
13316 connector->base.encoder = NULL;
13317 }
13318 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13319 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013320 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020013321 connector->base.encoder ? "enabled" : "disabled");
13322 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020013323}
13324
13325/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13326 * and i915 state tracking structures. */
13327void intel_modeset_setup_hw_state(struct drm_device *dev,
13328 bool force_restore)
13329{
13330 struct drm_i915_private *dev_priv = dev->dev_private;
13331 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020013332 struct intel_crtc *crtc;
13333 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020013334 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020013335
13336 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020013337
Jesse Barnesbabea612013-06-26 18:57:38 +030013338 /*
13339 * Now that we have the config, copy it to each CRTC struct
13340 * Note that this could go away if we move to using crtc_config
13341 * checking everywhere.
13342 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013343 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020013344 if (crtc->active && i915.fastboot) {
Daniel Vetterf6a83282014-02-11 15:28:57 -080013345 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030013346 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13347 crtc->base.base.id);
13348 drm_mode_debug_printmodeline(&crtc->base.mode);
13349 }
13350 }
13351
Daniel Vetter24929352012-07-02 20:28:59 +020013352 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010013353 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020013354 intel_sanitize_encoder(encoder);
13355 }
13356
Damien Lespiau055e3932014-08-18 13:49:10 +010013357 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020013358 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13359 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020013360 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020013361 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013362
Daniel Vetter35c95372013-07-17 06:55:04 +020013363 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13364 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13365
13366 if (!pll->on || pll->active)
13367 continue;
13368
13369 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13370
13371 pll->disable(dev_priv, pll);
13372 pll->on = false;
13373 }
13374
Ville Syrjälä96f90c52013-12-05 15:51:38 +020013375 if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030013376 ilk_wm_get_hw_state(dev);
13377
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010013378 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030013379 i915_redisable_vga(dev);
13380
Daniel Vetterf30da182013-04-11 20:22:50 +020013381 /*
13382 * We need to use raw interfaces for restoring state to avoid
13383 * checking (bogus) intermediate states.
13384 */
Damien Lespiau055e3932014-08-18 13:49:10 +010013385 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070013386 struct drm_crtc *crtc =
13387 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020013388
13389 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
Matt Roperf4510a22014-04-01 15:22:40 -070013390 crtc->primary->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010013391 }
13392 } else {
13393 intel_modeset_update_staged_output_state(dev);
13394 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013395
13396 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010013397}
13398
13399void intel_modeset_gem_init(struct drm_device *dev)
13400{
Jesse Barnes484b41d2014-03-07 08:57:55 -080013401 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070013402 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080013403
Imre Deakae484342014-03-31 15:10:44 +030013404 mutex_lock(&dev->struct_mutex);
13405 intel_init_gt_powersave(dev);
13406 mutex_unlock(&dev->struct_mutex);
13407
Chris Wilson1833b132012-05-09 11:56:28 +010013408 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020013409
13410 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080013411
13412 /*
13413 * Make sure any fbs we allocated at startup are properly
13414 * pinned & fenced. When we do the allocation it's too early
13415 * for this.
13416 */
13417 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010013418 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070013419 obj = intel_fb_obj(c->primary->fb);
13420 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080013421 continue;
13422
Matt Roper2ff8fde2014-07-08 07:50:07 -070013423 if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080013424 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13425 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100013426 drm_framebuffer_unreference(c->primary->fb);
13427 c->primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -080013428 }
13429 }
13430 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013431}
13432
Imre Deak4932e2c2014-02-11 17:12:48 +020013433void intel_connector_unregister(struct intel_connector *intel_connector)
13434{
13435 struct drm_connector *connector = &intel_connector->base;
13436
13437 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010013438 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020013439}
13440
Jesse Barnes79e53942008-11-07 14:24:08 -080013441void intel_modeset_cleanup(struct drm_device *dev)
13442{
Jesse Barnes652c3932009-08-17 13:31:43 -070013443 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030013444 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070013445
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013446 /*
13447 * Interrupts and polling as the first thing to avoid creating havoc.
13448 * Too much stuff here (turning of rps, connectors, ...) would
13449 * experience fancy races otherwise.
13450 */
13451 drm_irq_uninstall(dev);
Imre Deak1d0d3432014-08-18 14:42:44 +030013452 intel_hpd_cancel_work(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070013453 dev_priv->pm._irqs_disabled = true;
13454
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013455 /*
13456 * Due to the hpd irq storm handling the hotplug work can re-arm the
13457 * poll handlers. Hence disable polling after hpd handling is shut down.
13458 */
Keith Packardf87ea762010-10-03 19:36:26 -070013459 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013460
Jesse Barnes652c3932009-08-17 13:31:43 -070013461 mutex_lock(&dev->struct_mutex);
13462
Jesse Barnes723bfd72010-10-07 16:01:13 -070013463 intel_unregister_dsm_handler();
13464
Chris Wilson973d04f2011-07-08 12:22:37 +010013465 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070013466
Daniel Vetter8090c6b2012-06-24 16:42:32 +020013467 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000013468
Daniel Vetter930ebb42012-06-29 23:32:16 +020013469 ironlake_teardown_rc6(dev);
13470
Kristian Høgsberg69341a52009-11-11 12:19:17 -050013471 mutex_unlock(&dev->struct_mutex);
13472
Chris Wilson1630fe72011-07-08 12:22:42 +010013473 /* flush any delayed tasks or pending work */
13474 flush_scheduled_work();
13475
Jani Nikuladb31af1d2013-11-08 16:48:53 +020013476 /* destroy the backlight and sysfs files before encoders/connectors */
13477 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020013478 struct intel_connector *intel_connector;
13479
13480 intel_connector = to_intel_connector(connector);
13481 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020013482 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030013483
Jesse Barnes79e53942008-11-07 14:24:08 -080013484 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010013485
13486 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030013487
13488 mutex_lock(&dev->struct_mutex);
13489 intel_cleanup_gt_powersave(dev);
13490 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013491}
13492
Dave Airlie28d52042009-09-21 14:33:58 +100013493/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080013494 * Return which encoder is currently attached for connector.
13495 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010013496struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080013497{
Chris Wilsondf0e9242010-09-09 16:20:55 +010013498 return &intel_attached_encoder(connector)->base;
13499}
Jesse Barnes79e53942008-11-07 14:24:08 -080013500
Chris Wilsondf0e9242010-09-09 16:20:55 +010013501void intel_connector_attach_encoder(struct intel_connector *connector,
13502 struct intel_encoder *encoder)
13503{
13504 connector->encoder = encoder;
13505 drm_mode_connector_attach_encoder(&connector->base,
13506 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080013507}
Dave Airlie28d52042009-09-21 14:33:58 +100013508
13509/*
13510 * set vga decode state - true == enable VGA decode
13511 */
13512int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13513{
13514 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000013515 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100013516 u16 gmch_ctrl;
13517
Chris Wilson75fa0412014-02-07 18:37:02 -020013518 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13519 DRM_ERROR("failed to read control word\n");
13520 return -EIO;
13521 }
13522
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020013523 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13524 return 0;
13525
Dave Airlie28d52042009-09-21 14:33:58 +100013526 if (state)
13527 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13528 else
13529 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020013530
13531 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13532 DRM_ERROR("failed to write control word\n");
13533 return -EIO;
13534 }
13535
Dave Airlie28d52042009-09-21 14:33:58 +100013536 return 0;
13537}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013538
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013539struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013540
13541 u32 power_well_driver;
13542
Chris Wilson63b66e52013-08-08 15:12:06 +020013543 int num_transcoders;
13544
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013545 struct intel_cursor_error_state {
13546 u32 control;
13547 u32 position;
13548 u32 base;
13549 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010013550 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013551
13552 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020013553 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013554 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030013555 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010013556 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013557
13558 struct intel_plane_error_state {
13559 u32 control;
13560 u32 stride;
13561 u32 size;
13562 u32 pos;
13563 u32 addr;
13564 u32 surface;
13565 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010013566 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020013567
13568 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020013569 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020013570 enum transcoder cpu_transcoder;
13571
13572 u32 conf;
13573
13574 u32 htotal;
13575 u32 hblank;
13576 u32 hsync;
13577 u32 vtotal;
13578 u32 vblank;
13579 u32 vsync;
13580 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013581};
13582
13583struct intel_display_error_state *
13584intel_display_capture_error_state(struct drm_device *dev)
13585{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013586 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013587 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020013588 int transcoders[] = {
13589 TRANSCODER_A,
13590 TRANSCODER_B,
13591 TRANSCODER_C,
13592 TRANSCODER_EDP,
13593 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013594 int i;
13595
Chris Wilson63b66e52013-08-08 15:12:06 +020013596 if (INTEL_INFO(dev)->num_pipes == 0)
13597 return NULL;
13598
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013599 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013600 if (error == NULL)
13601 return NULL;
13602
Imre Deak190be112013-11-25 17:15:31 +020013603 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013604 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
13605
Damien Lespiau055e3932014-08-18 13:49:10 +010013606 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020013607 error->pipe[i].power_domain_on =
Imre Deakbfafe932014-06-05 20:31:47 +030013608 intel_display_power_enabled_unlocked(dev_priv,
13609 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020013610 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013611 continue;
13612
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030013613 error->cursor[i].control = I915_READ(CURCNTR(i));
13614 error->cursor[i].position = I915_READ(CURPOS(i));
13615 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013616
13617 error->plane[i].control = I915_READ(DSPCNTR(i));
13618 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013619 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030013620 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013621 error->plane[i].pos = I915_READ(DSPPOS(i));
13622 }
Paulo Zanonica291362013-03-06 20:03:14 -030013623 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13624 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013625 if (INTEL_INFO(dev)->gen >= 4) {
13626 error->plane[i].surface = I915_READ(DSPSURF(i));
13627 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
13628 }
13629
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013630 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030013631
Sonika Jindal3abfce72014-07-21 15:23:43 +053013632 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030013633 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020013634 }
13635
13636 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
13637 if (HAS_DDI(dev_priv->dev))
13638 error->num_transcoders++; /* Account for eDP. */
13639
13640 for (i = 0; i < error->num_transcoders; i++) {
13641 enum transcoder cpu_transcoder = transcoders[i];
13642
Imre Deakddf9c532013-11-27 22:02:02 +020013643 error->transcoder[i].power_domain_on =
Imre Deakbfafe932014-06-05 20:31:47 +030013644 intel_display_power_enabled_unlocked(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020013645 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020013646 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013647 continue;
13648
Chris Wilson63b66e52013-08-08 15:12:06 +020013649 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13650
13651 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13652 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13653 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13654 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13655 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13656 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13657 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013658 }
13659
13660 return error;
13661}
13662
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013663#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13664
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013665void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013666intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013667 struct drm_device *dev,
13668 struct intel_display_error_state *error)
13669{
Damien Lespiau055e3932014-08-18 13:49:10 +010013670 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013671 int i;
13672
Chris Wilson63b66e52013-08-08 15:12:06 +020013673 if (!error)
13674 return;
13675
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013676 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020013677 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013678 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013679 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010013680 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013681 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020013682 err_printf(m, " Power: %s\n",
13683 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013684 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030013685 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013686
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013687 err_printf(m, "Plane [%d]:\n", i);
13688 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
13689 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013690 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013691 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
13692 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013693 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030013694 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013695 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013696 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013697 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
13698 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013699 }
13700
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013701 err_printf(m, "Cursor [%d]:\n", i);
13702 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
13703 err_printf(m, " POS: %08x\n", error->cursor[i].position);
13704 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013705 }
Chris Wilson63b66e52013-08-08 15:12:06 +020013706
13707 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010013708 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020013709 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020013710 err_printf(m, " Power: %s\n",
13711 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020013712 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
13713 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
13714 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
13715 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
13716 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
13717 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
13718 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
13719 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013720}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030013721
13722void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
13723{
13724 struct intel_crtc *crtc;
13725
13726 for_each_intel_crtc(dev, crtc) {
13727 struct intel_unpin_work *work;
13728 unsigned long irqflags;
13729
13730 spin_lock_irqsave(&dev->event_lock, irqflags);
13731
13732 work = crtc->unpin_work;
13733
13734 if (work && work->event &&
13735 work->event->base.file_priv == file) {
13736 kfree(work->event);
13737 work->event = NULL;
13738 }
13739
13740 spin_unlock_irqrestore(&dev->event_lock, irqflags);
13741 }
13742}